Matthias Nott
2026-02-26 5d0247159b125bf035285d56c2b9bb58d6bb3029
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import asyncio
import os
import re
from typing import Any
from fastapi import APIRouter, Depends, HTTPException
from app.auth import verify_token
from app.ops_runner import run_command, run_command_host, run_ops, run_ops_host
router = APIRouter()
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _parse_disk_output(raw: str) -> list[dict[str, str]]:
    """Parse df-style output into a list of filesystem dicts."""
    filesystems: list[dict[str, str]] = []
    lines = raw.strip().splitlines()
    if not lines:
        return filesystems
    data_lines = lines[1:] if re.match(r"(?i)filesystem", lines[0]) else lines
    for line in data_lines:
        parts = line.split()
        if len(parts) >= 5:
            filesystems.append({
                "filesystem": parts[0],
                "size": parts[1],
                "used": parts[2],
                "available": parts[3],
                "use_percent": parts[4],
                "mount": parts[5] if len(parts) > 5 else "",
            })
    return filesystems
def _parse_health_output(raw: str) -> list[dict[str, str]]:
    """Parse health check output into check result dicts."""
    checks: list[dict[str, str]] = []
    for line in raw.strip().splitlines():
        line = line.strip()
        if not line:
            continue
        match = re.match(r"^\[(\w+)\]\s*(.+)$", line)
        if match:
            checks.append({"status": match.group(1), "check": match.group(2)})
        else:
            checks.append({"status": "INFO", "check": line})
    return checks
def _parse_timers_output(raw: str) -> list[dict[str, str]]:
    """Parse `systemctl list-timers` by anchoring on timestamp and .timer patterns."""
    timers: list[dict[str, str]] = []
    # Timestamp pattern: "Day YYYY-MM-DD HH:MM:SS TZ" or "-"
    ts = r"(?:\w{3} \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \w+|-)"
    timer_re = re.compile(
        rf"^(?P<next>{ts})\s+(?P<left>.+?)\s+"
        rf"(?P<last>{ts})\s+(?P<passed>.+?)\s+"
        r"(?P<unit>\S+\.timer)\s+(?P<activates>\S+)"
    )
    for line in raw.strip().splitlines():
        m = timer_re.match(line)
        if m:
            timers.append({k: v.strip() for k, v in m.groupdict().items()})
    return timers
def _read_memory() -> dict[str, Any]:
    """Read memory and swap from /proc/meminfo."""
    info: dict[str, int] = {}
    try:
        with open("/proc/meminfo") as f:
            for line in f:
                parts = line.split()
                if len(parts) >= 2:
                    key = parts[0].rstrip(":")
                    info[key] = int(parts[1]) * 1024  # kB → bytes
    except Exception:
        return {}
    mem_total = info.get("MemTotal", 0)
    mem_available = info.get("MemAvailable", 0)
    mem_used = mem_total - mem_available
    swap_total = info.get("SwapTotal", 0)
    swap_free = info.get("SwapFree", 0)
    swap_used = swap_total - swap_free
    return {
        "memory": {
            "total": mem_total,
            "used": mem_used,
            "available": mem_available,
            "percent": round(mem_used / mem_total * 100, 1) if mem_total else 0,
        },
        "swap": {
            "total": swap_total,
            "used": swap_used,
            "free": swap_free,
            "percent": round(swap_used / swap_total * 100, 1) if swap_total else 0,
        },
    }
def _read_cpu_stat() -> tuple[int, int]:
    """Read idle and total jiffies from /proc/stat."""
    with open("/proc/stat") as f:
        line = f.readline()
    parts = line.split()
    values = [int(x) for x in parts[1:]]
    idle = values[3] + (values[4] if len(values) > 4 else 0)  # idle + iowait
    return idle, sum(values)
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@router.get("/disk", summary="Disk usage")
async def disk_usage(
    _: str = Depends(verify_token),
) -> dict[str, Any]:
    """Returns disk usage via `ops disk` (fallback: `df -h`)."""
    result = await run_ops(["disk"])
    raw = result["output"]
    if not result["success"] or not raw.strip():
        fallback = await run_command(["df", "-h"])
        raw = fallback["output"]
        if not fallback["success"]:
            raise HTTPException(status_code=500, detail=f"Failed to get disk usage: {result['error']}")
    return {
        "filesystems": _parse_disk_output(raw),
        "raw": raw,
    }
