pyzabbix 接口使用
pyzabbix github地址: https://github.com/lukecyca/pyzabbix
zabbix官方接口: https://www.zabbix.com/documentation/4.0/zh/manual/api
1 from pyzabbix import ZabbixAPI 2 from zabbixApi.api.constP import ZABBIX_SERVER, USER, PASSWD 3 """ 4 模块说明:直接调用zabbix的函数集合 5 """ 6 7 def loginZabbix(): 8 '''登陆zabbix,返回登陆对象''' 9 user = ZabbixAPI(ZABBIX_SERVER) 10 user.login(USER, PASSWD) 11 return user 12 13 def hostAllInfo(user, host_name): 14 '''根据主机名,返回该主机的所有信息''' 15 host = user.host.get(search={"name": host_name}, output="extend") 16 if len(host) != 0: 17 return host 18 else: 19 return None 20 21 def hostIdInfo(user, host_name): 22 '''根据主机名,返回该主机id''' 23 host_id = user.host.get(search={"name": host_name}, output=["hostid"]) 24 if len(host_id) != 0: 25 return host_id[0]["hostid"] 26 else: 27 return None 28 29 def hostItemAllInfo(user, host_name, item): 30 '''根据主机名和监控key,返回该监控项的数据''' 31 item = user.item.get(host=host_name, search={"key_": item}, output="extend") 32 if len(item) != 0: 33 return item 34 else: 35 return None 36 37 def hostItemIdInfo(user, host_name, item): 38 '''根据主机名和监控key,返回该监控项的数据''' 39 item_id = user.item.get(host=host_name, search={"key_": item}, output=["itemid"]) 40 if len(item_id) != 0: 41 return item_id[0]["itemid"] 42 else: 43 return None 44 45 def timeProblemAllInfo(user, time_from): 46 '''获取某个时间点后面产生的告警问题''' 47 # problem.get的time_from用的是"YY-MM-DD HH:MM:SS" 48 return user.problem.get(time_from=time_from, output="extend") 49 50 def hostTimeProblemAllInfo(user, host_name, time_from): 51 '''查找某主机在某个时间点后面产生点告警问题''' 52 # problem.get的time_from用的是"YY-MM-DD HH:MM:SS" 53 host_id = hostIdInfo(user, host_name) 54 if host_id != None: 55 return user.problem.get(hostids=host_id, time_from=time_from, output="extend") 56 else: 57 return "该主机已被删除" 58 59 def timeAlertAllInfo(user, time_from): 60 '''获取某个时间点后面产生点告警问题''' 61 alert = user.alert.get(time_from=time_from, output="extend") 62 if len(alert) != 0: 63 return alert 64 else: 65 return None 66 67 def hostTimeAlertAllInfo(user, host_name, time_from): 68 '''查找某主机在某个时间点后面产生点告警问题''' 69 # alert.get的time_from用的是时间戳(1586012065)格式!!! 70 host_id = hostIdInfo(user, host_name) 71 if host_id != None: 72 return user.alert.get(hostids=host_id, time_from=time_from, output="extend") 73 else: 74 return "该主机已不存在" 75 76 def hostTimeHistoryInfo(user, host_name, item, time_from): 77 '''获取某主机点某个监控项在某个时间点后产生点历史数据''' 78 # history.get的time_from用的是时间戳(1586012065)格式!!! 79 host_id = hostIdInfo(user, host_name) 80 item_id = hostItemIdInfo(user, host_name, item) 81 if host_id != None and item_id != None: 82 return user.history.get(hostids=host_id, itemids=item_id, time_from=time_from) 83 else: 84 return "该主机活监控项不存在"