From d6368c86bc949371e904eed3d0a6583ebd53b055 Mon Sep 17 00:00:00 2001 From: Azat Bahawi Date: Wed, 12 Apr 2023 04:01:46 +0300 Subject: 2023-04-12 --- modules/nixos/monitoring/default.nix | 195 +++++++++++------------------------ 1 file changed, 63 insertions(+), 132 deletions(-) (limited to 'modules/nixos/monitoring/default.nix') diff --git a/modules/nixos/monitoring/default.nix b/modules/nixos/monitoring/default.nix index a492a47..37e34d9 100644 --- a/modules/nixos/monitoring/default.nix +++ b/modules/nixos/monitoring/default.nix @@ -124,146 +124,77 @@ in { prometheus = { # It would be nice if these could be generated dynamically. That would # require a complete rework of how configurations are defined, though. - scrapeConfigs = let - mkTargets = hosts: port: map (host: "${host.hostname}:${toString port}") hosts; - in - with my.configurations; - with config.services.prometheus.exporters; [ - { - job_name = "promtail"; - static_configs = [ - { - targets = - mkTargets - [ - manwe - varda - yavanna - ] - config.nixfiles.modules.promtail.port; - } - ]; - } - { - job_name = "ntfy"; - static_configs = [ - { - targets = - mkTargets - [ - manwe - ] - config.nixfiles.modules.ntfy.prometheus.port; - } - ]; - } - { - job_name = "soju"; - static_configs = [ - { - targets = [ - "127.0.0.1:${toString config.nixfiles.modules.soju.prometheus.port}" - ]; - } - ]; - } - { - job_name = "endlessh-go"; - static_configs = [ - { - targets = - mkTargets - [ - manwe - varda - yavanna - ] - config.services.endlessh-go.prometheus.port; - } - ]; - } - { - job_name = "nginx"; - static_configs = [ - { - targets = - mkTargets - [ - manwe - yavanna - ] - nginx.port; - } - ]; - } - { - job_name = "node"; - static_configs = [ - { - targets = - mkTargets - [ - manwe - varda - yavanna - ] - node.port; - } - ]; - } - { - job_name = "postgres"; + scrapeConfigs = with my.configurations; + mapAttrsToList + ( + name: value: { + job_name = name; static_configs = [ { - targets = - mkTargets - [ - manwe - ] - postgres.port; + targets = with value; + map (host: + concatStringsSep ":" [ + ( + if isAttrs host + then host.hostname + else host + ) + (toString port) + ]) + hosts; } ]; - } - { - job_name = "redis"; - static_configs = [ + relabel_configs = [ { - targets = - mkTargets - [ - manwe - ] - redis.port; + source_labels = ["__address__"]; + regex = "([^:]+):\\d+"; + target_label = "instance"; } ]; } - { - job_name = "unbound"; - static_configs = [ - { - targets = - mkTargets - [ - manwe - ] - unbound.port; - } - ]; - } - { - job_name = "wireguard"; - static_configs = [ - { - targets = - mkTargets - [ - manwe - ] - wireguard.port; - } - ]; - } - ]; + ) + { + promtail = { + hosts = [manwe varda yavanna]; + inherit (config.nixfiles.modules.promtail) port; + }; + ntfy = { + hosts = [manwe]; + inherit (config.nixfiles.modules.ntfy.prometheus) port; + }; + soju = { + hosts = ["127.0.0.1"]; + inherit (config.nixfiles.modules.soju.prometheus) port; + }; + endlessh-go = { + hosts = [manwe varda yavanna]; + inherit (config.services.endlessh-go.prometheus) port; + }; + nginx = { + hosts = [manwe yavanna]; + inherit (config.services.prometheus.exporters.nginx) port; + }; + node = { + hosts = [manwe varda yavanna]; + inherit (config.services.prometheus.exporters.node) port; + }; + postgres = { + hosts = [manwe]; + inherit (config.services.prometheus.exporters.postgres) port; + }; + redis = { + hosts = [manwe]; + inherit (config.services.prometheus.exporters.redis) port; + }; + unbound = { + hosts = [manwe]; + inherit (config.services.prometheus.exporters.unbound) port; + }; + wireguard = { + hosts = [manwe]; + inherit (config.services.prometheus.exporters.wireguard) port; + }; + }; ruleFiles = [ ./rules/nginx.yaml -- cgit v1.2.3