1. Introduction
Use Zabbix_sender to send collected web state values and use Pycurl to capture the Web state.
2. Implement
The Python script is as follows:
#!/usr/bin/env python#Coding=utf-8ImportOSImportSYSImportFileinputImportPycurlImportLogginghostname="Zabbix Server"Zabbix_server="127.0.0.1"Zabbix_sender="/usr/local/zabbix/bin/zabbix_sender"List= ['www.test.com','cdn.test.com']key= ['http_ressize','Http_restime','Http_rescode','Http_resspeed']log_file="/tmp/http_response.log"logging.basicconfig (filename=log_file,level=logging.info,filemode='W') Run_cmd="%s-z%s-i%s >/tmp/http_response.temp"%(Zabbix_sender,zabbix_server,log_file)#Print Run_cmdclassTest ():def __init__(self): self.contents="' defBody_callback (self,buf): Self.contents= Self.contents +bufdefcheck_http (URL): t=Test ()#gzip_test = File ("Gzip_test.txt", ' W ')c =Pycurl. Curl () c.setopt (Pycurl. Writefunction,t.body_callback)#request with gzip transfer #c.setopt (Pycurl. ENCODING, ' gzip ') Try: C.setopt (Pycurl. ConnectTimeout,30L#Link Timeoutc.setopt (Pycurl. Url,url) C.perform ()#perform the above access URL operation exceptPycurl.error:Print "URL%s"%URL http_document_size=c.getinfo (c.size_download)#http_download_speed = Round ((C.getinfo (Pycurl. Speed_download)/1024), 2)Http_download_speed = Round ((C.getinfo (Pycurl. Speed_download)), 2) Http_total_time= Round ((C.getinfo (Pycurl. Total_time) * 1000), 2) Http_response_code=C.getinfo (Pycurl. Http_code) Logging.info (hostname+' '+key[0] +'['+ K +']'+' '+str (http_document_size)) Logging.info (hostname+' '+KEY[1] +'['+ K +']'+' '+str (http_total_time)) Logging.info (hostname+' '+KEY[2] +'['+ K +']'+' '+str (http_response_code)) Logging.info (hostname+' '+KEY[3] +'['+ K +']'+' '+str (http_download_speed))defruncmd (command): forUinchList:url=uGlobalkifU.startswith ('https:'): K= U.split ('/') [2] Else: K=u.split ('/') [0] Check_http (URL) forLineinchFileinput.input (log_file,inplace=1): PrintLine.replace ('Info:root:',"'), returnos.system (command) runcmd (run_cmd)
check_http_response.py
If you need to monitor multiple websites, modify the site address in the list to add scheduled tasks, collected every 5 minutes
Monitoring key:
Response time: http_restime[www.test.com]
Status code: http_rescode[www.test.com]
File size: http_ressize[www.test.com]
Download speed: http_resspeed[www.test.com]
Test whether you can use Zabbix_sender to work properly:./zabbix_sender-s "Zabbix Server"-Z 127.0.0.1-k http_rescode[www.test.com]-O 200
Zabbix Server add monitoring templates, monitoring items, triggers
Zabbix Monitoring WEB Application Performance