系統運維工程師 李超php
利用zabbix的API功能能夠方便地經過其餘程序調用zabbix,從而實現靈活的擴展Zabbix方式。
前端
1、zabbixAPI簡介
java
Zabbix的API具備重要的功能,爲第三方調用zabbix、批量操做提供可編程接口,從而輕鬆地用於本身的業務系統,將zabbix監控系統與運維繫統相集成。zabbix API是基於前端HTTP協議實現的,也就是能夠經過HTTP請求實現的API,數據傳輸採用JSON RPC協議。python
JSON-RPC是基於JSON的跨語言遠程調用協議,比XML-RPC、Webservice等基於文本的協議傳輸數據量要小;相比Hessian、java-RPC等二進制協議更便於調試、實現、擴展、是很是優秀的一種遠程調用協議。目前主流語言都已有JSON-RPC的實現框架,java語言中較好的JSON-RPC實現框架有jsonrpc4j、Jproxy、JSON-RPC,其中jsonrpc4j既能夠獨立使用,又能夠與Spring無縫結合,比較適合用於基於Spring的項目開發mysql
2、zabbix API的簡單使用
nginx
zabbix api相關:官方文檔:https://www.zabbix.com/documentation/2.4/manual/api/web
一、使用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)
二、使用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}
三、查看zabbix監控的hosts,這裏有一個腳本sql
#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臺主機!
四、使用api批量添加新主機數據庫
腳本以下:apache
#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
#這裏是生成一個json格式的數據,用戶名和密碼
auth_data = json.dumps(
{
"jsonrpc"
:
"2.0"
,
"method"
:
"user.login"
,
"params"
:
{
"user"
:zabbix_user,
"password"
:zabbix_pass
},
"id"
:0
})