monitor_process_config.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import datetime
  2. import logging
  3. import os
  4. import re
  5. import sys
  6. import time
  7. import psutil
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  9. from format_convert.utils import get_ip_port, get_intranet_ip, get_args_from_config, get_all_ip, get_using_ip
  10. ip_port_dict = get_ip_port()
  11. ip = get_using_ip()
  12. print("local ip:", ip)
  13. # 获取各个参数
  14. convert_port_list = get_args_from_config(ip_port_dict, ip, "convert", "MASTER")
  15. if convert_port_list:
  16. convert_port_list = convert_port_list[0]
  17. ocr_port_list = get_args_from_config(ip_port_dict, ip, "ocr")
  18. otr_port_list = get_args_from_config(ip_port_dict, ip, "otr")
  19. idc_port_list = get_args_from_config(ip_port_dict, ip, "idc")
  20. isr_port_list = get_args_from_config(ip_port_dict, ip, "isr")
  21. atc_port_list = get_args_from_config(ip_port_dict, ip, "atc")
  22. soffice_port_list = get_args_from_config(ip_port_dict, ip, "office", "MASTER")
  23. if soffice_port_list:
  24. soffice_port_list = soffice_port_list[0]
  25. python_path_list = get_args_from_config(ip_port_dict, ip, "python_path")
  26. project_path_list = get_args_from_config(ip_port_dict, ip, "project_path")
  27. gunicorn_path_list = get_args_from_config(ip_port_dict, ip, "gunicorn_path")
  28. std_out = " >>/convert.out 2>&1 &"
  29. std_out_gpu = " >>/gpu.out 2>&1 &"
  30. std_out_schedule = " >>/schedule.out 2>&1 &"
  31. print("convert_port_list", convert_port_list)
  32. print("ocr_port_list", ocr_port_list)
  33. print("otr_port_list", otr_port_list)
  34. print("idc_port_list", idc_port_list)
  35. print("isr_port_list", isr_port_list)
  36. print("atc_port_list", atc_port_list)
  37. print("soffice_port_list", soffice_port_list)
  38. # 根据port生成gunicorn语句
  39. ocr_comm_list = []
  40. otr_comm_list = []
  41. isr_comm_list = []
  42. idc_comm_list = []
  43. atc_comm_list = []
  44. for i in range(len(ocr_port_list)):
  45. ocr_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(ocr_port_list[i]))
  46. + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  47. + project_path_list[i] + "/ocr ocr_interface:app" + std_out_gpu)
  48. for i in range(len(otr_port_list)):
  49. otr_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(otr_port_list[i]))
  50. + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  51. + project_path_list[i] + "/otr otr_interface:app" + std_out_gpu)
  52. for i in range(len(idc_port_list)):
  53. idc_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(idc_port_list[i]))
  54. + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  55. + project_path_list[i] + "/idc idc_interface:app" + std_out_gpu)
  56. for i in range(len(isr_port_list)):
  57. isr_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(isr_port_list[i]))
  58. + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  59. + project_path_list[i] + "/isr isr_interface:app" + std_out_gpu)
  60. for i in range(len(atc_port_list)):
  61. atc_comm_list.append("nohup " + gunicorn_path_list[i] + " -w " + str(len(atc_port_list[i]))
  62. + " -t 300 --keep-alive 600 -b 0.0.0.0:# --chdir "
  63. + project_path_list[i] + "/atc atc_interface:app" + std_out_gpu)
  64. convert_comm = "nohup " + gunicorn_path_list[0] + " -w " + str(len(convert_port_list)) + " -t 300 -b 0.0.0.0:# --chdir " \
  65. + project_path_list[0] + "/format_convert convert:app" + std_out
  66. soffice_comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
  67. def get_port():
  68. net_conn = psutil.net_connections()
  69. current_port_list = []
  70. for conn in net_conn:
  71. current_port_list.append(str(conn.laddr.port))
  72. current_port_list = list(set(current_port_list))
  73. current_port_list.sort(key=lambda x: x)
  74. return current_port_list
  75. def restart(process_type, port, index=0):
  76. if process_type == "convert":
  77. _comm = re.sub("#", port, convert_comm)
  78. elif process_type == "ocr":
  79. _comm = re.sub("#", port, ocr_comm_list[index])
  80. elif process_type == "otr":
  81. _comm = re.sub("#", port, otr_comm_list[index])
  82. elif process_type == "soffice":
  83. _comm = re.sub("#", port, soffice_comm)
  84. elif process_type == "idc":
  85. _comm = re.sub("#", port, idc_comm_list[index])
  86. elif process_type == "isr":
  87. _comm = re.sub("#", port, isr_comm_list[index])
  88. elif process_type == "atc":
  89. _comm = re.sub("#", port, atc_comm_list[index])
  90. else:
  91. _comm = "netstat -nltp"
  92. print("no process_type", process_type)
  93. # os.system("echo $(date +%F%n%T)")
  94. print(datetime.datetime.now(), "restart comm", _comm)
  95. os.system(_comm)
  96. def kill_soffice(limit_sec=30):
  97. try:
  98. pid_list = psutil.pids()
  99. for pid in pid_list:
  100. process = psutil.Process(pid)
  101. process_cmd = ''
  102. for c in process.cmdline():
  103. process_cmd += c + " "
  104. if process_cmd.strip() == "":
  105. continue
  106. if process.status() == "zombie":
  107. print("zombie cmd", process_cmd)
  108. if re.search("soffice", process.exe()):
  109. start_time = process.create_time()
  110. now_time = time.time()
  111. run_time = now_time-start_time
  112. if run_time >= limit_sec:
  113. comm = "kill -9 " + str(pid)
  114. print(datetime.datetime.now(), "kill process ", str(pid), str(process.exe()), str(run_time), ">", limit_sec)
  115. os.system(comm)
  116. except:
  117. pass
  118. def kill_nested_timeout_process():
  119. try:
  120. pid_list = psutil.pids()
  121. suspect_pid_list = []
  122. for pid in pid_list:
  123. process = psutil.Process(pid)
  124. process_cmd = ''
  125. for c in process.cmdline():
  126. process_cmd += c + " "
  127. if process_cmd.strip() == "":
  128. continue
  129. if re.search("convert:app", process_cmd):
  130. ppid = process.ppid()
  131. start_time = process.create_time()
  132. now_time = time.time()
  133. run_time = now_time-start_time
  134. if str(ppid) == "1":
  135. suspect_pid_list.append([str(pid), float(run_time)])
  136. # 时间最久的父进程为1的不能杀,是接口主进程
  137. if len(suspect_pid_list) <= 1:
  138. return
  139. else:
  140. suspect_pid_list.sort(key=lambda x: x[1], reverse=True)
  141. for pid, run_time in suspect_pid_list[1:]:
  142. # print("pid", pid, run_time)
  143. comm = "kill -9 " + str(pid)
  144. print(datetime.datetime.now(), "kill process ", str(pid), "father is 1", process_cmd)
  145. os.system(comm)
  146. except:
  147. pass
  148. def monitor():
  149. current_port_list = get_port()
  150. if convert_port_list:
  151. for p in convert_port_list[:1]:
  152. if p not in current_port_list:
  153. restart("convert", p)
  154. if ocr_port_list:
  155. for j in range(len(ocr_port_list)):
  156. for p in ocr_port_list[j][:1]:
  157. if p not in current_port_list:
  158. restart("ocr", p, index=j)
  159. if otr_port_list:
  160. for j in range(len(otr_port_list)):
  161. for p in otr_port_list[j][:1]:
  162. if p not in current_port_list:
  163. restart("otr", p, index=j)
  164. if idc_port_list:
  165. for j in range(len(idc_port_list)):
  166. for p in idc_port_list[j][:1]:
  167. if p not in current_port_list:
  168. restart("idc", p, index=j)
  169. if isr_port_list:
  170. for j in range(len(isr_port_list)):
  171. for p in isr_port_list[j][:1]:
  172. if p not in current_port_list:
  173. restart("isr", p, index=j)
  174. if atc_port_list:
  175. for j in range(len(atc_port_list)):
  176. for p in atc_port_list[j][:1]:
  177. if p not in current_port_list:
  178. restart("atc", p, index=j)
  179. if soffice_port_list:
  180. for p in soffice_port_list:
  181. if p not in current_port_list:
  182. restart("soffice", p)
  183. kill_soffice()
  184. kill_nested_timeout_process()
  185. # if schedule_port_list:
  186. # for p in schedule_port_list:
  187. # if p not in current_port_list:
  188. # restart("schedule", p)
  189. if __name__ == "__main__":
  190. for i in range(6):
  191. # os.system("echo $(date +%F%n%T)")
  192. monitor()
  193. time.sleep(10)