Zabbix通过Python/Shell对HTTP服务状态的监控
跳到导航
跳到搜索
目录
py脚本
py2版本
#!/usr/bin/python # -*- coding: utf-8 -*- #mon.web.py import urllib2 import sys def monitor_http(url): response = None try: response = urllib2.urlopen(url,timeout=5) #print response.info() # header print response.getcode() except urllib2.URLError as e: if hasattr(e, 'code'): print e.code elif hasattr(e, 'reason'): print e.reason finally: if response: response.close() url = sys.argv[1] monitor_http(url) #usage python2 1.py 'http://www.baidu.com' #就判 404吧 #UserParameter=mon.web[*],/data/app/zabbix/bin/monitor_http.py $1
py3版本脚本
#!/usr/bin/python3 # -*- coding: utf-8 -*- #mon.web.py import urllib import sys import urllib.request #import urllib.error import urllib.error def monitor_http(url): response = None try: #response = urllib.urlopen(url,timeout=5) response = urllib.request.urlopen(url,timeout=5) #print response.info() # header print("%s" %(response.getcode())) except Exception as e: # except urllib.URLError as e: if hasattr(e, 'code'): print (e.code) elif hasattr(e, 'reason'): print (e.reason) finally: if response: response.close() url = sys.argv[1] monitor_http(url) #usage 有返回码的才对 # /data/mon/mon_web.py "http://market-socket.a.com" #400
整合到Zabbix
首先在ZabbixAgent配置文件中配置一条UserParameter
# zabbix client UserParameter=mon.web[*],/data/mon/mon.web.py $1
然后配置item:
名 api status code 键值 mon.web[http://market-api.cailuw.com]
接下来配置一个Trigger:
右边添加 双击 选择刚才有关的 监控项 特别是 respo 什么的那个 然后不等于 我这里是 404 一般正常应该是200 这里我们定义http状态码不为404(一般情形是200)时发出告警
on zbx server
zabbix_get -s 10.3.10.143 -k "mon.web[1]"
404