2022-04-10 11:54:58 +02:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}: let
|
|
|
|
inherit
|
|
|
|
(lib)
|
2022-01-11 16:08:21 +01:00
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
mkOption
|
2022-04-10 11:54:58 +02:00
|
|
|
;
|
2022-01-11 16:08:21 +01:00
|
|
|
|
2021-07-13 00:57:33 +02:00
|
|
|
cfg = config.my.services.monitoring;
|
|
|
|
domain = config.networking.domain;
|
2022-06-12 17:18:58 +02:00
|
|
|
hostname = config.networking.hostName;
|
|
|
|
fqdn = "${hostname}.${domain}";
|
2021-01-27 21:49:03 +01:00
|
|
|
in {
|
2022-04-10 11:54:58 +02:00
|
|
|
options.my.services.monitoring = let
|
|
|
|
inherit (lib) types;
|
|
|
|
in {
|
2021-01-27 21:49:03 +01:00
|
|
|
enable = mkEnableOption "Enable monitoring";
|
|
|
|
|
|
|
|
domain = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "monitoring.${config.networking.domain}";
|
|
|
|
example = "monitoring.example.com";
|
|
|
|
description = "Domain to use in reverse proxy";
|
|
|
|
};
|
2021-07-15 10:14:03 +02:00
|
|
|
|
|
|
|
scrapeInterval = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "15s";
|
|
|
|
example = "1m";
|
|
|
|
description = "prometheus scrape interval";
|
|
|
|
};
|
2021-01-27 21:49:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
services.grafana = {
|
|
|
|
enable = true;
|
|
|
|
domain = cfg.domain;
|
|
|
|
port = 3000;
|
|
|
|
addr = "127.0.0.1";
|
|
|
|
|
|
|
|
provision = {
|
|
|
|
enable = true;
|
|
|
|
|
|
|
|
datasources = [
|
|
|
|
{
|
|
|
|
name = "Prometheus";
|
|
|
|
type = "prometheus";
|
|
|
|
url = "http://localhost:${toString config.services.prometheus.port}";
|
2021-07-15 10:14:03 +02:00
|
|
|
jsonData = {
|
|
|
|
timeInterval = cfg.scrapeInterval;
|
|
|
|
};
|
2021-01-27 21:49:03 +01:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
dashboards = [
|
|
|
|
{
|
2021-07-13 17:13:07 +02:00
|
|
|
name = "Node Exporter";
|
2021-07-14 21:09:26 +02:00
|
|
|
options.path = pkgs.packages.grafanaDashboards.node-exporter;
|
2021-07-13 17:13:07 +02:00
|
|
|
disableDeletion = true;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "NGINX";
|
2021-07-14 21:09:26 +02:00
|
|
|
options.path = pkgs.packages.grafanaDashboards.nginx;
|
2021-01-27 21:49:03 +01:00
|
|
|
disableDeletion = true;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.prometheus = {
|
|
|
|
enable = true;
|
|
|
|
port = 9090;
|
|
|
|
listenAddress = "127.0.0.1";
|
|
|
|
|
2021-07-12 22:12:03 +02:00
|
|
|
retentionTime = "2y";
|
|
|
|
|
2021-01-27 21:49:03 +01:00
|
|
|
exporters = {
|
|
|
|
node = {
|
|
|
|
enable = true;
|
2022-04-10 11:54:58 +02:00
|
|
|
enabledCollectors = ["systemd"];
|
2021-01-27 21:49:03 +01:00
|
|
|
port = 9100;
|
2021-07-13 14:17:34 +02:00
|
|
|
listenAddress = "127.0.0.1";
|
2021-01-27 21:49:03 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-07-15 10:14:03 +02:00
|
|
|
globalConfig = {
|
|
|
|
scrape_interval = cfg.scrapeInterval;
|
|
|
|
};
|
|
|
|
|
2021-01-27 21:49:03 +01:00
|
|
|
scrapeConfigs = [
|
|
|
|
{
|
|
|
|
job_name = config.networking.hostName;
|
2022-04-10 11:54:58 +02:00
|
|
|
static_configs = [
|
|
|
|
{
|
|
|
|
targets = ["127.0.0.1:${toString config.services.prometheus.exporters.node.port}"];
|
|
|
|
}
|
|
|
|
];
|
2021-01-27 21:49:03 +01:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
services.nginx = {
|
2022-06-12 17:18:58 +02:00
|
|
|
virtualHosts.${cfg.domain} = {
|
2021-01-27 21:49:03 +01:00
|
|
|
locations."/" = {
|
|
|
|
proxyPass = "http://127.0.0.1:${toString config.services.grafana.port}";
|
|
|
|
proxyWebsockets = true;
|
|
|
|
};
|
|
|
|
|
2021-07-13 00:57:33 +02:00
|
|
|
forceSSL = true;
|
2022-06-12 17:18:58 +02:00
|
|
|
useACMEHost = fqdn;
|
2021-01-27 21:49:03 +01:00
|
|
|
};
|
|
|
|
};
|
2022-06-12 17:18:58 +02:00
|
|
|
|
|
|
|
security.acme.certs.${fqdn}.extraDomainNames = [cfg.domain];
|
2021-01-27 21:49:03 +01:00
|
|
|
};
|
|
|
|
}
|