@router.get("/health", summary="System health checks")
async def health_check(
    _: str = Depends(verify_token),
) -> dict[str, Any]:
    """Returns health check results via `ops health` on the host."""
    result = await run_ops_host(["health"])
    if not result["success"] and not result["output"].strip():
        raise HTTPException(status_code=500, detail=f"Failed to run health checks: {result['error']}")
    return {
        "checks": _parse_health_output(result["output"]),
        "raw": result["output"],
    }
@router.get("/timers", summary="Systemd timers")
async def list_timers(
    _: str = Depends(verify_token),
) -> dict[str, Any]:
    """Lists systemd timers via nsenter on the host."""
    result = await run_command_host(["systemctl", "list-timers", "--no-pager"])
    if not result["success"] and not result["output"].strip():
        raise HTTPException(status_code=500, detail=f"Failed to list timers: {result['error']}")
    return {
        "timers": _parse_timers_output(result["output"]),
        "raw": result["output"],
    }
@router.get("/info", summary="System information with CPU/memory")
async def system_info(
    _: str = Depends(verify_token),
) -> dict[str, Any]:
    """
    Returns system uptime, CPU usage, memory, and swap.
    CPU usage is measured over a 0.5s window from /proc/stat.
    Memory/swap are read from /proc/meminfo.
    """
    uptime_str = ""
    # Uptime
    try:
        with open("/proc/uptime") as f:
            seconds_up = float(f.read().split()[0])
            days = int(seconds_up // 86400)
            hours = int((seconds_up % 86400) // 3600)
            minutes = int((seconds_up % 3600) // 60)
            uptime_str = f"{days}d {hours}h {minutes}m"
    except Exception:
        pass
    # CPU usage (two samples, 0.5s apart)
    cpu_info: dict[str, Any] = {}
    try:
        idle1, total1 = _read_cpu_stat()
        await asyncio.sleep(0.5)
        idle2, total2 = _read_cpu_stat()
        total_delta = total2 - total1
        if total_delta > 0:
            usage = round((1 - (idle2 - idle1) / total_delta) * 100, 1)
        else:
            usage = 0.0
        cpu_info = {
            "usage_percent": usage,
            "cores": os.cpu_count() or 1,
        }
    except Exception:
        pass
    # Memory + Swap
    mem_info = _read_memory()
    # Container count
    containers_str = ""
    try:
        result = await run_command(["docker", "ps", "--format", "{{.State}}"])
        if result["success"]:
            states = [s for s in result["output"].strip().splitlines() if s]
            running = sum(1 for s in states if s == "running")
            containers_str = f"{running}/{len(states)}"
    except Exception:
        pass
    # Process count
    processes = 0
    try:
        with open("/proc/loadavg") as f:
            parts = f.read().split()
            if len(parts) >= 4:
                # /proc/loadavg field 4 is "running/total" processes
                processes = int(parts[3].split("/")[1])
    except Exception:
        pass
    # Fallback for uptime if /proc wasn't available
    if not uptime_str:
        result = await run_command(["uptime"])
        if result["success"]:
            raw = result["output"].strip()
            up_match = re.search(r"up\s+(.+?),\s+\d+\s+user", raw)
            if up_match:
                uptime_str = up_match.group(1).strip()
    return {
        "uptime": uptime_str or "unavailable",
        "cpu": cpu_info or None,
        "containers": containers_str or "n/a",
        "processes": processes or 0,
        **mem_info,
    }