Created
January 8, 2015 02:48
-
-
Save eerien/41680ad4de2e3769182b to your computer and use it in GitHub Desktop.
Zabbix API class for Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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