這兩天一直做一個(gè)叫集群配置管理平臺(tái)的自動(dòng)化項(xiàng)目,寫了有 20 多天了,項(xiàng)目做的還算順利,只是一堆的接口需要寫,有點(diǎn)煩。因?yàn)?clusterops 項(xiàng)目到最后肯定是要和監(jiān)控平臺(tái)做結(jié)合的,這兩天也抽時(shí)間看了下。以前自己也寫過不少類似 zabbix 的接口調(diào)用教程,當(dāng)時(shí)看的時(shí)候,由于時(shí)間有限,也都是草草跑 demo。
請(qǐng)大家多關(guān)注下我的獨(dú)立博客,更多的關(guān)于 zabbix 二次開發(fā)的話題,http://xiaorui.cc
zabbix 的接口挺好理解,任何的程序都可以寫,甚至是 linux 的 curl 命令。我這邊用 python 的 urllib、urllib2 來搞的,當(dāng)然會(huì) php 的就更好了,因?yàn)?zabbix 的接口是 php 寫的,懂 php 可以直接用現(xiàn)成的。
zabbix 官網(wǎng)有大量的接口,你只要會(huì)用 zabbix,然后看下 api 的說明,應(yīng)該就沒啥問題了
https://www.zabbix.com/documentation/1.8/api
簡單說三個(gè)例子,入個(gè)門。
獲取 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']
添加主機(jī)
#!/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
我個(gè)人覺得 zabbix 的 rest api 難點(diǎn)在于 key 相關(guān)的認(rèn)證,會(huì)了之后,再看官網(wǎng)的 api 文檔就一目了然了。
在我的集群平臺(tái)下,我可以把暫時(shí)下線的服務(wù)器,在平臺(tái)上去除,但是大家有沒有想到,你要是吧主機(jī)刪掉后,監(jiān)控端會(huì)有一堆的通知發(fā)給你,所以,在處理主機(jī)的時(shí)候,順便調(diào)用 zabbix 的接口,把該主機(jī)的監(jiān)控項(xiàng)目給刪掉。
在我通過 saltstack 添加 lvs 后端主機(jī)的時(shí)候,我也同樣可以調(diào)用接口,把后端的主機(jī)相應(yīng)的監(jiān)控都給加進(jìn)去。
就先這樣,有時(shí)間再豐富下該文章。
本文出自 “峰云,就她了?!?博客,謝絕轉(zhuǎn)載!