{ lib, config, nixLib, ... }: let cfg = config.vhack.monitoring.prometheus; commonLabels = { hostname = config.networking.hostName; }; prom_cfg = config.services.prometheus; in { options.vhack.monitoring.prometheus = { enable = lib.mkEnableOption "prometheus"; port = lib.mkOption { type = lib.types.port; description = "Port where Prometheus listens to HTTP requests."; default = 3001; }; sources = lib.mkOption { type = lib.types.listOf (lib.types.submodule { options = { name = lib.mkOption { type = lib.types.str; }; target = lib.mkOption { type = lib.types.str; }; extra = lib.mkOption { type = lib.types.attrsOf lib.types.anything; }; }; }); }; }; config = lib.mkIf cfg.enable { vhack = { nginx.enable = true; persist.directories = [ { directory = "/var/lib/prometheus2"; user = "prometheus"; group = "prometheus"; mode = "0700"; } ]; monitoring.prometheus.sources = [ { name = "node"; target = "127.0.0.1:${toString prom_cfg.exporters.node.port}"; } { name = "prometheus_internal"; target = "127.0.0.1:${toString prom_cfg.port}"; } { name = "systemd"; target = "127.0.0.1:${toString prom_cfg.exporters.systemd.port}"; } ]; }; users = { groups = { node-exporter.gid = config.vhack.constants.ids.gids.node-exporter; }; users = { node-exporter = { isSystemUser = true; group = "node-exporter"; uid = config.vhack.constants.ids.uids.node-exporter; }; }; }; services = { prometheus = { enable = true; inherit (cfg) port; globalConfig = { scrape_interval = "15s"; }; scrapeConfigs = builtins.map (attr: nixLib.warnMerge { job_name = attr.name; static_configs = [ { targets = [attr.target]; labels = commonLabels; } ]; } attr.extra "prometheus scrapeConfig") cfg.sources; # ++ (lib.optional (builtins.length (lib.attrNames config.services.redis.servers) > 0) { # job_name = "redis"; # static_configs = [ # { # targets = ["127.0.0.1:${toString config.services.prometheus.exporters.redis.port}"]; # } # ]; # }) ++ (lib.optional (builtins.length (lib.attrNames config.services.openvpn.servers) > 0) { # job_name = "openvpn"; # static_configs = [ # { # targets = ["127.0.0.1:${toString config.services.prometheus.exporters.openvpn.port}"]; # } # ]; # }) # ++ (lib.optional config.services.dnsmasq.enable { # job_name = "dnsmasq"; # static_configs = [ # { # targets = ["127.0.0.1:${toString config.services.prometheus.exporters.dnsmasq.port}"]; # labels = commonLabels; # } # ]; # }); exporters = { node = { enable = true; # https://github.com/prometheus/node_exporter#collectors enabledCollectors = [ "arp" "cpu" "cpufreq" "diskstats" "dmi" "edac" "entropy" "filefd" "filesystem" "hwmon" "loadavg" "meminfo" "netclass" "netdev" "netstat" "nvme" "os" "pressure" "rapl" "schedstat" "stat" "thermal_zone" "time" "uname" "vmstat" "zfs" # Disabled by default "cgroups" "drm" "ethtool" "logind" "wifi" ]; port = 9112; listenAddress = "127.0.0.1"; }; # services.prometheus.exporters.redis = lib.mkIf (builtins.length (lib.attrNames config.services.redis.servers) > 0) { # enable = true; # port = 9119; # listenAddress = "127.0.0.1"; # }; # services.prometheus.exporters.openvpn = lib.mkIf (builtins.length (lib.attrNames config.services.openvpn.servers) > 0) { # enable = true; # port = 9121; # listenAddress = "127.0.0.1"; # statusPaths = lib.mapAttrsToList (name: _config: "/tmp/openvpn/${name}.status") config.services.openvpn.servers; # }; # dnsmasq = lib.mkIf config.services.dnsmasq.enable { # enable = true; # port = 9211; # listenAddress = "127.0.0.1"; # }; systemd = { enable = true; port = 9116; listenAddress = "127.0.0.1"; extraFlags = [ "--log.level=info" "--web.disable-exporter-metrics" # "--systemd.collector.enable-resolved" "--systemd.collector.enable-restart-count" # "--systemd.collector.enable-file-descriptor-size" "--systemd.collector.enable-ip-accounting" "--systemd.collector.unit-include=\".+\"" "--systemd.collector.unit-exclude=\".+\\\\.(device)\"" ]; }; }; }; }; }; }