From 3e9d971f1bcfe3dae52951ac29c2c84e8444a1c1 Mon Sep 17 00:00:00 2001 From: Sakib Date: Wed, 8 Nov 2023 14:29:41 +0600 Subject: [PATCH 1/2] collect system info --- .gitignore | 2 ++ node_cli.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/.gitignore b/.gitignore index 1cf824fc6..705c64212 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,5 @@ web_modules/ .rts2_cache_cjs/ .rts2_cache_es/ .rts2_cache_umd/ + +system_info.json \ No newline at end of file diff --git a/node_cli.py b/node_cli.py index 5459cf1d0..cef07bf8e 100755 --- a/node_cli.py +++ b/node_cli.py @@ -14,6 +14,10 @@ import shutil import time +import psutil +import threading + + # Disable WebdriverManager SSL verification. os.environ['WDM_SSL_VERIFY'] = '0' @@ -119,6 +123,39 @@ from Framework.deploy_handler import long_poll_handler from Framework.deploy_handler import adapter +def get_system_info(): + while True: + # Current timestamp + current_time = dt.now().strftime('%Y-%m-%d %H:%M:%S') + + # Current CPU usage + cpu_usage = psutil.cpu_percent(interval=1) + + # Current RAM usage + ram_usage = psutil.virtual_memory().percent + + # Top 10 processes with highest CPU usage + top_cpu_processes = [p.info for p in psutil.process_iter(attrs=['pid', 'name', 'cpu_percent'])] + top_cpu_processes = sorted(top_cpu_processes, key=lambda x: x['cpu_percent'], reverse=True)[:10] + + # Top 10 processes with highest RAM usage + top_ram_processes = [p.info for p in psutil.process_iter(attrs=['pid', 'name', 'memory_percent'])] + top_ram_processes = sorted(top_ram_processes, key=lambda x: x['memory_percent'], reverse=True)[:10] + + data = { + "Timestamp": current_time, + "CPU_Usage": cpu_usage, + "RAM_Usage": ram_usage, + "Top_CPU_Processes": top_cpu_processes, + "Top_RAM_Processes": top_ram_processes + } + + # Append data to the JSON file + with open('system_info.json', 'a') as file: + file.write(json.dumps(data, indent=4) + "\n") + + time.sleep(5) # Adjust the interval (in seconds) as needed + def signal_handler(sig, frame): CommonUtil.run_cancelled = True print("Disconnecting from server...") @@ -1201,10 +1238,17 @@ def Bypass(): print("Exiting...") sys.exit(1) + thread = threading.Thread(target=get_system_info) + thread.daemon = True + thread.start() + if local_run: Local_run(log_dir=log_dir) else: # Bypass() Login(cli=True, run_once=RUN_ONCE, log_dir=log_dir) + + + CommonUtil.run_cancelled = True CommonUtil.ShutdownExecutor() From 405a112f5f044917d23d934cf185146831f2b625 Mon Sep 17 00:00:00 2001 From: Sakib Date: Wed, 8 Nov 2023 14:46:31 +0600 Subject: [PATCH 2/2] keep data upto 3days --- node_cli.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/node_cli.py b/node_cli.py index cef07bf8e..434a5670a 100755 --- a/node_cli.py +++ b/node_cli.py @@ -151,8 +151,22 @@ def get_system_info(): } # Append data to the JSON file - with open('system_info.json', 'a') as file: - file.write(json.dumps(data, indent=4) + "\n") + with open('system_info.json', 'r+') as file: + try: + existing_data = json.load(file) + except json.decoder.JSONDecodeError: + existing_data = [] + + # Filter out data older than 3 days + existing_data = [entry for entry in existing_data if dt.strptime(entry['Timestamp'], '%Y-%m-%d %H:%M:%S') >= dt.now() - datetime.timedelta(days=3)] + + # Append new data + existing_data.append(data) + + # Reset file pointer and write back to the file + file.seek(0) + file.truncate() + json.dump(existing_data, file, indent=4) time.sleep(5) # Adjust the interval (in seconds) as needed