Zabbix now so popular, personal feeling with Zabbix powerful API has a big relationship, using the API can help us do a lot of things:
1, get the related group, host information.
2, for example, some people like to get historical data to re-plot.
3, add delete host, bind delete template.
4. Add Delete Maintenance period
Here I use the Zabbix_client module provided by pipy, so that we do not have to write the login function, as long as the module begins to specify the user name password:
1, installation zabbix_client:
Pip Install Zabbxi_client
2, API management script, replaced by their own user name password can:
#coding: Utf-8import timefrom zabbix_client import zabbixserverproxyclass zabbix (): def __init__ (self): self.zb = zabbixserverproxy ("Http://192.168.10.100/zabbix") Self.zb.user.login (user= "Admin", password= "Zabbix") ############## Query group all groups get group Id ############### def get_hostgroup (self): data = { " Output ": [' GroupID ', ' name '] } ret = self.zb.hostgroup.get (**data) return ret ########### get all hosts within a related group by group ID ###############
def get_hostid (self,groupids=2): data = { " Output ": [" HostID ", " name "], " Groupids ": Groupids } ret = self.zb.host.get (**data) return ret ########## find related surveillance by getting HostID to itemid ################### def item_get (self, hostids= "10115"): data = { "Output": ["Itemids", "Key_"], "Hostids": hostids, }
ret = self.zb.item.get (**data) return ret ######### Get historical data for related monitoring items by Itemid (incoming Itemid and i0 representing flast types) ########### 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 ############### Add hosts and assign to groups (incoming host name, IP address, and group ID) ##################### &Nbsp;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 #################### #查看现有模板, bind host to template ################## ##### def get_template (self,hostid=10129): datalist = [] datadict={} data = { "HostID": hostid, "Output": ["TemplateID", "name"] } ret = Self.zb.template.get (data) for i in ret: datadict[i[' name ']] = i[' TemplateID '] datalist.append (datadict) return datalist #################### associated host to template ################################## Def link_template (self, HOSTID=10156, TEMPLATEIDS=10001): data = { "HostID":hostid, "Templates":templateids } ret = self.zb.host.update (data) return ret ################### add a maintenance cycle, ############################# ######### 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 ############# #获取维护周期之后, remove maintenance cycles by passing in 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, incidentally also delete template ######### 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)
More Zabbix videos can be viewed:
Http://www.roncoo.com/details?cid=fb3050a5b34b42f39ccad83ebebc89c1
This article is from the "Little Luo" blog, please be sure to keep this source http://xiaoluoge.blog.51cto.com/9141967/1827151
Manage with the Zabbix API