import socket
import shutil
from datetime import datetime


def cpu_temp():
    try:
        with open("/sys/class/thermal/thermal_zone0/temp") as f:
            return round(int(f.read()) / 1000, 1)
    except:
        return None


def memory():
    try:
        data = {}

        with open("/proc/meminfo") as f:
            for line in f:
                k, v = line.split(":", 1)
                data[k] = int(v.strip().split()[0])

        total = data["MemTotal"]
        free = data["MemAvailable"]
        used = total - free

        return round(used / total * 100, 1)
    except:
        return None


def ip_address():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
        s.close()
        return ip
    except:
        return "—"


disk = shutil.disk_usage("/")

print("=== RPi STATUS ===")
print("Time:", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print("IP:", ip_address())
print("CPU temperature:", str(cpu_temp()) + " °C")
print("RAM used:", str(memory()) + " %")
print("Disk free:", round(disk.free / 1024 / 1024 / 1024, 1), "GB")
print("====================")