Skip to content

Commit 486e40d

Browse files
committed
renamed and fixed wrong "trend_data.py" into "history_data.py"
1 parent 3ef875c commit 486e40d

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

examples/history_data.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Retrieves history data for a given numeric (either int or float) item_id
3+
"""
4+
5+
from pyzabbix import ZabbixAPI
6+
from datetime import datetime
7+
import time
8+
9+
# The hostname at which the Zabbix web interface is available
10+
ZABBIX_SERVER = 'http://localhost/zabbix'
11+
12+
zapi = ZabbixAPI(ZABBIX_SERVER)
13+
14+
# Login to the Zabbix API
15+
zapi.login('Admin', 'zabbix')
16+
17+
item_id = 'item_id'
18+
19+
# Create a time range
20+
time_till = time.mktime(datetime.now().timetuple())
21+
time_from = time_till - 60 * 60 * 4 # 4 hours
22+
23+
# Query item's history (integer) data
24+
history = zapi.history.get(itemids=[item_id],
25+
time_from=time_from,
26+
time_till=time_till,
27+
output='extend',
28+
limit='5000',
29+
)
30+
31+
# If nothing was found, try getting it from history (float) data
32+
if not len(history):
33+
history = zapi.history.get(itemids=[item_id],
34+
time_from=time_from,
35+
time_till=time_till,
36+
output='extend',
37+
limit='5000',
38+
history=0,
39+
)
40+
41+
# Print out each datapoint
42+
for point in history:
43+
print("{0}: {1}".format(datetime.fromtimestamp(int(point['clock']))
44+
.strftime("%x %X"), point['value']))

0 commit comments

Comments
 (0)