iac/nixos/hosts/asxpio-ops/monitoring/default.nix
Sergei Poljanski f1baa4025c
Some checks failed
nixos / nixos (push) Failing after 57s
nixos: rotate Forgejo's internal token and JWT secrets, Grafana's key
Forgejo's INTERNAL_TOKEN, JWT_SECRET and LFS_JWT_SECRET sat in the old
repository history for a year; Grafana ran on its public default
secret_key. New values, generated by Forgejo and openssl, straight into
sops. SECRET_KEY stays: changing it would break 2FA.

sops-nix does not restart anything when a secret changes. The secrets
now name the unit that reads them (Forgejo, Grafana, postgres-roles,
garage-bootstrap), so a changed value takes effect on the deploy.

Assisted-by: Claude:opus-5-5
2026-11-17 10:38:32 +04:00

471 lines
14 KiB
Nix

# Prometheus, Loki, Grafana and the blackbox prober, for both hosts.
#
# gf.asxp.io Grafana, WireGuard only (through Traefik)
# 10.10.0.10:3100 Loki, where every host's Alloy pushes
# 127.0.0.1:9090 Prometheus
#
# Dashboards (./dashboards) and alert rules (./alert-rules.yaml) are
# provisioned read-only: change them here, not in the UI.
{
config,
lib,
pkgs,
unstable,
...
}:
let
hosts = {
asxpio-ops = "10.10.0.10";
asxpio-services = "10.10.0.20";
};
perHost =
port:
lib.mapAttrsToList (name: address: {
targets = [ "${address}:${toString port}" ];
labels.host = name;
}) hosts;
probes = [
{ name = "asxp.io"; target = "https://asxp.io/"; module = "http_2xx"; }
{ name = "blog"; target = "https://blog.asxp.io/"; module = "http_2xx"; }
{ name = "czsk.it"; target = "https://czsk.it/"; module = "http_2xx"; }
{ name = "datacenterbingo"; target = "https://datacenterbingo.xyz/"; module = "http_2xx"; }
{ name = "datacenterbingo data.json"; target = "https://datacenterbingo.xyz/data.json"; module = "http_2xx"; }
{ name = "forgejo"; target = "https://git.asxp.io/"; module = "http_2xx"; }
{ name = "s3"; target = "https://s3.asxp.io/"; module = "http_up"; }
{ name = "sillytavern"; target = "http://10.20.0.20:8000/"; module = "http_up"; }
{ name = "xmpp c2s"; target = "xmpp.asxp.io:5222"; module = "tcp_connect"; }
{ name = "xmpp c2s tls"; target = "xmpp.asxp.io:5223"; module = "tls_connect"; }
{ name = "xmpp c2s tls asxp.io"; target = "xmpp.asxp.io:5223"; module = "tls_connect_asxpio"; }
{ name = "xmpp s2s"; target = "xmpp.asxp.io:5269"; module = "tcp_connect"; }
{ name = "turn"; target = "xmpp.asxp.io:3478"; module = "tcp_connect"; }
{ name = "xray"; target = "46.62.228.216:443"; module = "tcp_connect"; }
{ name = "minecraft"; target = "mc.asxp.io:25565"; module = "tcp_connect"; }
];
blackbox = (pkgs.formats.yaml { }).generate "blackbox.yml" {
modules =
let
tcp = extra: {
prober = "tcp";
timeout = "10s";
tcp = { preferred_ip_protocol = "ip4"; } // extra;
};
in
{
http_2xx = {
prober = "http";
timeout = "10s";
http = {
preferred_ip_protocol = "ip4";
follow_redirects = true;
};
};
http_up = {
prober = "http";
timeout = "10s";
http = {
preferred_ip_protocol = "ip4";
valid_status_codes = [ 200 301 302 401 403 404 ];
};
};
tcp_connect = tcp { };
tls_connect = tcp { tls = true; };
tls_connect_asxpio = tcp {
tls = true;
tls_config.server_name = "asxp.io";
};
};
};
in
{
sops.secrets = {
"monitoring/grafana_admin_password" = {
owner = "grafana";
restartUnits = [ "grafana.service" ];
};
"monitoring/hcloud_ro_token".owner = "prometheus";
"forgejo/metrics_token".owner = lib.mkForce "prometheus"; # Forgejo reads it as root, at start
"alert/smtp_user".owner = lib.mkForce "grafana";
"alert/smtp_password".owner = lib.mkForce "grafana";
};
sops.templates."loki.env".content = ''
S3_ACCESS_KEY=${config.sops.placeholder."s3/access_key"}
S3_SECRET_KEY=${config.sops.placeholder."s3/secret_key"}
'';
# --- Prometheus ---------------------------------------------------------
services.prometheus = {
enable = true;
package = unstable.prometheus; # 26.05 has 3.12, the data is from 3.16
listenAddress = "127.0.0.1";
port = 9090;
retentionTime = "90d";
# The token files only exist at run time
checkConfig = "syntax-only";
globalConfig = {
scrape_interval = "30s";
evaluation_interval = "30s";
};
scrapeConfigs = [
{
job_name = "node";
static_configs = perHost 9100;
}
{
job_name = "cadvisor";
scrape_interval = "60s";
static_configs = perHost 9180;
metric_relabel_configs = [
{
source_labels = [ "name" ];
regex = "FORGEJO-ACTIONS-TASK-.*";
action = "drop";
}
];
}
{
job_name = "alloy";
static_configs = perHost 12345;
}
{
job_name = "traefik";
static_configs = [
{
targets = [ "127.0.0.1:8082" ];
labels.host = "asxpio-ops";
}
];
}
{
job_name = "forgejo";
metrics_path = "/metrics";
authorization.credentials_file = config.sops.secrets."forgejo/metrics_token".path;
static_configs = [
{
targets = [ "127.0.0.1:3000" ];
labels.host = "asxpio-ops";
}
];
}
{
job_name = "postgres";
static_configs = [
{
targets = [ "127.0.0.1:9187" ];
labels.host = "asxpio-ops";
}
];
}
{
job_name = "monitoring";
static_configs = [
{
targets = [
"127.0.0.1:9090"
"10.10.0.10:3100"
"127.0.0.1:3001"
"127.0.0.1:9115"
];
labels.host = "asxpio-ops";
}
];
}
# Every server in the Hetzner project; one that is not ours gets
# known="false" and an alert. Scrapes Prometheus itself: only the
# discovered labels matter, the samples are dropped.
{
job_name = "hcloud";
scrape_interval = "60s";
hetzner_sd_configs = [
{
role = "hcloud";
authorization.credentials_file = config.sops.secrets."monitoring/hcloud_ro_token".path;
}
];
relabel_configs = [
{
source_labels = [ "__meta_hetzner_server_name" ];
target_label = "server";
}
{
source_labels = [ "__meta_hetzner_server_name" ];
target_label = "instance";
}
{
source_labels = [ "__meta_hetzner_public_ipv4" ];
target_label = "public_ip";
}
{
regex = "__meta_hetzner_hcloud_private_ipv4_.+";
action = "labelmap";
replacement = "private_ip";
}
{
source_labels = [ "__meta_hetzner_hcloud_server_type" ];
target_label = "type";
}
{
source_labels = [ "__meta_hetzner_server_status" ];
target_label = "status";
}
{
target_label = "known";
replacement = "false";
}
{
source_labels = [ "__meta_hetzner_server_name" ];
regex = lib.concatStringsSep "|" (lib.attrNames hosts);
target_label = "known";
replacement = "true";
}
{
target_label = "__address__";
replacement = "127.0.0.1:9090";
}
];
metric_relabel_configs = [ { action = "drop"; } ];
}
]
++ map (module: {
job_name = "probe_${module}";
metrics_path = "/probe";
params.module = [ module ];
static_configs = map (p: {
targets = [ p.target ];
labels.probe = p.name;
}) (lib.filter (p: p.module == module) probes);
relabel_configs = [
{
source_labels = [ "__address__" ];
target_label = "__param_target";
}
{
source_labels = [ "__param_target" ];
target_label = "instance";
}
{
target_label = "__address__";
replacement = "127.0.0.1:9115";
}
];
}) (lib.unique (map (p: p.module) probes));
exporters.blackbox = {
enable = true;
listenAddress = "127.0.0.1";
port = 9115;
configFile = blackbox;
};
exporters.postgres = {
enable = true;
listenAddress = "127.0.0.1";
port = 9187;
runAsLocalSuperUser = true;
};
};
# --- Loki ---------------------------------------------------------------
services.loki = {
enable = true;
package = pkgs.grafana-loki;
extraFlags = [ "-config.expand-env=true" ];
configuration = {
auth_enabled = false;
server = {
http_listen_address = "10.10.0.10";
http_listen_port = 3100;
grpc_listen_address = "127.0.0.1";
log_level = "warn";
};
common = {
path_prefix = "/var/lib/loki";
# Its own components talk gRPC over loopback only
instance_addr = "127.0.0.1";
replication_factor = 1;
ring.kvstore.store = "inmemory";
storage.filesystem = {
chunks_directory = "/var/lib/loki/chunks";
rules_directory = "/var/lib/loki/rules";
};
};
schema_config.configs = [
{
from = "2026-11-01";
store = "tsdb";
object_store = "filesystem";
schema = "v13";
index = {
prefix = "index_";
period = "24h";
};
}
# Chunks since then are in Object Storage (asxpio-loki-main)
{
from = "2026-11-14";
store = "tsdb";
object_store = "aws";
schema = "v13";
index = {
prefix = "index_";
period = "24h";
};
}
];
storage_config.aws = {
endpoint = "hel1.your-objectstorage.com";
region = "hel1";
bucketnames = config.asxpio.lokiBucket;
access_key_id = "\${S3_ACCESS_KEY}";
secret_access_key = "\${S3_SECRET_KEY}";
};
limits_config = {
retention_period = "17520h"; # 2 years
reject_old_samples = true;
reject_old_samples_max_age = "168h";
ingestion_rate_mb = 8;
ingestion_burst_size_mb = 16;
volume_enabled = true;
};
compactor = {
working_directory = "/var/lib/loki/compactor";
retention_enabled = true;
delete_request_store = "aws";
};
analytics.reporting_enabled = false;
};
};
systemd.services.loki = {
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig.EnvironmentFile = config.sops.templates."loki.env".path;
};
# --- Grafana ------------------------------------------------------------
services.grafana = {
enable = true;
package = unstable.grafana; # 26.05 has 13.0, the database is from 13.3
settings = {
server = {
http_addr = "127.0.0.1";
http_port = 3001;
root_url = "https://gf.asxp.io/";
};
security = {
admin_user = "admin";
admin_password = "$__file{${config.sops.secrets."monitoring/grafana_admin_password".path}}";
cookie_secure = true;
secret_key = "$__file{${config.sops.secrets."monitoring/grafana_secret_key".path}}";
};
users.allow_sign_up = false;
"auth.anonymous".enabled = false;
analytics = {
reporting_enabled = false;
check_for_updates = false;
};
news.news_feed_enabled = false;
dashboards.default_home_dashboard_path = "${./dashboards}/overview.json";
smtp = {
enabled = true;
host = "smtp.fastmail.com:587";
user = "$__file{${config.sops.secrets."alert/smtp_user".path}}";
password = "$__file{${config.sops.secrets."alert/smtp_password".path}}";
from_address = "$__file{${config.sops.secrets."alert/smtp_user".path}}";
from_name = "asxpio monitoring";
startTLS_policy = "MandatoryStartTLS";
};
};
provision = {
enable = true;
datasources.settings = {
apiVersion = 1;
datasources = [
{
name = "Prometheus";
uid = "prometheus";
type = "prometheus";
access = "proxy";
url = "http://127.0.0.1:9090";
isDefault = true;
jsonData.timeInterval = "30s";
}
{
name = "Loki";
uid = "loki";
type = "loki";
access = "proxy";
url = "http://10.10.0.10:3100";
}
];
};
dashboards.settings = {
apiVersion = 1;
providers = [
{
name = "asxpio";
type = "file";
disableDeletion = true;
allowUiUpdates = false;
options = {
path = ./dashboards;
foldersFromFilesStructure = true;
};
}
];
};
alerting = {
rules.path = ./alert-rules.yaml;
contactPoints.settings = {
apiVersion = 1;
contactPoints = [
{
orgId = 1;
name = "email";
receivers = [
{
uid = "email-me";
type = "email";
settings = {
addresses = config.asxpio.alertEmail;
singleEmail = true;
};
}
];
}
];
};
policies.settings = {
apiVersion = 1;
policies = [
{
orgId = 1;
receiver = "email";
group_by = [ "alertname" "host" ];
group_wait = "1m";
group_interval = "5m";
repeat_interval = "4h";
}
];
};
};
};
};
sops.secrets."monitoring/grafana_secret_key" = {
owner = "grafana";
restartUnits = [ "grafana.service" ];
};
asxpio.routes.grafana = {
hosts = [ "gf.asxp.io" ];
url = "http://127.0.0.1:3001";
middlewares = [ "wireguard-only" ];
};
asxpio.security.ports = [ "tcp/3100/loki" ];
asxpio.waitForData = [
"prometheus"
"loki"
"grafana"
];
asxpio.backup.paths = [ "/var/lib/grafana" ];
}