From 96f71648f526b459245105e420751540ecdf48ea Mon Sep 17 00:00:00 2001 From: sinavir Date: Thu, 18 Apr 2024 20:27:17 +0200 Subject: [PATCH] feat: scrape probes --- lib/default.nix | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ nixos/module.nix | 2 ++ 2 files changed, 71 insertions(+) create mode 100644 lib/default.nix diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..c1549aa --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,69 @@ +{ lib }: +rec { + mkFqdn = _: cfg: cfg.networking.fqdn; + + fromHive = + { + builder, + nodes, + excludes ? [ ], + }: + lib.mkMerge ( + builtins.map (node: builder node nodes.${node}) ( + lib.subtractLists excludes (builtins.attrNames nodes) + ) + ); + + pingProbesFromHive = + { + mkHost, + nodes, + prefix ? "Ping ", + excludes ? [ ], + tags ? [ ], + }: + fromHive { + builder = ( + node: module: { + monitors = { + ${prefix + node} = { + type = "ping"; + inherit tags; + hostname = mkHost node module.config; + }; + }; + } + ); + inherit nodes excludes; + }; + + httpProbesFromConfig = + { + config, + excludes ? [ ], + prefix ? "", + type ? "keyword", + tags ? [ ], + }: + let + filter = k: v: !builtins.elem k excludes && v.globalRedirect == null; + in + { + monitors = lib.mapAttrs' ( + vhostName: vhost: + let + hasSSL = vhost.onlySSL || vhost.enableSSL || vhost.addSSL || vhost.forceSSL; + serverName = if vhost.serverName != null then vhost.serverName else vhostName; + in + { + name = prefix + serverName; + value = { + inherit type; + inherit tags; + url = "http${lib.optionalString hasSSL "s"}://${serverName}"; + method = "get"; + }; + } + ) (lib.filterAttrs filter config.services.nginx.virtualHosts); + }; +} diff --git a/nixos/module.nix b/nixos/module.nix index 7c4e43b..eef273c 100644 --- a/nixos/module.nix +++ b/nixos/module.nix @@ -22,6 +22,7 @@ in Extra arguments to use for executing `stateless-uptime-kuma`. ''; }; + lib = lib.mkOption { type = lib.types.raw; }; probesConfig = { monitors = lib.mkOption { inherit (probesFormat) type; @@ -38,6 +39,7 @@ in }; }; config.statelessUptimeKuma = { + lib = import ../lib { inherit lib; }; build = { json = probesFormat.generate "probes.json" cfg.probesConfig; script = pkgs.writeShellApplication {