python联合saltstack调用zabbix api接口实现自定义监控

前言:

    前段时间一直忙着开发叫集群配置管理平台,写了有20多天了,项目做的还算顺利,只是一堆的接口需要写,有点烦。因为clusterops项目到最后肯定是要和监控平台做结合的,这两天也抽时间看了下。   以前自己也写过不少类似zabbix的接口调用教程,当时看的时候,由于时间有限,也都是草草跑demo,这次来真的了。 


请大家多关注下我的独立博客,更多的关于zabbix二次开发的话题,xiaorui.cc


       zabbix的接口看起来很是友好。我这边用python的urllib、urllib2来搞的,当然会php的就更好了,因为zabbix的接口是php写的,懂php的可以直接从zabbix源码中获取。

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

我个人觉得zabbix的rest api难点在于key相关的认证,会了之后,再看官网的api文档就一目了然了。

啥时候用?

在我的集群平台下,我可以把暂时下线的服务器,在平台上去除,但是大家有没有想到,你要是吧主机删掉后,监控端会有一堆的通知发给你,所以,在处理主机的时候,顺便调用zabbix的接口,把该主机的监控项目给删掉。

    在我通过saltstack添加lvs后端主机的时候,我也同样可以调用接口,把后端的主机相应的监控都给加进去。  今天说的有些入门,明天来个给力的。 




大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

1 Response

  1. knight 2016年2月28日 / 上午12:34

    博主 要是用pyzabbix就方便多了 省了很多事情

发表评论

邮箱地址不会被公开。 必填项已用*标注