24123r2 发表于 2016-12-30 10:21:50

Zabbix批量添加web监控模板

                      本文参考自 清风拂面 《python实现zabbix批量监控WEB网站和批量监控主机端口》 修改,使得以后添加web时更加方便,zabbix agent端完成1-3步,在zabbix web端直接导入模板即可使用。

web监控模板(zbx_web_monitor_templates.xml)
   

#!/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 != '#':
      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
      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 -o /dev/null -s -w %s %s' % ("%{http_code}", sys.argv)
    reply_code = os.popen(cmd).readlines()
    return reply_code


if __name__ == "__main__":
    try:
      if sys.argv == "web_site_discovery":
            print web_site_discovery()
      elif sys.argv == "web_site_code":
            print web_site_code()
      else:
            print "Pls sys.argv web_site_discovery | web_site_code"
    except Exception as msg:
      print msg
      # 这里对脚本传递进来的第一个参数做判断去执行不同的函数,为什么要这样,因为通过一个脚本写了两个功能





2、zabbix agent配置文件添加如下内容,内容含义请参考《python实现zabbix批量监控WEB网站和批量监控主机端口》 已经详细解释说明

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

http://www.ttlsa.com/zabbix/zabbix-zabbix_get-get-items/
#www.qq.com
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、查看监控项目



web监控模板
                  

roadhappy 发表于 2017-1-21 11:18:55

不错,好东西,谢谢分享

roadhappy 发表于 2017-2-22 08:11:09



不错,学习了!!!!!

sololove 发表于 2018-4-27 16:50:43

模版在哪里

吴舟_lOKaz 发表于 2018-6-6 20:14:47

感谢楼主
不错,好东西,谢谢分享
页: [1]
查看完整版本: Zabbix批量添加web监控模板