Zabbix API 其实没想象的那么难,了解JSON-RPC协议,支持的数据类型,按格式编写即可。基本和JAVA的json数据类型相似。JSON-RPC协议的知识会在Zabbix分类里后续介绍,敬请期待!
-
#!/usr/bin/env python
-
# -*- encoding: utf8 -*-
-
-
#导入模块,urllib2是一个模拟浏览器HTTP方法的模块
-
import json
-
import urllib2
-
import sys
-
from urllib2 import Request,urlopen,URLError,HTTPError
-
-
#url and url header
-
#zabbix的API地址、用户名、密码、这里修改为实际的参数
-
zabbix_url=" />
-
zabbix_header = {"Content-Type":"application/json"}
-
zabbix_user = "admin"
-
zabbix_pass = "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])
-
-
#认证和获取SESSION ID
-
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())
-
-
'''
-
#如果访问成功或者失败,这里的数据会显示如下
-
sucess result:
-
{"jsonrpc":"2.0",
-
"result": "182395ea90c1c983a6154dbe0b5bdb40",
-
"id":0
-
}
-
error result:
-
{'code': -32602
-
'data': 'Login name or password is incorrect.',
-
'message':'Invalid params.'
-
}
-
'''
-
#判断SESSIONID是否在返回的数据中
-
if 'result' in response:
-
auth_code = response['result']
-
else:
-
print response['error']['data']
-
-
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'])
例子来自于《Zabbix企业级分布式监控系统》Zabbix API 中,其实书中只是讲了简单的用法,还需要读者去举一反三,到官网上看文档,掌握一种学习的方法。
阅读(5801) | 评论(0) | 转发(0) |