赞
踩
#-*- coding:utf-8 -*- # python2.7 '''从zabbix 监控获取业务组报警的信息''' import requests import json import time import datetime import re server_url = "http://monitor.test.com:8000/api_jsonrpc.php" header = {"Content-Type": "application/json"} username = "admin" password = "****" def get_token(): ‘’‘ 获取api 返回的token 值 ’‘’ login_data = json.dumps({ "jsonrpc": "2.0", "method": "user.login", "params": { 'user': username, 'password': password }, "id": 0 }) response_auth = requests.post(server_url, data=login_data, headers=header) token = json.loads(response_auth.text)["result"] return token start_time_str = (datetime.datetime.now()+datetime.timedelta(days=-3)).strftime("%Y-%m-%d %H:%M") end_time_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") # 转换为时间戳 start_time = time.mktime(time.strptime(start_time_str,"%Y-%m-%d %H:%M")) end_time = time.mktime(time.strptime(end_time_str,"%Y-%m-%d %H:%M")) def get_alert(): data = json.dumps( {"jsonrpc": "2.0", "method": "alert.get", "params": {"output": "extend", "time_from":start_time,"time_till":end_time}, "auth": get_token(), "id": 0} ) responese = requests.post(server_url, data=data, headers=header) alter_list = responese.json()["result"] for item in alter_list: message = item["message"] messagestr = message.encode("utf-8") #print messagestr if messagestr != "Zabbix Server 心跳检测": messageDict = json.loads(messagestr) hostname = messageDict["hostname"] current_status = messageDict["current_status"] yw_hostname = re.findall(r"^yw.*|^sh1-yw.*|.*?yewu.*", hostname, re.I) message = re.findall(r"Zabbix agent.*|.*restarted.*|.*Hostname was changed.*|.*Host information.*",messageDict["message"],re.I) if len(yw_hostname) > 0 and not current_status.startswith("OK") and len(message) == 0: print messageDict if __name__ == "__main__": get_alert()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。