通过zabbix提供的api进行维护模式的设置
#!/usr/bin/env python # -*-coding:utf-8-*- import urllib import urllib2 import json import sys import platform import time def auth(uid, username, password, api_url): """ zabbix认证 :param uid: :param username: :param password: :return: """ dict_data = {} dict_data[‘method‘] = ‘user.login‘ # 方法 dict_data[‘id‘] = uid # 用户id dict_data[‘jsonrpc‘] = "2.0" # api版本 dict_data[‘params‘] = {"user": username, "password": password} # 用户账号密码 jdata = json.dumps(dict_data) # 格式化json数据 content = post_data(jdata, api_url) # post json到接口 return content # 返回信息 def post_data(jdata, url): """ POST方法 :param jdata: :param 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, period, auth_code, api_url): """ create maintenance :return: """ dict_data = {} dict_data[‘method‘] = ‘maintenance.create‘ # 方法 dict_data[‘id‘] = uid # 用户id dict_data[‘jsonrpc‘] = "2.0" # api版本 dict_data[‘auth‘] = auth_code # api版本 dict_data[‘description‘] = "UPDATE" + hostid # api版本 # host hostids = [hostid] # timeperiods timeperiods = [{"timeperiod_type": 0, "start_time": 64800, "period": period}] dict_data[‘params‘] = {"name": name, "active_since": active_since, "timeperiods": timeperiods, "active_till": active_till, "hostids": hostids} # 用户账号密码 jdata = json.dumps(dict_data) # 格式化json数据 content = post_data(jdata, api_url) # post json到接口 print content return content # 返回信息 def get_hostid(hostname, auth_code, uid, api_url): """ use hostname get hostid :param hostname: :param auth: :param uid: :return: """ dict_data = {} dict_data[‘method‘] = ‘host.getobjects‘ # 方法 dict_data[‘id‘] = uid # 用户id dict_data[‘jsonrpc‘] = "2.0" # api版本 dict_data[‘params‘] = {"name": hostname} # 主机名 dict_data[‘auth‘] = auth_code # auth串 jdata = json.dumps(dict_data) # 格式化json数据 content = post_data(jdata, api_url) # post json到接口 print content return content # 返回信息 def logout(uid, auth_code, api_url): """ 退出 :param uid: :param auth_code: :return: """ dict_data = {} dict_data[‘method‘] = ‘user.logout‘ # 方法 dict_data[‘id‘] = uid # 用户id dict_data[‘jsonrpc‘] = "2.0" # api版本 dict_data[‘params‘] = [] dict_data[‘auth‘] = auth_code # auth串 jdata = json.dumps(dict_data) # 格式化json数据 content = post_data(jdata, api_url) # post json到接口 return content # 返回信息 if __name__ == ‘__main__‘: # user info uid = 1 # 用户ID username = ‘admin‘ password = ‘123456‘ api_url = "http://192.168.1.100/api_jsonrpc.php" res = auth(1, username, password, api_url) # 认证 if res[‘result‘]: auth_code = res[‘result‘] # 认证串 hostname = platform.node() # 主机名 res = get_hostid(hostname, auth_code, uid, api_url) if res[‘result‘]: period = 600 # 维护时长 active_since = int(time.time()) # 开始时间 active_till = int(time.time()) + period # 结束时间 hostid = res[‘result‘][0][‘hostid‘] # 主机 res = create_maintenance(‘AutoMaintenance_‘ + hostname + ‘_‘ + str(active_since), hostid, active_since, active_till, period, auth_code, api_url) # 创建维护 logout(uid, auth_code, api_url) # 退出登录 print res else: pass
需要修改uid、username、password、api_url以及res中的auth函数的uid值,通过修改period的值来修改维护时长。
该脚本通过platform模块获取到agent的主机名,确保server端的主机名与客户端一致,且存在,,否则设置不成功。
设置完成后可在Configuration->Maintenance页面中查看设置的维护模板。
可以通过在Configuration->Hosts页面中查看是否生效。
zabbix api 设置维护模式