File size: 2,601 Bytes
fea1bd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import platform
import psutil
import socket
import os
from utils.format import fmt_bytes

def get_system_info() -> str:
    """
    Coleta e formata informações detalhadas do sistema, hardware e usuário.
    Handler para o comando /sysinfo.
    """
    try:
        # --- Basic System Info ---
        uname = platform.uname()
        info = ["System Info"]
        info.append(f"  - OS Platform: {uname.system} {uname.release} (Version: {uname.version})")
        info.append(f"  - Architecture: {uname.machine} ({platform.architecture()[0]})")
        info.append(f"  - Processor: {uname.processor}")
        info.append(f"  - Hostname: {socket.gethostname()}")
        try:
            user = os.getlogin()
            info.append(f"  - Running as User: {user}")
        except OSError:
            info.append("  - Running as User: Not available")

        # --- CPU Info ---
        info.append(f"\nCPU Info")
        cpu_freq = psutil.cpu_freq()
        info.append(f"  - Physical Cores: {psutil.cpu_count(logical=False)}")
        info.append(f"  - Logical Cores: {psutil.cpu_count(logical=True)}")
        if cpu_freq:
            info.append(f"  - Max Frequency: {cpu_freq.max:.2f} Mhz")
            info.append(f"  - Current Frequency: {cpu_freq.current:.2f} Mhz")
        info.append(f"  - Total CPU Usage: {psutil.cpu_percent(interval=1)}%")

        # --- Memory Info ---
        mem = psutil.virtual_memory()
        info.append(f"\nMemory (RAM)")
        info.append(f"  - Total: {fmt_bytes(mem.total)}")
        info.append(f"  - Available: {fmt_bytes(mem.available)}")
        info.append(f"  - Used: {fmt_bytes(mem.used)} ({mem.percent}%)")

        # --- Disk Info ---
        info.append(f"\nDisk Partitions")
        partitions = psutil.disk_partitions()
        for p in partitions:
            try:
                usage = psutil.disk_usage(p.mountpoint)
                info.append(f"  - Device: {p.device} ({p.fstype}) at {p.mountpoint}")
                info.append(f"    - Size: {fmt_bytes(usage.total)}")
                info.append(f"    - Used: {fmt_bytes(usage.used)} ({usage.percent}%)")
                info.append(f"    - Free: {fmt_bytes(usage.free)}")
            except (FileNotFoundError, PermissionError) as e:
                info.append(f"  - Could not inspect {p.mountpoint}: {e}")
        
        return "\n".join(info)

    except Exception as e:
        return f"Error collecting system info: {e}"

# Exemplo de como registrar (a lógica real está no __init__.py do tools)
# from tools.registry import register
# register("/sysinfo", get_system_info)