设为首页 收藏本站
查看: 1233|回复: 0

[经验分享] 关于python调用zabbix api接口

[复制链接]

尚未签到

发表于 2018-1-1 15:40:43 | 显示全部楼层 |阅读模式
  因公司业务需要,引进了自动化运维,所用到的监控平台为zbbix3.2,最近正在学习python,计划使用python调用zabbix api接口去做些事情,如生成报表,我想最基本的是要取得zabbix中的数据,这是第一步,今天先体验了一把,已经成功获取得到部分数据,所以记录下来。
  操作系统:win10
  zabbix版本:3.2
  python版本:2.7.14
  IDE:PyCharm 2017.2.3
  Build #PY-172.3968.37, built on September 1, 2017
  Licensed to smile
  JRE: 1.8.0_152-release-915-b11 amd64
  JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
  1. user.login方法获取zabbix server的认证密钥。官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/user/login
  python实现方法:
  

#!/usr/bin/env python2.7  
#
coding=utf-8  
import json
  
import urllib2
  
# based url and required header
  
url = "http://192.168.0.217/zabbix/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 urllib2.URLError as e:
  print "Auth Failed, Please Check Your Name AndPassword:",e.reason
  
else:
  response = json.loads(result.read())
  result.close()

  print"Auth Successful. The Auth>  

  输出结果:
DSC0000.png

  2.hostgroup.get方法获取所有主机组ID。把第一步获得的认证密钥填写到“auth”中,每次获取数据时都需要认证。此处是获取zabbix server上的所有主机组名称与ID号。
  官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/hostgroup/get
  python实现方法:
  

#!/usr/bin/env python2.7  
#
coding=utf-8  
import json
  
import urllib2
  
# based url and required header
  
url = "http://192.168.0.217/zabbix/api_jsonrpc.php"
  
header = {"Content-Type":"application/json"}
  
# request json
  
data = json.dumps(
  
{
  "jsonrpc":"2.0",
  "method":"hostgroup.get",
  "params":{
  "output":["groupid","name"],
  },

  "auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth>  "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 urllib2.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'])
  #print response
  for group in response['result']:

  print "Group>  

  输出结果:
DSC0001.png

  3.host.get方法获取单个主机组下所有的主机ID。把第二点中获取到的主机组id,填入到下面代码“groupids”中,即可获得该主机组下所有的主机id。
  官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/host/get
  python实现方法:
  

#!/usr/bin/env python2.7  
#
coding=utf-8  
import json
  
import urllib2
  
# based url and required header
  
url = "http://192.168.0.217/zabbix/api_jsonrpc.php"
  
header = {"Content-Type":"application/json"}
  
# request json
  
data = json.dumps(
  
{
  "jsonrpc":"2.0",
  "method":"host.get",
  "params":{
  "output":["hostid","name"],
  "groupids":"8",
  },

  "auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth>  "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 urllib2.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>  

  输出结果:
DSC0002.png

  4.itemsid.get方法获取单个主机下所有的监控项ID。根据第三点中获取到的所有主机id与名称,找到你想要获取的主机id,填写到下面代码“hostids”中,获取它下面的所有监控项。
  官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/item/get
  python实现方法:
  

#!/usr/bin/env python2.7  
#
coding=utf-8  
import json
  
import urllib2
  
# based url and required header
  
url = "http://192.168.0.217/zabbix/api_jsonrpc.php"
  
header = {"Content-Type":"application/json"}
  
# request json
  
data = json.dumps(
  
{
  "jsonrpc":"2.0",
  "method":"item.get",
  "params":{
  "output":["itemids","key_"],
  "hostids":"10123",
  },

  "auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth>  "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 urllib2.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

  #print "Host>  

  输出结果:
DSC0003.png

  5.history.get方法获取单个监控项的历史数据。根据第4点获取到的所有items>
  官方地址:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/history/get
  python实现方法:
  

#!/usr/bin/env python2.7  
#
coding=utf-8  
import json
  
import urllib2
  
# based url and required header
  
url = "http://192.168.0.217/zabbix/api_jsonrpc.php"
  
header = {"Content-Type":"application/json"}
  
# request json
  
data = json.dumps(
  
{
  "jsonrpc":"2.0",
  "method":"history.get",
  "params":{
  "output":"extend",
  "history":3,
  "itemids":"27019",
  "limit":10
  },

  "auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth>  "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 urllib2.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

  #print "Host>  

  输出结果:
DSC0004.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-430554-1-1.html 上篇帖子: Stay Hungry, Stay Foolish. 下篇帖子: CentOS下Zabbix安装部署及汉化
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表