接上面的內容;zabbix監控機制中,通常主機出故障的時候會無限制的發送報警郵件,容易讓人心煩;於是找到zabbix的維護週期,利用api創建一個維護週期,當我們把主機修好之後再刪除維護,重新監控主機;考慮到維護主機都是臨時創建,這裏我定義了1-3個小時的維護週期不設定維護週期計劃任務,需要維護人員登錄添加即可。
實現步驟:
1、我定義一個第三方的表,用來前端展示;當添加維護週期時候往數據庫裏添加數據。
2、調用api來操作maintenance
3、定義host.update 這個添加狀態爲非監控狀態(0表示監控,1表示不監控)
models表:
class Maintenance(db.Model): __tablename__ = "maintenance" id = db.Column(db.Integer, primary_key=True) maintenance_name = db.Column(db.String(50)) hostname = db.Column(db.String(50), index=True) maintenance_time = db.Column(db.Integer) update_time = db.Column(db.DateTime)
實現代碼:
1、創建維護週期api操作
def create_maintenance(self, params): return self.zb.maintenance.create(**params)
2、更新host status
def host_status(self, hostid, status): return self.zb.host.update(hostid=hostid, status = status)
3、具體實現:
@main.route("/monitor/zabbix/maintenance/add", methods=["POST"]) def maintenance_add(): from app.common.zabbix import zabbix_server from app.common.zabbix import create_maintenance data = dict(request.form) #{'name': [u'mail.shihuasuan.com'], 'time': [u'1']} maintenance_name = data['maintenance_name'][0] hostname = data["name"][0] time_to_go = data["time"][0] time_long = int(time_to_go) * 60 *60 ret = zabbix_server.get_hosts() for host in ret: if hostname == host["host"]: hostids = host["hostid"] try: result = create_maintenance(maintenance_name,hostids,time_long) result_data = zabbix_server.host_status(hostid=hostids, status="1") except: return "check your maintenance_name" update_time = time.strftime("%Y-%m-%d %H:%I:%S", time.localtime(time.time())) add_data = {"maintenance_name":maintenance_name, "hostname":hostname, "maintenance_time":time_to_go,"update_time":update_time} obj = Maintenance(**add_data) db.session.add(obj) try: db.session.commit() except Exception,e: raise Exception(e.message.split(")")[1]) return "1" return "200"
實現效果:
1、前端添加維護週期:
2、查看維護週期:
登錄zabbix查看:
3、查看zabbix host 狀態(顯示爲not monitored):