Automatic discovery realizes url+ response time monitoring

Source: Internet
Author: User

URL Auto-Discovery script:

[email protected] scripts]# cat urldiscovery.py

#!/usr/bin/env python#Coding:utf-8ImportOSImportSYSImportJSON#This function mainly constructs a dictionary of a specific format for Zabbixdefweb_site_discovery (): Web_list=[] web_dict={"Data": None} with open ("/etc/zabbix/server_list","R") as F: forwr.inchf:dict={} dict["{#SERVICENAME}"]=Url.strip (). Split () [0] dict["{#SITENAME}"]=url.strip (). Split () [1] Web_list.append (dict) web_dict["Data"]=web_list jsonstr= Json.dumps (Web_dict, Sort_keys=true, indent=4)    returnJsonstrif __name__=="__main__":    PrintWeb_site_discovery ()

URL Response Time Monitoring script:

[email protected] scripts]# cat urlresponse.py

#!/usr/bin/env python#-*-coding:utf-8-*-ImportOs,sysImportPycurl fromSubprocessImportPopen,pipeclasshttp_test:def __init__(self): self.contents="'    defBody_callback (self,buf): Self.contents= Self.contents +bufdefgeturlinfo (URL): C=Pycurl. Curl () T=http_test () c.setopt (Pycurl. Url,url)#Specify the requested URLC.setopt (Pycurl. maxredirs,5)#set the maximum number of redirectsC.setopt (Pycurl. connecttimeout,10)#to define the wait connection time for a requestC.setopt (Pycurl. TIMEOUT,300)#to define the time-out for a requestC.setopt (Pycurl. UserAgent,'mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 1.1.4322)')#Configuring the User-agent of the request HTTP headerC.setopt (Pycurl. ENCODING,'gzip')#using gzip transferC.setopt (Pycurl. dns_cache_timeout,600)#setting the time to save DNSc.setopt (Pycurl. Writefunction,t.body_callback)Try: C.perform ()#submit the request, return the generated contentCode = C.getinfo (Pycurl. HTTP_CODE)#Get HTTP Status codeConn_time = Round ((C.getinfo (Pycurl. Connect_time) *1000), 2)#gets the time the connection was establishedPre_tran = Round ((C.getinfo (Pycurl. Pretransfer_time) *1000), 2)#Prepare transfer TimeStart_tran = Round ((C.getinfo (Pycurl. Starttransfer_time) *1000), 2)#Start transfer TimeTotal_time = Round ((C.getinfo (Pycurl. Total_time) *1000), 2)#total time to get transferSize = C.getinfo (pycurl. Size_download)#get the download packet sizeNamelookup = Round ((C.getinfo (Pycurl. Namelookup_time) *1000), 2)#Get DNS resolution timeHeadersize = C.getinfo (c.header_size)#HTTP Header SizeDownloadspeed = C.getinfo (c.speed_download)#Get Download speed    exceptException as E:#Print (' connection error: ' +str (e))c.close () code=0 Conn_time=0 Pre_tran=0 Start_tran=0 Total_time=0 Size=0 Namelookup=0 headersize=0 Downloadspeed=0" "info= "" "Response Status Code:%s DNS resolution time:%.3f MS Connection settling time:%.3f ms Preparing transfer time:%.3f MS Transfer start time:%.3f ms Total transmission time:%.3f MS Packet Size:%s bytes HTTP Header size:%s bytes Average download speed:%s bytes/s "" "% (code, Namel Ookup, Conn_time, Pre_tran, Start_tran, total_time, size, headersize, downloadspeed)" "Info={"Httpcode": Code,"Conntime": Conn_time,"Pretran":p Re_tran,"Starttran": Start_tran,"TotalTime": Total_time,"size": Size,"Namelookup": Namelookup,"headersize": Headersize,"Downloadspeed":d Ownloadspeed}returnInfodefgeturllist (urlfile): Urllist=[] with open (Urlfile,'R') as FD: forIinchFd:urlList.append (I.split ())returnurllistdefSendData (zabbixconf,tmpfile): args="/usr/bin/zabbix_sender-c {0}-i {1}-vv"Process=popen (Args.format (zabbixconf,tmpfile), shell=true,stdout=pipe,stderr=PIPE) Stdout,stderr=process.communicate ()#Os.unlink (tmpfile)if __name__=='__main__': Urllist=geturllist ("/etc/zabbix/server_list") Tmpfile="/tmp/tmpfile"zabbixconf="/etc/zabbix/zabbix_agentd.conf"F=open (Tmpfile,'W')     forI,jinchUrllist:urlinfo=Geturlinfo (j) forKvinchUrlinfo.items (): Key='url.info[{0},{1},{2}]'. Format (i,j,k) F.write ("-%s%s\n"%(key, V)) F.close () senddata (zabbixconf,tmpfile)

URL Monitoring list:

[Email protected] scripts]# cat/etc/zabbix/server_list
API Https://www.xxxxx.de/rest/v1/tags
Awscdn https://d3m2x14ac3st9e.cloudfront.net

URL access will be unstable, need to adjust the/etc/zabbix/zabbix_agentd.conf time-out

Timeout=30

Add a URL monitoring configuration file

[email protected] zabbix_agentd.conf.d]# cat userparameter_url.conf
userparameter=web_site_discovery,/usr/bin/python/etc/zabbix/scripts/urldiscovery.py
userparameter=url.info,/usr/bin/python/etc/zabbix/scripts/urlresponse.py >/dev/null 2>&1 && Echo 0 | | Echo 1

Zabbix's monitoring template

Zbx_url_templates.xml

Automatic discovery realizes url+ response time monitoring

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.