Chinaunix首页 | 论坛 | 博客
  • 博客访问: 137077
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 309
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-06 11:27
个人简介

开启暴走模式。

文章分类

全部博文(31)

文章存档

2017年(19)

2016年(1)

2015年(11)

我的朋友

分类: Python/Ruby

2015-08-28 15:50:07

Zabbix API 其实没想象的那么难,了解JSON-RPC协议,支持的数据类型,按格式编写即可。基本和JAVA的json数据类型相似。JSON-RPC协议的知识会在Zabbix分类里后续介绍,敬请期待!

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- encoding: utf8 -*-

  3. #导入模块,urllib2是一个模拟浏览器HTTP方法的模块
  4. import json
  5. import urllib2
  6. import sys
  7. from urllib2 import Request,urlopen,URLError,HTTPError

  8. #url and url header
  9. #zabbix的API地址、用户名、密码、这里修改为实际的参数
  10. zabbix_url=" />
  11. zabbix_header = {"Content-Type":"application/json"}
  12. zabbix_user = "admin"
  13. zabbix_pass = "zabbix"
  14. auth_code = ""

  15. #auth user and password
  16. #用户认证信息的部分,最终的目的是得到一个SESSIONID
  17. #下面是生成一个JSON格式的数据:用户名和密码
  18. auth_data = json.dumps(
  19.         {
  20.                 "jsonrpc" : "2.0",
  21.                 "method" : "user.login",
  22.                 "params" :
  23.                                 {
  24.                                         "user":zabbix_user,
  25.                                         "password":zabbix_pass
  26.                                 },
  27.                 "id":0
  28.         })

  29. # create request object
  30. request = urllib2.Request(zabbix_url,auth_data)
  31. for key in zabbix_header:
  32.         request.add_header(key,zabbix_header[key])

  33. #认证和获取SESSION ID
  34. try:
  35.         result = urllib2.urlopen(request)
  36. #对于认证出错的处理
  37. except HTTPError,e:
  38.         print 'The server couldn\'t fulfill the request, Error code: ',e.code
  39. except URLError,e:
  40.         print 'We failed to reach a server.Reason: ',e.reason
  41. else:
  42.         response = json.loads(result.read())

  43. '''
  44. #如果访问成功或者失败,这里的数据会显示如下
  45. sucess result:
  46.         {"jsonrpc":"2.0",
  47.          "result": "182395ea90c1c983a6154dbe0b5bdb40",
  48.          "id":0
  49.         }
  50. error result:
  51.         {'code': -32602
  52.          'data': 'Login name or password is incorrect.',
  53.          'message':'Invalid params.'
  54.         }
  55. '''
  56. #判断SESSIONID是否在返回的数据中
  57. if 'result' in response:
  58.         auth_code = response['result']
  59. else:
  60.         print response['error']['data']

  61. json_data ={
  62.         "method":"host.get",
  63.         "params":{
  64.                         "output":"extend",
  65.         }
  66. }

  67. json_base={
  68.         "jsonrpc":"2.0",
  69.         "auth":auth_code,
  70.         "id":1
  71. }

  72. json_data.update(json_base)
  73. #用得到的SESSIONID去验证,获取主机的信息(用http.get方法)
  74. if len(auth_code) == 0:
  75.         sys.exit(1)
  76. if len(auth_code) != 0:
  77.         get_host_data = json.dumps(json_data)

  78.         #create request object
  79.         request = urllib2.Request(zabbix_url,get_host_data)
  80.         for key in zabbix_header:
  81.                 request.add_header(key,zabbix_header[key])

  82.         #get host list
  83.         try:
  84.                 result = urllib2.urlopen(request)
  85.         except URLError as e:
  86.                 if hasattr(e,'reason'):
  87.                         print 'We failed to reach a server.'
  88.                         print 'Reason: ',e.reason
  89.                 elif hasattr(e,'code'):
  90.                         print 'The server could not fulfill the request.'
  91.                         print 'Error code: ',e.code
  92.         else:
  93.                 response = json.loads(result.read())
  94.                 result.close()

  95.                 #将所有的主机信息显示出来
  96.                 print response
  97.                 #显示主机个数
  98.                 print "Number Of Hosts: ",len(response['result'])

例子来自于《Zabbix企业级分布式监控系统》Zabbix API 中,其实书中只是讲了简单的用法,还需要读者去举一反三,到官网上看文档,掌握一种学习的方法。
阅读(5754) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~