New MCP server that queries live Prometheus and Alertmanager HTTP APIs with 8 tools: list_alerts, get_alert, search_metrics, get_metric_metadata, query (PromQL), list_targets, list_silences, and create_silence. Extends the MCP core with ModeCustom and NewGenericServer for servers that don't require a database. Includes CLI with direct commands (alerts, query, targets, metrics), NixOS module, and comprehensive httptest-based tests. Bumps existing binaries to 0.2.1 due to shared internal/mcp change. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
140 lines
3.8 KiB
Nix
140 lines
3.8 KiB
Nix
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
cfg = config.services.lab-monitoring;
|
|
|
|
mkHttpFlags = httpCfg: lib.concatStringsSep " " ([
|
|
"--transport http"
|
|
"--http-address '${httpCfg.address}'"
|
|
"--http-endpoint '${httpCfg.endpoint}'"
|
|
"--session-ttl '${httpCfg.sessionTTL}'"
|
|
] ++ lib.optionals (httpCfg.allowedOrigins != []) (
|
|
map (origin: "--allowed-origins '${origin}'") httpCfg.allowedOrigins
|
|
) ++ lib.optionals httpCfg.tls.enable [
|
|
"--tls-cert '${httpCfg.tls.certFile}'"
|
|
"--tls-key '${httpCfg.tls.keyFile}'"
|
|
]);
|
|
in
|
|
{
|
|
options.services.lab-monitoring = {
|
|
enable = lib.mkEnableOption "Lab Monitoring MCP server";
|
|
|
|
package = lib.mkPackageOption pkgs "lab-monitoring" { };
|
|
|
|
prometheusUrl = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "http://localhost:9090";
|
|
description = "Prometheus base URL.";
|
|
};
|
|
|
|
alertmanagerUrl = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "http://localhost:9093";
|
|
description = "Alertmanager base URL.";
|
|
};
|
|
|
|
http = {
|
|
address = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "127.0.0.1:8084";
|
|
description = "HTTP listen address for the MCP server.";
|
|
};
|
|
|
|
endpoint = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "/mcp";
|
|
description = "HTTP endpoint path for MCP requests.";
|
|
};
|
|
|
|
allowedOrigins = lib.mkOption {
|
|
type = lib.types.listOf lib.types.str;
|
|
default = [ ];
|
|
description = "Allowed Origin headers for CORS.";
|
|
};
|
|
|
|
sessionTTL = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = "30m";
|
|
description = "Session TTL for HTTP transport.";
|
|
};
|
|
|
|
tls = {
|
|
enable = lib.mkEnableOption "TLS for HTTP transport";
|
|
|
|
certFile = lib.mkOption {
|
|
type = lib.types.nullOr lib.types.path;
|
|
default = null;
|
|
description = "Path to TLS certificate file.";
|
|
};
|
|
|
|
keyFile = lib.mkOption {
|
|
type = lib.types.nullOr lib.types.path;
|
|
default = null;
|
|
description = "Path to TLS private key file.";
|
|
};
|
|
};
|
|
};
|
|
|
|
openFirewall = lib.mkOption {
|
|
type = lib.types.bool;
|
|
default = false;
|
|
description = "Whether to open the firewall for the MCP HTTP server.";
|
|
};
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
assertions = [
|
|
{
|
|
assertion = !cfg.http.tls.enable || (cfg.http.tls.certFile != null && cfg.http.tls.keyFile != null);
|
|
message = "services.lab-monitoring.http.tls: both certFile and keyFile must be set when TLS is enabled";
|
|
}
|
|
];
|
|
|
|
systemd.services.lab-monitoring = {
|
|
description = "Lab Monitoring MCP Server";
|
|
wantedBy = [ "multi-user.target" ];
|
|
after = [ "network.target" ];
|
|
|
|
environment = {
|
|
PROMETHEUS_URL = cfg.prometheusUrl;
|
|
ALERTMANAGER_URL = cfg.alertmanagerUrl;
|
|
};
|
|
|
|
script = let
|
|
httpFlags = mkHttpFlags cfg.http;
|
|
in ''
|
|
exec ${cfg.package}/bin/lab-monitoring serve ${httpFlags}
|
|
'';
|
|
|
|
serviceConfig = {
|
|
Type = "simple";
|
|
DynamicUser = true;
|
|
Restart = "on-failure";
|
|
RestartSec = "5s";
|
|
|
|
# Hardening
|
|
NoNewPrivileges = true;
|
|
ProtectSystem = "strict";
|
|
ProtectHome = true;
|
|
PrivateTmp = true;
|
|
PrivateDevices = true;
|
|
ProtectKernelTunables = true;
|
|
ProtectKernelModules = true;
|
|
ProtectControlGroups = true;
|
|
RestrictNamespaces = true;
|
|
RestrictRealtime = true;
|
|
RestrictSUIDSGID = true;
|
|
MemoryDenyWriteExecute = true;
|
|
LockPersonality = true;
|
|
};
|
|
};
|
|
|
|
networking.firewall = lib.mkIf cfg.openFirewall (let
|
|
addressParts = lib.splitString ":" cfg.http.address;
|
|
port = lib.toInt (lib.last addressParts);
|
|
in {
|
|
allowedTCPPorts = [ port ];
|
|
});
|
|
};
|
|
}
|