Zabbix using Pycurl module to monitor Web page status

Source: Internet
Author: User

Due to network problems, Zabbix with the Web module can not use, backstage research and Development 2b, always update the formal environment installation package, leading to problems, always give them wipe the buttocks, said this matter, they do not cooperate, now problems, very cool 650) this.width=650; "Src=" Http://img.baidu.com/hi/jx2/j_0025.gif "alt=" J_0025.gif "/>, this pot I said no back, I found pycurl this module to write a monitoring.


Pycurl Module Usage:

(This piece is copy of 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0023.gif "alt=" J_0023.gif "/>, reference address:/HTTP blog.csdn.net/xsj_blog/article/details/52102652)

c = Pycurl. Curl () #创建一个curl对象

C.setopt (Pycurl. ConnectTimeout, 5) #连接的等待时间, set to 0 does not wait

C.setopt (Pycurl. TIMEOUT, 5) #请求超时时间

C.setopt (Pycurl. noprogress, 0) #是否屏蔽下载进度条, not 0 shielded

C.setopt (Pycurl. Maxredirs, 5) #指定HTTP重定向的最大数

C.setopt (Pycurl. Forbid_reuse, 1) #完成交互后强制断开连接, no reuse

C.setopt (Pycurl. fresh_connect,1) #强制获取新的连接, which is the substitution of connections in the cache

C.setopt (Pycurl. dns_cache_timeout,60) #设置保存DNS信息的时间, default is 120 seconds

C.setopt (Pycurl. URL, "http://www.baidu.com") #指定请求的URL

C.setopt (Pycurl. UserAgent, "mozilla/5.2 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 1.1.4322;. NET CLR 2.0.50324) #配置请求HTTP头的User-agent

C.setopt (Pycurl. Headerfunction, GetHeader) #将返回的HTTP header directed to the callback function GetHeader

C.setopt (Pycurl. Writefunction, GetBody) #将返回的内容定向到回调函数getbody

C.setopt (Pycurl. Writeheader, Fileobj) #将返回的HTTP header directed to Fileobj file object

C.setopt (Pycurl. WriteData, Fileobj) #将返回的HTML内容定向到fileobj文件对象

C.getinfo (Pycurl. Http_code) #返回的HTTP状态码

C.getinfo (Pycurl. Total_time) #传输结束所消耗的总时间

C.getinfo (Pycurl. Namelookup_time) #DNS解析所消耗的时间

C.getinfo (Pycurl. Connect_time) #建立连接所消耗的时间

C.getinfo (Pycurl. Pretransfer_time) #从建立连接到准备传输所消耗的时间

C.getinfo (Pycurl. Starttransfer_time) #从建立连接到传输开始消耗的时间

C.getinfo (Pycurl. Redirect_time) #重定向所消耗的时间

C.getinfo (Pycurl. Size_upload) #上传数据包大小

C.getinfo (Pycurl. Size_download) #下载数据包大小

C.getinfo (Pycurl. Speed_download) #平均下载速度

C.getinfo (Pycurl. Speed_upload) #平均上传速度

C.getinfo (Pycurl. Header_size) #HTTP头部大小


The code is as follows:

#!/usr/bin/env python# __*__coding:utf8__*__#author:wangpengtai#blog:http:// wangpengtai.blog.51cto.com/import pycurlimport sysimport stringio # The reason for referencing this module is: The use of Pycurl will print out the content of the page, we do not need to see this content, only need to get page feedback information on the line, can only write to the cache, there is no good way, the apprenticeship is not fine, will not use redirection to write to the Os.devnull, but the first choice ... #开始使用的是写入临时文件, but there are permissions issues that cause Zabbix to not get the data. Class webstatus (object):     def __init__ (Self, url):         self.url = url         Self.curl = pycurl. Curl ()         self.string = stringio.stringio ()          #  connection Wait time, 0 do not wait          Self.curl.setopt (Pycurl. CONNECTTIMEOUT, 5)         #  time-out          self.curl.setopt (Pycurl. TIMEOUT, 5)         #  download progress bar, not 0 shielded         self.curl.setopt (pycurl. noprogress, 1)         #  Specify the maximum number of HTTP redirects          self.curl.setopt (Pycurl. MAXREDIRS, 5)         #  Force Disconnect after completion of interaction, no reuse          self.curl.setopt (Pycurl. FORBID_REUSE, 1)         #  Set DNS information save time, default is 120 seconds          self.curl.setopt (Pycurl. DNS_CACHE_TIMEOUT, 60)         #  set url    of the request      self.curl.setopt (Pycurl. Url, self.url)         self.curl.setopt (Pycurl. Writefunction, self.string.write) #将页面内容写入缓存          Self.curl.perform ()     def request_valuE (self):        data = {              "Http_code":  self.curl.getinfo (Pycurl. Http_code),             "Speed_download":  Self.curl.getinfo (Pycurl. Speed_download),             "Connect_time":  Self.curl.getinfo (Pycurl. Connect_time),             "Total_time":  Self.curl.getinfo (Pycurl. Total_time),             "Dnslookup_time":  Self.curl.getinfo (Pycurl. Namelookup_time),             "Redirect_time":  self.curl.getinfo (Pycurl. Redirect_time)         }         return data   &nbSp;def __end__ (self):         self.string.close ()          self.curl.close () if __name__ ==  "__main__":     Usage =  "" "usage: python web_monitor.py url [http_code| speed_download| connect_time| total_time| dnslookup_time| redirect_time]     "" "    try:         url = sys.argv[1]        request =  sys.argv[2]        try:             s = webstatus (URL)              try:                 print s.request_value () [request]            except KeyError:                 print  "make sure 2nd argument is right!"         except pycurl.error:             print  "Make sure the url is right or  reachable! "     except IndexError:        print  " must be 2 arguments given!%s " % usage



Second, configure the Zabbix to customize the monitoring

This is relatively flexible, you can find a machine dedicated to monitoring, only need to configure the following on this machine to monitor multiple URLs.

A template can be configured in the Zabbix interface to hang it on the machine.


1. Write the code to the following directory and add the executable permission

[Email protected] scripts]# pwd/etc/zabbix/scripts[[email protected] scripts]# vim web_monitor.py [[email protected] scripts]# chmod +x web_monitor.py


2, Configuration zabbix_agentd.conf

[Email protected] scripts]# cat/etc/zabbix_agentd.confuserparameter=web[*],/etc/zabbix/scripts/web_monitor.py $ 2


