main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import json
  2. from MyUtils.Dingtalk import send_message_markdown
  3. import datetime
  4. import time
  5. import pymysql
  6. import pytz
  7. 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 = '--';"
  8. SELECT_MEETING_SQL = " select COUNT(*) from meeting where id not in (select meeting_id from space_meet_set)"
  9. def datetime_now():
  10. # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  11. #容器时间
  12. # tz = pytz.timezone('Asia/Shanghai') # 东八区
  13. datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
  14. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  15. return datetime_now
  16. # 读取配置文件信息
  17. with open("config.json", "r") as f:
  18. data = json.load(f)
  19. mysql = data["mysql"]
  20. database = mysql["database"]
  21. dingding = data["dingding"]
  22. print("%s 等待开始检查..." % datetime_now())
  23. while True:
  24. time_now = datetime.datetime.fromtimestamp(int(time.time()),
  25. pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
  26. # time_now = time.strftime("%H:%M:%S", time.localtime()) # 刷新
  27. am_set_time = "04:00:00"
  28. if time_now == am_set_time: # 此处设置每天定时的时间
  29. # 钉钉推送消息
  30. messages = []
  31. # 连接mysql
  32. conn = pymysql.connect(**mysql)
  33. mysql_cur = conn.cursor()
  34. # 获取空值
  35. mysql_cur.execute(SELECT_NULL_SQL)
  36. null_sum = mysql_cur.fetchall()[0][0]
  37. if null_sum >0:
  38. print("%s 表space_meet_set里有%s个空值"%(datetime_now(),null_sum))
  39. message = (" - **类型:** 空间id为空 \n" + " - **数量:** <font color=#FF0000>%s</font> \n") % (null_sum)
  40. messages.append(message)
  41. messages.append("___")
  42. else:
  43. print("%s 表space_meet_set里没有空值"%(datetime_now()))
  44. # 获取未对应
  45. mysql_cur.execute(SELECT_MEETING_SQL)
  46. meeting_sum = mysql_cur.fetchall()[0][0]
  47. if meeting_sum > 0:
  48. print("%s meeting_id未对应%s个"%(datetime_now(),meeting_sum))
  49. message = (" - **类型:** meeting_id未对应 \n" + " - **数量:** <font color=#FF0000>%s</font> \n") % (meeting_sum)
  50. messages.append(message)
  51. messages.append("___")
  52. else:
  53. print("%s meeting_id未对应0个" % (datetime_now()))
  54. # 关闭数据库
  55. mysql_cur.close()
  56. conn.close()
  57. if messages:
  58. print("%s 发送钉钉报警" % datetime_now())
  59. messages.append("<font color=#0080FF>详情查看MySQL数据库sagacloud_setup里的space_meet_set表</font>")
  60. messages_info = "\n".join(messages)
  61. # 如果有报警,发送钉钉
  62. title = "## **【之江实验室】meeting_id报警** "
  63. messages_info = title + "\n" + "___" + "\n" + messages_info
  64. send_message_markdown(title, messages_info, dingding)
  65. time.sleep(2)