Skip to content

Instantly share code, notes, and snippets.

@chenryn
Created July 24, 2014 11:43
Show Gist options
  • Save chenryn/be67c4f4af0fb43229f1 to your computer and use it in GitHub Desktop.
Save chenryn/be67c4f4af0fb43229f1 to your computer and use it in GitHub Desktop.
get zabbix history then plot it
#!/usr/bin/env python
"""
Read item history from zabbix, and plot as histogram
"""
import matplotlib
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import requests
import json
import time
from datetime import datetime
ZABBIX_URI = 'http://127.0.0.1/zabbix/api_jsonrpc.php'
ZABBIX_USR = 'user'
ZABBIX_PWD = 'pass'
HOURS = 24 * 1
def zabbixLogin(user, passwd):
params = {
'user':user,
'password':passwd
}
return zabbixCall('user.login', params)
def zabbixCall(method='', params={}, auth=''):
data = {
'jsonrpc':'2.0',
'method':method,
'params':params,
'id':1
}
if len(auth) != 0:
data['auth'] = auth
r = requests.post(ZABBIX_URI, data=json.dumps(data), headers={'content-type':'application/json-rpc'})
return r.json()['result']
authId = zabbixLogin(ZABBIX_USR, ZABBIX_PWD)
print('Get Auth ID: ' + authId)
params = {
'groupids':21,
'hostids':11036,
'graphids':1824829
}
items = zabbixCall('item.get', params, authId)
begin = time.mktime(datetime.now().timetuple()) - 3600 * HOURS
print('Begin loop for history...')
for item in items:
params = {
'output':'extend',
'history':0,
'itemids':item['itemid'],
'time_from':begin
}
ret = zabbixCall('history.get', params, authId)
# for x in ret:
# print int(x['clock']),',',float(x['value'])
history = map(lambda x: float(x['value']), ret)
v = np.array(history)
plt.figure()
plt.hist(v, bins=200, normed=1)
plt.title('item: ' + item['itemid'])
# lline = numpy.percentile(v, 25)
# uline = numpy.percentile(v, 75)
# length = uline - lline
# low = lline - length
# up = uline + length
# print(low, up)
plt.figure()
plt.boxplot(v, sym='+', notch=True)
plt.title('item: ' + item['itemid'])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment