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 "該主機活監控項不存在"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM