除了上一篇使用自己定义的函数,我们还可使用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模块的更多相关文章

  1. Python3之turtle模块的使用

    Python3之turtle模块的使用     直接扣代码就行: import turtle as t t.pensize(4) t.hideturtle() t.colormode(255) t.c ...

  2. python基础系列教程——Python3.x标准模块库目录

    python基础系列教程——Python3.x标准模块库目录 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata ...

  3. Python3:Requests模块的异常值处理

    Python3:Requests模块的异常值处理 用Python的requests模块进行爬虫时,一个简单高效的模块就是requests模块,利用get()或者post()函数,发送请求. 但是在真正 ...

  4. Python3中正则模块re.compile、re.match及re.search函数用法详解

    Python3中正则模块re.compile.re.match及re.search函数用法 re模块 re.compile.re.match. re.search 正则匹配的时候,第一个字符是 r,表 ...

  5. Python3安装Celery模块后执行Celery命令报错

    1 Python3安装Celery模块后执行Celery命令报错 pip3 install celery # 安装正常,但是执行celery 命令的时候提示没有_ssl模块什么的 手动在Python解 ...

  6. Python3之configparser模块

    1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configpars ...

  7. python3.7 os模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 os模块 #os模块是与操作系统交互的一个接口 # os.get ...

  8. python3.7 json模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 json模块 ''' 要在不同的编程语言之间传递对象,就必须把对 ...

  9. python3.7 random模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 random模块 import random #随机模块 # r ...

随机推荐

  1. 2018.09.11 poj2976Dropping tests(01分数规划)

    传送门 01分数规划板子题啊. 就是简单变形移项就行了. 显然 ∑i=1na[i]∑i=1nb[i]≤k" role="presentation" style=" ...

  2. 2018.08.22 NOIP模拟 or(线段树)

    or [描述] 构造一个长度为 n 的非负整数序列 x,满足 m 个条件,第 i 个条件为x[li] | x[li+1] | - | x[ri]=pi. [输入] 第一行两个整数 n,m.接下来 m ...

  3. hdu-1070(水题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070 题意:一个人喝牛奶,有三个原则: 1.牛奶的日期不超过6天,就是最多5天. 2.每次只喝200m ...

  4. 记一次项目使用webuploader爬坑之旅

       因前端页面开发使用的为VUE开发,又要支持IE9,遂只有基于webuploader封装一个上传组件.地址:https://github.com/z719725611/vue-upload-web ...

  5. CreateDialog()与CreateDialogIndrect()

    CreateDialog() 概述 函数功能:CreateDialog宏从一个对话框模板资源创建一个无模式的对话框,CreateDiaog宏使用CreateDialogParam函数. 函数原型:HW ...

  6. HDU6023 Automatic Judge 2017-05-07 18:30 73人阅读 评论(0) 收藏

    Automatic Judge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. uva 579 ClockHands 几何初接触 求时针与分针的夹角

    貌似是第一次接触几何题... 求时针与分针的夹角,这不是小学生的奥数题么.我小时候也想过这问题的. 每过一小时时针走1/12*360=30度,每过一分钟时针走1/60*30=0.5度,分针走1/60* ...

  8. Launch Google Map in Android / IOS Mobile

    <!--This only works in android mobile phone--><a href="geo:0,0?q=myaddress+encode)__&q ...

  9. Python3------反射详解

    反射: 根据字符串动态的判断,调用,添加/修改,删除类或类的实例化对象中的方法或属性 反射共有四种方法hasattr(),getattr(),setattr(),delattr() 1.通过字符串来判 ...

  10. go语言最快最好运用最广的web框架比较(大多数人不了解的特性)

    令人敬畏的Web框架 如果你为自己设计一个小应用程序,你可能不需要一个Web框架,但如果你正在进行生产,那么你肯定需要一个,一个好的应用程序. 虽然您认为自己拥有必要的知识和经验,但您是否愿意自行编写 ...