1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import os
- import re
- import time
- import psutil
- import subprocess
- from datetime import datetime, timedelta
- def monitor():
- pid_list = psutil.pids()
- main_pid_list = []
- for pid in pid_list:
- try:
- process = psutil.Process(pid)
- except:
- continue
- process_cmd = ''
- for c in process.cmdline():
- process_cmd += c + " "
- if process_cmd.strip() == "":
- continue
- if re.search('convert:app', process_cmd):
- # print(pid, process_cmd)
- main_pid_list.append(pid)
- main_pid_list.sort(key=lambda x: x)
- print('main_pid_list', main_pid_list)
- now = datetime.now()
- last_10_min = now - timedelta(minutes=10)
- now = now.strftime("%Y-%m-%d %H:%M:%S")
- last_10_min = last_10_min.strftime("%Y-%m-%d %H:%M:%S")
- now = now[:-4] + '0:00'
- last_10_min = last_10_min[:-4] + '0:00'
- command = "sed -n '/%s/,/%s/p' /convert.out" % (last_10_min, now)
- print('command', command)
- result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
- text = result.stdout
- text = str(text).split('\n')
- all_free_time = 0
- for pid in main_pid_list:
- print('pid', pid)
- time_len = len('2024-06-07 10:15:12')
- time_finish = None
- free_time = 0
- for line in text:
- line = str(line)
- try:
- if re.search(str(pid), line):
- if time_finish is not None and re.search('into convert', line):
- free_time += (datetime.strptime(line[:time_len], "%Y-%m-%d %H:%M:%S")-time_finish).seconds
- # print('time_finish', str(time_finish), 'time_start', line[:time_len])
- # print('add free time', free_time)
- if re.search('is_success', line):
- time_finish = datetime.strptime(line[:time_len], "%Y-%m-%d %H:%M:%S")
- # print('set time_finish', line[:time_len])
- except:
- continue
- all_free_time += free_time
- print(pid, 'free time in 10 min:', free_time)
- print(round(all_free_time / len(main_pid_list), 2))
- if __name__ == '__main__':
- monitor()
|