一、效果图:
二、需求说明:
最近需要对节点到源站、办公环境访问业务平台网站质量和办公网络线路质量的监控,简单的ping可以检测到一些东西,但是http请求的检查也要进行,于是就研究了下pycurl。
PycURl是一个C语言写的libcurl的python绑定库。libcurl 是一个自由的,并且容易使用的用在客户端的 URL 传输库。它的功能很强大,PycURL 是一个非常快速(参考多并发操作)和丰富完整特性的,但是有点复杂的接口。
三、python脚本:
1、编写httptime.py脚本
命令vi zabbix_agentd.conf.d/httptime.py
#!/usr/bin/python
# coding: UTF-8
import pycurl
import sys
import os
import json
class Test:
def __init__(self):
self.contents = ‘‘
def body_callback(self,buf):
self.contents = self.contents + buf
def web_performance(input_url,mykey):
t = Test()
c = pycurl.Curl()
c.setopt(pycurl.WRITEFUNCTION,t.body_callback)
c.setopt(pycurl.ENCODING, ‘gzip‘)
c.setopt(pycurl.URL,input_url)
c.perform()
if mykey == "NAMELOOKUP_TIME":
#DNS解析时间ms
NAMELOOKUP_TIME = c.getinfo(c.NAMELOOKUP_TIME)
print "%.2f"%(NAMELOOKUP_TIME*1000)
#return mykey
elif mykey == "CONNECT_TIME":
#建立连接时间ms
CONNECT_TIME = c.getinfo(c.CONNECT_TIME)
print "%.2f" %(CONNECT_TIME*1000)
#return mykey
elif mykey == "PRETRANSFER_TIME":
#准备传输时间ms
PRETRANSFER_TIME = c.getinfo(c.PRETRANSFER_TIME)
print "%.2f" %(PRETRANSFER_TIME*1000)
#return mykey
elif mykey == "STARTTRANSFER_TIME":
#传输开始时间ms
STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)
print "%.2f" %(STARTTRANSFER_TIME*1000)
#return mykey
elif mykey == "TOTAL_TIME":
#传输结束总时间ms
TOTAL_TIME = c.getinfo(c.TOTAL_TIME)
print "%.2f" %(TOTAL_TIME*1000)
#return mykey
elif mykey == "SIZE_DOWNLOAD":
#下载数据包大小bytes/s
SIZE_DOWNLOAD = c.getinfo(c.SIZE_DOWNLOAD)
print "%d" %(SIZE_DOWNLOAD)
#return mykey
elif mykey == "HEADER_SIZE":
#HTTP头部大小bytes
HEADER_SIZE = c.getinfo(c.HEADER_SIZE)
print "%d" %(HEADER_SIZE)
#return mykey
elif mykey == "SPEED_DOWNLOAD":
#平均下载速度bytes/s
SPEED_DOWNLOAD=c.getinfo(c.SPEED_DOWNLOAD)
print "%d" %(SPEED_DOWNLOAD)
#return mykey
def web_discovery():
website = [‘www.qq.com‘,‘www.sina.com‘,‘sunday208.blog.51cto.com‘];
devices = []
for devpath in website:
device = os.path.basename(devpath)
devices += [{‘{#SITENAME}‘:device}]
print json.dumps({‘data‘:devices},sort_keys=True,indent=7,separators=(‘,‘,‘:‘))
if __name__ == ‘__main__‘:
if sys.argv[1] == "web_discovery":
web_discovery()
if sys.argv[1] == "web_performance":
input_url = sys.argv[2]
mykey = sys.argv[3]
web_performance(input_url,mykey)
2、加上执行权限:
命令chmod +x zabbix_agentd.conf.d/httptime.py
3、应用到zabbix客户端配置:
命令tail -1 zabbix_agentd.conf
UserParameter=HTTP_CURL[*],python /opt/soft/zabbix/etc/zabbix_agentd.conf.d/httptime.py $1 $2 $3
4、重启zabbix客户端:
/etc/init.d/zabbix_agentd restart
5、服务端测试:
命令如下:
zabbix_get -s 192.168.2.32 -p 10050 -k "HTTP_CURL[web_discovery]"
zabbix_get -s 192.168.2.32 -p 10050 -k "HTTP_CURL[web_performance,www.qq.com,TOTAL_TIME]"
四、WEB界面配置:
1、下载附件模板文件(web访问质量监控.xml)后,导入到zabbix里。
2、模板中相关配置项展示:
3、创建主机并链接模板
4、数据展示
懒人使用懒方法。
第一次编写python,如果有问题,请大家及时更正。