
云原生 API 网关 APISIX 入门教程
这里我使用pipy提供的zabbix_client模块来进行,这样就不用我们自己去写登录函数,只要在模块开始指定用户名密码即可:
pip install zabbxi_client
#coding:utf-8
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")
def get_hostgroup(self):
data = {
"output":['groupid','name']
}
ret = self.zb.hostgroup.get(**data)
return ret
def get_hostid(self,groupids=2):
data = {
"output": ["hostid", "name"],
"groupids": groupids
}
ret = self.zb.host.get(**data)
return ret
def item_get(self, hostids="10115"):
data = {
"output":["itemids","key_"],
"hostids": hostids,
}
ret = self.zb.item.get(**data)
return ret
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
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
def del_maintenance(self,maintenanceids):
return self.zb.maintenance.delete(maintenanceids)
def host_status(self, hostid, status):
data = {
"hostid":hostid,
"status":status
}
return self.zb.host.update(data)
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)
本文章转载微信公众号@领课云