因公司业务需要,引进了自动化运维,所用到的监控平台为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 ID Is:",response['result']

输出结果:

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 is what auth script returns, remeber it is string
"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 ID:",group['groupid'],"\tGroupName:",group['name']

输出结果:

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":"",
},
"auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth id is what auth script returns, remeber it is string
"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 ID:",host['hostid'],"HostName:",host['name']

输出结果:

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":"",
},
"auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth id is what auth script returns, remeber it is string
"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 ID:",host['hostid'],"HostName:",host['name']

输出结果:

5.history.get方法获取单个监控项的历史数据。根据第4点获取到的所有items id的值,找到想要监控的那项,填写到下面代码“itemids”中,获取它的历史数据。
官方地址: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":"",
"limit":10
},
"auth":"4c38be0e3cda326c63e4f4be8f73a056", # theauth id is what auth script returns, remeber it is string
"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 ID:",host['hostid'],"HostName:",host['name']

输出结果:

关于python调用zabbix api接口的更多相关文章

  1. python 调用zabbix api接口实现主机的增删改查

    python程序调用zabbix系统的api接口实现对zabbix_server端主机的增删改查,使用相关功能时候,需要打开脚本中的相关函数. 函数说明: zabbixtools()  调用zabbi ...

  2. python 调用zabbix api实现查询主机信息,输出所有主机ip

    之前发现搜索出来的主机调用zabbix api信息都不是那么明确,后来通过zabbix官方文档,查到想要的api信息,随后写一篇自己这次项目中用到的api. #!/usr/bin/env python ...

  3. 使用Python调用Zabbix API

    Zabbix API官方文档: https://www.zabbix.com/documentation/4.0/zh/manual/api 1.向 api_jsonrpc.php 发送HTTP_PO ...

  4. Python调用zabbix API批量添加主机 (读取Excel)

    本文转载自:http://blog.mreald.com/178 Zabbix可以通过自发现添加主机,不过有时候不准确,通过API添加会更加准确! 脚本使用的跟zabbix相关的内容.参考的是zabb ...

  5. python调用zabbix接口实现Action配置

    要写这篇博客其实我的内心是纠结的,老实说,我对zabbix的了解实在不多.但新公司的需求不容置疑,当我顶着有两个头大的脑袋懵懵转入运维领域时,面前摆着两百多组.上千台机器等着写入zabbix监控的需求 ...

  6. 使用Python调用Flickr API抓取图片数据

    Flickr是雅虎旗下的图片分享网站,上面有全世界网友分享的大量精彩图片,被认为是专业的图片网站.其API也很友好,可以实现多种功能.这里我使用了Python调用其API获得了大量的照片数据.需要注意 ...

  7. 03: zabbix API接口 对 主机、主机组、模板、应用集、监控项、触发器等增删改查

    目录:Django其他篇 01: 安装zabbix server 02:zabbix-agent安装配置 及 web界面管理 03: zabbix API接口 对 主机.主机组.模板.应用集.监控项. ...

  8. Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口

    1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...

  9. 基于python调用libvirt API

    基于python调用libvirt API 1.程序代码 #!/usr/bin/python import libvirt import sys def createConnection(): con ...

随机推荐

  1. ZH奶酪:Ubuntu14.04 安装Android SDK(SDK tools only)

    1.安装JDK(我安装的是Oracle的,而不是openjdk) jdk目录:usr/lib/jvm/java-7-oracle/bin/java 2.下载Android-SDK,在下边的网页选择对应 ...

  2. OSX: SSH密钥使用日记(2)

    准备钥匙和锁(密钥对): $ pwd /Users/test $ ssh-keygen -t dsa -C "$(whoami)@$(hostname),$(date '+%F %T')&q ...

  3. java 设计模式大全

    在线学习网址: http://www.runoob.com/design-pattern/

  4. 〖Linux〗build sqlite3 for Arm

    Version: sqlite-autoconf-3080100.tar.gz Download: https://www.sqlite.org/download.html 1. toolchains ...

  5. CAS 5.1.x 的搭建和使用(四)—— 配置使用HTTP协议访问的服务端

    CAS单点登录系列: CAS 5.1.x 的搭建和使用(一)—— 通过Overlay搭建服务端 CAS5.1.x 的搭建和使用(二)—— 通过Overlay搭建服务端-其它配置说明 CAS5.1.x ...

  6. openssh入门

    ssh (secure shell),ssh协议支持登录及文件的传输加密防止数据截留! 01.下载 https://www.ssh.com/                               ...

  7. 速度挑战 - 2小时完成HTML5拼图小游戏

    概述 我用lufylegend.js开发了第一个HTML5小游戏——拼图游戏,还写了篇博文来炫耀一下:HTML5小游戏<智力大拼图>发布,挑战你的思维风暴. 详细 代码下载:http:// ...

  8. leetcode621 贪心:任务安排

    题目链接 给定26种任务,每种任务的数量已知. 相同任务之间必须间隔n个时间段,为了不足n个时间段,可以让及其休息. 问:最少需要多长时间才能处理完这些任务? 这道题用贪心策略解决:每次安排任务时,优 ...

  9. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  10. 【java设计模式】之 代理(Proxy)模式

    代理模式的核心作用就是通过代理,控制对对象的访问.这跟实际中是一样的,比如说明星都有经纪人,这就是一个代理,比如有人要找某明星拍戏,那么首先处理这事的是他的经纪人,虽然拍戏需要自己拍,但是拍戏前后的一 ...