標籤:set 大小 tin none url 技術 -o int 分享
1.介紹
使用 zabbix_sender 發送採集的 WEB 狀態值,使用 pycurl 來採集 WEB 狀態。
2.實現
Python 指令碼如下:
#!/usr/bin/env python#coding=utf-8import osimport sysimport fileinputimport pycurlimport logginghostname = "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_cmdclass Test(): def __init__(self): self.contents = ‘‘ def body_callback(self,buf): self.contents = self.contents + bufdef Check_Http(URL): t = Test() #gzip_test = file("gzip_test.txt", ‘w‘) c = pycurl.Curl() c.setopt(pycurl.WRITEFUNCTION,t.body_callback) #請求採用Gzip傳輸 #c.setopt(pycurl.ENCODING, ‘gzip‘) try: c.setopt(pycurl.CONNECTTIMEOUT, 60) #連結逾時 c.setopt(pycurl.URL,URL) c.perform() #執行上述訪問網址的操作 except pycurl.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))def runCmd(command): for u in list: URL = u global k if u.startswith(‘https:‘): k = u.split(‘/‘)[2] else: k=u.split(‘/‘)[0] Check_Http(URL) for line in fileinput.input(log_file,inplace=1): print line.replace(‘INFO:root:‘,‘‘), return os.system(command)runCmd(run_cmd)
Check_HTTP_Response.py
如果需要監控多個網站,修改 list 裡的網站地址 添加計劃任務, 每 5 分鐘採集一次
監控key:
回應時間:HTTP_ResTime[www.test.com]
狀態代碼:HTTP_ResCode[www.test.com]
文檔大小:HTTP_ResSize[www.test.com]
下載速度:HTTP_ResSpeed[www.test.com]
測試能否使用zabbix_sender正常工作:./zabbix_sender -s "Zabbix server" -z 127.0.0.1 -k HTTP_ResCode[www.test.com] -o 200
zabbix server 添加監控模板、監控項、觸發器
Zabbix 監控 WEB 應用效能