main.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import json
  2. from MyUtils.Dingtalk import send_message_markdown
  3. import datetime
  4. import time
  5. import pymysql
  6. import pytz,os
  7. SELECT_SQL = "SELECT sms.space_id, m2.id, m2. NAME FROM meeting m2 " \
  8. "LEFT JOIN space_meet_set sms ON sms.meeting_id = m2.id WHERE m2.id IN" \
  9. " ( SELECT id FROM meeting m WHERE classification = %s AND m.building_id = '%s' AND m. STATUS = 1 );"
  10. def datetime_now():
  11. # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  12. #容器时间
  13. # tz = pytz.timezone('Asia/Shanghai') # 东八区
  14. datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
  15. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  16. return datetime_now
  17. building_id = "d89a6d4c73094f9f82eafb5d071e0934"
  18. classifications = {"1":"公共会议室","0":"租区会议室"}
  19. # # 读取配置文件信息
  20. # with open("config.json", "r") as f:
  21. # data = json.load(f)
  22. # mysql = data["mysql"]
  23. # database = mysql["database"]
  24. # dingding = data["dingding"]
  25. mysql = {
  26. "host": os.getenv("host"),
  27. "port": int(os.getenv("port")),
  28. "user": os.getenv("user"),
  29. "passwd": os.getenv("passwd"),
  30. "database": os.getenv("database")
  31. }
  32. dingding = os.getenv("dingding")
  33. print("%s 等待开始检查..." % datetime_now())
  34. while True:
  35. time_now = datetime.datetime.fromtimestamp(int(time.time()),
  36. pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
  37. # time_now = time.strftime("%H:%M:%S", time.localtime()) # 刷新
  38. am_set_time = "11:00:00"
  39. pm_set_time = "16:00:00"
  40. if time_now == am_set_time or time_now == pm_set_time: # 此处设置每天定时的时间
  41. #钉钉推送消息
  42. messages = []
  43. # 连接mysql
  44. conn = pymysql.connect(**mysql)
  45. mysql_cur = conn.cursor()
  46. for classification in classifications:
  47. name = classifications[classification]
  48. print("%s,四川大厦,开始检查%s数据"%(datetime_now(),name))
  49. # 获取值
  50. mysql_cur.execute(SELECT_SQL%(classification,building_id))
  51. datas = mysql_cur.fetchall()
  52. if datas:
  53. null_sum = [i for i in datas if i[0] == None]
  54. if len(null_sum) != 0:
  55. print("%s,四川大厦, %s: 总共%s个, 有%s个未绑定空间"%(datetime_now(),name,len(datas),len(null_sum)))
  56. message = (" - **类型:** %s \n" + "- **总数量:** %s \n"+ " - **未绑定空间数量:** <font color=#FF0000>%s</font> \n") % (name,len(datas),len(null_sum))
  57. messages.append(message)
  58. messages.append("___")
  59. else:
  60. print("%s,四川大厦, 没有异常数据" % (datetime_now()))
  61. else:
  62. print("%s,四川大厦, 没有查询到%s数据"%(datetime_now(),name))
  63. # 关闭数据库
  64. mysql_cur.close()
  65. conn.close()
  66. if messages:
  67. print("%s 发送钉钉报警" % datetime_now())
  68. messages.append("<font color=#0080FF>详情查看MySQL数据库sagacloud_setup里的space_meet_set表</font>")
  69. messages_info = "\n".join(messages)
  70. # 如果有报警,发送钉钉
  71. title = "## **【四川大厦】会议室未绑定空间报警** "
  72. messages_info = title + "\n" + "___" + "\n" + messages_info
  73. send_message_markdown(title, messages_info, dingding)
  74. time.sleep(2)