zabbix☞web监控

阅读量: zyh 2020-05-14 09:34:44
Categories: > Tags:

1. 构建 zabbix_agentd 端配置

选择一个主机用于发起监控

# 目录结构
[root@ip-10-230-10-105 zabbix]# pwd
/etc/zabbix
[root@ip-10-230-10-105 zabbix]# tree {etc,shell}
etc
├── zabbix_agentd.conf
├── zabbix_agentd.conf.bak
└── zabbix_agentd.conf.d
    └── http_status.conf # 我是 zabbix_agentd 数据项配置

shell
└── web
    ├── http_status.py # 我是自动发现脚本 + 数据采集脚本

    └── WEB.txt  # 我是自动发现的数据源


2 directories, 5 files
# zabbix_agentd 配置 http_status.conf
UserParameter=web.site.code[*],/usr/bin/python /etc/zabbix/shell/web/http_status.py web_site_code $1
UserParameter=web.site.discovery,/usr/bin/python /etc/zabbix/shell/web/http_status.py web_site_discovery
# 自动发现规则配置文件 WEB.txt
# 一行一个监控地址
# get 原样写入
# post 模仿get多加一个?
https://abc.com??<post_kv>
https://abc.com?<get_kv>
https://abc.com

#!/usr/bin/env python
#encoding=utf-8
# python2.7
# 自动发现脚本 + 数据采集脚本 http_status.py
# 请将我添加 o+x 权限
# ConfigParser 模块是需要安装的
import urllib2, sys, json, ConfigParser, os 

a1 = sys.argv[1]
def web_site_code(args):
    response = None
    WEB_TXT = "%s/WEB.txt" % (os.path.dirname(os.path.realpath(__file__)))
    with open(WEB_TXT, 'ro') as f:
        for line in f.readlines():
            line = line.strip()
            if args in line:
                if "??" in line:
                    line = line.split("??")
                elif "?" in line:
                    line = line.split("?")
                else:
                    pass
                try:
                    try:
                        response = urllib2.urlopen(line[0], data=line[1], timeout=5)
                        print response.code
                    except IndexError:
                        response = urllib2.urlopen(line[0], timeout=5)
                        print response.code
                    finally:
                        response = urllib2.urlopen(line, timeout=5)
                        print response.code
                except urllib2.URLError,e:
                    if hasattr(e, 'code'):
                        print e.code
                    elif hasattr(e, 'reason'):
                        print 500
                    else:
                        print 500
                finally:
                    if response:
                        response.close()
                    exit()

def web_site_discovery():
    Dict = {"data":[]}
    WEB_TXT = "%s/WEB.txt" % (os.path.dirname(os.path.realpath(__file__)))
    for line in open(WEB_TXT):
        if "??" in line:
            line = line.strip('\n').split("??")
            line = {"{#SITENAME}":line[0]}
        elif "?" in line:
            line = line.strip('\n').split("?")
            line = {"{#SITENAME}":line[0]}
        else:
            line = line.strip('\n')
            line = {"{#SITENAME}":line}
        Dict["data"].append(line)
    print json.dumps(Dict, indent=2)

if a1 == 'web_site_code':
    url = sys.argv[2]
    web_site_code(url)
elif a1 == 'web_site_discovery':
    web_site_discovery()

{ #SITENAME} 自动发现脚本输出的重要变量 servername 地址,将会用于 web 控制台配置

2. 构建 web 控制台配置