zabbix自动发现监控url

1.在监控客户机上
自动发现监控url中,url如果是带%的,原来的脚本会有问题,printf 输出的时候%会被解析 解决办法如下新修改的脚本
2.如果url 也就是自动一参数中带特殊字符的时候,是使用不了的
Allow all characters to be passed in arguments to user-defined parameters. Supported since Zabbix 1.8.2.
The following characters are not allowed:
\ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @
Additionally, newline characters are not allowed.
解决办法:在客户端修改配置文件
UnsafeUserParameters=1
web_site_code_status.sh:
#!/bin/bash

UrlFile="/opt/scripts/WEB.txt"
IFS=$'\n'

web_site_discovery () {
    WEB_SITE=($(cat $UrlFile|grep -v "^#"))
    printf '{\n'
    printf '\t"data":[\n'
    num=${#WEB_SITE[@]}
    for site in ${WEB_SITE[@]}
    do
        num=$(( $num - 1 ))
    url=$(echo $site|awk '{print $1}')
    ip=$(echo $site|awk '{print $2}')
        if [ $num -ne 0 ] ; then
            printf "\t\t{\"{#SITENAME}\":\""${url}"\",\"{#PROXYIP}\":\""${ip}"\"},\n"
        else

            printf "\t\t{\"{#SITENAME}\":\""${url}"\",\"{#PROXYIP}\":\""${ip}"\"}\n"
            printf '\t]\n'
            printf '}\n'
        fi
    done
}

web_site_code () {
    if [ "$2" == "" ]; then
        curl -s --connect-timeout 2 -m 4 -o /dev/null -w %{http_code} $1
    elif echo $2 |grep ':' &>/dev/null ; then
        curl -s --connect-timeout 2 -m 4 -o /dev/null -w %{http_code} $1 -x $2
    else
        curl -s --connect-timeout 2 -m 4 -o /dev/null -w %{http_code} $1 -x $2:80
    fi
}

case "$1" in
    web_site_discovery)
        web_site_discovery
        ;;
    web_site_code)
        web_site_code $2 $3
        ;;
    *)
        echo "Usage:$0 {web_site_discovery|web_site_code [URL]}"
        ;;
esac

修改过的脚本:

#!/bin/bash

UrlFile="/opt/scripts/WEB.txt"
IFS=$'\n'

web_site_discovery () {
    WEB_SITE=($(cat $UrlFile|grep -v "^#"))
    printf '{\n'
    printf '\t"data":[\n'
    num=${#WEB_SITE[@]}
    for site in ${WEB_SITE[@]}
    do
        num=$(( $num - 1 ))
        url=$(echo $site|awk '{print $1}')
        ip=$(echo $site|awk '{print $2}')
        if [ $num -ne 0 ] ; then
            printf "\t\t{\"{#SITENAME}\":\""%s"\",\"{#PROXYIP}\":\""${ip}"\"},\n" ${url}
        else
            printf "\t\t{\"{#SITENAME}\":\""%s"\",\"{#PROXYIP}\":\""${ip}"\"}\n" ${url}
            printf '\t]\n'
            printf '}\n'
        fi
    done
}

web_site_code () {
    if [ "$2" == "" ]; then
        curl -s --connect-timeout 2 -m 4 -o /dev/null -w %{http_code} "$1"
    elif echo $2 |grep ':' &>/dev/null ; then
        curl -s --connect-timeout 2 -m 4 -o /dev/null -w %{http_code} "$1" -x $2
    else
        curl -s --connect-timeout 2 -m 4 -o /dev/null -w %{http_code} "$1" -x $2:80
    fi
}

case "$1" in
    web_site_discovery)
        web_site_discovery
        ;;
    web_site_code)
        web_site_code $2 $3
        ;;
    *)
        echo "Usage:$0 {web_site_discovery|web_site_code [URL]}"
        ;;
esac

 

 

 
/opt/scripts/WEB.txt 在脚本路径创建WEB.txt文件,文件内容为要监控url,格式如下:

http://www.baidu.com
http://www.sina.com.cn
http://www.163.com 10.10.10.10
http://www.sohu.com 115.23.16.97:80
http://www.111.com

在zabbix客户端加配置文件:
vim /etc/zabbix/zabbix_agentd.d/web_site_discovery.conf

UserParameter=web.site.discovery,/opt/scripts/web_site_code_status.sh web_site_discovery
UserParameter=web.site.code[*],/opt/scripts/web_site_code_status.sh web_site_code $1 $2

测试是否正常:
$ zabbix_get -s 10.0.0.109 -k web.site.discovery
{
"data":[
{
"{#SITENAME}":"www.baidu.com"},
{
"{#SITENAME}":"www.sina.com.cn"},
{
"{#SITENAME}":"www.****.com"},
{
"{#SITENAME}":"www.****.com"}]}
$ zabbix_get -s 10.0.0.109 -k web.site.code[www.163.com]
200
在zabbix server添加:
1.首先选定主机,创建application,application用来管理各种item,可以归类
2.创建discovery 规则: 里面有个keep lost resource period 时间,这个时间就是监控url更新没有后保留的时间 ,我设置为0表示,WEB.txt文件更新后,监控跟着更新
 
 
3.创建item,选择创建的application
4.创建trriger:选择select prototype
 添加报警就不说了,根据状态码或者相关报警
 后面的就是网站真实ip地址,不加默认直接监控,不通过代理ip。

基本步骤如上,模板也做出来了,脚本,zabbix模板更新在github上,有疑问可以留言探讨

github地址

posted @ 2016-12-09 15:19  行知散人  阅读(5750)  评论(8编辑  收藏  举报