You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

40 lines
1.2 KiB

import subprocess
import psutil
import time
def get_gpu_stats():
command = [
"nvidia-smi",
"--query-gpu=index,name,memory.used,memory.free,power.draw,temperature.gpu",
"--format=csv,noheader,nounits"
]
result = subprocess.run(command, stdout=subprocess.PIPE, text=True)
stats = result.stdout.strip().split(", ")
return {
"index": int(stats[0]),
"name": str(stats[1]),
"memory_used": int(stats[2]), # in MB
"memory_free": int(stats[3]), # in MB
"power_draw": float(stats[4]), # in watts
"temperature": float(stats[5]) # in Celsius
}
def get_cpu_memory_stats():
cpu_usage = psutil.cpu_percent(interval=1)
memory_info = psutil.virtual_memory()
return {
"cpu_usage": cpu_usage,
"memory_used": memory_info.used // (1024 ** 2), # in MB
"memory_total": memory_info.total // (1024 ** 2) # in MB
}
def main():
import json
while True:
gpu_stats = get_gpu_stats()
cpu_stats = get_cpu_memory_stats()
print(json.dumps([gpu_stats, cpu_stats]))
time.sleep(1) # Update every second
if __name__ == "__main__":
main()