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
92
93
94
95
96
97
98
99
100
101
102
| #!/usr/bin/env python
# coding:utf-8
##########################
# Function : 该script可以针对部署的监控进行的检测,也可进行日常巡检
# Editer : graysky
# Version : 1.0.0
# Mail : 714810243@qq.com
# Date : 2016-08-14
##########################
import json
import urllib2
import xlrd
class api_work:
def __init__(self, url, user, passwd):
self.url = url
self.user = user
self.passwd = passwd
self.login_authid = self.zabbix_login()
def json_work(self, work_json):
zabbix_header = {"Content-Type": "application/json"}
self.work_json = work_json
used_json = json.dumps(self.work_json)
used_json_reques = urllib2.Request(self.url, used_json)
for key in zabbix_header:
used_json_reques.add_header(key, zabbix_header[key])
try:
used_json_result = urllib2.urlopen(used_json_reques)
except Exception:
print "Get failed"
else:
used_json_respones = json.loads(used_json_result.read())
used_json_group = used_json_respones['result']
used_json_result.close()
return used_json_group
def zabbix_login(self):
login_json = {"jsonrpc": "2.0",
"method": "user.login",
"params": {"user": self.user, "password": self.passwd},
"id": 0}
login_authid = self.json_work(login_json)
return login_authid
def get_hostinfo(self):
hostinfo_group = {}
host_json = {"jsonrpc": "2.0",
"method": "host.get",
"params": {"output": "extend",
},
"auth": self.zabbix_login(),
"id": 0,}
for hostinfo in self.json_work(host_json):
hostinfo_group[hostinfo["hostid"]] = hostinfo["host"]
return hostinfo_group
def get_itemsinfo_by_hostidmonitor(self,hostid,monitor_key):
itemsinfo_group = {}
items_json = {"jsonrpc": "2.0",
"method": "item.get","params":
{"output": "extend",
"hostids": hostid,
"search":{"key_": monitor_key},
"sortfield": "name"
},
"auth": self.login_authid,
"id": 0}
for itemsinfo in self.json_work(items_json):
itemsinfo_group[itemsinfo['itemid']] = itemsinfo['status']
return itemsinfo_group
if __name__ == "__main__":
def provincework(provinceinfo,key_info):
if provinceinfo == "xxxx":
zabbix_url = "http://xxxx/zabbix/api_jsonrpc.php"
zabbix_user = "Admin"
zabbix_passwd = "xxxx"
elif provinceinfo == "yyyy":
zabbix_url = "http://yyyyy/zabbix/api_jsonrpc.php"
zabbix_user = "Admin"
zabbix_passwd = "yyyy"
api_init = api_work(zabbix_url,zabbix_user,zabbix_passwd)
hostinfogroup = api_init.get_hostinfo()
'''
for hostip in hostinfogroup.values():
print hostip
'''
ItemsErrorHostIP = []
for hostid in hostinfogroup.keys():
itemsstatusinfo_group = api_init.get_itemsinfo_by_hostidmonitor(hostid,key_info)
for itemsstatusinfo in itemsstatusinfo_group.values():
if int(itemsstatusinfo) > 0 :
ItemsErrorHostIP.append(hostinfogroup[hostid])
return ItemsErrorHostIP
provincegroup = ['xxxx','yyyy']
keyinfo = 'OS.Core.Dir.Change[/etc/init.d/]'
for province in provincegroup:
erroritemsipgroup = provincework(province,keyinfo)
if len(erroritemsipgroup) == 0 :
print "省份%s检测结果: 添加完成-通过 "%province
else:
print "省份%s检测结果: 未添加完成-未通过IP如下:"%province
for erroritemsip in erroritemsipgroup:
type(erroritemsip)
print "\t\t\t\t%s"%erroritemsip
|