125 lines
6.3 KiB
Plaintext
125 lines
6.3 KiB
Plaintext
# -*- coding: utf-8 -*-
|
|
from flask import Flask, render_template, request
|
|
from flask_mail import Mail, Message
|
|
from threading import Thread
|
|
from mmysql import MysqlBase
|
|
import time
|
|
|
|
app = Flask(__name__)
|
|
app.config['MAIL_SERVER'] = "smtp.exmail.qq.com"
|
|
app.config['MAIL_PORT'] = '25'
|
|
app.config['MAIL_USERNAME'] = 'moretv_alert@moretv.com.cn'
|
|
app.config['MAIL_PASSWORD'] = 'OX7O0ahM'
|
|
|
|
mail = Mail(app)
|
|
|
|
|
|
def send_async_email(app, msg):
|
|
with app.app_context():
|
|
mail.send(msg)
|
|
|
|
|
|
def get_data_mysql(conn, sql):
|
|
m = MysqlBase(**conn)
|
|
data = m.query(sql)
|
|
return data
|
|
|
|
|
|
def return_yesterday(interval=1):
|
|
base_time = time.time()
|
|
before = base_time - interval * 24 * 3600
|
|
return time.strftime("%Y%m%d", time.localtime(before))
|
|
|
|
|
|
@app.route('/send-gaoshen-mail/')
|
|
def indexgaoshen():
|
|
days = request.args.get('days')
|
|
if not days:
|
|
days = return_yesterday()
|
|
sql_moguv = "select day,host,sid,title,round(ratio*100,2),rank from cdn_sendbytes_statistics where rank<=10 and host='mediags.moguv.com' and day='%s';" % days
|
|
sql_moguv_2 = "select day,host,sid,title,round(ratio*100,2),rank from cdn_sendbytes_statistics where rank<=10 and host='mediags2.moguv.com' and day='%s';" % days
|
|
sql_moretv = "select day,host,sid,title,round(ratio*100,2),rank from cdn_sendbytes_statistics where rank<=10 and host='media-gs.mairx.com' and day='%s';" % days
|
|
sql_p2p = "select day,host,sid,title,round(ratio*100,2),rank from cdn_sendbytes_statistics where rank<=10 and host='p2p-gs.mairx.com' and day='%s';" % days
|
|
|
|
conn_data = dict()
|
|
conn_data['db'] = "bi"
|
|
conn_data['host'] = "10.19.72.224"
|
|
conn_data['user'] = "bi"
|
|
conn_data['pswd'] = "mlw321@moretv"
|
|
|
|
data_moguv = get_data_mysql(conn_data, sql_moguv)
|
|
data_moguv_2 = get_data_mysql(conn_data, sql_moguv_2)
|
|
data_moretv = get_data_mysql(conn_data, sql_moretv)
|
|
data_p2p = get_data_mysql(conn_data, sql_p2p)
|
|
|
|
# if data_moguv and data_moguv_2:
|
|
msg = Message('主题', sender="whaley_alert@moretv.com.cn",
|
|
recipients=["peng.tao@moretv.com.cn", "wu.jiulin@moretv.com.cn", "wu.qi@moretv.com.cn", "wu.qiang@moretv.com.cn",
|
|
"ge.yongliang@moretv.com.cn", "xie.haofeng@moretv.com.cn", "xie.jiangming@moretv.com.cn",
|
|
"jin.zhoulu@moretv.com.cn", "ma.xiaoqing@moretv.com.cn", "ren.wei@moretv.com.cn", "fan.hua@moretv.com.cn",
|
|
"dai.yunseng@moretv.com.cn", "liu.xi@moretv.com.cn", "pan.jian@moretv.com.cn",
|
|
"xu.shihuan@moretv.com.cn"])
|
|
msg.subject = "%s高升节目数据" % str(days)
|
|
msg.html = render_template('gaoshen_cdn_traffic.html', data_moguv=data_moguv, data_moguv_2=data_moguv_2,
|
|
data_moretv=data_moretv, data_p2p=data_p2p, days=days)
|
|
# else:
|
|
# msg = Message('主题', sender="whaley_alert@moretv.com.cn",
|
|
# recipients=["peng.tao@moretv.com.cn", "wu.jiulin@moretv.com.cn", "lian.kai@moretv.com.cn",
|
|
# "ge.yongliang@moretv.com.cn"])
|
|
# msg.subject = "%s高升节目数据异常报警" % days
|
|
# msg.html = render_template('gaoshen_cdn_traffic_error.html', data_moguv=data_moguv, data_moguv_2=data_moguv_2,
|
|
# data_moretv=data_moretv, data_p2p=data_p2p, days=days)
|
|
thread = Thread(target=send_async_email, args=[app, msg])
|
|
thread.start()
|
|
|
|
return '<h1>邮件发送成功</h1>'
|
|
|
|
|
|
@app.route('/send-azure-mail/')
|
|
def indexazure():
|
|
# 'media-wr.moguv.com', 'media2-wr.moguv.com', 'media-wr.moretv.com.cn'
|
|
days = request.args.get('days')
|
|
if not days:
|
|
days = return_yesterday()
|
|
|
|
sql_moguv = "select day,host,sid,title,round(ratio*100,2),rank from cdn_sendbytes_statistics where rank<=10 and host='media-wr.moguv.com' and day='%s';" % days
|
|
sql_moguv_2 = "select day,host,sid,title,round(ratio*100,2),rank from cdn_sendbytes_statistics where rank<=10 and host='media2-wr.moguv.com' and day='%s';" % days
|
|
sql_moretv = "select day,host,sid,title,round(ratio*100,2),rank from cdn_sendbytes_statistics where rank<=10 and host='media-wr.mairx.com' and day='%s';" % days
|
|
sql_p2p = "select day,host,sid,title,round(ratio*100,2),rank from cdn_sendbytes_statistics where rank<=10 and host='p2p-wr.mairx.com' and day='%s';" % days
|
|
|
|
conn_data = dict()
|
|
conn_data['db'] = "bi"
|
|
conn_data['host'] = "10.19.72.224"
|
|
conn_data['user'] = "bi"
|
|
conn_data['pswd'] = "mlw321@moretv"
|
|
|
|
data_moguv = get_data_mysql(conn_data, sql_moguv)
|
|
data_moguv_2 = get_data_mysql(conn_data, sql_moguv_2)
|
|
data_moretv = get_data_mysql(conn_data, sql_moretv)
|
|
data_p2p = get_data_mysql(conn_data, sql_p2p)
|
|
# if data_moguv and data_moguv_2:
|
|
msg = Message('主题', sender="whaley_alert@moretv.com.cn",
|
|
recipients=["peng.tao@moretv.com.cn", "wu.jiulin@moretv.com.cn", "wu.qi@moretv.com.cn", "wu.qiang@moretv.com.cn",
|
|
"ge.yongliang@moretv.com.cn", "xie.haofeng@moretv.com.cn", "xie.jiangming@moretv.com.cn",
|
|
"jin.zhoulu@moretv.com.cn", "ma.xiaoqing@moretv.com.cn", "ren.wei@moretv.com.cn", "fan.hua@moretv.com.cn",
|
|
"dai.yunseng@moretv.com.cn", "liu.xi@moretv.com.cn", "pan.jian@moretv.com.cn",
|
|
"xu.shihuan@moretv.com.cn"])
|
|
msg.subject = "%s微软节目数据" % str(days)
|
|
msg.html = render_template('azure_cdn_traffic.html', data_moguv=data_moguv, data_moguv_2=data_moguv_2,
|
|
data_moretv=data_moretv, data_p2p=data_p2p, days=days)
|
|
# else:
|
|
# msg = Message('主题', sender="whaley_alert@moretv.com.cn",
|
|
# recipients=["peng.tao@moretv.com.cn", "wu.jiulin@moretv.com.cn", "lian.kai@moretv.com.cn",
|
|
# "ge.yongliang@moretv.com.cn"])
|
|
# msg.subject = "%s微软节目数据异常报警" % days
|
|
# msg.html = render_template('azure_cdn_traffic_error.html', data_moguv=data_moguv, data_moguv_2=data_moguv_2,
|
|
# data_moretv=data_moretv, data_p2p=data_p2p, days=days)
|
|
thread = Thread(target=send_async_email, args=[app, msg])
|
|
thread.start()
|
|
|
|
return '<h1>邮件发送成功</h1>'
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000, debug=False)
|