自动发现实现url+响应时间监控

时间:2024-04-01 23:07:33

url自动发现脚本:

[root@jenkins scripts]# cat  urlDiscovery.py

#!/usr/bin/env python
#coding:utf-8 import os
import sys
import json #这个函数主要是构造出一个特定格式的字典,用于zabbix
def web_site_discovery():
web_list=[]
web_dict={"data":None}
with open("/etc/zabbix/server_list","r") as f:
for url in f:
dict={}
dict["{#SERVICENAME}"]=url.strip().split()[0]
dict["{#SITENAME}"]=url.strip().split()[1]
web_list.append(dict) web_dict["data"]=web_list
jsonStr = json.dumps(web_dict, sort_keys=True, indent=4)
return jsonStr if __name__ == "__main__":
print web_site_discovery() 

url响应时间监控脚本:

[root@jenkins scripts]# cat urlResponse.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os,sys
import pycurl
from subprocess import Popen,PIPE
class Http_Test:
def __init__(self):
self.contents = ''
def body_callback(self,buf):
self.contents = self.contents + buf
def getUrlInfo(url):
c = pycurl.Curl()
t = Http_Test()
c.setopt(pycurl.URL,url) #指定请求的url
c.setopt(pycurl.MAXREDIRS,5) #设置最大重定向次数
c.setopt(pycurl.CONNECTTIMEOUT,10) #定义请求的等待连接时间
c.setopt(pycurl.TIMEOUT,300) #定义请求的超时时间
c.setopt(pycurl.USERAGENT,'Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)') #配置请求HTTP头的User-Agent
c.setopt(pycurl.ENCODING, 'gzip')#采用gzip传输
c.setopt(pycurl.DNS_CACHE_TIMEOUT,600) #设置保存DNS的时间
c.setopt(pycurl.WRITEFUNCTION,t.body_callback)
try:
c.perform() #提交请求,返回生成内容
code = c.getinfo(pycurl.HTTP_CODE) #获取 HTTP 状态码
conn_time = round((c.getinfo(pycurl.CONNECT_TIME)*1000),2) #获取连接建立的时间
pre_tran = round((c.getinfo(pycurl.PRETRANSFER_TIME)*1000),2) #准备传输时间
start_tran = round((c.getinfo(pycurl.STARTTRANSFER_TIME)*1000),2) #开始传输时间
total_time = round((c.getinfo(pycurl.TOTAL_TIME)*1000),2) #获取传输的总时间
size = c.getinfo(pycurl.SIZE_DOWNLOAD) #获取下载数据包大小
namelookup = round((c.getinfo(pycurl.NAMELOOKUP_TIME)*1000),2) #获取DNS解析时间
headerSize = c.getinfo(c.HEADER_SIZE) #HTTP头大小
downloadSpeed = c.getinfo(c.SPEED_DOWNLOAD) #获取下载速度
except Exception as e:
#print('connection error:'+str(e))
c.close()
code = 0
conn_time = 0
pre_tran = 0
start_tran = 0
total_time = 0
size = 0
namelookup = 0
headerSize = 0
downloadSpeed = 0
'''info="""
响应状态码:%s
Dns解析时间:%.3f ms
连接建立时间:%.3f ms
准备传输时间:%.3f ms
传输开始时间:%.3f ms
总共传输时间:%.3f ms
数据包大小:%s bytes
http头大小:%s bytes
平均下载速度:%s bytes/s
"""%(code, namelookup, conn_time, pre_tran, start_tran, total_time, size, headerSize, downloadSpeed)
'''
info={"httpCode":code,"connTime":conn_time,"preTran":pre_tran,"startTran":start_tran,
"totalTime":total_time,"size":size,"namelookup":namelookup,"headerSize":headerSize,"downloadSpeed":downloadSpeed}
return info
def getUrlList(urlfile):
urlList=[]
with open(urlfile,'r') as fd:
for i in fd:
urlList.append(i.split())
return urlList
def sendData(zabbixConf,tmpfile):
args="/usr/bin/zabbix_sender -c {0} -i {1} -vv"
process=Popen(args.format(zabbixConf,tmpfile),shell=True,stdout=PIPE,stderr=PIPE)
stdout,stderr=process.communicate()
#os.unlink(tmpfile)
if __name__=='__main__':
urlList=getUrlList("/etc/zabbix/server_list")
tmpfile="/tmp/tmpfile"
zabbixConf="/etc/zabbix/zabbix_agentd.conf"
f=open(tmpfile,'w')
for i,j in urlList:
urlinfo=getUrlInfo(j)
for k,v in urlinfo.items():
key='url.info[{0},{1},{2}]'.format(i,j,k)
f.write("- %s %s\n" % (key, v))
f.close()
sendData(zabbixConf,tmpfile)

url监控列表:

[root@jenkins scripts]# cat /etc/zabbix/server_list
api https://www.xxxxx.de/rest/v1/tags
AWScdn https://d3m2x14ac3st9e.cloudfront.net

url访问会出现不稳定的情况,需要调整/etc/zabbix/zabbix_agentd.conf 的超时时间

Timeout=30

添加url监控配置文件

[root@jenkins zabbix_agentd.conf.d]# cat userparameter_url.conf
UserParameter=web_site_discovery,/usr/bin/python /etc/zabbix/scripts/urlDiscovery.py
UserParameter=url.info,/usr/bin/python /etc/zabbix/scripts/urlResponse.py >/dev/null 2>&1 && echo 0 || echo 1

zabbix的监控模板

zbx_url_templates.xml