关于python调用zabbix api接口
因公司业务需要,引进了自动化运维,所用到的监控平台为zbbix3.2,最近正在学习python,计划使用python调用zabbix api接口去做些事情,如生成报表,我想最基本的是要取得zabbix中的数据,这是第一步,今天先体验了一把,已经成功获取得到部分数据,所以记录下来。
操作系统:win10
zabbix版本:3.2
python版本:2.7.14
IDE:PyCharm 2017.2.3
Build #PY-172.3968.37, built on September 1, 2017
Licensed to smile
JRE: 1.8.0_152-release-915-b11 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
1. user.login方法获取zabbix server的认证密钥。官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/user/login
python实现方法:
#!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 # based url and required header url = "http://192.168.0.217/zabbix/api_jsonrpc.php" header = {"Content-Type":"application/json"} # auth user and password data = json.dumps( { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 0 }) # create request object request = urllib2.Request(url,data) for key in header: request.add_header(key,header[key]) # auth and get authid try: result = urllib2.urlopen(request) except urllib2.URLError as e: print "Auth Failed, Please Check Your Name AndPassword:",e.reason else: response = json.loads(result.read()) result.close() print"Auth Successful. The Auth ID Is:",response['result']
输出结果:
2.hostgroup.get方法获取所有主机组ID。把第一步获得的认证密钥填写到“auth”中,每次获取数据时都需要认证。此处是获取zabbix server上的所有主机组名称与ID号。
官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/hostgroup/get
python实现方法:
#!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 # based url and required header url = "http://192.168.0.217/zabbix/api_jsonrpc.php" header = {"Content-Type":"application/json"} # request json data = json.dumps( { "jsonrpc":"2.0", "method":"hostgroup.get", "params":{ "output":["groupid","name"], }, "auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth id is what auth script returns, remeber it is string "id":1, }) # create request object request = urllib2.Request(url,data) for key in header: request.add_header(key,header[key]) # get host list try: result = urllib2.urlopen(request) except urllib2.URLError as e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'The server could not fulfill the request.' print 'Error code: ', e.code else: response = json.loads(result.read()) result.close() print "Number Of Hosts: ", len(response['result']) #print response for group in response['result']: print "Group ID:",group['groupid'],"\tGroupName:",group['name']
输出结果:
3.host.get方法获取单个主机组下所有的主机ID。把第二点中获取到的主机组id,填入到下面代码“groupids”中,即可获得该主机组下所有的主机id。
官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/host/get
python实现方法:
#!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 # based url and required header url = "http://192.168.0.217/zabbix/api_jsonrpc.php" header = {"Content-Type":"application/json"} # request json data = json.dumps( { "jsonrpc":"2.0", "method":"host.get", "params":{ "output":["hostid","name"], "groupids":"8", }, "auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth id is what auth script returns, remeber it is string "id":1, }) # create request object request = urllib2.Request(url,data) for key in header: request.add_header(key,header[key]) # get host list try: result = urllib2.urlopen(request) except urllib2.URLError as e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'The server could not fulfill the request.' print 'Error code: ', e.code else: response = json.loads(result.read()) result.close() print "Number Of Hosts: ", len(response['result']) for host in response['result']: print "Host ID:",host['hostid'],"HostName:",host['name']
输出结果:
4.itemsid.get方法获取单个主机下所有的监控项ID。根据第三点中获取到的所有主机id与名称,找到你想要获取的主机id,填写到下面代码“hostids”中,获取它下面的所有监控项。
官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/item/get
python实现方法:
#!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 # based url and required header url = "http://192.168.0.217/zabbix/api_jsonrpc.php" header = {"Content-Type":"application/json"} # request json data = json.dumps( { "jsonrpc":"2.0", "method":"item.get", "params":{ "output":["itemids","key_"], "hostids":"10123", }, "auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth id is what auth script returns, remeber it is string "id":1, }) # create request object request = urllib2.Request(url,data) for key in header: request.add_header(key,header[key]) # get host list try: result = urllib2.urlopen(request) except urllib2.URLError as e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'The server could not fulfill the request.' print 'Error code: ', e.code else: response = json.loads(result.read()) result.close() print "Number Of Hosts: ", len(response['result']) for host in response['result']: print host #print "Host ID:",host['hostid'],"HostName:",host['name']
输出结果:
5.history.get方法获取单个监控项的历史数据。根据第4点获取到的所有items id的值,找到想要监控的那项,填写到下面代码“itemids”中,获取它的历史数据。
官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/history/get
python实现方法:
#!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 # based url and required header url = "http://192.168.0.217/zabbix/api_jsonrpc.php" header = {"Content-Type":"application/json"} # request json data = json.dumps( { "jsonrpc":"2.0", "method":"history.get", "params":{ "output":"extend", "history":3, "itemids":"27019", "limit":10 }, "auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth id is what auth script returns, remeber it is string "id":1, }) # create request object request = urllib2.Request(url,data) for key in header: request.add_header(key,header[key]) # get host list try: result = urllib2.urlopen(request) except urllib2.URLError as e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'The server could not fulfill the request.' print 'Error code: ', e.code else: response = json.loads(result.read()) result.close() print "Number Of Hosts: ", len(response['result']) for host in response['result']: print host #print "Host ID:",host['hostid'],"HostName:",host['name']
输出结果: