• 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=$'
    '
    
    web_site_discovery () {
        WEB_SITE=($(cat $UrlFile|grep -v "^#"))
        printf '{
    '
        printf '	"data":[
    '
        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 "		{"{#SITENAME}":""${url}"","{#PROXYIP}":""${ip}""},
    "
            else
    
                printf "		{"{#SITENAME}":""${url}"","{#PROXYIP}":""${ip}""}
    "
                printf '	]
    '
                printf '}
    '
            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=$'
    '
    
    web_site_discovery () {
        WEB_SITE=($(cat $UrlFile|grep -v "^#"))
        printf '{
    '
        printf '	"data":[
    '
        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 "		{"{#SITENAME}":""%s"","{#PROXYIP}":""${ip}""},
    " ${url}
            else
                printf "		{"{#SITENAME}":""%s"","{#PROXYIP}":""${ip}""}
    " ${url}
                printf '	]
    '
                printf '}
    '
            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地址

  • 相关阅读:
    BSGS
    [AT1252] IOIOI カード占い
    [十二省联考2019]春节十二响
    [CF912E] Prime Gift
    CDQ分治
    [CF747F] Igor and Interesting Numbers
    [十二省联考2019]异或粽子
    51Nod 2128 前缀异或
    51Nod 3212 数字变位
    HDU 1106 排序
  • 原文地址:https://www.cnblogs.com/mikeluwen/p/6149654.html
Copyright © 2020-2023  润新知