Zabbix 監控 WEB 應用效能

來源:互聯網
上載者:User

標籤: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 應用效能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.