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, background research and Development 2b, always update the formal environment installation package, leading to problems, always give them to wipe the buttocks, said this matter, they do not cooperate, now the problem, very cool, this pot I say no back, find the Pycurl This module write a monitor.

1234567891011121314151617181920212223242526 c = pycurl.Curl()    #创建一个curl对象 c.setopt(pycurl.CONNECTTIMEOUT, 5)    #连接的等待时间,设置为0则不等待  c.setopt(pycurl.TIMEOUT, 5)           #请求超时时间  c.setopt(pycurl.NOPROGRESS, 0)        #是否屏蔽下载进度条,非0则屏蔽  c.setopt(pycurl.MAXREDIRS, 5)         #指定HTTP重定向的最大数  c.setopt(pycurl.FORBID_REUSE, 1)      #完成交互后强制断开连接,不重用  c.setopt(pycurl.FRESH_CONNECT,1)      #强制获取新的连接,即替代缓存中的连接  c.setopt(pycurl.DNS_CACHE_TIMEOUT,60) #设置保存DNS信息的时间,默认为120秒  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-Agentc.setopt(pycurl.HEADERFUNCTION, getheader)   #将返回的HTTP HEADER定向到回调函数getheaderc.setopt(pycurl.WRITEFUNCTION, getbody)      #将返回的内容定向到回调函数getbodyc.setopt(pycurl.WRITEHEADER, fileobj)        #将返回的HTTP HEADER定向到fileobj文件对象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:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 #!/usr/bin/env python# __*__coding:utf8__*__#Author:wangpengtai#Blog:http://wangpengtai.blog.51cto.com/importpycurlimportsysimportStringIO #引用该模块的原因是:使用pycurl后会打印出页面内容,我们不需要看到这个内容,只需要获取页面反馈信息就行了,只能将其写入缓存中,目前没找到好办法,学艺不精,不会使用重定向写到os.devnull中,无奈初次下策。。。#开始使用的是写入临时文件,但是会有权限问题,导致zabbix无法获取到数据。class WebStatus(object):    def__init__(self, url):        self.url =url        self.curl =pycurl.Curl()        self.string =StringIO.StringIO()        # 连接等待时间,0则不等待        self.curl.setopt(pycurl.CONNECTTIMEOUT, 5)        # 超时时间        self.curl.setopt(pycurl.TIMEOUT, 5)        # 下载进度条,非0则屏蔽        self.curl.setopt(pycurl.NOPROGRESS, 1)        # 指定HTTP重定向最大次数        self.curl.setopt(pycurl.MAXREDIRS, 5)        # 完成交互后强制断开连接,不重用        self.curl.setopt(pycurl.FORBID_REUSE, 1)        # 设置DNS信息保存时间,默认为120秒        self.curl.setopt(pycurl.DNS_CACHE_TIMEOUT, 60)        # 设置请求的Url        self.curl.setopt(pycurl.URL, self.url)        self.curl.setopt(pycurl.WRITEFUNCTION, self.string.write)#将页面内容写入缓存        self.curl.perform()    defrequest_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),            "Redirect_count"self.curl.getinfo(pycurl.REDIRECT_COUNT)        }        returndata    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|Redirect_count]    """    try:        url =sys.argv[1]        request =sys.argv[2]        try:            =WebStatus(url)            try:                prints.request_value()[request]            exceptKeyError:                print"Make sure 2nd argument is right!"        exceptpycurl.error:            print"Make sure the url is right or reachable!"    exceptIndexError:        print "Must be 2 arguments given!%s"%Usage

Verification: Www.baidu.com has always been the method and ingredient of the object board for my test (tapping)

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

1234 [[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

12 [[email protected] scripts]# cat /etc/zabbix_agentd.confUserParameter=web[*],/etc/zabbix/scripts/web_monitor.py $1 $2

3. Restart Zabbix-agentd

1 [[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 to describe.

Zabbix Monitor Web page status using Pycurl module

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.