From 7fbb1de6719a9b8de8e63b34f638c0a983c8a99d Mon Sep 17 00:00:00 2001 From: SebastianStork Date: Sat, 20 Sep 2025 15:37:48 +0200 Subject: [PATCH] alloy: Actually implement the functionality --- modules/system/services/alloy.nix | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/modules/system/services/alloy.nix b/modules/system/services/alloy.nix index c7330e5..454b1b6 100644 --- a/modules/system/services/alloy.nix +++ b/modules/system/services/alloy.nix @@ -13,6 +13,18 @@ in type = lib.types.port; default = 12345; }; + metricsEndpoint = lib.mkOption { + type = lib.types.nonEmptyStr; + default = "https://metrics.${config.custom.services.tailscale.domain}/prometheus/api/v1/write"; + }; + logsEndpoint = lib.mkOption { + type = lib.types.nonEmptyStr; + default = "https://logs.${config.custom.services.tailscale.domain}/insert/loki/api/v1/push"; + }; + collect = { + hostMetrics = lib.mkEnableOption ""; + sshdLogs = lib.mkEnableOption ""; + }; }; config = lib.mkIf cfg.enable { @@ -28,5 +40,44 @@ in "--disable-reporting" ]; }; + + environment.etc = { + "alloy/endpoints.alloy".text = '' + prometheus.remote_write "default" { + endpoint { + url = "${cfg.metricsEndpoint}" + } + } + + loki.write "default" { + endpoint { + url = "${cfg.logsEndpoint}" + } + } + ''; + + "alloy/node-exporter.alloy" = lib.mkIf cfg.collect.hostMetrics { + text = '' + prometheus.exporter.unix "default" { + enable_collectors = [ "systemd" ] + } + + prometheus.scrape "node_exporter" { + targets = prometheus.exporter.unix.default.targets + forward_to = [prometheus.remote_write.default.receiver] + scrape_interval = "15s" + } + ''; + }; + + "alloy/sshd-logs.alloy" = lib.mkIf cfg.collect.sshdLogs { + text = '' + loki.source.journal "sshd" { + matches = "_SYSTEMD_UNIT=sshd.service" + forward_to = [loki.write.default.receiver] + } + ''; + }; + }; }; }