系统运维工程师 李超
利用zabbix的API功能可以方便地通过其他程序调用zabbix,从而实现灵活的扩展Zabbix方式。
一、zabbixAPI简介
Zabbix的API具有重要的功能,为第三方调用zabbix、批量操作提供可编程接口,从而轻松地用于自己的业务系统,将zabbix监控系统与运维系统相集成。zabbix API是基于前端HTTP协议实现的,也就是可以通过HTTP请求实现的API,数据传输采用JSON RPC协议。
JSON-RPC是基于JSON的跨语言远程调用协议,比XML-RPC、Webservice等基于文本的协议传输数据量要小;相比Hessian、java-RPC等二进制协议更便于调试、实现、扩展、是非常优秀的一种远程调用协议。目前主流语言都已有JSON-RPC的实现框架,java语言中较好的JSON-RPC实现框架有jsonrpc4j、Jproxy、JSON-RPC,其中jsonrpc4j既可以独立使用,又可以与Spring无缝结合,比较适合用于基于Spring的项目开发
二、zabbix API的简单使用
zabbix api相关:官方文档:https://www.zabbix.com/documentation/2.4/manual/api/
1、使用user.login方法
params后面的用户名和密码是zabbix的web界面登录名和密码!!!
#curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc": "2.0","method":"user.login","params":{"user":"admin","password":"密码xxx"},"auth": null,"id":0}' http://192.168.30.3/zabbix/api_jsonrpc.php
HTTP
/1
.1 200 OK
Date: Mon, 24 Oct 2016 10:33:34 GMT
Server: Apache
/2
.2.15 (CentOS)
X-Powered-By: PHP
/5
.3.3
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
Content-Length: 68
Connection: close
Content-Type: application
/json
{
"jsonrpc"
:
"2.0"
,
"result"
:
"a21db81b19908971f9a8518b5092b414"
,
"id"
:0}
执行OK:
到zabbix库中查看session
mysql>
select
* from zabbix.sessions;
+----------------------------------+--------+------------+--------+
| sessionid | userid | lastaccess | status |
+----------------------------------+--------+------------+--------+
| 0123b9ec3295562c23a13bc87e8f8b28 | 1 | 1457432079 | 0 |
| 0b13dce7637ea03667b2cae9199db559 | 2 | 1477302663 | 0 |
| 0e2ef1a01dca95bc23171d314d82f1c8 | 1 | 1469440342 | 0 |
| 1a6439ab2bcdd6e6a4dd549e02a09173 | 1 | 1473418801 | 0 |
| 294643838565c4b2c57902c06f0e6e12 | 2 | 1477302700 | 0 |
| 4062d9f3a89a14f734ea73299130a19f | 1 | 1465352471 | 0 |
| 5d14d651069a95c30f313753dfdcd541 | 1 | 1477304346 | 0 |
| 5f2cbe9a9c406f1053ffe8fd7c98e376 | 1 | 1477302828 | 1 |
| 750f97a0b59b00f8ea1a2af05e52c7c8 | 1 | 1464612638 | 0 |
| 910945f4f8ba5b27489c963e73619039 | 1 | 1477363990 | 0 |
| 925832711e34d009245232da998d9382 | 1 | 1453813826 | 0 |
| 986a8661ecab7676fd8716da60c1f9f0 | 1 | 1477375294 | 0 |
| a1d15429a29ae56e8a347647ba600d51 | 1 | 1453814055 | 0 |
| a21db81b19908971f9a8518b5092b414 | 1 | 1477305214 | 0 |
##可以看到了
| a43e362ed8268af31d8b15c8d3234acb | 1 | 1476275454 | 0 |
| b29b878a0868f344896b78f5cf843a5c | 2 | 1477302723 | 0 |
| b55a3b569c95b9158ec5bf67b8c4870f | 1 | 1476434671 | 0 |
| c4703a34d0afe9130cbb921995ad4f7e | 1 | 1473669065 | 0 |
| db2111abc2e6d96f6baaaa5089a538c1 | 1 | 1453962136 | 0 |
+----------------------------------+--------+------------+--------+
19 rows
in
set
(0.00 sec)
2、使用host.get方法
这里host留空,表示查看所有的
#curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc": "2.0","method":"host.get","params":{"output":"hostid","selectGroups":"extend","filter":{"host":""}},"auth":"a21db81b19908971f9a8518b5092b414","id":1}' http://192.168.30.3/zabbix/api_jsonrpc.php
HTTP
/1
.1 200 OK
Date: Tue, 25 Oct 2016 06:49:20 GMT
Server: Apache
/2
.2.15 (CentOS)
X-Powered-By: PHP
/5
.3.3
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
Connection: close
Transfer-Encoding: chunked
Content-Type: application
/json
{
"jsonrpc"
:
"2.0"
,
"result"
:[{
"hostid"
:
"10084"
,
"groups"
:[{
"groupid"
:
"4"
,
"name"
:
"Zabbix servers"
,
"internal"
:
"0"
,
"flags"
:
"0"
}]},{
"hostid"
:
"10105"
,
"groups"
:[{
"groupid"
:
"2"
,
"name"
:
"Linux servers"
,
"internal"
:
"0"
,
"flags"
:
"0"
}]},{
"hostid"
:
"10106"
,
"groups"
:
………………………… 太多了
…………………………
查看Linux servers组,如下:
#curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc": "2.0","method":"host.get","params":{"output":"extend","filter":{"host":"Linux servers"}},"auth":"a21db81b19908971f9a8518b5092b414","id":1}' http://192.168.30.3/zabbix/api_jsonrpc.php
HTTP
/1
.1 200 OK
Date: Tue, 25 Oct 2016 06:27:19 GMT
Server: Apache
/2
.2.15 (CentOS)
X-Powered-By: PHP
/5
.3.3
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
Content-Length: 36
Connection: close
Content-Type: application
/json
{
"jsonrpc"
:
"2.0"
,
"result"
:[],
"id"
:1}
3、查看zabbix监控的hosts,这里有一个脚本
#cat zabbixgethost.py
#!/usr/bin/env python
#coding=utf-8
#导入模块,urllib2是一个模拟浏览器HTTP方法的模块
import
json
import
urllib2
import
sys
from urllib2
import
Request, urlopen, URLError, HTTPError
#url and url header
#zabbix的api 地址,用户名,密码,这里修改为自己实际的参数
zabbix_url=
"http://192.168.30.3/zabbix/api_jsonrpc.php"
#10.0.18.12是我自己的zabbix serverip
zabbix_header = {
"Content-Type"
:
"application/json"
}
zabbix_user =
"admin"
#web界面进入zabbix的用户名
zabbix_pass =
"xxxx"
#web界面进入zabbix的密码
auth_code =
""
#auth user and password
#用户认证信息的部分,最终的目的是得到一个SESSIONID
#这里是生成一个json格式的数据,用户名和密码
auth_data = json.dumps(
{
"jsonrpc"
:
"2.0"
,
"method"
:
"user.login"
,
"params"
:
{
"user"
:zabbix_user,
"password"
:zabbix_pass
},
"id"
:0
})
# create request object
request = urllib2.Request(zabbix_url,auth_data)
for
key
in
zabbix_header:
request.add_header(key,zabbix_header[key])
#auth and get authid
try:
result = urllib2.urlopen(request)
#对于出错新的处理
except HTTPError, e:
print
'The server couldn\'t fulfill the request, Error code: '
, e.code
except URLError, e:
print
'We failed to reach a server.Reason: '
, e.reason
else
:
response=json.loads(result.
read
())
result.close()
#判断SESSIONID是否在返回的数据中
if
'result'
in
response:
auth_code=response[
'result'
]
else
:
print response[
'error'
][
'data'
]
# request json
json_data={
"method"
:
"host.get"
,
"params"
:{
"output"
:
"extend"
,
}
}
json_base={
"jsonrpc"
:
"2.0"
,
"auth"
:auth_code,
"id"
:1
}
json_data.update(json_base)
#用得到的SESSIONID去通过验证,获取主机的信息(用http.get方法)
if
len(auth_code) == 0:
sys.
exit
(1)
if
len(auth_code) != 0:
get_host_data = json.dumps(json_data)
# create request object
request = urllib2.Request(zabbix_url,get_host_data)
for
key
in
zabbix_header:
request.add_header(key,zabbix_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 response
#显示主机的个数
print
"Number Of Hosts: "
, len(response[
'result'
])
执行:
#python zabbixgethost.py
{u
'jsonrpc'
: u
'2.0'
, u
'result'
: [{u
'available'
: u
'1'
, u
'maintenance_type'
: u
'0'
, u
'ipmi_errors_from'
: u
'0'
, u
'ipmi_username'
: u
''
, u
'snmp_disable_until'
: u
'0'
, u
'ipmi_authtype'
: u
'-1'
, u
'ipmi_disable_until'
:
…………………… 省略
'10267'
, u
'name'
: u
'datainsight_backstage1'
, u
'jmx_errors_from'
: u
'0'
, u
'jmx_disable_until'
: u
'0'
, u
'flags'
: u
'0'
, u
'error'
: u
''
, u
'maintenance_from'
: u
'0'
, u
'errors_from'
: u
'0'
}], u
'id'
: 1}
Number Of Hosts: 460
一共有460台主机!
4、使用api批量添加新主机
脚本如下:
#cat zabbixhostcreate.py
#!/usr/bin/env python
#coding=utf-8
#导入模块,urllib2是一个模拟浏览器HTTP方法的模块
import
json
import
urllib2
import
sys
from urllib2
import
Request, urlopen, URLError, HTTPError
#url and url header
#zabbix的api 地址,用户名,密码,这里修改为自己实际的参数
zabbix_url=
"http://192.168.30.3/zabbix/api_jsonrpc.php"
zabbix_header = {
"Content-Type"
:
"application/json"
}
zabbix_user =
"admin"
zabbix_pass =
"xxxx"
auth_code =
""
#auth user and password
#用户认证信息的部分,最终的目的是得到一个SESSIONID
#这里是生成一个json格式的数据,用户名和密码
auth_data = json.dumps(
{
"jsonrpc"
:
"2.0"
,
"method"
:
"user.login"
,
"params"
:
{
"user"
:zabbix_user,
"password"
:zabbix_pass
},
"id"
:0
})
# create request object
request = urllib2.Request(zabbix_url,auth_data)
for
key
in
zabbix_header:
request.add_header(key,zabbix_header[key])
#auth and get authid
try:
result = urllib2.urlopen(request)
#对于出错新的处理
except HTTPError, e:
print
'The server couldn\'t fulfill the request, Error code: '
, e.code
except URLError, e:
print
'We failed to reach a server.Reason: '
, e.reason
else
:
response=json.loads(result.
read
())
result.close()
#判断SESSIONID是否在返回的数据中
if
'result'
in
response:
auth_code=response[
'result'
]
else
:
print response[
'error'
][
'data'
]
# request json
json_data={
"method"
:
"host.create"
,
"params"
: {
"host"
:
""
,
#新主机hostname,这里留空为了批量添加
"interfaces"
: [
{
"type"
: 1,
"main"
: 1,
"useip"
: 1,
"ip"
:
""
,
#新主机ip地址,这里留空为了批量添加
"dns"
:
""
,
"port"
:
"10050"
#zabbix client 端口
}
],
"groups"
: [
{
"groupid"
:
"2"
#组id
}
],
"templates"
: [
{
"templateid"
:
"10001"
#模板id
}
],
"inventory"
: {
"macaddress_a"
:
""
,
"macaddress_b"
:
""
}
}
}
json_base={
"jsonrpc"
:
"2.0"
,
"auth"
:auth_code,
"id"
:1
}
json_data.update(json_base)
print json_data
#用得到的SESSIONID去通过验证,获取主机的信息(用http.get方法)
with
open
(
"serverlist.txt"
) as f:
#这里是一个从serverlist获取新hosts的hostname和ip的方法
data=f.readlines()
f.close()
for
line
in
data:
host,ip=line.
split
()
json_data[
'params'
][
'host'
]=host
json_data[
'params'
][
'interfaces'
][0][
'ip'
]=ip
print json_data
#到这里结束
if
len(auth_code) == 0:
sys.
exit
(1)
if
len(auth_code) != 0:
get_host_data = json.dumps(json_data)
# create request object
request = urllib2.Request(zabbix_url,get_host_data)
for
key
in
zabbix_header:
request.add_header(key,zabbix_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 response
#显示主机的个数
print
"Number Of Hosts: "
, len(response[
'result'
])
开始批量添加
#cat serverlist.txt #将需要加入监控的新主机按照如下格式写入serverlist文件中
point_soa1 192.168.30.1
point_soa2 192.168.30.2
执行:
#python zabbixhostcreate.py
{
'jsonrpc'
:
'2.0'
,
'params'
: {
'templates'
: [{
'templateid'
:
'10001'
}],
'host'
:
''
,
'interfaces'
: [{
'ip'
:
''
,
'useip'
: 1,
'dns'
:
''
,
'main'
: 1,
'type'
: 1,
'port'
:
'10050'
}],
'groups'
: [{
'groupid'
:
'2'
}],
'inventory'
: {
'macaddress_b'
:
''
,
'macaddress_a'
:
''
}},
'method'
:
'host.create'
,
'auth'
: u
'550390eace5ebcd60168d791c38b4f0d'
,
'id'
: 1}
{
'jsonrpc'
:
'2.0'
,
'params'
: {
'templates'
: [{
'templateid'
:
'10001'
}],
'host'
:
'point_soa1'
,
'interfaces'
: [{
'ip'
:
'192.168.30.1'
,
'useip'
: 1,
'dns'
:
''
,
'main'
: 1,
'type'
: 1,
'port'
:
'10050'
}],
'groups'
: [{
'groupid'
:
'2'
}],
'inventory'
: {
'macaddress_b'
:
''
,
'macaddress_a'
:
''
}},
'method'
:
'host.create'
,
'auth'
: u
'550390eace5ebcd60168d791c38b4f0d'
,
'id'
: 1}
{u
'jsonrpc'
: u
'2.0'
, u
'result'
: {u
'hostids'
: [u
'10289'
]}, u
'id'
: 1}
Number Of Hosts: 1
{
'jsonrpc'
:
'2.0'
,
'params'
: {
'templates'
: [{
'templateid'
:
'10001'
}],
'host'
:
'point_soa2'
,
'interfaces'
: [{
'ip'
:
'192.168.30.2'
,
'useip'
: 1,
'dns'
:
''
,
'main'
: 1,
'type'
: 1,
'port'
:
'10050'
}],
'groups'
: [{
'groupid'
:
'2'
}],
'inventory'
: {
'macaddress_b'
:
''
,
'macaddress_a'
:
''
}},
'method'
:
'host.create'
,
'auth'
: u
'550390eace5ebcd60168d791c38b4f0d'
,
'id'
: 1}
{u
'jsonrpc'
: u
'2.0'
, u
'result'
: {u
'hostids'
: [u
'10290'
]}, u
'id'
: 1}
Number Of Hosts: 1
可以看到一共添加了2台host!到zabbix的web界面查看,是添加成功的!
但是以上的批量添加的脚本只能实现添加一个模板,即Template OS Linux (id为10001)这个基本模板,如果想添加2个后者多个模板,需要使用下面这个改动过的脚本,如下:
#cat zabbixhostcreate2.py
#!/usr/bin/env python
#coding=utf-8
#导入模块,urllib2是一个模拟浏览器HTTP方法的模块
import
json
import
urllib2
import
sys
from urllib2
import
Request, urlopen, URLError, HTTPError
#url and url header
#zabbix的api 地址,用户名,密码,这里修改为自己实际的参数
zabbix_url=
"http://192.168.30.3/zabbix/api_jsonrpc.php"
zabbix_header = {
"Content-Type"
:
"application/json"
}
zabbix_user =
"admin"
zabbix_pass =
"xxxx"
auth_code =
""
#auth user and password
#用户认证信息的部分,最终的目的是得到一个SESSIONID