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地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM