feature_spark_monitor.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import configparser
  2. from datetime import datetime, timedelta
  3. from typing import Dict, List
  4. from client import YarnClient
  5. from util import date_util, feishu_inform_util
  6. yarn_client = YarnClient.YarnClient("192.168.203.16")
  7. table_list = [
  8. "alg_mid_feature_sharecf",
  9. "alg_vid_feature_all_share",
  10. "alg_vid_feature_all_return",
  11. "alg_vid_feature_share2return",
  12. "alg_vid_feature_basic_info",
  13. "alg_recsys_feature_cf_i2i_new",
  14. "alg_vid_feature_all_exp_v2",
  15. "alg_vid_feature_exp2share_v2",
  16. "alg_vid_feature_feed_noflow_exp_v2",
  17. "alg_vid_feature_feed_noflow_root_share_v2",
  18. "alg_vid_feature_feed_noflow_root_return_v2",
  19. "alg_vid_feature_feed_flow_exp_v2",
  20. "alg_vid_feature_feed_flow_root_share_v2",
  21. "alg_vid_feature_feed_flow_root_return_v2",
  22. "alg_vid_feature_feed_province_exp_v2",
  23. "alg_vid_feature_feed_province_root_share_v2",
  24. "alg_vid_feature_feed_province_root_return_v2",
  25. "alg_recsys_feature_cf_i2i_new_v2",
  26. "alg_cid_feature_basic_info",
  27. "alg_cid_feature_adver_action",
  28. "alg_cid_feature_cid_action",
  29. "alg_cid_feature_region_action",
  30. "alg_cid_feature_app_action",
  31. "alg_cid_feature_week_action",
  32. "alg_cid_feature_hour_action",
  33. "alg_cid_feature_brand_action",
  34. "alg_cid_feature_weChatVersion_action",
  35. "alg_cid_feature_vid_cf",
  36. "alg_cid_feature_vid_cf_rank",
  37. "alg_mid_feature_ad_action",
  38. "alg_mid_feature_play",
  39. "alg_mid_feature_share_and_return",
  40. "alg_mid_feature_play_tags",
  41. "alg_mid_feature_return_tags",
  42. "alg_mid_feature_share_tags",
  43. "alg_mid_feature_returncf",
  44. "alg_mid_feature_feed_exp_return_tags_v2",
  45. "alg_mid_feature_feed_exp_share_tags_v2"
  46. ]
  47. filter_date = datetime(2024, 1, 1)
  48. def handle_table(table_name: str, spark_task_list: List[Dict]) -> (bool, str):
  49. filtered_data = [
  50. item for item in spark_task_list
  51. if table_name == item['name']
  52. ]
  53. if filtered_data:
  54. latest_started_time = max(
  55. [date_util.str_cover_date(item['finishedTime']) for item in filtered_data]
  56. )
  57. print(f"表: {table_name}, 最后一次同步完成时间为: {latest_started_time}")
  58. time_difference = datetime.now() - latest_started_time
  59. if time_difference > timedelta(minutes=140):
  60. return True, latest_started_time
  61. else:
  62. return False, filtered_data
  63. else:
  64. # 如果没有找到,表示近七个小时都没有同步过
  65. return True, "七小时之前"
  66. def send_error_info(table_name: str, latest_started_time: str, webhook: str):
  67. mgs_text = f"\n- 大数据表名: {table_name}" \
  68. f"\n- 最后一次同步时间: {latest_started_time}" \
  69. f"\n- 超过两个小时没有同步,请关注"
  70. card_json = {
  71. "config": {},
  72. "i18n_elements": {
  73. "zh_cn": [
  74. {
  75. "tag": "markdown",
  76. "content": "",
  77. "text_align": "left",
  78. "text_size": "normal"
  79. },
  80. {
  81. "tag": "markdown",
  82. "content": mgs_text,
  83. "text_align": "left",
  84. "text_size": "normal"
  85. }
  86. ]
  87. },
  88. "i18n_header": {
  89. "zh_cn": {
  90. "title": {
  91. "tag": "plain_text",
  92. "content": "特征同步延迟告警"
  93. },
  94. "subtitle": {
  95. "tag": "plain_text",
  96. "content": ""
  97. },
  98. "template": "red"
  99. }
  100. }
  101. }
  102. feishu_inform_util.send_card_msg_to_feishu(webhook, card_json)
  103. def _main():
  104. # 读取配置文件
  105. config = configparser.ConfigParser()
  106. config.read("/home/monitor/model_monitor/config/config.ini")
  107. webhook_url = config.get("feishu", "model.webhook")
  108. # 获取最近七小时的Spark任务
  109. hours_7_early = int((datetime.now() - timedelta(hours=7)).timestamp()) * 1000
  110. result = yarn_client.get_apps(finished_time_begin=hours_7_early)
  111. result = [
  112. {**item, 'name': item['name'].split(":")[1].strip()}
  113. for item in result
  114. if item['finalStatus'] == "SUCCEEDED"
  115. ]
  116. if len(result) == 0:
  117. print("未获取已完成的任务,跳过")
  118. return
  119. early_started_time = min(
  120. [date_util.str_cover_date(item['startedTime']) for item in result]
  121. )
  122. if (datetime.now() - early_started_time) < timedelta(hours=3):
  123. print("任务最开始时间小于六个小时,疑似清理过任务。跳过")
  124. return
  125. for table_name in table_list:
  126. # 判断最后一次同步是否为两个小时以内
  127. b, latest_started_time = handle_table(table_name, result)
  128. if b:
  129. print(f"表: {table_name}, 最后一次同步时间距当前时间超过140分钟")
  130. send_error_info(table_name, latest_started_time, webhook_url)
  131. if __name__ == '__main__':
  132. _main()