64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
from collections import Counter
|
|
from html import escape
|
|
|
|
from ..agent import validate_host
|
|
from ..agent.tools import ToolArgumentError
|
|
from ..models import STATE_NAMES, STATE_OK, state_name
|
|
from .constants import HELP_LINES, PREFIXES, SIMPLE_HELP
|
|
from .context import CommandContext
|
|
from .models import Reply
|
|
from .render import join, plain, service_list
|
|
|
|
NO_PROBLEMS = "Keine Probleme. Alles OK."
|
|
HOST_USAGE = "Verwendung: {p}host <name>"
|
|
|
|
|
|
async def help_command(ctx: CommandContext) -> Reply:
|
|
prefix = PREFIXES.get(ctx.message.backend, "/")
|
|
lines = []
|
|
for name, text in HELP_LINES.items():
|
|
if name not in ctx.registry:
|
|
continue
|
|
lines.append(f"{prefix}{name}: {text}" if name in SIMPLE_HELP else text.format(p=prefix))
|
|
return plain("Befehle:\n" + "\n".join(lines))
|
|
|
|
|
|
async def status_command(ctx: CommandContext) -> Reply:
|
|
problems = await ctx.deps.reader.problems()
|
|
if not problems:
|
|
return plain(NO_PROBLEMS)
|
|
counts = Counter(item.state for item in problems)
|
|
parts = [f"{STATE_NAMES[s]}: {counts.get(s, 0)}" for s in (1, 2, 3)]
|
|
downtime = sum(1 for item in problems if item.in_downtime)
|
|
return plain(f"Probleme: {len(problems)} ({', '.join(parts)}), davon in Downtime: {downtime}")
|
|
|
|
|
|
async def problems_command(ctx: CommandContext) -> Reply:
|
|
problems = await ctx.deps.reader.problems()
|
|
if not problems:
|
|
return plain(NO_PROBLEMS)
|
|
texts, htmls = service_list(problems)
|
|
return join(f"Probleme ({len(problems)}):", texts, htmls)
|
|
|
|
|
|
async def host_command(ctx: CommandContext) -> Reply:
|
|
prefix = PREFIXES.get(ctx.message.backend, "/")
|
|
if len(ctx.command.args) != 1:
|
|
return plain(HOST_USAGE.format(p=prefix))
|
|
try:
|
|
host = validate_host(ctx.command.args[0])
|
|
except ToolArgumentError:
|
|
return plain("Ungültiger Hostname.")
|
|
services = await ctx.deps.reader.host_services(host)
|
|
if not services:
|
|
return Reply(f"Host {host} nicht gefunden oder ohne Services.",
|
|
f"Host {escape(host)} nicht gefunden oder ohne Services.")
|
|
bad = [item for item in services if item.state != STATE_OK]
|
|
counts = Counter(state_name(item.state) for item in services)
|
|
summary = f"{host}: {len(services)} Services, " + ", ".join(
|
|
f"{name}: {count}" for name, count in counts.items())
|
|
if not bad:
|
|
return Reply(summary, escape(summary))
|
|
texts, htmls = service_list(bad, with_host=False)
|
|
return join(summary, texts, htmls)
|