“Zabbix通过Python/Shell对HTTP服务状态的监控”的版本间的差异
跳到导航
跳到搜索
(创建页面,内容为“=py脚本= ==py2版本== <pre> #!/usr/bin/python # -*- coding: utf-8 -*- #mon.web.py import urllib2 import sys def monitor_http(url): response = None try:…”) |
|||
(未显示同一用户的14个中间版本) | |||
第1行: | 第1行: | ||
− | =py脚本= | + | =py脚本 on zbx client vm = |
==py2版本== | ==py2版本== | ||
<pre> | <pre> | ||
第33行: | 第33行: | ||
#就判 404吧 | #就判 404吧 | ||
− | |||
− | |||
#UserParameter=mon.web[*],/data/app/zabbix/bin/monitor_http.py $1 | #UserParameter=mon.web[*],/data/app/zabbix/bin/monitor_http.py $1 | ||
第40行: | 第38行: | ||
</pre> | </pre> | ||
+ | |||
+ | ==py3版本脚本== | ||
+ | <pre> | ||
+ | |||
+ | #!/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 | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </pre> | ||
+ | |||
+ | =整合到Zabbix web监控= | ||
+ | |||
+ | ==首先在ZabbixAgent配置文件中配置一条UserParameter== | ||
+ | <pre> | ||
+ | # zabbix client | ||
+ | UserParameter=mon.web[*],/data/mon/mon.web.py $1 | ||
+ | </pre> | ||
+ | ==然后配置item:== | ||
+ | <pre> | ||
+ | |||
+ | 名 api status code #zabbix_get 根据上面 的UserParameter 来配 | ||
+ | 键值 mon.web[http://market-api.cailuw.com] | ||
+ | |||
+ | |||
+ | </pre> | ||
+ | |||
+ | ==接下来配置一个Trigger:== | ||
+ | <pre> | ||
+ | 右边添加 双击 选择刚才有关的 监控项 特别是 respo 什么的那个 然后不等于 我这里是 404 一般正常应该是200 | ||
+ | |||
+ | |||
+ | 这里我们定义http状态码不为404(一般情形是200)时发出告警 | ||
+ | |||
+ | on zbx server | ||
+ | |||
+ | zabbix_get -s 10.3.10.143 -k "mon.web[http://market-api.cailuw.com]" | ||
+ | 404 | ||
+ | </pre> | ||
+ | |||
+ | =see also= | ||
+ | [https://www.jianshu.com/p/43050495ba57 如何通过Python/Shell对HTTP服务状态的监控?] | ||
+ | |||
+ | [https://blog.csdn.net/enweitech/article/details/78468000 通过Python/Shell对HTTP服务状态的监控] | ||
+ | |||
+ | =使用的知识点补充= | ||
+ | [https://blog.csdn.net/duxu24/article/details/77414298 Python3中urllib使用介绍 和py2 urllib2的区别] | ||
+ | |||
+ | [https://www.jianshu.com/p/2e190438bd9c 详解 python3 urllib] | ||
+ | |||
+ | [https://www.cnblogs.com/Lands-ljk/p/5447127.html Python3学习笔记(urllib模块的使用) ] | ||
+ | |||
+ | |||
+ | [https://blog.csdn.net/zsuguangh/article/details/6226385 python3.x 的urllib使用例子] | ||
+ | [[category:zabbix]] |
2024年10月21日 (一) 15:33的最新版本
目录
py脚本 on zbx client vm
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 web监控
首先在ZabbixAgent配置文件中配置一条UserParameter
# zabbix client UserParameter=mon.web[*],/data/mon/mon.web.py $1
然后配置item:
名 api status code #zabbix_get 根据上面 的UserParameter 来配 键值 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[http://market-api.cailuw.com]" 404