-
Notifications
You must be signed in to change notification settings - Fork 0
/
getValue.py
99 lines (97 loc) · 2.92 KB
/
getValue.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
97
98
99
#coding=utf-8
import json
import urllib2,time
# based url and required header
url = "http://192.168.168.147/zabbix/api_jsonrpc.php"
header = {"Content-Type":"application/json"}
# request json
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"history.get",
# history参数可能的取值
# 0 - float;
# 1 - string;
# 2 - log;
# 3 - integer;
# 4 - text.
"params":{
"output":"extend",
"history":3,
"sortfield":"clock",
"itemids":"24407",
"sortorder":"DESC",
"limit":600
},
"auth":"eb9c2a73493243deef3b8de155bc13d6", # theauth id is what auth script returns, remeber it is string
"id":1,
})
# create request object
request = urllib2.Request(url,data)
for key in header:
request.add_header(key,header[key])
# get host list
try:
result = urllib2.urlopen(request)
except Exception 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 "in- Number Of Hosts: ", len(response['result'])
for host in response['result']:
# print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(float(host["clock"]))) + ' , ' + str(int(host["value"]))
# print time.strftime("%Y-%m-%d",time.localtime())
print host["clock"],host["value"]
#
#
#
# data = json.dumps(
# {
# "jsonrpc":"2.0",
# "method":"history.get",
# # history参数可能的取值
# # 0 - float;
# # 1 - string;
# # 2 - log;
# # 3 - integer;
# # 4 - text.
# "params":{
# "output":"extend",
# "history":3,
# "sortfield":"clock",
# "itemids":"24115",
# "sortorder":"DESC",
# "limit":60
# },
# "auth":"a560febb370e15a80c0aaa3a4543451c", # theauth id is what auth script returns, remeber it is string
# "id":1,
# })
# # create request object
# request = urllib2.Request(url,data)
# for key in header:
# request.add_header(key,header[key])
# # get host list
# try:
# result = urllib2.urlopen(request)
# except Exception 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 "========== 华丽丽的分割线 ==========="
# print "out- Number Of Hosts: ", len(response['result'])
# for host in response['result']:
# print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) + ' , ' + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(float(host["clock"]))) + ' , ' + str(int(host["value"])/1024)
# # print time.strftime("%Y-%m-%d",time.localtime())
# #print "Host ID:",host['hostid'],"HostName:",host['name']