Files
tg-admin-bot/system_checks.py

108 lines
2.3 KiB
Python

import subprocess
def _cmd(cmd: str) -> str:
"""
Safe shell command runner
"""
try:
return subprocess.getoutput(cmd)
except Exception as e:
return f"ERROR: {e}"
# ---------- SECURITY ----------
def security() -> str:
out = _cmd("sshd -T | grep -i '^permitrootlogin'")
if not out or "ERROR:" in out:
return "🔐 Security\n\n⚠️ permitrootlogin not found"
if "no" in out.lower():
return "🔐 Security\n\n🟢 Root login disabled"
return "🔐 Security\n\n🔴 Root login ENABLED"
# ---------- DISKS ----------
def list_disks() -> list[str]:
out = _cmd("lsblk -dn -o NAME,TYPE")
disks = []
for line in out.splitlines():
parts = line.split()
if len(parts) != 2:
continue
name, typ = parts
if typ == "disk":
disks.append(f"/dev/{name}")
return disks
def smart_health(dev: str) -> str:
out = _cmd(f"smartctl -H {dev}")
if not out or "ERROR:" in out:
return "⚠️ ERROR"
if "PASSED" in out:
return "🟢 PASSED"
if "FAILED" in out:
return "🔴 FAILED"
return "⚠️ UNKNOWN"
def disk_temperature(dev: str) -> str:
out = _cmd(f"smartctl -A {dev}")
if not out or "ERROR:" in out:
return "n/a"
# NVMe
for line in out.splitlines():
if "Temperature:" in line and "Celsius" in line:
try:
temp = int("".join(filter(str.isdigit, line)))
return f"{temp}°C"
except Exception:
pass
# SATA attributes
for line in out.splitlines():
if line.strip().startswith(("194", "190")):
parts = line.split()
for p in parts[::-1]:
if p.isdigit():
return f"{p}°C"
return "n/a"
def disks() -> str:
disks = list_disks()
if not disks:
return "💽 Disks\n\n❌ No disks found"
lines = ["💽 Disks (SMART)\n"]
for d in disks:
health = smart_health(d)
temp = disk_temperature(d)
icon = "🟢"
if temp != "n/a":
t = int(temp.replace("°C", ""))
if t > 50:
icon = "🔴"
elif t > 40:
icon = "🟡"
lines.append(f"{icon} {d}{health}, 🌡 {temp}")
return "\n".join(lines)