chore(monitoring): nf-fmt-nix

This commit is contained in:
arcnmx 2024-05-30 14:10:18 -07:00
parent 6fb140c7e6
commit a0a7df1b0e
6 changed files with 164 additions and 141 deletions

View file

@ -11,18 +11,24 @@
nodeExporterSystems = nodeExporterSystems =
filter ( filter (
system: system:
system.config.access.online.enable && system.config.access.online.enable
system.config.exports.prometheus.exporter.services != [ ] && system.config.exports.prometheus.exporter.services != []
) )
(attrValues systems); (attrValues systems);
mkPortTarget = { system, service, portName }: let mkPortTarget = {
system,
service,
portName,
}: let
port = service.ports.${portName}; port = service.ports.${portName};
in "${access.getAddressFor system.config.name "lan"}:${toString port.port}"; in "${access.getAddressFor system.config.name "lan"}:${toString port.port}";
mkServiceConfig = system: serviceName: let mkServiceConfig = system: serviceName: let
service = system.config.exports.services.${serviceName}; service = system.config.exports.services.${serviceName};
targets = map (portName: mkPortTarget { targets = map (portName:
mkPortTarget {
inherit system service portName; inherit system service portName;
}) service.prometheus.exporter.ports; })
service.prometheus.exporter.ports;
in { in {
job_name = "${system.config.name}-${service.id}"; job_name = "${system.config.name}-${service.id}";
static_configs = [ static_configs = [

View file

@ -8,7 +8,8 @@
inherit (lib.lists) concatMap toList; inherit (lib.lists) concatMap toList;
allExporters = let allExporters = let
exporters = removeAttrs config.services.prometheus.exporters ["unifi-poller"]; exporters = removeAttrs config.services.prometheus.exporters ["unifi-poller"];
in concatMap toList (attrValues exporters); in
concatMap toList (attrValues exporters);
in { in {
config = { config = {
services.prometheus.exporters = { services.prometheus.exporters = {
@ -57,8 +58,11 @@ in {
} }
]; ];
}; };
networking.firewall.interfaces.lan.allowedTCPPorts = map (exporter: networking.firewall.interfaces.lan.allowedTCPPorts =
mkIf (exporter.enable && !exporter.openFirewall) 999 map (
) allExporters; exporter:
mkIf (exporter.enable && !exporter.openFirewall) exporter.port
)
allExporters;
}; };
} }

View file

@ -1,8 +1,5 @@
let let
portModule = { portModule = {lib, ...}: let
lib,
...
}: let
inherit (lib.options) mkEnableOption; inherit (lib.options) mkEnableOption;
in { in {
options.prometheus = with lib.types; { options.prometheus = with lib.types; {
@ -41,7 +38,8 @@ let
exporter.ports = mkOptionDefault (attrNames exporterPorts); exporter.ports = mkOptionDefault (attrNames exporterPorts);
}; };
}; };
in { in
{
config, config,
gensokyo-zone, gensokyo-zone,
lib, lib,
@ -51,7 +49,11 @@ in {
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.modules) mkIf mkOptionDefault; inherit (lib.modules) mkIf mkOptionDefault;
inherit (lib.attrsets) attrNames filterAttrs nameValuePair; inherit (lib.attrsets) attrNames filterAttrs nameValuePair;
mkExporter = { name, port }: nameValuePair "prometheus-exporters-${name}" ({config, ...}: { mkExporter = {
name,
port,
}:
nameValuePair "prometheus-exporters-${name}" ({config, ...}: {
nixos = { nixos = {
serviceAttrPath = ["services" "prometheus" "exporters" name]; serviceAttrPath = ["services" "prometheus" "exporters" name];
assertions = mkIf config.enable [ assertions = mkIf config.enable [
@ -61,15 +63,20 @@ in {
}) })
]; ];
}; };
ports.default = mapAlmostOptionDefaults { ports.default =
mapAlmostOptionDefaults {
inherit port; inherit port;
protocol = "http"; protocol = "http";
} // { }
// {
prometheus.exporter.enable = true; prometheus.exporter.enable = true;
}; };
}); });
exporters = mapListToAttrs mkExporter [ exporters = mapListToAttrs mkExporter [
{ name = "node"; port = 9091; } {
name = "node";
port = 9091;
}
]; ];
in { in {
options.exports = with lib.types; { options.exports = with lib.types; {
@ -89,7 +96,8 @@ in {
in { in {
exporter.services = mkOptionDefault (attrNames exporterServices); exporter.services = mkOptionDefault (attrNames exporterServices);
}; };
config.exports.services = { config.exports.services =
{
prometheus = {config, ...}: { prometheus = {config, ...}: {
id = mkAlmostOptionDefault "prometheus"; id = mkAlmostOptionDefault "prometheus";
nixos = { nixos = {
@ -149,12 +157,15 @@ in {
}) })
]; ];
}; };
ports.default = mapAlmostOptionDefaults { ports.default =
mapAlmostOptionDefaults {
port = 9094; port = 9094;
protocol = "http"; protocol = "http";
} // { }
// {
prometheus.exporter.enable = true; prometheus.exporter.enable = true;
}; };
}; };
} // exporters; }
// exporters;
} }

View file

@ -146,7 +146,9 @@
}; };
in { in {
options.exports = with lib.types; { options.exports = with lib.types; {
defaultServices = mkEnableOption "common base services" // { defaultServices =
mkEnableOption "common base services"
// {
default = config.type == "NixOS"; default = config.type == "NixOS";
}; };
services = mkOption { services = mkOption {