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:
inherit system service portName; mkPortTarget {
}) service.prometheus.exporter.ports; inherit system service portName;
})
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

@ -7,8 +7,9 @@
inherit (lib.attrsets) attrValues; inherit (lib.attrsets) attrValues;
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; {
@ -27,7 +24,7 @@ let
}; };
labels = mkOption { labels = mkOption {
type = attrsOf str; type = attrsOf str;
default = { }; default = {};
}; };
}; };
}; };
@ -41,120 +38,134 @@ let
exporter.ports = mkOptionDefault (attrNames exporterPorts); exporter.ports = mkOptionDefault (attrNames exporterPorts);
}; };
}; };
in { in
config, {
gensokyo-zone, config,
lib, gensokyo-zone,
... lib,
}: let ...
inherit (gensokyo-zone.lib) mapListToAttrs mapAlmostOptionDefaults mkAlmostOptionDefault; }: let
inherit (lib.options) mkOption; inherit (gensokyo-zone.lib) mapListToAttrs mapAlmostOptionDefaults mkAlmostOptionDefault;
inherit (lib.modules) mkIf mkOptionDefault; inherit (lib.options) mkOption;
inherit (lib.attrsets) attrNames filterAttrs nameValuePair; inherit (lib.modules) mkIf mkOptionDefault;
mkExporter = { name, port }: nameValuePair "prometheus-exporters-${name}" ({config, ...}: { inherit (lib.attrsets) attrNames filterAttrs nameValuePair;
nixos = { mkExporter = {
serviceAttrPath = ["services" "prometheus" "exporters" name]; name,
assertions = mkIf config.enable [ port,
(nixosConfig: { }:
assertion = config.ports.default.port == nixosConfig.services.prometheus.exporters.${name}.port; nameValuePair "prometheus-exporters-${name}" ({config, ...}: {
message = "port mismatch"; nixos = {
}) serviceAttrPath = ["services" "prometheus" "exporters" name];
]; assertions = mkIf config.enable [
}; (nixosConfig: {
ports.default = mapAlmostOptionDefaults { assertion = config.ports.default.port == nixosConfig.services.prometheus.exporters.${name}.port;
inherit port; message = "port mismatch";
protocol = "http"; })
} // { ];
prometheus.exporter.enable = true; };
}; ports.default =
}); mapAlmostOptionDefaults {
exporters = mapListToAttrs mkExporter [ inherit port;
{ name = "node"; port = 9091; } protocol = "http";
]; }
in { // {
options.exports = with lib.types; { prometheus.exporter.enable = true;
prometheus = { };
exporter.services = mkOption {
type = listOf str;
};
};
services = mkOption {
type = attrsOf (submoduleWith {
modules = [serviceModule];
}); });
}; exporters = mapListToAttrs mkExporter [
}; {
config.exports.prometheus = let name = "node";
exporterServices = filterAttrs (_: service: service.enable && service.prometheus.exporter.ports != [ ]) config.exports.services; port = 9091;
}
];
in { in {
exporter.services = mkOptionDefault (attrNames exporterServices); options.exports = with lib.types; {
}; prometheus = {
config.exports.services = { exporter.services = mkOption {
prometheus = {config, ...}: { type = listOf str;
id = mkAlmostOptionDefault "prometheus"; };
nixos = {
serviceAttr = "prometheus";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.prometheus.port;
message = "port mismatch";
})
];
}; };
ports.default = mapAlmostOptionDefaults { services = mkOption {
port = 9090; type = attrsOf (submoduleWith {
protocol = "http"; modules = [serviceModule];
});
}; };
}; };
grafana = {config, ...}: { config.exports.prometheus = let
id = mkAlmostOptionDefault "grafana"; exporterServices = filterAttrs (_: service: service.enable && service.prometheus.exporter.ports != []) config.exports.services;
nixos = { in {
serviceAttr = "grafana"; exporter.services = mkOptionDefault (attrNames exporterServices);
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.grafana.settings.server.http_port;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
port = 9092;
protocol = "http";
};
}; };
loki = {config, ...}: { config.exports.services =
id = mkAlmostOptionDefault "loki"; {
nixos = { prometheus = {config, ...}: {
serviceAttr = "loki"; id = mkAlmostOptionDefault "prometheus";
assertions = mkIf config.enable [ nixos = {
(nixosConfig: { serviceAttr = "prometheus";
assertion = config.ports.default.port == nixosConfig.services.loki.settings.httpListenPort; assertions = mkIf config.enable [
message = "port mismatch"; (nixosConfig: {
}) assertion = config.ports.default.port == nixosConfig.services.prometheus.port;
]; message = "port mismatch";
}; })
ports.default = mapAlmostOptionDefaults { ];
port = 9093; };
protocol = "http"; ports.default = mapAlmostOptionDefaults {
}; port = 9090;
}; protocol = "http";
promtail = {config, ...}: { };
id = mkAlmostOptionDefault "promtail"; };
nixos = { grafana = {config, ...}: {
serviceAttr = "promtail"; id = mkAlmostOptionDefault "grafana";
assertions = mkIf config.enable [ nixos = {
(nixosConfig: { serviceAttr = "grafana";
assertion = config.ports.default.port == nixosConfig.services.promtail.settings.httpListenPort; assertions = mkIf config.enable [
message = "port mismatch"; (nixosConfig: {
}) assertion = config.ports.default.port == nixosConfig.services.grafana.settings.server.http_port;
]; message = "port mismatch";
}; })
ports.default = mapAlmostOptionDefaults { ];
port = 9094; };
protocol = "http"; ports.default = mapAlmostOptionDefaults {
} // { port = 9092;
prometheus.exporter.enable = true; protocol = "http";
}; };
}; };
} // exporters; loki = {config, ...}: {
} id = mkAlmostOptionDefault "loki";
nixos = {
serviceAttr = "loki";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.loki.settings.httpListenPort;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
port = 9093;
protocol = "http";
};
};
promtail = {config, ...}: {
id = mkAlmostOptionDefault "promtail";
nixos = {
serviceAttr = "promtail";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.promtail.settings.httpListenPort;
message = "port mismatch";
})
];
};
ports.default =
mapAlmostOptionDefaults {
port = 9094;
protocol = "http";
}
// {
prometheus.exporter.enable = true;
};
};
}
// exporters;
}

View file

@ -146,9 +146,11 @@
}; };
in { in {
options.exports = with lib.types; { options.exports = with lib.types; {
defaultServices = mkEnableOption "common base services" // { defaultServices =
default = config.type == "NixOS"; mkEnableOption "common base services"
}; // {
default = config.type == "NixOS";
};
services = mkOption { services = mkOption {
type = attrsOf (submoduleWith { type = attrsOf (submoduleWith {
modules = [serviceModule]; modules = [serviceModule];
@ -170,7 +172,7 @@ in {
"sshd" "sshd"
"prometheus-exporters-node" "prometheus-exporters-node"
"promtail" "promtail"
] (_: { enable = mkAlmostOptionDefault true; }); ] (_: {enable = mkAlmostOptionDefault true;});
in { in {
services = mkIf cfg.defaultServices defaultServices; services = mkIf cfg.defaultServices defaultServices;
}; };

View file

@ -1,6 +1,6 @@
_: { _: {
services = { services = {
prometheus.exporters.node.enable = true; prometheus.exporters.node.enable = true;
promtail.enable = true; promtail.enable = true;
}; };
} }

View file

@ -1,7 +1,7 @@
_: { _: {
services = { services = {
grafana.enable = true; grafana.enable = true;
loki.enable = true; loki.enable = true;
prometheus.enable = true; prometheus.enable = true;
}; };
} }