import time
from zabbix_client import ZabbixServerProxy
class Zabbix():
def __init__(self):
self.zb = ZabbixServerProxy("http://192.168.10.100/zabbix")
self.zb.user.login(user="Admin", password="zabbix")

查詢組所有組獲取組id

def get_hostgroup(self):
data = {
"output":['groupid','name']
}
ret = self.zb.hostgroup.get(**data)
return ret

通過組id獲取相關組內的所有主機

def get_hostid(self,groupids=2):
data = {
"output": ["hostid", "name"],
"groupids": groupids
}
ret = self.zb.host.get(**data)
return ret

通過獲取的hostid查找相關監控想itemid?

def item_get(self, hostids="10115"):
data = {
"output":["itemids","key_"],
"hostids": hostids,
}
ret = self.zb.item.get(**data)
return ret

通過itemid(傳入itemid和i0表示flast類型)獲取相關監控項的歷史數據

def history_get(self, itemid, i ,limit=10):
data = { "output": "extend",
"history": i,
"itemids": [itemid],
"limit": limit
}
ret = self.zb.history.get(**data)
return ret

添加主機并且指定到組(傳入主機名,IP地址和組ID)

def add_zabbix_host(self,hostname="test_zabbix",ip="192.168.10.100",groupid="2"):
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):
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):
data = {
"hostid":hostid,
"templates":templateids
}

ret = self.zb.host.update(data)
return ret

添加維護周期,,

def create_maintenance(self,name="test",hostids=10156,time=2):
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):
data = {
"output": "extend",
"selectGroups": "extend",
"selectTimeperiods": "extend"
}
ret = self.zb.maintenance.get(data)
return ret

獲取維護周期之后,通過傳入maintenanceid刪除維護周期

def del_maintenance(self,maintenanceids):
return self.zb.maintenance.delete(maintenanceids)

添加維護周期時候需要吧zabbix_host設置成非監控狀態

def host_status(self, hostid, status):
data = {
"hostid":hostid,
"status":status
}
return self.zb.host.update(data)

通過hostids刪除主機id,順帶也刪除模板

def host_del(self,hostids=10155):
return self.zb.host.delete(hostids)
if __name__ == "__main__":
zabbix_server = Zabbix()
#print zabbix_server.get_hostgroup()
#print zabbix_server.get_hostid()
#print zabbix_server.item_get(10156)
#data = zabbix_server.history_get("24889",0)
#print zabbix_server.get_hostgroup()
#print zabbix_server.add_zabbix_host()
#data = 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)

本文章轉載微信公眾號@領課云

上一篇:

【豆包大模型】-Python調用豆包大模型API及文本轉語音TTS

下一篇:

使用 TypeScript、PostgreSQL、Next.js、Prisma 和 GraphQL 的全棧應用程序:GraphQL API
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費