{ config, lib, pkgs, ... }: let cfg = config.orbekk.monitoring-server; aliases = import ../data/aliases.nix; in { options = { orbekk.monitoring-server = { enable = lib.mkEnableOption "Enable monitoring server"; grafana-port = lib.mkOption { type = lib.types.int; default = aliases.services.grafana.port; }; textFileDir = lib.mkOption { type = lib.types.str; default = "/var/lib/prometheus-node-exporter-text-files"; }; grafana-domain = lib.mkOption { type = lib.types.str; default = "grafana.orbekk.com"; }; prometheus-port = lib.mkOption { type = lib.types.int; default = aliases.services.prometheus.port; }; prometheus-exporter-port = lib.mkOption { type = lib.types.int; default = aliases.services.prometheus-exporter.port; }; prometheus-snmp-exporter-port = lib.mkOption { type = lib.types.int; default = aliases.services.prometheus-snmp-exporter.port; }; prometheus-pms7003-exporter = lib.mkOption { type = lib.types.str; default = with aliases.services.prometheus-pms7003-exporter; "${host}:${toString port}"; }; snmp-targets = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ aliases.ip.ap2 ]; }; }; }; config = lib.mkIf cfg.enable { services.grafana = { enable = true; settings = { smtp = lib.mkIf config.orbekk.postfix.enable { enable = true; host = "localhost:25"; fromAddress = "root@orbekk.com"; }; server.root_url = "https://grafana.orbekk.com/"; server.domain = cfg.grafana-domain; server.http_port = cfg.grafana-port; server.http_addr = "127.0.0.1"; }; provision = { enable = true; datasources.settings.datasources = [ { name = "Prometheus"; type = "prometheus"; access = "proxy"; orgId = 1; editable = false; url = "http://127.0.0.1:${toString cfg.prometheus-port}"; isDefault = true; } ]; }; }; services.prometheus = { enable = true; port = cfg.prometheus-port; retentionTime = "730d"; scrapeConfigs = [ { job_name = "self"; static_configs = [ { targets = ["127.0.0.1:${toString cfg.prometheus-exporter-port}"]; } ]; } { job_name = "pms7003"; static_configs = [ { targets = ["${cfg.prometheus-pms7003-exporter}"]; } ]; } { job_name = "snmp"; metrics_path = "/snmp"; params = { module = [ "cisco_if_mib" ]; }; relabel_configs = [ { source_labels = ["__address__"]; target_label = "__param_target"; } { source_labels = ["__param_target"]; target_label = "instance"; } { source_labels = []; target_label = "__address__"; replacement = "localhost:${toString cfg.prometheus-snmp-exporter-port}"; } ]; static_configs = [{ targets = cfg.snmp-targets; }]; } ]; exporters = { node = { enable = true; enabledCollectors = [ "systemd" ]; port = cfg.prometheus-exporter-port; extraFlags = ["--collector.textfile.directory=${cfg.textFileDir}"]; }; snmp = { enable = true; configurationPath = ../data/snmp/snmp.yml; port = cfg.prometheus-snmp-exporter-port; }; }; }; system.activationScripts.node-exporter-directory.text = '' # Needed for Prometheus node exporter. mkdir -pm 0775 ${cfg.textFileDir} ''; services.nginx.virtualHosts.${cfg.grafana-domain} = { enableACME = true; forceSSL = true; locations."/" = { proxyPass = "http://127.0.0.1:${toString cfg.grafana-port}"; proxyWebsockets = true; }; }; }; }