monitor_main_interface.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os
  2. import re
  3. import time
  4. import psutil
  5. import subprocess
  6. from datetime import datetime, timedelta
  7. def monitor():
  8. pid_list = psutil.pids()
  9. main_pid_list = []
  10. for pid in pid_list:
  11. try:
  12. process = psutil.Process(pid)
  13. except:
  14. continue
  15. process_cmd = ''
  16. for c in process.cmdline():
  17. process_cmd += c + " "
  18. if process_cmd.strip() == "":
  19. continue
  20. if re.search('convert:app', process_cmd):
  21. # print(pid, process_cmd)
  22. main_pid_list.append(pid)
  23. main_pid_list.sort(key=lambda x: x)
  24. print('main_pid_list', main_pid_list)
  25. now = datetime.now()
  26. last_10_min = now - timedelta(minutes=10)
  27. now = now.strftime("%Y-%m-%d %H:%M:%S")
  28. last_10_min = last_10_min.strftime("%Y-%m-%d %H:%M:%S")
  29. now = now[:-4] + '0:00'
  30. last_10_min = last_10_min[:-4] + '0:00'
  31. command = "sed -n '/%s/,/%s/p' /convert.out" % (last_10_min, now)
  32. print('command', command)
  33. result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
  34. text = result.stdout
  35. text = str(text).split('\n')
  36. all_free_time = 0
  37. for pid in main_pid_list:
  38. print('pid', pid)
  39. time_len = len('2024-06-07 10:15:12')
  40. time_finish = None
  41. free_time = 0
  42. for line in text:
  43. line = str(line)
  44. try:
  45. if re.search(str(pid), line):
  46. if time_finish is not None and re.search('into convert', line):
  47. free_time += (datetime.strptime(line[:time_len], "%Y-%m-%d %H:%M:%S")-time_finish).seconds
  48. # print('time_finish', str(time_finish), 'time_start', line[:time_len])
  49. # print('add free time', free_time)
  50. if re.search('is_success', line):
  51. time_finish = datetime.strptime(line[:time_len], "%Y-%m-%d %H:%M:%S")
  52. # print('set time_finish', line[:time_len])
  53. except:
  54. continue
  55. all_free_time += free_time
  56. print(pid, 'free time in 10 min:', free_time)
  57. print(round(all_free_time / len(main_pid_list), 2))
  58. if __name__ == '__main__':
  59. monitor()