Zabbix批量新增web監控模板
阿新 • • 發佈:2019-01-10
本文參考自 清風拂面 《python實現zabbix批量監控WEB網站和批量監控主機埠》 修改,使得以後新增web時更加方便,zabbix agent端完成1-3步,在zabbix web端直接匯入模板即可使用。
1、agent端批量新增指令碼/data/zabbix/scripts/web_site_moniter.py
#!/usr/bin/env python # coding:utf-8 import os, sys, json # 將要監控的web站點url新增到urllist列表 # urllist = ["http://baidu.com", # "http://www.qq.com", # "http://www.sina.com.cn/"] files='/etc/zabbix/test/WEB.txt' fd = open(files,'r') urllist=[] for i in fd.readlines(): if i[0] != '#': urllist.append(i.strip('\n')) fd.close() # 這個函式主要是構造出一個特定格式的字典,用於zabbix def web_site_discovery(): web_list = [] web_dict = {"data": None} for url in urllist: url_dict = {} url_dict["{#SITENAME}"] = url.split('|')[0] url_dict["{#URL}"] = url.split('|')[1] web_list.append(url_dict) web_dict["data"] = web_list jsonStr = json.dumps(web_dict, sort_keys=True, indent=4) return jsonStr # 這個函式用於測試站點返回的狀態碼,注意在cmd命令中如果有%{}這種字元要使用佔位符代替,否則會報錯 def web_site_code(): cmd = 'curl --connect-timeout 10 -m 20 -o /dev/null -s -w %s %s' % ("%{http_code}", sys.argv[2]) reply_code = os.popen(cmd).readlines()[0] return reply_code if __name__ == "__main__": try: if sys.argv[1] == "web_site_discovery": print web_site_discovery() elif sys.argv[1] == "web_site_code": print web_site_code() else: print "Pls sys.argv[0] web_site_discovery | web_site_code[URL]" except Exception as msg: print msg # 這裡對指令碼傳遞進來的第一個引數做判斷去執行不同的函式,為什麼要這樣,因為通過一個指令碼寫了兩個功能
UnsafeUserParameters=1 UserParameter=web_site_discovery,/data/zabbix/scripts/web_site_moniter.py web_site_discovery UserParameter=web_site_code[*],/data/zabbix/scripts/web_site_moniter.py web_site_code $1
3、zabbix agent新增需要監控web /data/zabbix/test/WEB.txt
#SITENAME|URL
#QQ|www.qq.com
BAIDU|www.baidu.com
新浪|www.sina.com
#號將表示取消web監控
修改zabbix agent配置後需要重啟agent生效
4、匯入web監控模板zbx_web_monitor_templates.xml
Configuration ---> Templates ---> Import --->選擇模板zbx_web_monitor_templates.xml
5、模板匯入之後Configuration ---> Templates可以找到Templates Web Monitor 模板
6、選擇Templates Web Monitor模板進行管理主機組或者主機
7、Configuration ---> Hosts --->某主機Applications
可以發現Templates Web Monitor: Web Monitor Service監控專案
8、檢視監控專案