python3 使用 zabbix_client模块
除了上一篇使用自己定义的函数,我们还可使用pipy提供的zabbix_client模块,这样就不用我们自己去写登录函数,只要在模块开始指定用户名密码即可,方便很多。
#!/usr/bin/env python
# Version = 3.5.2
# __auth__ = '无名小妖'
import time
from zabbix_client import ZabbixServerProxy ZABBIX_URL = 'http://alyz.zabbix.com'
ZABBIX_USERNAME = "Admin"
ZABBIX_PASSWORD = "zabbix" class Zabbix: def __init__(self):
self.zb = ZabbixServerProxy(ZABBIX_URL)
self.zb.user.login(user=ZABBIX_USERNAME, password=ZABBIX_PASSWORD) def get_hostgroup(self):
"""
查询组所有组获取组id和name
:return: groupid和name
"""
data = {
"output": ['groupid', 'name'] # "output": "extend", 查看所有字段
}
ret = self.zb.hostgroup.get(**data)
return ret def get_hostid(self, groupids=None):
"""
通过组id获取相关组内的所有主机
:param groupids: None表示获取所有组的主机,可以通过列表,元组等传入多个组id
:return: "hostid"和"name"
"""
data = {
"output": ["hostid", "name"],
"groupids": groupids
}
ret = self.zb.host.get(**data)
return ret def item_get(self, hostids=None):
"""
通过获取的hostid查找相关监控想itemid
:param hostids: None表示获取所有主机的item,可以通过列表,元组等传入多个itemid
:return: "itemids", "key_"
"""
data = {
"output": ["itemids", "key_"],
"hostids": hostids,
} ret = self.zb.item.get(**data)
return ret def history_get(self, itemid, i, limit=10):
"""
通过itemid 获取相关监控项的历史数据
:param itemid:
:param i: 0 - numeric float; 1 - character; 2 - log; 3 - numeric unsigned; 4 - text.
:param limit: 获取数据的数量
:return:
"""
data = {"output": "extend",
"history": i,
"itemids": [itemid],
"limit": limit
}
ret = self.zb.history.get(**data)
return ret def add_zabbix_host(self, hostname="test_zabbix", ip="192.168.10.100", groupid="2"):
"""
添加主机并且指定到组(传入主机名,IP地址和组ID)
:param hostname:
:param ip:
:param groupid:
:return:
"""
data = {
"host": hostname,
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": ip,
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": groupid
}
]
}
ret = self.zb.host.create(data)
return ret def get_template(self):
"""
查看现有模板
:return:
"""
datalist = []
datadict = {}
data = {
"output": ["templateid", "name"]
}
ret = self.zb.template.get(data)
for i in ret:
datadict[i['name']] = i['templateid']
datalist.append(datadict)
return datalist def link_template(self, hostid=10156, templateids=10001):
"""
关联主机到模板
:param hostid:
:param templateids:
:return:
"""
data = {
"hostid": hostid,
"templates": templateids
} ret = self.zb.host.update(data)
return ret def create_maintenance(self, name="test", hostids=10156, time=2):
"""
添加维护周期
:param name:
:param hostids:
:param time:
:return:
"""
data = {
"name": name,
"active_since": 1458142800,
"active_till": 1489678800,
"hostids": [
hostids
],
"timeperiods": [
{
"timeperiod_type": 0,
"period": 3600
}
]
}
ret = self.zb.maintenance.create(data)
self.host_status(10130, 1)
return ret def get_maintenance(self):
"""
获取维护周期
:return:
"""
data = {
"output": "extend",
"selectGroups": "extend",
"selectTimeperiods": "extend"
}
ret = self.zb.maintenance.get(data)
return ret
#
# def del_maintenance(self, maintenanceids):
# """
# 获取维护周期之后,通过传入maintenanceid删除维护周期
# :param maintenanceids:
# :return:
# """
# return self.zb.maintenance.delete(maintenanceids) def host_status(self, hostid, status):
"""
添加维护周期时候需要吧zabbix_host设置成非监控状态
:param hostid:
:param status:
:return:
"""
data = {
"hostid": hostid,
"status": status
}
return self.zb.host.update(data) # def host_del(self, hostids=10155):
# """
# 通过hostids删除主机id,顺带也删除模板
# :param hostids:
# :return:
# """
# return self.zb.host.delete(hostids) if __name__ == "__main__":
zabbix_server = Zabbix()
print(zabbix_server.get_hostgroup())
print(zabbix_server.get_hostid([2, 1]))
print(zabbix_server.item_get())
# data = zabbix_server.history_get("24889",0)
# print(zabbix_server.add_zabbix_host())
print(zabbix_server.get_template())
# print(data[0]['Template OS Linux'])
# print(zabbix_server.link_template())
# print(zabbix_server.create_maintenance())
# print(zabbix_server.host_del(10155))
# print(zabbix_server.get_maintenance())
# print(zabbix_server.del_maintenance(15)))
python3 使用 zabbix_client模块的更多相关文章
- Python3之turtle模块的使用
Python3之turtle模块的使用 直接扣代码就行: import turtle as t t.pensize(4) t.hideturtle() t.colormode(255) t.c ...
- python基础系列教程——Python3.x标准模块库目录
python基础系列教程——Python3.x标准模块库目录 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata ...
- Python3:Requests模块的异常值处理
Python3:Requests模块的异常值处理 用Python的requests模块进行爬虫时,一个简单高效的模块就是requests模块,利用get()或者post()函数,发送请求. 但是在真正 ...
- Python3中正则模块re.compile、re.match及re.search函数用法详解
Python3中正则模块re.compile.re.match及re.search函数用法 re模块 re.compile.re.match. re.search 正则匹配的时候,第一个字符是 r,表 ...
- Python3安装Celery模块后执行Celery命令报错
1 Python3安装Celery模块后执行Celery命令报错 pip3 install celery # 安装正常,但是执行celery 命令的时候提示没有_ssl模块什么的 手动在Python解 ...
- Python3之configparser模块
1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configpars ...
- python3.7 os模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 os模块 #os模块是与操作系统交互的一个接口 # os.get ...
- python3.7 json模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 json模块 ''' 要在不同的编程语言之间传递对象,就必须把对 ...
- python3.7 random模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 random模块 import random #随机模块 # r ...
随机推荐
- 2018.09.16 spoj104Highways (矩阵树定理)
传送门 第一次写矩阵树定理. 就是度数矩阵减去邻接矩阵之后得到的基尔霍夫矩阵的余子式的行列式值. 这个可以用高斯消元O(n3)" role="presentation" ...
- xampp虚拟主机的配置
ps:来源 https://blog.csdn.net/qq_17335153/article/details/52091869 一.修改httpd.conf 文件目录 xampp => ...
- hdu-1121(差分法--数学问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1121 参考文章:https://blog.csdn.net/fengzhizi76506/articl ...
- MySQL终端下常用命令
一:控制类命令 1.show variables like "%datadir%";显示注册在variables中(一个注册表key-value的格式存储数据)key能匹配%dat ...
- PHP数据库抽象层--PDO(PHP Data Object) [一]
1.简介:(PDO数据库访问抽象层,统一各种 数据库的访问接口 ) PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口.实现 PDO 接口的每个数据库驱动可以公开具体数据库 ...
- linux环境下的mysql,httpd以及与宿主机的调试问题总结
1. 首先www服务器,在宿主主机浏览器无法访问? (1)修改linux的防火墙,允许宿主主机ip访问即可. (2)关闭防火墙,即可访问. service iptables stop; 2. mysq ...
- Toad 实现 SQL 优化
It is very easy for us to implement sql tuning by toad. We need to do is just give complex sql stat ...
- C#图片处理,缩略图制作
准备参数:图片文件流.文件名 方法:1.先将图片流通过System.Drawing.Image.FromStream方法转成图片对象 2.通过图片对象.GetThumbnailImage方法生成自定义 ...
- NetCore入门篇:(七)Net Core项目使用Controller之二
一.简介 1.说明Post,Get定义的区别. 2.说明如何路由定义. 二.Get.Post定义 1.api不定义访问方式时,同时支持get 和 post.如果定义某种方式,则仅支持某种方式.具体看代 ...
- Keil下Debug随笔
很多时候我们需要通过硬件仿真来调试程序,在仿真时有时候会遇到这样的情况,那就是选择全速运行时,我们的全局变量无法随时更新,而在那设一个断点后发现值是变化的,那么为什么会出现这种情况呢,那就是可能是我们 ...