zabbix3批量监控URL

zabixserver,agentd安装

模板文件下载

1、zabbix server导入模板

2、agent端添加配置文件

下载压缩包的url.conf配置文件内容追加到zabbix_agentd.conf里面

[root@M2 ~] cat url.conf   >> /usr/local/zabbix/conf/zabbix_agentd.conf

脚本路径请自行修改

UnsafeUserParameters=1 
UserParameter=web.site.discovery,/usr/local/zabbix/scripts/web_site_code_status.sh web_site_discovery 
UserParameter=web.site.code[*],/usr/local/zabbix/scripts/web_site_code_status.sh web_site_code $1

3、将脚本导入agentd的脚本目录,请注意脚本路径

下载压缩包的web.txt和web_site_code_status.sh 导入到zabbix_agentd定义的脚本目录,并根据实际情况对web_site_code_status.sh 的web.txt路径进行修改

脚本添加可以执行权限

[root@M2 ~] chmod +x web_site_code_status.sh
[root@M2 ~] chown -R zabbix.zabbix /usr/local/zabbix/

重启

[root@M2 ~] service zabbix_agentd  restart

web.txt记录需要监控的URL,一行一条需要监控的URL

通过zabbix_get程序查看能否获取正确的监控URL

[root@M2 ~] ./zabbix_get -s 192.168.30.1  -p10050 -k web.site.discovery

[root@M2 ~] ./zabbix_get -s 192.168.130.2  -p10050 -k web.site.code[http://www.baidu.com]

4、主机添加模板

5、等待120S后就能自动发现

到此说明URL批量监控已经添加成功,后期需要添加URL监控直接在agentd的web.txt文件添加就可以了

6、web_site_code_status脚本内容

shell:

#!/bin/bash 
# function:monitor tcp connect status from zabbix 
source /etc/bashrc >/dev/null 2>&1 
source /etc/profile >/dev/null 2>&1 
#/usr/bin/curl -o /dev/null -s -w {5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418}{http_code} $1 

web_site_discovery () { 
WEB_SITE=($(cat /usr/local/zabbix/scripts/web.txt|grep -v "^#")) 
printf '{\n' 
printf '\t"data":[\n' 
for((i=0;i<${#web_site[@]};++i)) { num="$(echo" $((${#web_site[@]}-1))) if [ "$i" !="${num}" ]; then printf "\t\t{ \n" "\t\t\t\"{#sitename}\":\"${web_site[$i]}\"},\n" else "\t\t\t\"{#sitename}\":\"${web_site[$num]}\"}]}\n" fi } web_site_code () usr bin curl -o dev null -s -w {5749fe182deba6f703e69800a8cc3afb9894ad400f350437bd2be724fa41f418}{http_code} $1 case "$1" in web_site_discovery) web_site_discovery ;; web_site_code) $2 *) echo "usage:$0 {web_site_discovery|web_site_code [url]}" esac < pre>

python:

#!/usr/bin/python
import re
import os, sys
import  requests
import json
def web_site_discovery():
    file="/web/data/zabbix4.0/interface_url.txt"
    with open(file,'r') as f:
        lines=f.read()
    li=lines.split("\n")
    while '' in li:
        li.remove('')
    url_name=[]
    for app in li:
        if len(app) != 0:
            url_name.append({'{#STATUS_NAME}': app})
    print(json.dumps({'data': url_name}, indent=4, separators=(',', ':')))

a="web_site_discovery"
b="web_site_code"
def web_site_code(bb):
    try:
        code=requests.get(bb,timeout=5).status_code
        print(code)
    except Exception as e:
        print("Faild")
if len(sys.argv) == 2 and sys.argv[1] == a:
    web_site_discovery()
elif len(sys.argv) == 3 and sys.argv[1] ==  b:
    web_site_code(sys.argv[2])
else:
    c=sys.argv[0]
    print("Usage:%s {web_site_discovery|web_site_code [URL]}" % (c))