Tiktok-Talent-Info/monitor.py

33 lines
1.1 KiB
Python

import psutil
import GPUtil
import time
import logging
# Specify the full path for the log file
log_file_path = "resource_usage.log"
# Configure logging
logging.basicConfig(filename=log_file_path, level=logging.INFO)
logging.info("Logging started") # Add this line to confirm logging is working
try:
while True:
# Monitor CPU
cpu_usage = psutil.cpu_percent(interval=1)
memory_usage = psutil.virtual_memory().percent
cpu_message = f"CPU Usage: {cpu_usage}% | Memory Usage: {memory_usage}%"
print(cpu_message) # Print to console
logging.info(cpu_message) # Log to file
# Monitor GPU
GPUs = GPUtil.getGPUs()
for gpu in GPUs:
gpu_message = f"GPU {gpu.id} | Usage: {gpu.load * 100}% | Memory: {gpu.memoryUsed}MB / {gpu.memoryTotal}MB"
print(gpu_message) # Print to console
logging.info(gpu_message) # Log to file
time.sleep(1)
except Exception as e:
error_message = f"An error occurred: {e}"
print(error_message) # Print to console
logging.error(error_message) # Log to file