继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

关于python调用zabbix api接口的自动化实例 [结合saltstack]

holdtom
关注TA
已关注
手记 1846
粉丝 240
获赞 991


前言:

   这两天一直做一个叫集群配置管理平台的自动化项目,写了有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后端主机的时候,我也同样可以调用接口,把后端的主机相应的监控都给加进去。

就先这样,有时间再丰富下该文章。

©著作权归作者所有:来自51CTO博客作者rfyiamcool的原创作品,谢绝转载,否则将追究法律责任

zabbix apipython zabbixzabbix二次开发自动化项目


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP