<h2>前言:</h2>


    這兩天一直做一個叫集群配置管理平台的自動化項目,寫了有 20 多天了,項目做的還算順利,隻是一堆的接口需要寫,有點煩。因為 clusterops 項目到最後肯定是要和監控平台做結合的,這兩天也抽時間看了下。以前自己也寫過不少類似 zabbix 的接口調用教程,當時看的時候,由於時間有限,也都是草草跑 demo。


    請大家多關注下我的獨立博客,更多的關於 zabbix 二次開發的話題,http://xiaorui


    zabbix 的接口挺好理解,任何的程序都可以寫,甚至是 linux 的 curl 命令。我這邊用 python 的 urllib、urllib2 來搞的,當然會 php 的就更好了,因為 zabbix 的接口是 php 寫的,懂 php 可以直接用現成的。


    zabbix 官網有大量的接口,你隻要會用 zabbix,然後看下 api 的說明,應該就沒啥問題了


    https://.zabbix/documentation/1.8/api


    簡單說三個例子,入個門。


    獲取 key


    !/usr/bin/env python2.7 #coding=utf-8import jsonimport urllib2 # based url and required headerurl = "http://monitor.example/api_jsonrpc.php"header = {"content-type": "application/json"} # auth user and passworddata = json.dumps({    "jsonrpc": "2.0",    "method": "user.login",    "params": {    "user": "admin",    "password": "zabbix"},"id": 0}) # create request objectrequest = urllib2.request(url,data)for key in header:    request.add_header(key,header[key]) # auth and get authidtry:    result = urllib2.urlopen(request)except urlerror as e:    print "auth failed, please check your name and password:",e.codeelse:    response = json.loads(result.read)    result.close    print "auth sessful. the auth id is:",response[\''result\'']  </pre>


    獲取 hostlist


    #!/usr/bin/env python2.7 #coding=utf-8import jsonimport urllib2 #xiaoruiurl = "http://10.10.10.61/api_jsonrpc.php"header = {"content-type": "application/json"} # request jsondata = json.dumps({    "jsonrpc":"2.0",    "method":"host.get",    "params":{"output":["hostid","name"],"filter":{"host":""}    },    "auth":"dbcd2bd8abc0f0320fffab34c6d749d3",    "id":1,}) # create request objectrequest = urllib2.request(url,data)for key in header:    request.add_header(key,header[key]) # get host listtry:    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.codeelse:    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\'']  </pre>


    添加主機


    #!/usr/bin/env python2.7 #coding=utf-8import jsonimport urllib2 #xiaoruiurl = "http://10.10.10.61/api_jsonrpc.php"header = {"content-type": "application/json"} # request jsondata = 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"}],"temtes": [{"temteid": "10087"}]},    "auth":"dbcd2bd8abc0f0320fffab34c6d749d3",    "id":1,}) # create request objectrequest = urllib2.request(url,data)for key in header:    request.add_header(key,header[key]) # get host listtry:    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.codeelse:    response = json.loads(result.read)    result.close    print \''ok\''zai  </pre>


    原文: http://rfyiamcool.blog.51cto/1030776/1358792


    我個人覺得 zabbix 的 rest api 難點在於 key 相關的認證,會了之後,再看官網的 api 文檔就一目了然了。  <h2>啥時候用?</h2>


    在我的集群平台下,我可以把暫時下線的服務器,在平台上去除,但是大家有沒有想到,你要是吧主機刪掉後,監控端會有一堆的通知發給你,所以,在處理主機的時候,順便調用 zabbix 的接口,把該主機的監控項目給刪掉。


    在我通過 saltstack 添加 lvs 後端主機的時候,我也同樣可以調用接口,把後端的主機相應的監控都給加進去。


    就先這樣,有時間再豐富下該文章。


    本文出自 “峰雲,就她了。” 博客,謝絕轉載!

章節目錄

閱讀記錄

Python實戰-從菜鳥到大牛的進階之路所有內容均來自互聯網,uu小說網隻為原作者極客學院的小說進行宣傳。歡迎各位書友支持極客學院並收藏Python實戰-從菜鳥到大牛的進階之路最新章節