前言:
這兩天一直做一個叫叢集配置管理平臺的自動化專案,寫了有20多天了,專案做的還算順利,只是一堆的介面需要寫,有點煩。因為clusterops專案到最後肯定是要和監控平臺做結合的,這兩天也抽時間看了下。 以前自己也寫過不少類似zabbix的介面呼叫教程,當時看的時候,由於時間有限,也都是草草跑demo。
請大家多關注下我的獨立部落格,更多的關於zabbix二次開發的話題,http://xiaorui.cc
zabbix的介面挺好理解,任何的程式都可以寫,甚至是linux的curl命令。我這邊用python的urllib、urllib2來搞的,當然會php的就更好了,因為zabbix的介面是php寫的,懂php可以直接用現成的。
zabbix官網有大量的介面,你只要會用zabbix,然後看下api的說明,應該就沒啥問題了
https://www.zabbix.com/documentation/1.8/api
簡單說三個例子,入個門。
獲取KEY
!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 # based url and required header url = "http://monitor.example.com/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 URLError as e: print "Auth Failed, Please Check Your Name And Password:",e.code else: response = json.loads(result.read()) result.close() print "Auth Successful. The Auth ID Is:",response[`result`]
獲取hostlist
#!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 #xiaorui.cc url = "http://10.10.10.61/api_jsonrpc.php" header = {"Content-Type": "application/json"} # request json data = json.dumps( { "jsonrpc":"2.0", "method":"host.get", "params":{ "output":["hostid","name"], "filter":{"host":""} }, "auth":"dbcd2bd8abc0f0320fffab34c6d749d3", "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 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`],"Host Name:",host[`name`]
新增主機
#!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 #xiaorui.cc url = "http://10.10.10.61/api_jsonrpc.php" header = {"Content-Type": "application/json"} # request json data = json.dumps( { "jsonrpc":"2.0", "method":"host.create", "params":{ "host": "10.10.10.67","interfaces": [{"type": 1,"main": 1,"useip": 1,"ip": "10.10.10.67","dns": "","port": "10050"}], "groups": [{"groupid": "2"}],"templates": [{"templateid": "10087"}] }, "auth":"dbcd2bd8abc0f0320fffab34c6d749d3", "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 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 `ok`zai
原文: http://rfyiamcool.blog.51cto.com/1030776/1358792
我個人覺得zabbix的rest api難點在於key相關的認證,會了之後,再看官網的api文件就一目瞭然了。
啥時候用?
在我的叢集平臺下,我可以把暫時下線的伺服器,在平臺上去除,但是大家有沒有想到,你要是吧主機刪掉後,監控端會有一堆的通知發給你,所以,在處理主機的時候,順便呼叫zabbix的介面,把該主機的監控專案給刪掉。
在我通過saltstack新增lvs後端主機的時候,我也同樣可以呼叫介面,把後端的主機相應的監控都給加進去。
就先這樣,有時間再豐富下該文章。