zabbix批量添加web监控

最近有那么一批的域名希望添加到zabbix的Web scenarios。想着每个域名还得添加告警触发器,这样工作重复性就大了。所以利用zabbix的API,写了一个批量添加的脚本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# coding: utf-8
from __future__ import print_function
from pyzabbix import ZabbixAPI

server_name_in_zabbix = 'my server'
hostid = # This is what host you want to add the scenarios
applicationid = # This is what application you want to add
retries = 1
delay = 10
status = 200
the_string_required = 'Copyright'
agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.8 (KHTML, like Gecko) Chrome/17.0.940.0 Safari/535.8'


def auth():
zapi = ZabbixAPI("http://zabbix url")
zapi.login("my username", "my secret")
return zapi


def get_host_name(login_zabbix):
host_dict = login_zabbix.host.get(filter={"name": server_name_in_zabbix})[0]
host_name = host_dict.get('host')
return host_name


def get_host_id(login_zabbix):
host_dict = login_zabbix.host.get(filter={"name":server_name_in_zabbix})[0]
hostid = host_dict.get("hostid")
return hostid


def get_url(login_zabbix, host_name):
with open('your file') as f:
for url in f:
monitor_url = url
name = monitor_url.split('//')[-1].split('\n')[0]
create_web_scenarios(login_zabbix, host_name, monitor_url=monitor_url, name=name)


def create_web_scenarios(login_zabbix, host_name, monitor_url, name):
request = login_zabbix.httptest.get(filter={"name": name})
if request:
print ('"{}" is exist'.format(name))
create_trigger(login_zabbix, host_name, name)
else:
login_zabbix.httptest.create({
"name": name,
"agent": agent,
"hostid": hostid,
"applicationid": applicationid,
"delay": delay,
"retries": retries,
"steps": [{
'name': name+' index',
'url': monitor_url,
'required': the_string_required,
'status_codes': status,
'no': '1'}]
})
create_trigger(login_zabbix, host_name, name)


def create_trigger(login_zabbix, host_name, name):
login_zabbix.trigger.create({
"description": "{} http code got an exception".format(name),
"expression": "{%s:web.test.rspcode[%s,%s index].last()}>400" % (host_name, name, name),
"priority": "2"
})
login_zabbix.trigger.create({
"description": "{} response slow now".format(name),
"expression": "{%s:web.test.time[%s,%s index,resp].count(2m,10,"gt")}>5" % (host_name, name, name),
"priority": "2"
})
login_zabbix.trigger.create({
"description": "{} seems error".format(name),
"expression": "{%s:web.test.error[%s].str(required pattern,3)}=1" % (host_name, name),
"priority": "2"
})


def main():
login_zabbix = auth()
host_name = get_host_name(login_zabbix)
# host_id = get_host_id(login_zabbix)
get_url(login_zabbix, host_name)
print ("Everything is done,please check on your zabbix server")

if __name__ == '__main__':
main()

具体这段代码的使用,首先需要准备一个逐行记录待添加域名的文件,然后将文件路径替换代码里的your file,这里没有写成参数的形式,偷懒了一把,哈哈。

其次将代码里server_name_in_zabbix的值替换成需要添加主机的Visible name。这里我只用到利用Visible name提取host_name。所以代码里备注了,自己将hostid和applicationid填写进去。我用的hostid和applicationid,是用API的方式找到然后填写进去的。虽然host_id的提取写好了,但是没有用上。

最后在auth函数里,把自己环境的zabbix url以及用户名和密码填写好,并用pip install pyzabbix安装好模块,就可以使用了。使用完成,会生成三个触发器:状态码异常报警,响应时间慢报警以及访问页面没有匹配我定义的Copyright字符报警。

目前我的这段代码完成度不是很高,不过在GitHub上看到这么一个脚本,地址戳这里。他的完成度感觉还是不错的。不过他利用的zabbix api是2.2的版本,如果你的zabbix版本在2.4及以上,需要将脚本里webcheck替换成httptest