需求:
现在有大量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