#!/usr/bin/env python # -*- encoding: utf-8 -*- import os import json import requests import time import hmac import hashlib import base64 import urllib.parse from urllib.parse import urlparse import redis import re from flask import Flask from flask import request import math app = Flask(__name__) expire_time = 75 interval_time = 2 * 60 base_services = {"encrypt.vipthink.cn", "payservice.vipthink.cn", "ops.vipthink.cn", "commodity.vipthink.cn", "complex.vipthink.cn", "order.vipthink.cn", "assets.vipthink.cn", "base.vipthink.cn", "radar-admin.vipthink.cn", "radar-engine.vipthink.cn", "wechat.vipthink.cn", "scheduled.vipthink.cn", "flow.vipthink.cn", "registry.vipthink.cn", "job.vipthink.cn", "gw.vipthink.cn", "pjob.vipthink.cn", "admin-server.vipthink.cn", "socket.vipthink.cn", "ticket.vipthink.cn", "auth-center.vipthink.cn", "supplychain.vipthink.cn", "crm.vipthink.cn"} crm = {"shop.vipthink.cn", "www.vipthink.cn", "web.vipthink.cn", "marketing-executor.vipthink.cn", "vippi.net.cn", "mkt.vipthink.cn", "crm-editor.vipthink.cn", "www.vippi.net.cn", "ug.vipthink.cn"} edu = {"cs.vipthink.cn", "train.vipthink.cn", "pb.vipthink.cn", "education-executor.vipthink.cn", "vns.vipthink.cn", "edu.vipthink.cn", "fz.vipthink.cn", "store.vipthink.cn", "ems.vipthink.cn"} bigdata = {"md.vipthink.cn", "bd-query.vipthink.cn", "metadata-mg.vipthink.cn", "et.vipthink.cn", "bd-api.vipthink.cn", "tagsys.vipthink.cn"} cc = {"cc.vipthink.cn"} lp = {"lp.vipthink.cn","ac.vipthink.cn","lp-mobile.vipthink.cn","svc-core.vipthink.cn","csd.vipthink.cn"} jy = {"jy.vipthink.cn"} ld = {"ld.vipthink.cn","ld-mobile.vipthink.cn"} class RedisHandler: def __init__(self): self.host = '127.0.0.1' self.port = 6379 self.db = 8 self.passwd = 'Klzz@3321' if not hasattr(RedisHandler, 'pool'): RedisHandler.create_pool(self.host, self.port, self.db, self.passwd) self._connection = redis.Redis(connection_pool=RedisHandler.pool) @staticmethod def create_pool(HOST,PORT,DB,PASSWD): RedisHandler.pool = redis.ConnectionPool(host=HOST, port=PORT, db=DB, password=PASSWD) def get(self, key): tmp = self._connection.get(key) if tmp: return tmp else: return None def set(self, key, value, redis_expire): self._connection.set(key, value) self._connection.expire(key, redis_expire) def send_dingtalk(content, title, domain, status, messageUrl=None): if messageUrl is None: messageUrl = "https://elk-new.vipthink.cn/app/kibana#/discover?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-30m,to:now))&_a=(columns:!(request,http_referrer,status),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'2f6904c0-f283-11e9-bf42-efcd553e8cfb',key:http_host,negate:!f,params:(query:{0}),type:phrase,value:{0}),query:(match:(http_host:(query:{0},type:phrase)))),('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'2f6904c0-f283-11e9-bf42-efcd553e8cfb',key:status,negate:!f,params:(query:'{1}'),type:phrase,value:'{1}'),query:(match:(status:(query:'{1}',type:phrase))))),index:'2f6904c0-f283-11e9-bf42-efcd553e8cfb',interval:auto,query:(language:kuery,query:''),sort:!('@timestamp',desc))".format(domain, status) if domain in base_services: url = 'https://oapi.dingtalk.com/robot/send?access_token=d5287c1128c5d74fe46c1b369746a771efa76f5c390997a2fb8690829ceb523a' elif domain == 'carnie.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=dcc6b5b3c40e66baf744cbc16d2f61250fbc15062bad57317aee4a80d98175cc' elif domain == 'et.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=ec23471ad4e28da86758eab7346a94d48853296a92819eb1447555d229ac21e7' elif domain == 'workorder.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=257a0a1a0e84c61ea34a10d8431dd6a3ac31cde531026ffff3c4f104422f3585' elif domain == 'member.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=00df02ac34f6aa7927b1990045c65ca5887a258b7eef1f621b6133a7fd597bac' elif domain == 'auth.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=5e8fdd99cefd8cea2db50ea45f9f832deaf884aa61039fee7963d7f2a4315efb' elif domain == 'message.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=829f29f8fed21116920d3dea7ef88ef5c5b340334a7cc90aafc98b4492c724c6' elif domain in crm: url = 'https://oapi.dingtalk.com/robot/send?access_token=154b472751bd271c4a3e6b92ecc4d1551c823c86e821aaece660095ecbf5aabc' elif domain in edu: url = 'https://oapi.dingtalk.com/robot/send?access_token=efbc21575c2e3b77af30fb9fb36497d989a4bb567ea1aa27f6e003d1f86c98f8' elif domain in cc: url = 'https://oapi.dingtalk.com/robot/send?access_token=ab70e927be9119f3a4f91a7067ccd12b02186e005c1646b01a9e210833cafd4b' elif domain in ld: url = 'https://oapi.dingtalk.com/robot/send?access_token=0eff4dc77cdbf90bbb7d430c3265a8dc7f25f119b80b955724495ed7243ba999' elif domain in lp: url = 'https://oapi.dingtalk.com/robot/send?access_token=663785b07fd716049816971bdb01b56da033fb722a15c8128bf44e30da70f826' elif domain in jy: url = 'https://oapi.dingtalk.com/robot/send?access_token=9183837027b46e25bca7916020807404bef47cfe1b24e939fe7ead75bcea7b50' elif domain in bigdata: url = 'https://oapi.dingtalk.com/robot/send?access_token=7d77eff43a63e6a5b06ebf4b255f2e8feaaa1e992ff6850fbc321f28e62c6548' else: url = 'https://oapi.dingtalk.com/robot/send?access_token=d69203ef47671c8d0b5ee85c91c374c318d5a483943bfd017b5a701ae964548d' send_data = { "msgtype": "link", "link": { "text": content, "title": title, "picUrl": "", "messageUrl": messageUrl } } req = requests.post(url, json=send_data) result = req.json() def send_dingtalk_pv(content, title, domain, messageUrl=None): if domain in base_services: url = 'https://oapi.dingtalk.com/robot/send?access_token=d5287c1128c5d74fe46c1b369746a771efa76f5c390997a2fb8690829ceb523a' elif domain == 'carnie.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=dcc6b5b3c40e66baf744cbc16d2f61250fbc15062bad57317aee4a80d98175cc' elif domain == 'et.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=ec23471ad4e28da86758eab7346a94d48853296a92819eb1447555d229ac21e7' elif domain == 'workorder.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=257a0a1a0e84c61ea34a10d8431dd6a3ac31cde531026ffff3c4f104422f3585' elif domain == 'member.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=00df02ac34f6aa7927b1990045c65ca5887a258b7eef1f621b6133a7fd597bac' elif domain == 'auth.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=5e8fdd99cefd8cea2db50ea45f9f832deaf884aa61039fee7963d7f2a4315efb' elif domain == 'message.vipthink.cn': url = 'https://oapi.dingtalk.com/robot/send?access_token=829f29f8fed21116920d3dea7ef88ef5c5b340334a7cc90aafc98b4492c724c6' elif domain in crm: url = 'https://oapi.dingtalk.com/robot/send?access_token=154b472751bd271c4a3e6b92ecc4d1551c823c86e821aaece660095ecbf5aabc' elif domain in edu: url = 'https://oapi.dingtalk.com/robot/send?access_token=efbc21575c2e3b77af30fb9fb36497d989a4bb567ea1aa27f6e003d1f86c98f8' elif domain in bigdata: url = 'https://oapi.dingtalk.com/robot/send?access_token=7d77eff43a63e6a5b06ebf4b255f2e8feaaa1e992ff6850fbc321f28e62c6548' elif domain in cc: url = 'https://oapi.dingtalk.com/robot/send?access_token=ab70e927be9119f3a4f91a7067ccd12b02186e005c1646b01a9e210833cafd4b' elif domain in ld: url = 'https://oapi.dingtalk.com/robot/send?access_token=0eff4dc77cdbf90bbb7d430c3265a8dc7f25f119b80b955724495ed7243ba999' elif domain in lp: url = 'https://oapi.dingtalk.com/robot/send?access_token=663785b07fd716049816971bdb01b56da033fb722a15c8128bf44e30da70f826' elif domain in jy: else: url = 'https://oapi.dingtalk.com/robot/send?access_token=d69203ef47671c8d0b5ee85c91c374c318d5a483943bfd017b5a701ae964548d' if messageUrl is not None: send_data = { "msgtype": "text", "text": { "content": content, } } req = requests.post(url, json=send_data) result = req.json() print(result) return send_data = { "msgtype": "link", "link": { "text": content, "title": title, "picUrl": "", "messageUrl": "https://elk-new.vipthink.cn/app/kibana#/dashboard/aa3539e0-ffb1-11e9-bf42-efcd553e8cfb?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-30m,to:now))&_a=(description:'',filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'2f6904c0-f283-11e9-bf42-efcd553e8cfb',key:http_host.keyword,negate:!f,params:(query:{0}),type:phrase,value:{0}),query:(match:(http_host.keyword:(query:{0},type:phrase))))),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),panels:!((embeddableConfig:(),gridData:(h:43,i:'1',w:24,x:0,y:16),id:dbb85b00-fe03-11e9-bf42-efcd553e8cfb,panelIndex:'1',type:visualization,version:'7.3.0'),(embeddableConfig:(),gridData:(h:15,i:'2',w:24,x:24,y:16),id:'55630650-fe07-11e9-bf42-efcd553e8cfb',panelIndex:'2',type:visualization,version:'7.3.0'),(embeddableConfig:(),gridData:(h:16,i:'3',w:48,x:0,y:0),id:cdb4ae20-fe06-11e9-bf42-efcd553e8cfb,panelIndex:'3',type:visualization,version:'7.3.0'),(embeddableConfig:(),gridData:(h:27,i:'4',w:24,x:24,y:31),id:d8fbdcf0-521f-11ea-b1ff-55cf161b14bb,panelIndex:'4',type:visualization,version:'7.3.0')),query:(language:kuery,query:''),timeRestore:!f,title:Nginx-Core,viewMode:view)".format(domain) } } req = requests.post(url, json=send_data) result = req.json() def send_dingtalk_error(content, title, domain, messageUrl=None): if messageUrl is None: messageUrl = "https://elk-new.vipthink.cn/app/kibana#/discover?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-24h,to:now))&_a=(columns:!(_source),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:d3568400-fc4b-11e9-bf42-efcd553e8cfb,key:log.file.path,negate:!f,params:(query:%2Fdata%2Flogs%2Fapp%2Femserr.vipthink.cn.log),type:phrase,value:%2Fdata%2Flogs%2Fapp%2Femserr.vipthink.cn.log),query:(match:(log.file.path:(query:%2Fdata%2Flogs%2Fapp%2Femserr.vipthink.cn.log,type:phrase))))),index:'1527cdc0-742a-11ea-b1ff-55cf161b14bb',interval:auto,query:(language:kuery,query:'errorLevel:%208%20or%20errorLevel:%209%20'),sort:!('@timestamp',desc))" url = 'https://oapi.dingtalk.com/robot/send?access_token=2852ca77ec71f42d62469605445d94680835795eb08ce3ed605453cc99cb25b2' send_data = { "msgtype": "link", "link": { "text": content, "title": title, "picUrl": "", "messageUrl": messageUrl } } req = requests.post(url, json=send_data) result = req.json() def send_dingtalk_program_error(content, title, program_name, messageUrl=None): if messageUrl is None: messageUrl = "https://elk-new.vipthink.cn/app/kibana#/discover?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(columns:!(message,host.hostname),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:ce0fb650-1637-11ea-b48f-d774c72fb293,key:log.file.path,negate:!f,params:(query:%2Fdata%2Flogs%2Fapp%2Fjava%2Fmember.vipthink.cn.log),type:phrase,value:%2Fdata%2Flogs%2Fapp%2Fjava%2Fmember.vipthink.cn.log),query:(match:(log.file.path:(query:%2Fdata%2Flogs%2Fapp%2Fjava%2Fmember.vipthink.cn.log,type:phrase))))),index:ce0fb650-1637-11ea-b48f-d774c72fb293,interval:auto,query:(language:kuery,query:'message:%22*java.lang.OutOfMemoryError*%22'),sort:!('@timestamp',desc))" url = 'https://oapi.dingtalk.com/robot/send?access_token=00df02ac34f6aa7927b1990045c65ca5887a258b7eef1f621b6133a7fd597bac' send_data = { "msgtype": "link", "link": { "text": content, "title": title, "picUrl": "", "messageUrl": messageUrl } } req = requests.post(url, json=send_data) result = req.json() @app.route('/', methods=['POST', 'GET']) def send(): if request.method == 'POST': post_data = request.get_data() print(post_data) send_alert(json.loads(post_data)) return 'success' else: return 'must post' @app.route('/elk_pv', methods=['POST', 'GET']) def elk_pv(): if request.method == 'POST': post_data = str(request.get_data(), encoding = "utf8") domain = post_data.split("|")[0].split("check_")[1].replace("_",".") s_time = post_data.split("|")[1] r_handler = RedisHandler() str_time = r_handler.get(domain) if str_time and str(str_time, encoding = "utf8") == str(s_time): pass else: content = "{}最近15分钟的流量环比异常".format(domain) title = "告警" r_handler.set(domain, s_time, 30 * 60) send_dingtalk_pv(content, title, domain) return 'success' else: return 'must post' @app.route('/elk', methods=['POST', 'GET']) def send_elk(): try: if request.method == 'POST': timestamp = int(time.time()) post_data = str(request.get_data(), encoding = "utf8") r_handler = RedisHandler() #print(post_data.split("|")[0]) count = post_data.split("|")[1] key = post_data.split("|")[0] value = "{}|{}".format(count, timestamp) old_value = r_handler.get(key) domain = key.split("_")[0] code = key.split("_")[1] content = "{} http code {} 最近1分钟 count is {}".format(domain, code, count) title = "告警" if old_value: old_value = str(old_value, encoding = "utf8") old_timestamp = int(old_value.split("|")[1]) r_handler.set(key, "{}|{}".format(count, old_timestamp), expire_time) if (timestamp - old_timestamp) > interval_time: send_dingtalk(content, title, domain, code) else: r_handler.set(key, value, expire_time) send_dingtalk(content, title, domain, code) return 'success' else: return 'must post' except Exception as e: print(e) return 'failed' @app.route('/elk_new', methods=['POST', 'GET']) def elk_new(): try: if request.method == 'POST': timestamp = int(time.time()) post_data = str(request.get_data(), encoding = "utf8") r_handler = RedisHandler() #print(post_data.split("|")) count = post_data.split("|")[3] domain = post_data.split("|")[0] code = post_data.split("|")[2] path = post_data.split("|")[1] key = "{}{}|{}".format(domain, path, code) value = "{}|{}".format(count, timestamp) title = "告警" content = "{}{} http code {} 最近1分钟 count is {}".format(domain, path, code, count) old_value = r_handler.get(key) if old_value: old_timestamp = int(str(old_value, encoding = "utf8").split("|")[1]) r_handler.set(key, "{}|{}".format(count, old_timestamp), expire_time) if (timestamp - old_timestamp) > interval_time: send_dingtalk(content, title, domain, code) else: r_handler.set(key, value, expire_time) send_dingtalk(content, title, domain, code) return 'success' else: return 'must post' except Exception as e: print(e) return 'failed' @app.route('/elk_new_httpcode', methods=['POST', 'GET']) def elk_new_httpcode(): #try: if request.method == 'POST': timestamp = int(time.time()) post_data = str(request.get_data(), encoding = "utf8") r_handler = RedisHandler() count = post_data.split("|")[3] domain = post_data.split("|")[0] code = post_data.split("|")[2] path = post_data.split("|")[1] if len(path) == 0: return 'success' key = "{}{}|{}".format(domain, path, code) value = "{}|{}".format(count, timestamp) if str(code) == '502': title = "502告警" elif str(code) == '500': title = "500告警" elif str(code) == '504': title = "504告警" elif str(code) == '499': title = "499告警" else: title = "告警" content = "{}{} 最近1分钟请求状态码是{}出现{}次".format(domain, path, code, count) messageUrl = "https://es-ep1wde6v.kibana.tencentelasticsearch.com:5601/app/kibana#/discover?_g=(filters:!())&_a=(columns:!(request,status,host.name,http_referrer,remote_addr),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'89559940-baa5-11ea-b242-c723d86cf1aa',key:http_host,negate:!f,params:(query:{0}),type:phrase),query:(match:(http_host:(query:{0},type:phrase)))),('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'89559940-baa5-11ea-b242-c723d86cf1aa',key:status,negate:!f,params:(query:'{1}'),type:phrase),query:(match:(status:(query:'{1}',type:phrase))))),index:'89559940-baa5-11ea-b242-c723d86cf1aa',interval:auto,query:(language:kuery,query:''),sort:!(!('@timestamp',desc)))".format(domain, code) old_value = r_handler.get(key) if old_value: old_timestamp = int(str(old_value, encoding = "utf8").split("|")[1]) r_handler.set(key, "{}|{}".format(count, old_timestamp), expire_time) if (timestamp - old_timestamp) > interval_time: send_dingtalk(content, title, domain, code) else: r_handler.set(key, value, expire_time) send_dingtalk(content, title, domain, code) return 'success' else: return 'must post' #except Exception as e: #print(e) #return 'failed' @app.route('/elk_restore', methods=['POST', 'GET']) def elk_restore(): if request.method == 'POST': post_data = eval(str(request.get_data(), encoding = "utf-8")) domain = post_data.get("domain") code = post_data.get("code") content = "{} http code {} 最近1分钟 count is 0".format(domain, code) title = "恢复" send_dingtalk(content, title, domain, code) return 'success' else: return 'must post' def send_alert_data(data,statu): url = 'https://oapi.dingtalk.com/robot/send?access_token=abd5624f0f222a340ba95d2a9e9bbb6e03a910f2a1a90a57337c822874ac30a3' alerts = data alerts_content = statu for i in range(len(alerts)): annotations = alerts[i].get("annotations") alerts_content_tmp = "%s\n" %(annotations.get("description")) alerts_content += alerts_content_tmp send_data = {"msgtype": "text", "text": { "content": alerts_content }} print(send_data) req = requests.post(url, json=send_data) result = req.json() def send_alert(data): alerts = data['alerts'] alert_name = data['groupLabels']['alertname'] alerts_content_firing = '告警\n' alerts_content_resolved = '恢复\n' alerts_firing_data = [] alerts_resolved_data = [] for i in range(len(alerts)): if 'fingerprint' in alerts[i]: alerts[i].pop('fingerprint') if 'endsAt' in alerts[i]: alerts[i].pop('endsAt') if 'startsAt' in alerts[i]: alerts[i].pop('startsAt') if alerts[i].get('status') == 'resolved': if alerts[i] not in alerts_resolved_data: alerts_resolved_data.append(alerts[i]) elif alerts[i].get('status') == 'firing': if alerts[i] not in alerts_firing_data: alerts_firing_data.append(alerts[i]) if len(alerts_resolved_data) != 0: send_alert_data(alerts_resolved_data, alerts_content_resolved) if len(alerts_firing_data) !=0: send_alert_data(alerts_firing_data, alerts_content_firing) @app.route('/elk_new_timeout', methods=['POST', 'GET']) def elk_new_timeout(): try: if request.method == 'POST': timestamp = int(time.time()) post_data = str(request.get_data(), encoding = "utf8") r_handler = RedisHandler() count = post_data.split("|")[3] domain = post_data.split("|")[0] request_time = post_data.split("|")[2] path = post_data.split("|")[1] key = "{}{}|{}".format(domain, path, request_time) value = "{}|{}".format(count, timestamp) title = "超时告警" content = "{}{} 最近1分钟响应时间超过{}秒的请求出现{}次".format(domain, path, request_time, count) # messageUrl = "https://elk-new.vipthink.cn/app/kibana#/discover?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-30m,to:now))&_a=(columns:!(request_time,request),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'2f6904c0-f283-11e9-bf42-efcd553e8cfb',key:http_host,negate:!f,params:(query:{0}),type:phrase,value:{0}),query:(match:(http_host:(query:{0},type:phrase)))),('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'2f6904c0-f283-11e9-bf42-efcd553e8cfb',key:uri.keyword,negate:!f,params:(query:{1}),type:phrase,value:{1}),query:(match:(uri.keyword:(query:{1},type:phrase))))),index:'2f6904c0-f283-11e9-bf42-efcd553e8cfb',interval:auto,query:(language:kuery,query:'request_time%20%3E%3D%20{2}'),sort:!('@timestamp',desc))".format(domain, path, request_time) messageUrl = "https://es-ep1wde6v.kibana.tencentelasticsearch.com:5601/app/kibana#/discover?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_a=(columns:!(request,request_time,host.name,remote_addr),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'89559940-baa5-11ea-b242-c723d86cf1aa',key:http_host,negate:!f,params:(query:{0}),type:phrase),query:(match:(http_host:(query:{0},type:phrase)))),('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'89559940-baa5-11ea-b242-c723d86cf1aa',key:uri.keyword,negate:!f,params:(query:{1}),type:phrase),query:(match:(uri.keyword:(query:{1},type:phrase))))),index:'89559940-baa5-11ea-b242-c723d86cf1aa',interval:auto,query:(language:kuery,query:'request_time%20%3E%3D%20{2}'),sort:!(!('@timestamp',desc)))".format(domain, path, request_time) old_value = r_handler.get(key) if old_value: old_timestamp = int(str(old_value, encoding = "utf8").split("|")[1]) r_handler.set(key, "{}|{}".format(count, old_timestamp), expire_time) if (timestamp - old_timestamp) > interval_time: if domain != "1111carnie.vipthink.cn": print("111111") send_dingtalk(content, title, domain, request_time, messageUrl) else: if domain != "1111carnie.vipthink.cn": print("111111") r_handler.set(key, value, expire_time) send_dingtalk(content, title, domain, request_time, messageUrl) return 'success' else: return 'must post' except Exception as e: print(e) return 'failed' @app.route('/elk_new_error', methods=['POST', 'GET']) def elk_new_error(): try: if request.method == 'POST': timestamp = int(time.time()) post_data = str(request.get_data(), encoding = "utf8") r_handler = RedisHandler() print(post_data) count = post_data.split("|")[1] domain = post_data.split("|")[0] key = "{}|error".format(domain) value = "{}|{}".format(count, timestamp) title = "告警" content = "{} 最近1分钟\"errorLevel:8 or errorLevel:9\"出现{}次".format(domain, count) old_value = r_handler.get(key) if old_value: old_timestamp = int(str(old_value, encoding = "utf8").split("|")[1]) r_handler.set(key, "{}|{}".format(count, old_timestamp), expire_time) if (timestamp - old_timestamp) > interval_time: send_dingtalk_error(content, title, domain) else: r_handler.set(key, value, expire_time) send_dingtalk_error(content, title, domain) return 'success' else: return 'must post' except Exception as e: print(e) return 'failed' @app.route('/elk_new_program_error', methods=['POST', 'GET']) def elk_new_program_error(): try: if request.method == 'POST': timestamp = int(time.time()) post_data = str(request.get_data(), encoding = "utf8") r_handler = RedisHandler() print(post_data) count = post_data.split("|")[1] program_name = post_data.split("|")[0] key = "{}|OOM".format(program_name) value = "{}|{}".format(count, timestamp) title = "告警" content = "{} 最近1分钟\"OutOfMemoryError\"出现{}次".format(program_name, count) old_value = r_handler.get(key) if old_value: old_timestamp = int(str(old_value, encoding = "utf8").split("|")[1]) r_handler.set(key, "{}|{}".format(count, old_timestamp), expire_time) if (timestamp - old_timestamp) > interval_time: send_dingtalk_program_error(content, title, program_name) else: r_handler.set(key, value, expire_time) send_dingtalk_program_error(content, title, program_name) return 'success' else: return 'must post' except Exception as e: print(e) return 'failed' def exclude_(typicalSort, actualSort): print("###############典型", typicalSort," 实际", actualSort, "#########################") if typicalSort < 10000 and actualSort < 10000: print('typicalSort < 10000 and actualSort < 10000') return False elif typicalSort < 10000: if actualSort != 0: if typicalSort/actualSort < 2: if typicalSort != 0: if actualSort/typicalSort > 2: print('typicalSort < 10000 and actualSort/typicalSort > 2') return True else: print('typicalSort < 10000 and actualSort/typicalSort < 2') return False else: print('typicalSort < 10000 and typicalSort/actualSort < 2') return False #elif typicalSort != 0: # if actualSort/typicalSort > 2: # print('typicalSort < 10000 and actualSort/typicalSort > 2') # return True # else: # print('typicalSort < 10000 and actualSort/typicalSort < 2') # return False else: return True else: return True elif actualSort < 10000: if typicalSort != 0: if actualSort/typicalSort < 2: print('actualSort < 10000 and actualSort/typicalSort < 2') return False elif actualSort != 0: if typicalSort/actualSort > 2: print('actualSort < 10000 and typicalSort/actualSort > 2') return True else: print('actualSort < 10000 and typicalSort/actualSort < 2') return False else: return True else: return True elif actualSort != 0 and typicalSort/actualSort < 2: print("actualSort != 0 and typicalSort/actualSort < 2") return False elif typicalSort != 0 and actualSort/typicalSort < 2: print("typicalSort != 0 and actualSort/typicalSort < 2") return False else: return True @app.route('/trend', methods=['POST', 'GET']) def trend(): exclude = ['png', 'jpg', 'png'] if request.method == 'POST': post_data = str(request.get_data(), encoding = "utf8") data = json.loads(post_data) data_ = re.sub('false', '1', data[0]) data_ = re.sub('true', '0', data_) data_ = re.sub('null', '\'null\'', data_) data_0 = eval(data_) anomalies_data = data_0.get('anomalies') title = "PV异常告警" content = '' if data[1] is not None: domain = data[1] for i in anomalies_data: entityValue = i.get('entityValue') typicalSort = i.get('typicalSort') actualSort = i.get('actualSort') try: path_suffix = entityValue.split('.')[1] except: path_suffix = '' if path_suffix not in exclude: content = '{}{} 最近5分钟PV预估是 {},但实际是 {}'.format(domain, entityValue, math.ceil(typicalSort), math.ceil(actualSort)) if exclude_(math.ceil(typicalSort), math.ceil(actualSort)) is False: content = '' print(content) if content != '': print("#####################") print(content) print("#####################") send_dingtalk_pv(content, title, domain) else: for i in anomalies_data: domain = i.get('entityValue') if domain != '': typicalSort = i.get('typicalSort') actualSort = i.get('actualSort') content = '{} 5分钟PV预估是 {},但实际是 {}'.format(domain, math.ceil(typicalSort), math.ceil(actualSort)) if exclude_(math.ceil(typicalSort), math.ceil(actualSort)) is False: content = '' print(content) if content != '': print("#####################") print(content) print("#####################") send_dingtalk_pv(content, title, domain) return 'success' else: return 'must post' @app.route('/top_ip', methods=['POST', 'GET']) def top_ip(): if request.method == 'POST': post_data = str(request.get_data(), encoding = "utf8") data = json.loads(post_data) domain = data.get('domain') data = data.get('data') title = 'top10的ip' tmp = domain + ' 15分钟top10的ip告警\n' for k in data.get('buckets'): tmp = tmp + k.get('key') + '访问次数是 ' + str(k.get('doc_count')) + '次\n' send_dingtalk_pv(tmp, title, domain, "no_messageUrl") return 'success' else: return 'must post' @app.route('/elk_pv1', methods=['POST', 'GET']) def elk_pv1(): return 'success' if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)