zoukankan      html  css  js  c++  java
  • 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地址

  • 相关阅读:
    [Python] Unofficial Windows Binaries for Python Extension Packages
    [SublimeText] 之 Packages
    [Windows] Windows 8.x 取消触摸板切换界面
    [Shell] Backtick vs $() 两种方式内嵌值
    [OSX] 在 OS X 中安装 MacPorts 指南
    [OSX] 使用 MacPorts 安装 Python 和 pip 指南
    关于 g++ 编译器
    梦想成真,喜获微软MVP奖项,微软MVP FAQ?
    拥抱.NET Core,如何开发一个跨平台类库 (1)
    拥抱.NET Core,学习.NET Core的基础知识补遗
  • 原文地址:https://www.cnblogs.com/mikeluwen/p/6149654.html
Copyright © 2011-2022 走看看