-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbix_interface_json.py
executable file
·73 lines (54 loc) · 2.17 KB
/
zabbix_interface_json.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
#!/usr/bin/env python2.7
"""
Zabbix API Wrapper by Espen Holm Nilsen
Hackish script to return some values I needed from Zabbix, returns beautiful json output
that can be used by other scripts/things/graphers/etc!
I am using this with openhab and highcharts to do whatever magic I need at home.
Requires: pyzabbix (pip install pyzabbix)
Run as CGI, call with this_script.py?itemid=<itemid>
Demo:
$ curl "http://192.168.1.1/cgi/zinterf.py?itemid=25895"
{"itemid": "25895", "ago": "1 minutter siden", "ns": "565486588", "value": "29.0000", "clock": "1450859171"}
$
OpenHAB config:
$ cat /etc/openhab/configurations/transform/getvalue.js
JSON.parse(input).value;
$ grep getvalue /etc/openhab/configurations/items/*.items
Number Temperature_FF_Living "Temperature [%.2f C]" <temperature> (Temperature, FF_Living) { http="<[http://192.168.1.1/zinterf.py?itemid=25882:10000:JS(getvalue.js)]" }
"""
from pyzabbix import ZabbixAPI
from datetime import datetime
import time, json, sys
import cgi
form = cgi.FieldStorage()
ZABBIX_SERVER = "https://my.zabbix/server/"
zapi = ZabbixAPI(ZABBIX_SERVER)
zapi.login('zabbix_api_user', 'password')
item_id = form.getvalue('itemid')
history = zapi.history.get(itemids=[item_id], history='0', sortfield='clock', sortorder='DESC', output='extend', limit='1')
lastvalue = 0.0
def humanAgo(seconds):
if seconds > 86400:
return """%d dager siden""" % (seconds/3600)
if seconds > 3600:
return """%d timer siden""" % (seconds/3600)
if seconds > 60:
return """%d minutter siden""" % (seconds/60)
else:
return """%d sekunder siden""" % seconds
if len(history) == 0:
currvalue = ""
statechange = int(time.time())
print "Content-type: application/json\n"
history = zapi.history.get(itemids=[int(item_id)], history='3', output='extend', limit='20000', sortfield='clock', sortorder='DESC')
for p in reversed(history):
if p['value'] != currvalue:
currvalue = p['value']
statechange = p['clock']
print json.dumps({'value': currvalue, 'ago': humanAgo(int(time.time()) - int(statechange))})
sys.exit(0)
last = history[0]
print "Content-type: application/json"
print
last['ago'] = humanAgo(int(time.time()) - int(last['clock']))
print json.dumps(last)