send_message.py 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. from Schedule.send_alarm import ram_dict, post_text, Get_Alarm_Info, Get_Renew_Info
  2. def send_message():
  3. Info = Get_Alarm_Info()
  4. alarm_list = []
  5. for point in ram_dict():
  6. alarm_list.append(f"""## <font color=\"green\">********阿里云{point}子RAM日常监控: ********</font>\n""")
  7. if not Info[point]:
  8. alarm_list.append(f'**当前账户暂无告警**')
  9. else:
  10. for alarm in Info[point]:
  11. alarm_list.append(f"""***告警节点:*** <font color=\"red\">{alarm['InstanceName']}</font\
  12. >\n**告警推送时间:** <font color=\"comment\">{alarm['AlertTime']}</font\
  13. >\n**告警持续时间(小时):** <font color=\"comment\">{alarm['LastTime']}</font\
  14. >\n**触发规则:** <font color=\"red\">{alarm['RuleName']}</font\
  15. >\n**告警值:** <font color=\"comment\">{alarm['Value']}</font>\n""")
  16. if alarm != Info[point][-1]:
  17. alarm_list.append('-' * 40 + '\n')
  18. send_str = ''.join(alarm_list)
  19. if len(send_str.encode('utf-8')) > 4000:
  20. new_list = list(send_str)[:3000]
  21. new_str = ''.join(new_list)
  22. new_str = new_str + '\n**markdown长度限制,多余部分省略。。。**'
  23. else:
  24. new_str = send_str
  25. try:
  26. post_text(new_str)
  27. alarm_list = []
  28. logger.info('日常监控定时任务执行成功。')
  29. except Exception as e:
  30. logger.error('日常监控定时任务异常:%s' % str(e))