auto generate prometheus scrape targets

This commit is contained in:
2025-01-31 13:35:19 +01:00
parent 34a3c831e8
commit f5fcfbded4
2 changed files with 68 additions and 22 deletions

View File

@@ -31,7 +31,7 @@
# Raspberry Pi 4 # Raspberry Pi 4
genepi = nixpkgs.lib.nixosSystem { genepi = nixpkgs.lib.nixosSystem {
specialArgs = { specialArgs = {
inherit inputs; inherit inputs self;
inherit (import ./parts) keys; inherit (import ./parts) keys;
}; };
system = "aarch64-linux"; system = "aarch64-linux";

View File

@@ -1,18 +1,81 @@
{ config, ... }: {
config,
lib,
self,
...
}:
let
domain = "home.rpqt.fr";
allHosts = self.nixosConfigurations;
hostInTailnetFilter = k: v: v.config.services.tailscale.enable;
tailnetHosts = lib.filterAttrs hostInTailnetFilter allHosts;
# Explicitly list the exporters as some are deprecated and can't be evaluated
possibleExporterNames = [
"node"
];
getEnabledExporters =
hostname: host:
lib.filterAttrs (k: v: v.enable == true) (
lib.getAttrs possibleExporterNames host.config.services.prometheus.exporters
);
enabledExporters = lib.mapAttrs getEnabledExporters tailnetHosts;
mkScrapeConfigExporter = hostname: exporterName: exporterCfg: {
job_name = "${hostname}-${exporterName}";
static_configs = [ { targets = [ "${hostname}:${toString exporterCfg.port}" ]; } ];
relabel_configs = [
{
target_label = "instance";
replacement = "${hostname}";
}
{
target_label = "job";
replacement = "${exporterName}";
}
];
};
mkScrapeConfigHost = hostname: exporters: lib.mapAttrs (mkScrapeConfigExporter hostname) exporters;
scrapeConfigsByHost = lib.mapAttrs mkScrapeConfigHost enabledExporters;
autogenScrapeConfigs = lib.flatten (
map builtins.attrValues (builtins.attrValues scrapeConfigsByHost)
);
in
{ {
services.grafana = { services.grafana = {
enable = true; enable = true;
settings = { settings = {
server = { server = {
http_port = 3000; http_port = 3000;
domain = "grafana.home.rpqt.fr"; domain = "grafana.${domain}";
};
};
provision = {
enable = true;
datasources = {
settings = {
datasources = [
{
name = "Prometheus";
type = "prometheus";
access = "proxy";
url = "http://127.0.0.1:${toString config.services.prometheus.port}";
isDefault = true;
}
];
};
}; };
}; };
}; };
services.nginx.virtualHosts.${config.services.grafana.settings.server.domain} = { services.nginx.virtualHosts.${config.services.grafana.settings.server.domain} = {
forceSSL = true; forceSSL = true;
useACMEHost = "home.rpqt.fr"; useACMEHost = "${domain}";
locations."/" = { locations."/" = {
proxyPass = "http://127.0.0.1:${toString config.services.grafana.settings.server.http_port}"; proxyPass = "http://127.0.0.1:${toString config.services.grafana.settings.server.http_port}";
proxyWebsockets = true; proxyWebsockets = true;
@@ -23,24 +86,7 @@
enable = true; enable = true;
port = 9001; port = 9001;
scrapeConfigs = [ scrapeConfigs = autogenScrapeConfigs;
{
job_name = "genepi";
static_configs = [
{
targets = [ "127.0.0.1:${toString config.services.prometheus.exporters.node.port}" ];
}
];
}
{
job_name = "crocus";
static_configs = [
{
targets = [ "crocus:9002" ];
}
];
}
];
exporters = { exporters = {
node = { node = {