- Setup of Maintenance mode with API provided by Zabbix
#!/usr/bin/env python#-*-coding:utf-8-*-import urllibimport urllib2import jsonimport sysimport platformimport Time def Auth (uid, username, password, api_url): "" "Zabbix Authentication:p Aram UID::p Aram Username::p Aram Password:: Retu RN: "" "Dict_data = {} dict_data[' method '] = ' user.login ' # methods dict_data[' id '] = UID # User ID dict_data[' JS Onrpc '] = "2.0" # API version dict_data[' params '] = {"User": Username, "password": password} # user account password Jdata = json.dumps (dict_data) # format JSON data content = Post_data (Jdata, Api_url) # Post JSON to interface return content # return information def Post_data (j Data, URL): "" "Post method:p Aram Jdata::p Aram URL:: return:" "" req = urllib2. Request (URL, jdata, {' Content-type ': ' Application/json '}) Response = Urllib2.urlopen (req) # Content = Response.read ( Content = Json.load (response) return content def create_maintenance (name, HostID, Active_since, Active_till, Peri OD, Auth_code, Api_url): "" "Create MaintenaNce:return: "" "Dict_data = {} dict_data[' method '] = ' maintenance.create ' # methods dict_data[' id '] = uid # with User ID dict_data[' jsonrpc '] = "2.0" # API version dict_data[' auth '] = auth_code # API version dict_data[' description '] = "upda TE "+ HostID # API version # Host hostids = [HostID] # Timeperiods timeperiods = [{" Timeperiod_type ": 0," Start_tim E ": 64800," period ": Period}] dict_data[' params '] = {" Name ": Name," Active_since ": Active_since," timeperiods ": Timeper IODs, "Active_till": Active_till, "Hostids": hostids} # user account password Jdata = json.dumps (dict_data) # format JSON data content = Post_data (Jdata, Api_url) # Post JSON to interface print content return content # return information def get_h Ostid (hostname, Auth_code, UID, api_url): "" "Use hostname get HostID:p aram hostname::p Aram Auth::p Aram UID:: Return: "" "Dict_data = {} dict_data[' method '] = ' host.getobjects ' # methods dict_data[' id '] = uid # user I D dict_data[' Jsonrpc '] = "2.0" # API version dict_data[' params '] = {"name": hostname} # hostname dict_data[' auth '] = auth_code # auth String jdata = Json.dumps (dict_data) # formatting JSON data content = Post_data (Jdata, Api_url) # Post JSON to interface print content return cont ENT # return Information def logout (UID, Auth_code, Api_url): "" "Exit:p Aram UID::p Aram Auth_code:: Return:" "" D Ict_data = {} dict_data[' method '] = ' user.logout ' # methods dict_data[' id '] = UID # User ID dict_data[' jsonrpc '] = "2.0 "# API version dict_data[' params '] = [] dict_data[' auth '] = auth_code # auth String jdata = Json.dumps (dict_data) # format JS On data content = Post_data (Jdata, Api_url) # Post JSON to interface return content # return information if __name__ = = ' __main__ ': # Use R info uid = 1 # User ID username = ' admin ' password = ' 123456 ' api_url = ' http://192.168.1.100/api_jsonrpc.php ' res = auth (1, username, password, api_url) # Certified if res[' result ': Auth_code = res[' result '] # authentication string ho Stname = Platform.node () # hostname res = Get_hostid (hostname, Auth_code, UID, api_url) if res[' result ': Period = 600 # Maintenance Long active_since = Int (Time.time ()) # start time Active_till = Int (Time.time ()) + Period # end time HostID = res[' result '][0][' HostID '] # host res = create_maintenance (' automaintenance_ ' + hostname + ' _ ' + str (a ctive_since), HostID, Active_since, Active_till, period, Auth_code, Api_url) # Create Maintenance Logout (UID, Auth_code, Api_url) # Exit login Print Res Else:pass
The UID values of the AUTH functions in the UID, username, password, api_url, and res need to be modified, and the maintenance duration modified by modifying the value of period.
The script obtains the host name of the agent through the Platform module, ensuring that the server-side hostname is consistent with the client and exists, otherwise the setting is unsuccessful.
After Setup is complete, you can view the settings maintenance template in the Configuration->maintenance page.
You can see whether it takes effect by looking in the Configuration->hosts page.
Zabbix API Settings Maintenance mode