Skip to content

Instantly share code, notes, and snippets.

@eerien
Created January 8, 2015 02:48
Show Gist options
  • Save eerien/41680ad4de2e3769182b to your computer and use it in GitHub Desktop.
Save eerien/41680ad4de2e3769182b to your computer and use it in GitHub Desktop.
Zabbix API class for Python
import requests
import json
class ZabbixAPI():
ZBX_COMMON_HEADERS = {'Content-Type': 'application/json-rpc'}
def __init__(self, url, username, password):
self.url = url
self._login(username, password)
def _login(self, username, password):
data = {
'jsonrpc': '2.0',
'method': 'user.login',
'params': {
'user': username,
'password': password
},
'id': 1
}
r = requests.post(self.url, headers=self.ZBX_COMMON_HEADERS, data=json.dumps(data))
r.raise_for_status()
self._auth_key = r.json()['result']
def request(self, method, in_params):
assert type(method) == str
assert type(in_params) == dict
assert self._auth_key
params = {
'output': 'extend'
}
params.update(in_params)
data = {
'jsonrpc': '2.0',
'method': method,
'params': params,
'auth': self._auth_key,
'id': 1
}
r = requests.post(self.url, headers=self.ZBX_COMMON_HEADERS, data=json.dumps(data))
r.raise_for_status()
return r.json()['result']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment