File: history_data.py

package info (click to toggle)
pyzabbix 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: python: 903; makefile: 45
file content (51 lines) | stat: -rw-r--r-- 1,156 bytes parent folder | download
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
"""
Retrieves history data for a given numeric (either int or float) item_id
"""

import time
from datetime import datetime

from pyzabbix import ZabbixAPI

# The hostname at which the Zabbix web interface is available
ZABBIX_SERVER = "http://localhost/zabbix"

zapi = ZabbixAPI(ZABBIX_SERVER)

# Login to the Zabbix API
zapi.login("Admin", "zabbix")

item_id = "item_id"

# Create a time range
time_till = time.mktime(datetime.now().timetuple())
time_from = time_till - 60 * 60 * 4  # 4 hours

# Query item's history (integer) data
history = zapi.history.get(
    itemids=[item_id],
    time_from=time_from,
    time_till=time_till,
    output="extend",
    limit="5000",
)

# If nothing was found, try getting it from history (float) data
if not len(history):
    history = zapi.history.get(
        itemids=[item_id],
        time_from=time_from,
        time_till=time_till,
        output="extend",
        limit="5000",
        history=0,
    )

# Print out each datapoint
for point in history:
    print(
        "{}: {}".format(
            datetime.fromtimestamp(int(point["clock"])).strftime("%x %X"),
            point["value"],
        )
    )