代码如下,进攻大家参考,转载的话说明来源
前面5个变量自行补齐,
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
|
#!/usr/local/python3.51/bin/python3
# Author: Anxinhai@wondersgroup
# Mail: 29338383@qq.com
from
pyzabbix
import
ZabbixAPI
import
sys
from
re
import
compile
,IGNORECASE
ZABBIX_SERVER
=
""
USER
=
""
PASSWORD
=
""
HOSTNAME
=
""
URL
=
""
def
login(ZABBIX_SERVER,USER,PASSWORD):
zapi
=
ZabbixAPI(ZABBIX_SERVER)
zapi.login(USER,PASSWORD)
return
zapi
def
gethostid(auth,HOSTNAME):
request
=
ZabbixAPI.do_request(auth,
'host.get'
, params
=
{
"filter"
: {
"host"
:HOSTNAME}})
if
request[
'result'
]:
return
request[
'result'
][
0
][
'hostid'
]
else
:
print
(
"找不到该主机"
)
sys.exit(
1
)
def
getapplicationid(auth,hostid):
try
:
request
=
ZabbixAPI.do_request(auth,
'application.create'
, params
=
{
"name"
:
"web监控"
,
"hostid"
: hostid})
except
Exception as e:
print
(e)
request
=
ZabbixAPI.do_request(auth,
'application.get'
, params
=
{
"hostids"
: hostid})
for
num
in
range
(
0
,
len
(request[
'result'
])):
if
request[
'result'
][num][
'name'
]
=
=
"web监控"
:
return
request[
'result'
][num][
'applicationid'
]
def
create_web_scenario(auth,URL,hostid,applicationid):
request
=
ZabbixAPI.do_request(auth,
'httptest.get'
, params
=
{
"filter"
: {
"name"
: URL}})
if
request[
'result'
]:
print
(
'该web监控已经添加过了'
)
else
:
try
:
ZabbixAPI.do_request(auth,
'httptest.create'
,params
=
{
"name"
: URL,
"hostid"
: hostid,
"applicationid"
: applicationid,
"delay"
:
'60'
,
"retries"
:
'3'
,
"steps"
: [ {
'name'
: URL,
'url'
: URL,
'no'
:
'1'
} ] } )
except
Exception as e:
print
(e)
def
create_trigger(auth,HOSTNAME,URL):
expression
=
"{"
+
"{0}:web.test.fail[{1}].last()"
.
format
(HOSTNAME,URL)
+
"}"
+
"<>0"
try
:
ZabbixAPI.do_request(auth,
'trigger.create'
, params
=
{
"description"
:
"从监控机(172.18.11.34)访问{0}出现问题,如果网络和主机性能没问题,并且是单节点报错请尝试重启对应的tomcat"
.
format
(URL),
"expression"
: expression,
"priority"
:
5
})
except
Exception as e:
print
(e)
auth
=
login(ZABBIX_SERVER,USER,PASSWORD)
hostid
=
gethostid(auth,HOSTNAME)
applicationid
=
getapplicationid(auth,hostid)
create_web_scenario(auth,URL,hostid,applicationid)
create_trigger(auth,HOSTNAME,URL)
|
本文转自 superbigsea 51CTO博客,原文链接:http://blog.51cto.com/superbigsea/1767957