nix-config/modules/services/monitoring.nix
Bruno BELANYI 77cf3430ae modules: services: use new nginx wrapper
And when not possible, document why.

Note for the future: there is some repetition in some modules to
configure the correct value of the subdomain, which I happen to know
will line up correctly thanks to the nginx wrapper. A good way to
refactor this in the future would involve avoiding this repetition,
allowing use to query the correct domain in some way...
2021-08-26 15:54:13 +02:00

123 lines
2.7 KiB
Nix

# Grafana dashboards for all the things!
{ config, lib, pkgs, ... }:
let
cfg = config.my.services.monitoring;
in
{
options.my.services.monitoring = with lib; {
enable = mkEnableOption "monitoring";
grafana = {
port = mkOption {
type = types.port;
default = 9500;
example = 3001;
description = "Internal port";
};
username = mkOption {
type = types.str;
default = "ambroisie";
example = "admin";
description = "Admin username";
};
passwordFile = mkOption {
type = types.str;
example = "/var/lib/grafana/password.txt";
description = "Admin password stored in a file";
};
};
prometheus = {
port = mkOption {
type = types.port;
default = 9501;
example = 3002;
description = "Internal port";
};
scrapeInterval = mkOption {
type = types.str;
default = "15s";
example = "1m";
description = "Scrape interval";
};
};
};
config = lib.mkIf cfg.enable {
services.grafana = {
enable = true;
domain = "monitoring.${config.networking.domain}";
port = cfg.grafana.port;
addr = "127.0.0.1"; # Proxied through Nginx
security = {
adminUser = cfg.grafana.username;
adminPasswordFile = cfg.grafana.passwordFile;
};
provision = {
enable = true;
datasources = [
{
name = "Prometheus";
type = "prometheus";
url = "http://localhost:${toString cfg.prometheus.port}";
jsonData = {
timeInterval = cfg.prometheus.scrapeInterval;
};
}
];
dashboards = [
{
name = "Node Exporter";
options.path = pkgs.nur.repos.alarsyo.grafanaDashboards.node-exporter;
disableDeletion = true;
}
];
};
};
services.prometheus = {
enable = true;
port = cfg.prometheus.port;
listenAddress = "127.0.0.1";
retentionTime = "2y";
exporters = {
node = {
enable = true;
enabledCollectors = [ "systemd" ];
port = 9100;
listenAddress = "127.0.0.1";
};
};
globalConfig = {
scrape_interval = cfg.prometheus.scrapeInterval;
};
scrapeConfigs = [
{
job_name = config.networking.hostName;
static_configs = [{
targets = [ "127.0.0.1:${toString config.services.prometheus.exporters.node.port}" ];
}];
}
];
};
my.services.nginx.virtualHosts = [
{
subdomain = "monitoring";
inherit (cfg.grafana) port;
}
];
};
}