feature_spark_monitor.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import argparse
  2. import configparser
  3. from datetime import datetime, timedelta
  4. from typing import Dict, List
  5. from client import YarnClient
  6. from util import date_util, feishu_inform_util
  7. yarn_client = YarnClient.YarnClient("192.168.203.16")
  8. table_list = [
  9. "alg_mid_feature_sharecf",
  10. "alg_vid_feature_all_share",
  11. "alg_vid_feature_all_return",
  12. "alg_vid_feature_share2return",
  13. "alg_vid_feature_basic_info",
  14. "alg_recsys_feature_cf_i2i_new",
  15. "alg_vid_feature_all_exp_v2",
  16. "alg_vid_feature_exp2share_v2",
  17. "alg_vid_feature_feed_noflow_exp_v2",
  18. "alg_vid_feature_feed_noflow_root_share_v2",
  19. "alg_vid_feature_feed_noflow_root_return_v2",
  20. "alg_vid_feature_feed_flow_exp_v2",
  21. "alg_vid_feature_feed_flow_root_share_v2",
  22. "alg_vid_feature_feed_flow_root_return_v2",
  23. "alg_vid_feature_feed_province_exp_v2",
  24. "alg_vid_feature_feed_province_root_share_v2",
  25. "alg_vid_feature_feed_province_root_return_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. "alg_vid_global_feature_20250212",
  47. "alg_vid_recommend_exp_feature_20250212",
  48. "alg_vid_recommend_flowpool_exp_feature_20250212",
  49. "alg_vid_apptype_recommend_exp_feature_20250212",
  50. "alg_vid_province_recommend_exp_feature_20250212",
  51. "alg_vid_brand_recommend_exp_feature_20250212",
  52. "alg_vid_hotsencetype_recommend_exp_feature_20250212",
  53. "scene_type_vid_cf_feature_20250212",
  54. "vid_click_cf_feature_20250212",
  55. "alg_recsys_feature_cf_i2i_v2",
  56. "alg_channel_recommend_exp_feature_20250212",
  57. "alg_merge_cate1_recommend_exp_feature_20250212",
  58. "alg_merge_cate2_recommend_exp_feature_20250212",
  59. "alg_video_unionid_recommend_exp_feature_20250212",
  60. "mid_merge_cate1_feature_20250212",
  61. "mid_merge_cate2_feature_20250212",
  62. "mid_global_feature_20250212",
  63. "mid_u2u_friend_index_feature_20250212",
  64. ""
  65. ]
  66. filter_date = datetime(2024, 1, 1)
  67. def handle_table(table_name: str, spark_task_list: List[Dict]) -> (bool, str):
  68. filtered_data = [
  69. item for item in spark_task_list
  70. if table_name == item['name']
  71. ]
  72. if not filtered_data:
  73. # 如果没有找到,表示近七个小时都没有同步过
  74. return True, "最近没有四小时同步过数据"
  75. # 判断最近一次完成时间是否大于两个小时
  76. filtered_data.sort(key=lambda item: date_util.str_cover_date(item['finishedTime']), reverse=True)
  77. last_finished_item = filtered_data[0]
  78. print(f"表: {table_name}, 最后一次完成时间为: {last_finished_item['finishedTime']}")
  79. time_difference = datetime.now() - date_util.str_cover_date(last_finished_item['finishedTime'])
  80. if time_difference > timedelta(minutes=120):
  81. return True, f"最近两个小时没有同步完成数据,最近一次完成时间为: {last_finished_item['finishedTime']}"
  82. # 判断持续时间是否超过一个小时
  83. elapse = (date_util.str_cover_date(last_finished_item['finishedTime']) -
  84. date_util.str_cover_date(last_finished_item['startedTime']))
  85. print(f"表: {table_name}, 最后一次任务持续时间为: {date_util.seconds_convert(elapse.seconds)}")
  86. if elapse > timedelta(minutes=50):
  87. return True, f"最近一次同步任务持续时间超过50分钟, 持续时间为: {date_util.seconds_convert(elapse.seconds)}"
  88. return False, ""
  89. def send_error_info(table_name: str, warn_reason: str, webhook: str):
  90. mgs_text = f"\n- 大数据表名: {table_name}" \
  91. f"\n- 告警原因: {warn_reason}" \
  92. f"\n- 请关注"
  93. card_json = {
  94. "config": {},
  95. "i18n_elements": {
  96. "zh_cn": [
  97. {
  98. "tag": "markdown",
  99. "content": "",
  100. "text_align": "left",
  101. "text_size": "normal"
  102. },
  103. {
  104. "tag": "markdown",
  105. "content": mgs_text,
  106. "text_align": "left",
  107. "text_size": "normal"
  108. }
  109. ]
  110. },
  111. "i18n_header": {
  112. "zh_cn": {
  113. "title": {
  114. "tag": "plain_text",
  115. "content": "特征同步延迟告警"
  116. },
  117. "subtitle": {
  118. "tag": "plain_text",
  119. "content": ""
  120. },
  121. "template": "red"
  122. }
  123. }
  124. }
  125. feishu_inform_util.send_card_msg_to_feishu(webhook, card_json)
  126. def print_config(config_path):
  127. print(f"配置文件路径: {config_path}")
  128. config = configparser.ConfigParser()
  129. config.read(config_path)
  130. for section in config.sections():
  131. print(f"[{section}]")
  132. for key, value in config.items(section):
  133. print(f"{key} = {value}")
  134. def _main():
  135. parser = argparse.ArgumentParser(description="feature_spark_task_monitor")
  136. parser.add_argument("-c", "--config", required=False, help="config file path",
  137. default="/home/monitor/model_script/config/config.ini")
  138. args = parser.parse_args()
  139. print_config(args.config)
  140. # 读取配置文件
  141. # config = configparser.ConfigParser()
  142. # config.read(args.config)
  143. # webhook_url = config.get("feishu", "model.webhook")
  144. webhook_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/540d4098-367a-4068-9a44-b8109652f07c'
  145. # 获取最近4小时的Spark任务
  146. hours_7_early = int((datetime.now() - timedelta(hours=4)).timestamp()) * 1000
  147. result = yarn_client.get_apps(finished_time_begin=hours_7_early)
  148. result = [
  149. {**item, 'name': item['name'].split(":")[1].strip()}
  150. for item in result
  151. if item['finalStatus'] == "SUCCEEDED"
  152. ]
  153. if len(result) == 0:
  154. print("未获取已完成的任务,跳过")
  155. return
  156. for table_name in table_list:
  157. b, warn_reason = handle_table(table_name, result)
  158. if b:
  159. print(f"表: {table_name}, 触发告警; 告警原因: {warn_reason}")
  160. send_error_info(table_name, warn_reason, webhook_url)
  161. if __name__ == '__main__':
  162. _main()