Commit 87b573be by 刘豪

添加告警

parent 4dc5b7e3
Showing with 76 additions and 24 deletions
......@@ -9,6 +9,7 @@ from utils import ftp
from utils import file_handle
from utils import rabbit_mq
from utils import log_manage
from utils import monitor
......@@ -194,13 +195,22 @@ def get_supplier_id(supplier_name: str, conf: dict) -> int:
db.close()
return supplier_id
def down_cn(ftp_client, extract_dir):
def down_cn(extract_dir, count=1):
if count > 3:
monitor.wechat_alarm("下载压缩rs大陆压缩包3次失败,请检查")
return
ftp_client = ftp.FTPUtil(host=conf_environment["ftp"]["host"],
port=conf_environment["ftp"]["port"],
passive=True,
username=conf_environment["ftp"]["username"],
password=conf_environment["ftp"]["password"],
logger = logger)
"""download the zip file"""
logger.info("下载cn压缩包")
ftp_client.download(remote_file_name_3, conf_environment["tmp_file_path"] + local_file_name_3)
"""unzip the zip file"""
try:
logger.info("解压压缩包")
logger.info("解压cn压缩包")
file_handle.unzip(conf_environment["tmp_file_path"] + local_file_name_3, extract_dir)
"""handle the file and then post to rabbitmq"""
rs_hk_supplier_id = get_supplier_id(supplier_name='Rs', conf=conf_environment)
......@@ -209,10 +219,23 @@ def down_cn(ftp_client, extract_dir):
logger.info("不支持的supplier_id")
sys.exit("can't query the correct supplier id.")
handle_cn_csv(extract_dir + handle_file_name_3, conf_environment, rs_cn_supplier_id)
remove_cn_file(extract_dir)
except Exception as e:
logger.error("报错:{}".format(e))
logger.error("cn报错:{},进行重试".format(e))
remove_cn_file(extract_dir)
return down_cn(extract_dir, count+1)
def down_hk(ftp_client, extract_dir):
def down_hk(extract_dir, count=1):
if count > 3:
monitor.wechat_alarm("下载压缩rs香港压缩包3次失败,请检查")
return
ftp_client = ftp.FTPUtil(host=conf_environment["ftp"]["host"],
port=conf_environment["ftp"]["port"],
passive=True,
username=conf_environment["ftp"]["username"],
password=conf_environment["ftp"]["password"],
logger = logger)
"""download the zip file"""
logger.info("下载hk压缩包")
......@@ -232,17 +255,24 @@ def down_hk(ftp_client, extract_dir):
logger.info("不支持的supplier_id")
sys.exit("can't query the correct supplier id.")
handle_hk_csv(extract_dir + handle_file_name_1, conf_environment, rs_hk_supplier_id)
remove_hk_file(extract_dir)
except Exception as e:
logger.error("报错:{}".format(e))
logger.error("hk报错:{}, 进行重试".format(e))
remove_hk_file(extract_dir)
return down_hk(extract_dir, count+1)
def remove_file(extract_dir):
def remove_cn_file(extract_dir):
"""remove the temp file, close the connection"""
logger.info("删除压缩包")
remove(extract_dir + handle_file_name_1)
logger.info("删除cn压缩包")
remove(extract_dir + handle_file_name_3)
remove(conf_environment["tmp_file_path"] + local_file_name_1)
remove(conf_environment["tmp_file_path"] + local_file_name_3)
def remove_hk_file(extract_dir):
"""remove the temp file, close the connection"""
logger.info("删除hk压缩包")
remove(extract_dir + handle_file_name_1)
remove(conf_environment["tmp_file_path"] + local_file_name_1)
def run():
if not os.path.exists(conf_environment["tmp_file_path"]):
......@@ -250,21 +280,10 @@ def run():
extract_dir = conf_environment["tmp_file_path"] + "extract/"
if not os.path.exists(extract_dir):
os.makedirs(extract_dir)
ftp_client = ftp.FTPUtil(host=conf_environment["ftp"]["host"],
port=conf_environment["ftp"]["port"],
passive=True,
username=conf_environment["ftp"]["username"],
password=conf_environment["ftp"]["password"],
logger = logger)
down_cn(ftp_client, extract_dir)
ftp_client = ftp.FTPUtil(host=conf_environment["ftp"]["host"],
port=conf_environment["ftp"]["port"],
passive=True,
username=conf_environment["ftp"]["username"],
password=conf_environment["ftp"]["password"],
logger = logger)
down_hk(ftp_client, extract_dir)
remove_file(extract_dir)
down_cn(extract_dir)
down_hk(extract_dir)
logger.info("运行完成")
......
import sys
import requests
ENVIRONMENT = 'test' if sys.platform in ('darwin', 'win32') else 'produce'
if ENVIRONMENT == "test":
env = "测试环境"
tom_key = "616e9c12-e212-4f26-8614-7550d07a4f20"
else:
env = "正式环境"
tom_key = "5a2e38a2-952b-4c70-b1dc-e8c6dd6a452b"
wechat_robot_api = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
def send_msg(msg, robot=tom_key):
data = {
"msgtype": "text",
"text": {
"content": msg
}
}
requests.post(wechat_robot_api + robot, json=data)
# print("robot 消息:" + str(msg))
def wechat_alarm(err_msg):
alarm_msg = {
"告警所属项目": "【木卫python】",
"告警所属环境": env,
"错误信息": err_msg
}
msgStr = "告警提示:\n"
for k, v in alarm_msg.items():
msgStr += k + ":" + str(v) + "\n"
send_msg(msgStr)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment