File size: 2,103 Bytes
a31f556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""

modules/dashboard.py — collects all live stats into one dict for GUI/HUD

"""
import psutil
import platform
import socket

from modules.network_watch import get_ping

GPU_OK = False
try:
    from pynvml import nvmlInit, nvmlDeviceGetHandleByIndex
    from pynvml import nvmlDeviceGetTemperature, nvmlDeviceGetFanSpeed
    from pynvml import nvmlDeviceGetUtilizationRates, nvmlDeviceGetMemoryInfo, NVML_TEMPERATURE_GPU
    nvmlInit()
    GPU_OK = True
except Exception:
    pass


def get_snapshot() -> dict:
    cpu   = psutil.cpu_percent(interval=0.3)
    ram   = psutil.virtual_memory()
    disk  = psutil.disk_usage("C:\\") if platform.system() == "Windows" else psutil.disk_usage("/")

    gpu_temp = gpu_util = gpu_fan = 0
    gpu_vram = "N/A"

    if GPU_OK:
        try:
            h = nvmlDeviceGetHandleByIndex(0)
            gpu_temp = nvmlDeviceGetTemperature(h, NVML_TEMPERATURE_GPU)
            gpu_util = nvmlDeviceGetUtilizationRates(h).gpu
            gpu_fan = nvmlDeviceGetFanSpeed(h)
            mi = nvmlDeviceGetMemoryInfo(h)
            gpu_vram = f"{mi.used//1048576}/{mi.total//1048576} MB"
        except Exception:
            pass

    batt = None
    try:
        b = psutil.sensors_battery()
        if b:
            batt = {"percent": round(b.percent), "plugged": b.power_plugged}
    except Exception:
        pass

    net = psutil.net_io_counters()

    return {
        "hostname":  socket.gethostname(),
        "os":        platform.system() + " " + platform.release(),
        "cpu_pct":   cpu,
        "ram_pct":   ram.percent,
        "ram_used":  ram.used  // 1073741824,
        "ram_total": ram.total // 1073741824,
        "disk_free": disk.free  // 1073741824,
        "disk_total":disk.total // 1073741824,
        "gpu_temp":  gpu_temp,
        "gpu_util":  gpu_util,
        "gpu_fan":   gpu_fan,
        "gpu_vram":  gpu_vram,
        "battery":   batt,
        "net_sent":  net.bytes_sent // 1024,
        "net_recv":  net.bytes_recv // 1024,
        "ping":      get_ping(),
    }