180 lines
6.4 KiB
Python
180 lines
6.4 KiB
Python
#sysinfo.py
|
|
#Version : 0.1.0
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import cpuinfo
|
|
import json
|
|
import psutil
|
|
import platform
|
|
import distro
|
|
import socket
|
|
import logging
|
|
import argparse
|
|
from datetime import datetime
|
|
|
|
try:
|
|
import GPUtil
|
|
gpu_available = True
|
|
except ImportError:
|
|
gpu_available = False
|
|
|
|
# Farben für Debug-Logging
|
|
class LogColors:
|
|
INFO = "\033[94m"
|
|
DEBUG = "\033[92m"
|
|
WARNING = "\033[93m"
|
|
ERROR = "\033[91m"
|
|
RESET = "\033[0m"
|
|
|
|
# Logging einrichten
|
|
LOG_FILE = os.path.expanduser("~/hardware_info.log")
|
|
logging.basicConfig(
|
|
filename=LOG_FILE,
|
|
level=logging.INFO,
|
|
format="%(asctime)s - %(levelname)s - %(message)s"
|
|
)
|
|
|
|
def log_message(level, message):
|
|
"""Loggt Nachrichten mit Farben für die CLI, falls Debug aktiv ist."""
|
|
if level == "info":
|
|
logging.info(message)
|
|
if args.debug:
|
|
print(f"{LogColors.INFO}[INFO]{LogColors.RESET} {message}")
|
|
elif level == "debug":
|
|
logging.debug(message)
|
|
if args.debug:
|
|
print(f"{LogColors.DEBUG}[DEBUG]{LogColors.RESET} {message}")
|
|
elif level == "warning":
|
|
logging.warning(message)
|
|
if args.debug:
|
|
print(f"{LogColors.WARNING}[WARNING]{LogColors.RESET} {message}")
|
|
elif level == "error":
|
|
logging.error(message)
|
|
if args.debug:
|
|
print(f"{LogColors.ERROR}[ERROR]{LogColors.RESET} {message}")
|
|
|
|
def get_hardware_info():
|
|
"""Sammelt die relevanten Hardware- und Systeminformationen."""
|
|
hardware_info = {}
|
|
|
|
# Betriebssystem
|
|
log_message("info", "Sammle Betriebssystem-Daten...")
|
|
try:
|
|
os_info = {
|
|
"name": platform.system(),
|
|
"distribution": distro.name(pretty=True)
|
|
}
|
|
log_message("info", f"OS: {os_info['name']} - {os_info['distribution']}")
|
|
except Exception as e:
|
|
os_info = {}
|
|
log_message("warning", f"Konnte Betriebssystem-Daten nicht ermitteln: {str(e)}")
|
|
hardware_info["os"] = os_info
|
|
|
|
# CPU
|
|
log_message("info", "Sammle CPU-Daten...")
|
|
try:
|
|
cpu_info = {
|
|
"model": cpuinfo.get_cpu_info().get('brand_raw', 'Unbekannt'),
|
|
"cores": psutil.cpu_count(logical=False),
|
|
"threads": psutil.cpu_count(logical=True),
|
|
"usage": psutil.cpu_percent(interval=1)
|
|
}
|
|
log_message("info", f"CPU: {cpu_info['model']} - {cpu_info['cores']} Cores, {cpu_info['threads']} Threads - {cpu_info['usage']}% Auslastung")
|
|
except Exception as e:
|
|
cpu_info = {}
|
|
log_message("warning", f"Konnte CPU-Daten nicht ermitteln: {str(e)}")
|
|
hardware_info["cpu"] = cpu_info
|
|
|
|
# RAM
|
|
log_message("info", "Sammle RAM-Daten...")
|
|
try:
|
|
ram_info = {
|
|
"total": round(psutil.virtual_memory().total / (1024**3), 2),
|
|
"used": round(psutil.virtual_memory().used / (1024**3), 2),
|
|
"available": round(psutil.virtual_memory().available / (1024**3), 2)
|
|
}
|
|
log_message("info", f"RAM: {ram_info['total']}GB gesamt, {ram_info['used']}GB genutzt, {ram_info['available']}GB verfügbar")
|
|
except Exception as e:
|
|
ram_info = {}
|
|
log_message("warning", f"Konnte RAM-Daten nicht ermitteln: {str(e)}")
|
|
hardware_info["ram"] = ram_info
|
|
|
|
# Festplatte
|
|
log_message("info", "Sammle Festplattendaten...")
|
|
try:
|
|
disk_info = {
|
|
"total": round(psutil.disk_usage('/').total / (1024**3), 2),
|
|
"used": round(psutil.disk_usage('/').used / (1024**3), 2),
|
|
"free": round(psutil.disk_usage('/').free / (1024**3), 2)
|
|
}
|
|
log_message("info", f"Disk: {disk_info['total']}GB gesamt, {disk_info['used']}GB genutzt, {disk_info['free']}GB frei")
|
|
except Exception as e:
|
|
disk_info = {}
|
|
log_message("warning", f"Konnte Festplatten-Daten nicht ermitteln: {str(e)}")
|
|
hardware_info["disk"] = disk_info
|
|
|
|
log_message("info", "Sammle Netzwerkinformationen...")
|
|
try:
|
|
network_info = {
|
|
"hostname": socket.gethostname(),
|
|
"ip_address": socket.gethostbyname(socket.gethostname())
|
|
}
|
|
log_message("info", f"Netzwerk: Hostname {network_info['hostname']}, IP {network_info['ip_address']}")
|
|
except Exception as e:
|
|
network_info = {}
|
|
log_message("warning", f"Konnte Netzwerkinformationen nicht ermitteln: {str(e)}")
|
|
hardware_info["network"] = network_info
|
|
|
|
log_message("info", "Sammle Benutzerinformationen...")
|
|
try:
|
|
user_info = {
|
|
"logged_in": [user.name for user in psutil.users()],
|
|
"active_user": os.getlogin()
|
|
}
|
|
log_message("info", f"Benutzer: {', '.join(user_info['logged_in'])}, Aktiver User: {user_info['active_user']}")
|
|
except Exception as e:
|
|
user_info = {}
|
|
log_message("warning", f"Konnte Benutzerinformationen nicht ermitteln: {str(e)}")
|
|
hardware_info["users"] = user_info
|
|
|
|
log_message("info", "Sammle dedizierte GPU-Daten...")
|
|
gpu_info = []
|
|
try:
|
|
if gpu_available:
|
|
gpus = GPUtil.getGPUs()
|
|
for gpu in gpus:
|
|
gpu_info.append({
|
|
"name": gpu.name,
|
|
"memory_total": f"{gpu.memoryTotal}MB",
|
|
"memory_used": f"{gpu.memoryUsed}MB",
|
|
"memory_free": f"{gpu.memoryFree}MB",
|
|
"load": f"{gpu.load * 100:.1f}%"
|
|
})
|
|
log_message("info", f"Gefundene GPUs: {len(gpus)}")
|
|
else:
|
|
log_message("warning", "GPUtil nicht installiert oder keine dedizierte GPU vorhanden.")
|
|
except Exception as e:
|
|
log_message("warning", f"Konnte GPU-Daten nicht ermitteln: {str(e)}")
|
|
hardware_info["gpu"] = gpu_info
|
|
|
|
log_message("info", "Hardware-Informationen erfolgreich erfasst.")
|
|
return hardware_info
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Ermittelt Hardware- und Systeminformationen.")
|
|
parser.add_argument("--debug", action="store_true", help="Aktiviert Debug-Logging auf der CLI.")
|
|
parser.add_argument("--json", type=str, help="Speichert die Ausgabe in einer JSON-Datei.")
|
|
args = parser.parse_args()
|
|
|
|
# Hardware-Informationen abrufen
|
|
hardware_info = get_hardware_info()
|
|
|
|
if args.json:
|
|
try:
|
|
with open(args.json, "w") as json_file:
|
|
json.dump(hardware_info, json_file, indent=4)
|
|
log_message("info", f"Hardware-Infos in JSON gespeichert: {args.json}")
|
|
except Exception as e:
|
|
log_message("error", f"Fehler beim Speichern der JSON-Datei: {str(e)}")
|