標籤:zabbix_sender zabbix trapper
HTTP服務目前最流行的互連網應用之一,如何監控服務的健康狀態對系統營運來說至關重要。
Zabbix本身提供了對WEB應用程式的監控,比如監控WEB程式的Download Speed,Response Time和Response Code等效能指標,但是配置起來比較繁瑣和複雜。下面通過 python pycurl模組來擷取HTTP回應時間,下載速度,狀態嗎等效能指標。然後通過zabbix trapper的方式來監控WEB應用的效能。
Zabbix trapper監控是用戶端收集監控資料,然後以zabbix_sender的方式發送給zabbix server或者proxy伺服器。發送的資料主要包括zabbix server或者proxy主機名稱,監控項和值。zabbix_sender具體用法如下:
[[email protected]]# /usr/local/zabbix/bin/zabbix_sender -helpZabbix Sender v2.2.3 (revision 44105) (7 April 2014)usage: zabbix_sender [-Vhv] {[-zpsI] -ko | [-zpI] -T -i <file> -r} [-c <file>]Options: -c --config <file> Absolute path to the configuration file -z --zabbix-server <server> Hostname or IP address of Zabbix server -p --port <server port> Specify port number of server trapper running on the server. Default is 10051 -s --host <hostname> Specify host name. Host IP address and DNS name will not work -I --source-address <IP address> Specify source IP address -k --key <key> Specify item key -o --value <key value> Specify value -i --input-file <input file> Load values from input file. Specify - for standard input Each line of file contains whitespace delimited: <hostname> <key> <value> Specify - in <hostname> to use hostname from configuration file or --host argument -T --with-timestamps Each line of file contains whitespace delimited: <hostname> <key> <timestamp> <value> This can be used with --input-file option Timestamp should be specified in Unix timestamp format -r --real-time Send metrics one by one as soon as they are received This can be used when reading from standard input -v --verbose Verbose mode, -vv for more detailsOther options: -h --help Give this help -V --version Display version number
下面是我用python寫的監控指令碼,如果要監控多個網站,只需在list列表裡面添加即可。
[[email protected] cron]# cat Check_HTTP_Response_Time.py#!/usr/bin/env python#coding=utf-8#Auth:davidimport osimport sysimport fileinputimport pycurlimport logginghostname = "monitor"#IP from Zabbix Server or proxy where data should be send to.zabbix_server = "192.168.100.200" zabbix_sender = "/usr/local/zabbix/bin/zabbix_sender"#If add url of website, please update list.list = [‘www.zmzblog.com‘,‘img.zmzblog.com‘]#This list define zabbix key.key = [‘HTTP_ResSize‘,‘HTTP_ResTime‘,‘HTTP_ResCode‘,‘HTTP_ResSpeed‘]#In the file to define the monitor host, key and value.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)class 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_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)
添加crontab,定期收集資料並發送給zabbix server伺服器。
*/5 * * * * /zabbix/python/cron/Check_HTTP_Response.py
然後在前端配置監控項,可以調用zabbix API大量新增監控項。下面以www.zmzblog.com為例來說明如何監控HTTP的回應時間。這裡所有的監控類型都是Zabbix_trapper的方式。監控key HTTP_ResTime[www.zmzblog.com],
HTTP_ResCode[www.zmzblog.com],HTTP_ResSize[www.zmzblog.com],HTTP_ResSpeed[www.zmzblog.com]分別表示HTTP的回應時間,狀態嗎,文檔大小和下載速度。
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/84/38/wKiom1eImBvjU16VAAC6jIWlVho294.jpg-wh_500x0-wm_3-wmp_4-s_84993031.jpg" title="01.jpg" alt="wKiom1eImBvjU16VAAC6jIWlVho294.jpg-wh_50" />
配置完監控項之後我們配置觸發器,因為現在網站的回應時間都是毫秒層級的,如果超過1000ms就警示。
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/84/38/wKiom1eImG2gw04FAAKCu221shY490.jpg-wh_500x0-wm_3-wmp_4-s_2107619572.jpg" title="03.jpg" alt="wKiom1eImG2gw04FAAKCu221shY490.jpg-wh_50" />
HTTP響應狀態嗎。
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/84/38/wKioL1eImJTSNAMUAAHn2-OhACY730.jpg-wh_500x0-wm_3-wmp_4-s_2481914869.jpg" title="04.jpg" alt="wKioL1eImJTSNAMUAAHn2-OhACY730.jpg-wh_50" />
總結:WEB應用效能監控主要從下面兩個方面進行監控。
1)HTTP的回應時間,隨著互連網的發展,使用者體驗提升。網站的開啟速度監控一定要快,至少要在毫秒層級。
2)HTTP的狀態嗎,即時監控網站的響應嗎是否正常,是否出現了404,500這樣的錯誤,這種錯誤是使用者無法忍受的,如果出現要第一時間解決。
3)由於網路或者其它原因,為了減少誤判,建議用下面的觸發器,即檢測2次如果狀態嗎不為200或者大於400的時候警示。
{Template HTTP Response:HTTP_ResCode[www.zmzblog.com].count(#2,200,”ne”)}=2
{Template HTTP Response:HTTP_ResCode[www.zmzblog.com].count(#2,400,”ge”)}=2
本文出自 “樸實的追夢者” 部落格,請務必保留此出處http://sfzhang88.blog.51cto.com/4995876/1826763
zabbix如何監控WEB應用效能