zabbix自动发现功能实现批量web url监控

需求:

    现在有大量url需要监控,形式如http://www.baidu.com ,要求url状态不为200即报警。

需求详细分析:

    大量的url,且url经常变化,现在监控用的是zabbix,如果手动添加模板,会造成大量重复工作,如果利用脚本+mail,无法图形呈现

解决方案:

zabbix有discovery功能,即可轻松解决此问题

首先我们找一个随便找一个zabbix客户端来实现接下来要做的功能

首先我们从头来,我们来装一下zabbix客户端

[root@test_ha_nginx ~]# yum install -y gcc gcc-c++ make pcre* openssl* cmake ncurses-devel gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel
[root@test_ha_nginx src]# tar -xf zabbix-3.0.3.tar.gz 
[root@test_ha_nginx src]# cd zabbix-3.0.3
[root@test_ha_nginx zabbix-3.0.3]# ./configure   --prefix=/data/usr/local/zabbix  --enable-agent
[root@test_ha_nginx zabbix-3.0.3]# make && make install
[root@test_ha_nginx zabbix-3.0.3]# groupadd zabbix
[root@test_ha_nginx zabbix-3.0.3]# useradd zabbix -g zabbix
[root@test_ha_nginx init.d]# /etc/init.d/zabbix_agentd restart
[root@test_ha_nginx scripts]# pwd
/data/usr/local/zabbix/scripts
[root@test_ha_nginx scripts]# vim web_site_code_status.sh 

 脚本内容为:

 1 #!/bin/bash 
 2 # function:monitor tcp connect status from zabbix 
 3  
 4 source /etc/bashrc >/dev/null 2>&1 
 5 source /etc/profile  >/dev/null 2>&1 
 6 #/usr/bin/curl -o /dev/null -s -w %{http_code} http://$1/ 
 7  
 8 web_site_discovery () { 
 9 WEB_SITE=($(cat  /data/usr/local/zabbix/sbin/WEB.txt|grep -v "^#")) 
10         printf '{\n' 
11         printf '\t"data":[\n' 
12 for((i=0;i<${#WEB_SITE[@]};++i)) 
13 { 
14 num=$(echo $((${#WEB_SITE[@]}-1))) 
15         if [ "$i" != ${num} ]; 
16                 then 
17         printf "\t\t{ \n" 
18         printf "\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$i]}\"},\n" 
19                 else 
20                         printf  "\t\t{ \n" 
21                         printf  "\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$num]}\"}]}\n" 
22         fi 
23 } 
24 } 
25  
26 web_site_code () { 
27 /usr/bin/curl -o /dev/null -s -w %{http_code} https://$1 
28 } 
29  
30 case "$1" in 
31 web_site_discovery) 
32 web_site_discovery 
33 ;;
34 web_site_code) 
35 web_site_code $2 
36 ;; 
37 *) 
38  
39 echo "Usage:$0 {web_site_discovery|web_site_code [URL]}" 
40 ;; 
41 esac
View Code

[root@test_ha_nginx scripts]# chmod a+x web_site_code_status.sh

 

2.创建WEB.txt文件

[root@test_ha_nginx sbin]# pwd
/data/usr/local/zabbix/sbin

 

3.客户端配置文件添加以下几行内容

[root@10-19-6-138 etc]# cat zabbix_agentd.conf | grep -v "^$" | grep -v "^#"
LogFile=/tmp/zabbix_agentd.log
Server=123.20.20.20
ServerActive=123.20.20.20
Hostname=test_web_174
##新加入以下内容
Include=/data/usr/local/zabbix/etc/zabbix_agentd.conf.d/*
UnsafeUserParameters=1
UserParameter=web.site.discovery,/data/usr/local/zabbix/scripts/web_site_code_status.sh web_site_discovery  
UserParameter=web.site.code[*],/data/usr/local/zabbix/scripts/web_site_code_status.sh web_site_code $1

4.现在我们重启一下客户端,然后在服务端测试一下

 

我们发现我们是能够测试成功的

此时返回状态为200

至此,脚本,客户端配置文件OK

接下来要做的就是zabbix 服务端的配置了

创建应用集

 

点击add

然后去创建发现规则

 

 

现在开始创建item

 

接下来要做的就是创建trigger报警了

 

 

 

 

这里需要注意的是,web.txt中的URL地址要是写错了的话,这边会一直报警,就算你给他改成正确的,我这边还是在报哪个错误的URL地址的警

这里的解决方法就是

到该主机监控项下面,把那个错误的监控项给停了

文章摘自:http://itnihao.blog.51cto.com/1741976/1129725

参考文档:https://www.zabbix.com/documentation/2.0/manual/discovery/low_level_discovery

此外,还可以参考我写的文档:http://code.google.com/p/auto-task-pe/downloads/list  zabbix自动发现监控http端口.pdf   如遇到问题,请先仔细阅读这2个文档

 

posted @ 2016-11-08 17:05  BigBao的博客  阅读(6023)  评论(0编辑  收藏  举报