dotfiles/modules/nixos/services/victoriametrics.nix

110 lines
3.2 KiB
Nix
Raw Normal View History

2024-01-10 04:11:11 +00:00
# VictoriaMetrics is a more efficient drop-in replacement for Prometheus and
# InfluxDB (timeseries databases built for monitoring system metrics).
2024-04-20 13:42:06 +00:00
{
config,
pkgs,
lib,
...
}:
2023-07-16 13:50:58 +00:00
let
username = "prometheus";
prometheusConfig = (pkgs.formats.yaml { }).generate "prometheus.yml" {
2024-04-20 13:42:06 +00:00
scrape_configs = [
{
job_name = config.networking.hostName;
stream_parse = true;
static_configs = [ { targets = config.prometheus.scrapeTargets; } ];
}
];
2023-07-16 13:50:58 +00:00
};
authConfig = (pkgs.formats.yaml { }).generate "auth.yml" {
2024-04-20 13:42:06 +00:00
users = [
{
username = username;
password = "%{PASSWORD}";
url_prefix = "http://localhost${config.services.victoriametrics.listenAddress}";
}
];
2023-07-16 13:50:58 +00:00
};
authPort = "8427";
2024-04-20 13:42:06 +00:00
in
{
2023-07-16 13:50:58 +00:00
config = {
2024-04-20 13:42:06 +00:00
services.victoriametrics.extraOptions = [ "-promscrape.config=${prometheusConfig}" ];
2023-07-16 13:50:58 +00:00
systemd.services.vmauth = lib.mkIf config.services.victoriametrics.enable {
description = "VictoriaMetrics basic auth proxy";
after = [ "network.target" ];
startLimitBurst = 5;
serviceConfig = {
Restart = "on-failure";
RestartSec = 1;
DynamicUser = true;
EnvironmentFile = config.secrets.vmauth.dest;
ExecStart = ''
${pkgs.victoriametrics}/bin/vmauth \
-auth.config=${authConfig} \
-httpListenAddr=:${authPort}'';
};
wantedBy = [ "multi-user.target" ];
};
secrets.vmauth = lib.mkIf config.services.victoriametrics.enable {
source = ../../../private/prometheus.age;
dest = "${config.secretsDirectory}/vmauth";
prefix = "PASSWORD=";
};
2024-04-20 13:42:06 +00:00
systemd.services.vmauth-secret = lib.mkIf config.services.victoriametrics.enable {
requiredBy = [ "vmauth.service" ];
before = [ "vmauth.service" ];
};
2023-07-16 13:50:58 +00:00
2024-04-20 13:42:06 +00:00
caddy.routes = lib.mkIf config.services.victoriametrics.enable [
{
match = [ { host = [ config.hostnames.prometheus ]; } ];
handle = [
{
handler = "reverse_proxy";
upstreams = [ { dial = "localhost:${authPort}"; } ];
}
];
}
];
2023-07-16 13:50:58 +00:00
# Configure Cloudflare DNS to point to this machine
services.cloudflare-dyndns.domains =
2024-04-20 13:42:06 +00:00
if config.services.victoriametrics.enable then [ config.hostnames.prometheus ] else [ ];
2023-07-16 13:50:58 +00:00
# VMAgent
services.vmagent.prometheusConfig = prometheusConfig; # Overwritten below
2024-04-20 13:42:06 +00:00
systemd.services.vmagent.serviceConfig = lib.mkIf config.services.vmagent.enable {
ExecStart = lib.mkForce ''
${pkgs.victoriametrics}/bin/vmagent \
-promscrape.config=${prometheusConfig} \
-remoteWrite.url="https://${config.hostnames.prometheus}/api/v1/write" \
-remoteWrite.basicAuth.username=${username} \
-remoteWrite.basicAuth.passwordFile=${config.secrets.vmagent.dest}'';
};
2023-07-16 13:50:58 +00:00
secrets.vmagent = lib.mkIf config.services.vmagent.enable {
source = ../../../private/prometheus.age;
dest = "${config.secretsDirectory}/vmagent";
owner = "vmagent";
group = "vmagent";
};
systemd.services.vmagent-secret = lib.mkIf config.services.vmagent.enable {
requiredBy = [ "vmagent.service" ];
before = [ "vmagent.service" ];
};
};
}