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

前言:

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

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.reasonelif 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.reasonelif 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

原文:

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

啥时候用?

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

在我通过saltstack添加lvs后端主机的时候,我也同样可以调用接口,把后端的主机相应的监控都给加进去。

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

本文出自 “峰云,就她了。” 博客,谢绝转载!

每天告诉自己一次,『我真的很不错』

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

相关文章:

你感兴趣的文章:

标签云: