refactor: overhaul daemon server, client, and handlers
This commit is contained in:
+16
-9
@@ -6,7 +6,14 @@ Reads system logs and journal entries.
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from daemon.server import registry
|
||||
from daemon.iface import (
|
||||
GET_LOGS_APP,
|
||||
GET_LOGS_DNSMASQ,
|
||||
GET_LOGS_JOURNAL,
|
||||
GET_LOGS_NGINX_ACCESS,
|
||||
GET_LOGS_NGINX_ERROR,
|
||||
)
|
||||
from daemon.server import NotFoundError, registry
|
||||
from lib.common import run_proc
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -36,9 +43,9 @@ def _tail_file(path: str, n: int = _MAX_LINES, sudo: bool = False) -> str:
|
||||
lines = f.readlines()
|
||||
return "".join(lines[-n:])
|
||||
except FileNotFoundError:
|
||||
return "(log file not found)\n"
|
||||
raise NotFoundError("log file not found") from None
|
||||
except PermissionError:
|
||||
return "(permission denied)\n"
|
||||
raise RuntimeError("permission denied") from None
|
||||
|
||||
|
||||
def _sudo_journalctl(unit: str, n: int = _MAX_LINES) -> str:
|
||||
@@ -61,34 +68,34 @@ def _sudo_journalctl(unit: str, n: int = _MAX_LINES) -> str:
|
||||
output = result.stdout.strip()
|
||||
return output if output else f"(no journal entries for {unit})\n"
|
||||
except Exception as exc:
|
||||
return f"(error reading journal: {exc})\n"
|
||||
raise RuntimeError(f"error reading journal: {exc}") from exc
|
||||
|
||||
|
||||
@registry.register("GET", "/logs/journal")
|
||||
@registry.register(GET_LOGS_JOURNAL)
|
||||
def journal(_request, _body) -> str:
|
||||
"""GET /logs/journal — return vacuum-wall daemon journal entries."""
|
||||
return _sudo_journalctl("vacuum-wall")
|
||||
|
||||
|
||||
@registry.register("GET", "/logs/nginx/access")
|
||||
@registry.register(GET_LOGS_NGINX_ACCESS)
|
||||
def nginx_access(_request, _body) -> str:
|
||||
"""GET /logs/nginx/access — return recent nginx access log lines."""
|
||||
return _tail_file("/var/log/nginx/access.log", sudo=True)
|
||||
|
||||
|
||||
@registry.register("GET", "/logs/nginx/error")
|
||||
@registry.register(GET_LOGS_NGINX_ERROR)
|
||||
def nginx_error(_request, _body) -> str:
|
||||
"""GET /logs/nginx/error — return recent nginx error log lines."""
|
||||
return _tail_file("/var/log/nginx/error.log", sudo=True)
|
||||
|
||||
|
||||
@registry.register("GET", "/logs/dnsmasq")
|
||||
@registry.register(GET_LOGS_DNSMASQ)
|
||||
def dnsmasq_log(_request, _body) -> str:
|
||||
"""GET /logs/dnsmasq — return recent dnsmasq journal entries."""
|
||||
return _sudo_journalctl("dnsmasq")
|
||||
|
||||
|
||||
@registry.register("GET", "/logs/app")
|
||||
@registry.register(GET_LOGS_APP)
|
||||
def app_log(_request, _body) -> str:
|
||||
"""GET /logs/app — return recent application log lines."""
|
||||
return _tail_file(str(_APP_LOG_FILE))
|
||||
|
||||
Reference in New Issue
Block a user