26 關於 python 調用 zabbix api 接口的自動化實例 [結合 saltstack]

前言:

這兩天一直做一個叫集群配置管理平台的自動化項目,寫了有 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-8import jsonimport urllib2 # based url and required headerurl = "http://monitor.example.com/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 Successful. The Auth ID Is:",response[\'result\']  

獲取 hostlist

 #!/usr/bin/env python2.7 #coding=utf-8import jsonimport urllib2 #xiaorui.ccurl = "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\']  

添加主機

 #!/usr/bin/env python2.7 #coding=utf-8import jsonimport urllib2 #xiaorui.ccurl = "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"}],"templates": [{"templateid": "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  

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

我個人覺得 zabbix 的 rest api 難點在於 key 相關的認證,會了之後,再看官網的 api 文檔就一目瞭然了。

啥時候用?

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

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

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

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

《Python實戰-從菜鳥到大牛的進階之路》