{ 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}"]; } ]; } ]; exporters = { node = { enable = true; enabledCollectors = [ "systemd" ]; port = cfg.prometheus-exporter-port; extraFlags = ["--collector.textfile.directory=${cfg.textFileDir}"]; }; }; }; 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; }; }; }; }