-
-
Save dminuoso/6fadc3855d2dd9f69b8d33e5f756fbea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ lib, config, options, nodes, ... }: | |
with lib; | |
let enabled = lib.filterAttrs (n: c: c.enable) config.services.prometheus.exporters; | |
primRole = config.profiles.primaryRole; | |
privIP = config.utils.privateIP; | |
fqdn = config.utils.fqdn; | |
cfg = config.services.prometheus.registry; | |
in | |
{ | |
options.services.prometheus.registry = with types; mkOption { | |
type = attrsOf (submodule ({ name, ... }: { | |
options.labels = mkOption { type = attrsOf str; }; | |
options.backend = mkOption { type = str; }; | |
config.labels = { | |
primary_role = primRole; | |
service_domain = "mailiverse"; | |
host = config.networking.hostName; | |
}; | |
})); | |
}; | |
config = { | |
services.prometheus.exporters.node = { | |
enable = true; | |
listenAddress = "127.0.0.1"; | |
}; | |
networking.firewall.allowedTCPPorts = [ 9100 ]; | |
services.nginx.virtualHosts.${privIP} = { | |
forceSSL = false; | |
listen = [{ addr = privIP; port = 9100; }]; | |
locations = lib.mapAttrs' (name: exp: | |
lib.nameValuePair "= /exporters/${name}" { | |
proxyPass = exp.backend; | |
} | |
) cfg; | |
}; | |
services.prometheus.registry = | |
lib.mapAttrs (name: exp: { | |
labels.exporter_type = name; | |
backend = "http://${exp.listenAddress}:${toString exp.port}/metrics"; | |
}) enabled; | |
}; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ pkgs, nodes, config, options, lib, ... }: | |
let fqdn = config.utils.fqdn; | |
privIP = config.utils.privateIP; | |
discovery = lib.flatten (lib.mapAttrsToList (_name: node: | |
let cfg = node.config.services.prometheus.registry; | |
in lib.mapAttrsToList (name: reg: { | |
labels = reg.labels // { __metrics_path__ = "/exporters/${name}"; }; | |
targets = [ "${node.config.utils.privateIP}:9100" ]; | |
}) cfg | |
) nodes); | |
discoveryDir = pkgs.writeTextDir "/discovery.json" (builtins.toJSON discovery); | |
in | |
{ | |
config = { | |
services.nginx.virtualHosts.${fqdn}.locations."/prometheus/" = { | |
alias = "${discoveryDir}/"; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment