Here's a problem I had, I'd like to collect the data used to make the graphs in zabbix so that I can analyse that data further.
There are details on python libraries here, but it looks simple enough from the API reference so I'll give it a go myself.
I've identified that I eventually want to call the history.get function, but it looks like it needs an itemid as input and I only have a graphid for now. Some more searching brings up the graphitem.get function which will take in a graphid and return any itemid's used to build it.
So I need to:
Next up I need to get my token:
At this point I have my token that I can use with the rest of my requests.
I'll call the zabbix graph function first to get the itemid values, then the history.get function to get the values over time:
There are details on python libraries here, but it looks simple enough from the API reference so I'll give it a go myself.
I've identified that I eventually want to call the history.get function, but it looks like it needs an itemid as input and I only have a graphid for now. Some more searching brings up the graphitem.get function which will take in a graphid and return any itemid's used to build it.
So I need to:
- Authenticate, to get a token
- Get itemid's from graphitem.get call, using graphid as input
- Get itemid history for each itemid returned.
First step, some authentication, I'll use getpass and raw_input for this:
from getpass import getpass username = raw_input('Username: ') password = getpass('Password: ')
Now i have a username and password stored that I can use later - note that the password is stored in cleartext in python.
Next up I need to get my token:
import json import requests import warnings def zabbix_request(data=zabbix_version_request): warnings.simplefilter('ignore') response = requests.post(zabbix_api_url, data=data, headers=zabbix_json_rpc_header, verify=False) return response.json()['result'] def zabbix_authenticate(username, password): auth_request = { u'params': { u'password': password, u'user': username }, u'jsonrpc': u'2.0', u'method': u'user.login', u'auth': None, u'id': 1 } token = zabbix_request(data=json.dumps(auth_request)) return token token = zabbix_authenticate(username, password)The warnings module was used here to surpress the SSL certificate warning from requests as I've turned certificate verification off. It's good that it's there but if I was giving it to someone else I'd remove the warning suppression so they understand they're connecting to a potentially insecure service.
At this point I have my token that I can use with the rest of my requests.
I'll call the zabbix graph function first to get the itemid values, then the history.get function to get the values over time:
def zabbix_get_history(token, item, limit=10): request_data = { u'params': { u'itemids': item, u'sortfield': u'clock', u'limit': limit, u'sortorder': u'ASC', u'output': u'extend', u'history': 0 }, u'jsonrpc': u'2.0', u'method': u'history.get', u'auth': token, u'id': 1 } return zabbix_request(data=json.dumps(request_data)) def zabbix_get_graph_items(token, graphid): request_data = { "jsonrpc": "2.0", "method": "graphitem.get", "params": { "output": "extend", "expandData": 1, "graphids": graphid }, "auth": token, "id": 1 } return zabbix_request(data=json.dumps(request_data)) graphitems = zabbix_get_graph_items(token, graphid) history = [] for graphitem in graphitems: history.append = zabbix_get_history(token, graphitem['itemid'], limit=100)And here we should have two values in the history list showing 100 of the latest values for each of the items in the graphid specified. This gets me exactly what I need. It also highlights that I've created two functions that look almost identical...
No comments:
Post a Comment