因公司業務需要,引進了自動化運維,所用到的監控平台為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']
輸出結果: