Zabbix API——开启运维监控自动化之路

随着互联网对监控需求的升级与扩大,Zabbix API 在监控中的角色也愈来愈重要,尤其是在集成第三方软件和自动化日常任务时。想象一下,如果没有自动化技术,管理数千台服务器是多么的困难!
Zabbix API 为自动化的批量操作和第三方软件集成以及其他作用提供可编程接口,从而提升了Zabbix功能上的各种可能性。安全性上,Zabbix API 中间件使得架构更加模块化也避免直接对数据库进行操作。
Zabbix API典型工作流

使用 API 的基本步骤
- 准备JSON对象,它描述了你想要做什么(创建主机,获取图像,更新监控项等)。
- 采用POST方法向http://example.com/zabbix/apijsonrpc.php发送此JSON对象. http://example.com/zabbix/是Zabbix前端地址。apijsonrpc.php是调用API的PHP脚本。可在安装可视化前端的目录下找到。
- 获取 SESSIONID
- 通过 SESSIONID 建立后续的连接
- 提交 POST 数据,格式为 JSON,其中放对应的方法,获取需要的数据。
Zabbix API功能与应用
Zabbix API 提供两项主要功能:
- 远程管理 Zabbix 配置
- 远程检索配置和历史数据
Zabbix API允许以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问;
它被广泛用于:
- 创建新的应用程序以使用Zabbix;
- 将Zabbix与第三方软件集成;
- 自动执行日常任务。
例如:1)使用zabbix进行批量管理,比如:我们要添加1000台主机。
2)使用zabbix结合微信、email、钉钉等进行移动端的报警。
Zabbix API的使用

使用 curl 模拟 API 的使用
1.获取认证
在访问Zabbix内部的任何数据之前,需要登录并获取身份验证令牌。这可以使用该user.login方法完成。假设要以标准Zabbix Admin用户身份登录。JSON请求如下所示:
$ curl -s -X POST -H 'Content-Type:application/json' -d '
> {
> "jsonrpc": "2.0",
> "method": "host.get",
> "params": {
> "user": "Admin",
> "password": "zabbix"
> },
> "id": 1
> }' http://172.16.241.130/zabbix/api_jsonrpc.php | python -m json.tool
{
"jsonrpc": "2.0",
"result": "581cc92624202bddaeff3a90cca181dc",
"id": 1
}请求对象的具体属性:
- jsonrpc- API使用的JSON-RPC协议版本; Zabbix API实现了JSON-RPC 2.0版;
- method- 被调用的API方法;
- params- 将传递给API方法的参数;
- id - 请求的任意标识符;
- auth - 用户认证令牌; 既然还没有,那就设定了null。
2.用获取的 SESSIONID 去调用 API 的 host.get 方法请求 hostid
$ curl -s -X POST -H 'Content-Type:application/json' -d '
> {
> "jsonrpc": "2.0",
> "method": "host.get",
> "params": {
> "output": ["hostid"]
> },
> "auth": "581cc92624202bddaeff3a90cca181dc",
> "id": 1
> }' http://172.16.241.130/zabbix/api_jsonrpc.php | python -m json.tool
{
"jsonrpc": "2.0",
"result": [
{
"hostid": "10084"
}
],
"id": 1
}响应对象的属性:
- jsonrpc - 再次,JSON-RPC协议的版本;
- result - 方法返回的数据;
- id - 相应请求的标识符。
python 调用 zabbix api 接口的自动化实例

1.获取 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']2.获取 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']目前已支持25个API接口,更多的应用请查看API官方文档。

相关推荐
染血白衣 2020-11-16
SAMXIE 2020-11-04
一个智障 2020-11-15
学习web前端 2020-11-09
yiranpiaoluo 2020-11-04
lxhuang 2020-11-03
82387067 2020-11-03
huangliuyu00 2020-10-29
Dayer 2020-10-27
小马的学习笔记 2020-10-23
liuweiITlove 2020-10-14
kjyiyi 2020-10-10
fanjunjaden 2020-10-09
zhyue 2020-09-28
huangliuyu00 2020-09-24
88397813 2020-09-23
jyj0 2020-09-21