import json
from MyUtils.Dingtalk import send_message_markdown
import datetime
import time
import pymysql
import pytz
SELECT_NULL_SQL = "SELECT COUNT(*) from sagacloud_setup.space_meet_set where space_id is null or space_id = '--' or meeting_id is null or meeting_id = '--';"
SELECT_MEETING_SQL = " select COUNT(*) from meeting where id not in (select meeting_id from space_meet_set)"
def datetime_now():
# datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
#容器时间
# tz = pytz.timezone('Asia/Shanghai') # 东八区
datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
return datetime_now
# 读取配置文件信息
with open("config.json", "r") as f:
data = json.load(f)
mysql = data["mysql"]
database = mysql["database"]
dingding = data["dingding"]
print("%s 等待开始检查..." % datetime_now())
while True:
time_now = datetime.datetime.fromtimestamp(int(time.time()),
pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
# time_now = time.strftime("%H:%M:%S", time.localtime()) # 刷新
am_set_time = "04:00:00"
if time_now == am_set_time: # 此处设置每天定时的时间
# 钉钉推送消息
messages = []
# 连接mysql
conn = pymysql.connect(**mysql)
mysql_cur = conn.cursor()
# 获取空值
mysql_cur.execute(SELECT_NULL_SQL)
null_sum = mysql_cur.fetchall()[0][0]
if null_sum >0:
print("%s 表space_meet_set里有%s个空值"%(datetime_now(),null_sum))
message = (" - **类型:** 空间id为空 \n" + " - **数量:** %s \n") % (null_sum)
messages.append(message)
messages.append("___")
else:
print("%s 表space_meet_set里没有空值"%(datetime_now()))
# 获取未对应
mysql_cur.execute(SELECT_MEETING_SQL)
meeting_sum = mysql_cur.fetchall()[0][0]
if meeting_sum > 0:
print("%s meeting_id未对应%s个"%(datetime_now(),meeting_sum))
message = (" - **类型:** meeting_id未对应 \n" + " - **数量:** %s \n") % (meeting_sum)
messages.append(message)
messages.append("___")
else:
print("%s meeting_id未对应0个" % (datetime_now()))
# 关闭数据库
mysql_cur.close()
conn.close()
if messages:
print("%s 发送钉钉报警" % datetime_now())
messages.append("详情查看MySQL数据库sagacloud_setup里的space_meet_set表")
messages_info = "\n".join(messages)
# 如果有报警,发送钉钉
title = "## **【之江实验室】meeting_id报警** "
messages_info = title + "\n" + "___" + "\n" + messages_info
send_message_markdown(title, messages_info, dingding)
time.sleep(2)