{ lib, config, ... }: let cfg = config.monitoring; in { config = lib.mkIf (cfg.monitorAll && config.services.postgresql.enable) { services.prometheus.exporters.postgres = { enable = true; extraFlags = [ "--collector.database_wraparound" "--collector.long_running_transactions" "--collector.postmaster" "--collector.process_idle" "--collector.stat_activity_autovacuum" "--collector.stat_statements" #"--collector.stat_wal_receiver" #we dont have WAL receivers "--collector.statio_user_indexes" "--collector.xlog_location" ]; }; services.prometheus.scrapeConfigs = ( lib.map (interval: { job_name = "postgres-${toString interval}s"; scrape_interval = "${toString interval}s"; static_configs = [ { targets = [ "localhost:${toString config.services.prometheus.exporters.postgres.port}" ]; } ]; }) cfg.prometheusScrapeIntervals ); services.grafana.provision.dashboards.settings = { apiVersion = 1; providers = [ { name = "14114-postgres-overview"; orgId = 1; type = "file"; options = { path = builtins.fetchurl { url = "https://grafana.com/api/dashboards/14114/revisions/1/download"; sha256 = "0hk0d2nk8nh5ldd17sy3wwpn1xhyinjlsy5vgmnvpvn6g97n342z"; }; }; } ]; }; }; }