feat: scrape probes

This commit is contained in:
sinavir 2024-04-18 20:27:17 +02:00
parent a6904ba370
commit 96f71648f5
2 changed files with 71 additions and 0 deletions

69
lib/default.nix Normal file
View file

@ -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);
};
}

View file

@ -22,6 +22,7 @@ in
Extra arguments to use for executing `stateless-uptime-kuma`. Extra arguments to use for executing `stateless-uptime-kuma`.
''; '';
}; };
lib = lib.mkOption { type = lib.types.raw; };
probesConfig = { probesConfig = {
monitors = lib.mkOption { monitors = lib.mkOption {
inherit (probesFormat) type; inherit (probesFormat) type;
@ -38,6 +39,7 @@ in
}; };
}; };
config.statelessUptimeKuma = { config.statelessUptimeKuma = {
lib = import ../lib { inherit lib; };
build = { build = {
json = probesFormat.generate "probes.json" cfg.probesConfig; json = probesFormat.generate "probes.json" cfg.probesConfig;
script = pkgs.writeShellApplication { script = pkgs.writeShellApplication {