Skip to content

Instantly share code, notes, and snippets.

@quiver
Created February 10, 2013 06:52
Show Gist options
  • Save quiver/4748667 to your computer and use it in GitHub Desktop.
Save quiver/4748667 to your computer and use it in GitHub Desktop.
Python script to demonstrate usage of Zabbix 1.8 API. Depends on requests module. (http://docs.python-requests.org/) API spec : https://www.zabbix.com/documentation/1.8/api
# vim: set fileencoding=utf8
#
# authenticate with zabbix api server, and retrieve monitored hosts
# specification : https://www.zabbix.com/documentation/1.8/api
#
# $ python zabbix_api.py
import requests
from pprint import pprint
import json
ZABIX_ROOT = 'http://localhost/zabbix'
url = ZABIX_ROOT + '/api_jsonrpc.php'
########################################
# user.login
########################################
payload = {
"jsonrpc" : "2.0",
"method" : "user.login",
"params": {
'user': 'api',
'password':'api',
},
"auth" : None,
"id" : 0,
}
headers = {
'content-type': 'application/json',
}
res = requests.post(url, data=json.dumps(payload), headers=headers)
res = res.json()
print 'user.login response'
pprint(res)
########################################
# host.get
########################################
payload = {
"jsonrpc" : "2.0",
"method" : "host.get",
"params": {
'output': [
'hostid',
'name'],
},
"auth" : res['result'],
"id" : 2,
}
res2 = requests.post(url, data=json.dumps(payload), headers=headers)
res2 = res2.json()
print 'host.get response'
pprint(res2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment