/
12.3.6-zabbixhostget.py
executable file
·96 lines (87 loc) · 2.74 KB
/
12.3.6-zabbixhostget.py
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
#!/usr/bin/env python
#coding=utf-8
#导入模块,urllib2是一个模拟浏览器HTTP方法的模块
import json
import urllib2
import sys
from urllib2 import Request, urlopen, URLError, HTTPError
#url and url header
#zabbix的api 地址,用户名,密码,这里修改为自己实际的参数
zabbix_url="http://zabbix-gui.itnihao.com/api_jsonrpc.php"
zabbix_header = {"Content-Type":"application/json"}
zabbix_user = "admin"
zabbix_pass = "zabbix"
auth_code = ""
#auth user and password
#用户认证信息的部分,最终的目的是得到一个SESSIONID
#这里是生成一个json格式的数据,用户名和密码
auth_data = json.dumps(
{
"jsonrpc":"2.0",
"method":"user.login",
"params":
{
"user":zabbix_user,
"password":zabbix_pass
},
"id":0
})
# create request object
request = urllib2.Request(zabbix_url,auth_data)
for key in zabbix_header:
request.add_header(key,zabbix_header[key])
#auth and get authid
try:
result = urllib2.urlopen(request)
#对于出错新的处理
except HTTPError, e:
print 'The server couldn\'t fulfill the request, Error code: ', e.code
except URLError, e:
print 'We failed to reach a server.Reason: ', e.reason
else:
response=json.loads(result.read())
result.close()
#判断SESSIONID是否在返回的数据中
if 'result' in response:
auth_code=response['result']
else:
print response['error']['data']
# request json
json_data={
"method":"host.get",
"params":{
"output": "extend",
}
}
json_base={
"jsonrpc":"2.0",
"auth":auth_code,
"id":1
}
json_data.update(json_base)
#用得到的SESSIONID去通过验证,获取主机的信息(用http.get方法)
if len(auth_code) == 0:
sys.exit(1)
if len(auth_code) != 0:
get_host_data = json.dumps(json_data)
# create request object
request = urllib2.Request(zabbix_url,get_host_data)
for key in zabbix_header:
request.add_header(key,zabbix_header[key])
# get host list
try:
result = urllib2.urlopen(request)
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server could not fulfill the request.'
print 'Error code: ', e.code
else:
response = json.loads(result.read())
result.close()
#将所有的主机信息显示出来
print response
#显示主机的个数
print "Number Of Hosts: ", len(response['result'])