3. Restart Zabbix-agentd

[Email protected] scripts]# service Zabbix-agentd restart

Third, configure Zabbix monitoring

Directly, the subsequent addition of the free play, a lot of return value can be out of the picture, can do trigger alarm and so on. Not much description of 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0042.gif "alt=" J_0042.gif "/>

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/00/04/wKiom1mU-YaDpLH8AAC9-gHCOUU707.jpg-wh_500x0-wm_ 3-wmp_4-s_2189949298.jpg "title=" tim20170817095840.jpg "alt=" Wkiom1mu-yadplh8aac9-ghcouu707.jpg-wh_50 "/>

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/9E/B4/wKioL1mU-daCJY3HAACk3ML1kVk955.jpg-wh_500x0-wm_ 3-wmp_4-s_4016537963.jpg "title=" tim20170817100445.jpg "alt=" Wkiol1mu-dacjy3haack3ml1kvk955.jpg-wh_50 "/>

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/9E/B4/wKioL1mU-iCynOqwAABiF67TeWU417.jpg-wh_500x0-wm_ 3-wmp_4-s_1483484018.jpg "title=" tim20170817100550.jpg "alt=" Wkiol1mu-icynoqwaabif67tewu417.jpg-wh_50 "/>



This article is from the "LINUX" blog, so be sure to keep this source http://wangpengtai.blog.51cto.com/3882831/1956973

Zabbix using Pycurl module to monitor Web page status

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.