关于python调用zabbix api接口
因公司业务需要,引进了自动化运维,所用到的监控平台为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接口的更多相关文章
- python 调用zabbix api接口实现主机的增删改查
python程序调用zabbix系统的api接口实现对zabbix_server端主机的增删改查,使用相关功能时候,需要打开脚本中的相关函数. 函数说明: zabbixtools() 调用zabbi ...
- python 调用zabbix api实现查询主机信息,输出所有主机ip
之前发现搜索出来的主机调用zabbix api信息都不是那么明确,后来通过zabbix官方文档,查到想要的api信息,随后写一篇自己这次项目中用到的api. #!/usr/bin/env python ...
- 使用Python调用Zabbix API
Zabbix API官方文档: https://www.zabbix.com/documentation/4.0/zh/manual/api 1.向 api_jsonrpc.php 发送HTTP_PO ...
- Python调用zabbix API批量添加主机 (读取Excel)
本文转载自:http://blog.mreald.com/178 Zabbix可以通过自发现添加主机,不过有时候不准确,通过API添加会更加准确! 脚本使用的跟zabbix相关的内容.参考的是zabb ...
- python调用zabbix接口实现Action配置
要写这篇博客其实我的内心是纠结的,老实说,我对zabbix的了解实在不多.但新公司的需求不容置疑,当我顶着有两个头大的脑袋懵懵转入运维领域时,面前摆着两百多组.上千台机器等着写入zabbix监控的需求 ...
- 使用Python调用Flickr API抓取图片数据
Flickr是雅虎旗下的图片分享网站,上面有全世界网友分享的大量精彩图片,被认为是专业的图片网站.其API也很友好,可以实现多种功能.这里我使用了Python调用其API获得了大量的照片数据.需要注意 ...
- 03: zabbix API接口 对 主机、主机组、模板、应用集、监控项、触发器等增删改查
目录:Django其他篇 01: 安装zabbix server 02:zabbix-agent安装配置 及 web界面管理 03: zabbix API接口 对 主机.主机组.模板.应用集.监控项. ...
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...
- 基于python调用libvirt API
基于python调用libvirt API 1.程序代码 #!/usr/bin/python import libvirt import sys def createConnection(): con ...
随机推荐
- mac 终端 使用 solarized 主题设置语法高亮
mac 终端 使用 solarized 主题设置语法高亮 先来看看 solarized 在 mac 终端上的效果图片 一:先下载 solarized 官网下载:https://github.com/a ...
- PHP高级教程-过滤器
PHP 过滤器 PHP 过滤器用于验证和过滤来自非安全来源的数据,比如用户的输入. 什么是 PHP 过滤器? PHP 过滤器用于验证和过滤来自非安全来源的数据. 测试.验证和过滤用户输入或自定义数据是 ...
- 代理(Proxy)模式简介
一.代理(Proxy)模式简介 代理模式是结构型模式. 代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 代理对象要继承于抽象主题,并控制原对象的引用 二.简单例子 抽象主题类 ...
- 如何在hosts文件添加自己想要解析的网站?及修改hosts的作用
http://union.zhuna.cn/help/144.asp 在Windows2003/XP系统中位于C:\Winnt\System32\Drivers\Etc 目录中,找到host文件. 首 ...
- java 动态生成类再编译最后代理
package spring.vhostall.com.proxy; public interface Store { public void sell(); } ------------------ ...
- 关闭xp防火墙
在控制面版关闭防火墙 禁用“Security Center”服务 禁用“WindowsFirewall/InternetConnectionSharing(ICS)”服务 删除服务:开始运行CMD,命 ...
- SSO之CAS + LDAP
本来主要详细是介绍CAS和LDAP整合实现单点登录的步骤. 1. 依<SSO之安装CAS Server>所述安装好CAS Server.2. 安装ApacheDS.安装好ApacheDS后 ...
- nginx 配置web 虚拟文件夹 而且codeIgniter,thinkphp 重定向url 地址
nginx 配置虚拟文件夹而且url 重定向 server { #侦听80port listen 8090; #定义使用www.xx.com訪问 server_name 127.0.0.1; #设定本 ...
- 【VUE+laravel5.4】vue给http请求 添加请求头数据
1.适用于 ajax和普通的http请求 2.vue添加用法如下: <script type="text/javascript src="/dist/js/app.min.j ...
- Visual C#两分钟搭建BHO IE钩子
微软在1997年正式推出Browser Helper Object (BHO), 使程序员能够更好的对IE进行二次开发和操作. 在通过编写BHO程序数月后, 我希望把我的一些经验告诉才开始的同志, 避 ...