2024-08-25 09:45:58 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
2024-07-31 06:27:03 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.custom.prometheus;
|
2024-08-25 09:45:58 +00:00
|
|
|
mkExporterOption =
|
|
|
|
enableOption:
|
|
|
|
(mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = enableOption;
|
|
|
|
description = "Enable this exporter";
|
|
|
|
});
|
2024-07-31 06:27:03 +00:00
|
|
|
|
|
|
|
mkRulesOption = mkOption {
|
2024-08-25 09:45:58 +00:00
|
|
|
type = types.listOf (
|
|
|
|
types.submodule {
|
|
|
|
options = {
|
|
|
|
name = mkOption { type = lib.types.str; };
|
|
|
|
rules = mkOption { type = lib.types.listOf lib.types.attrs; };
|
2024-07-31 06:27:03 +00:00
|
|
|
};
|
2024-08-25 09:45:58 +00:00
|
|
|
}
|
|
|
|
);
|
2024-07-31 06:27:03 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
imports = [
|
2024-07-31 07:37:42 +00:00
|
|
|
./blackbox.nix
|
2024-07-31 06:27:03 +00:00
|
|
|
./caddy.nix
|
|
|
|
./gotosocial.nix
|
2024-08-01 09:01:53 +00:00
|
|
|
./immich.nix
|
2024-08-05 12:04:10 +00:00
|
|
|
./miniflux.nix
|
2024-07-31 06:27:03 +00:00
|
|
|
./ntfy-sh.nix
|
|
|
|
./restic.nix
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
custom.prometheus = {
|
|
|
|
enable = mkEnableOption "Prometheus instance";
|
|
|
|
exporters = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Enable Prometheus exporter on every supported services";
|
|
|
|
};
|
|
|
|
|
|
|
|
restic.enable = mkExporterOption config.services.restic.server.enable;
|
2024-07-31 07:37:42 +00:00
|
|
|
blackbox.enable = mkExporterOption false;
|
2024-07-31 06:27:03 +00:00
|
|
|
caddy.enable = mkExporterOption config.services.caddy.enable;
|
|
|
|
gotosocial.enable = mkExporterOption config.services.gotosocial.enable;
|
2024-08-01 09:01:53 +00:00
|
|
|
immich.enable = mkExporterOption config.services.immich.enable;
|
2024-08-05 12:04:10 +00:00
|
|
|
miniflux.enable = mkExporterOption config.services.miniflux.enable;
|
|
|
|
ntfy-sh.enable = mkExporterOption config.services.ntfy-sh.enable;
|
2024-07-31 06:27:03 +00:00
|
|
|
};
|
|
|
|
grafana = {
|
|
|
|
enable = mkEnableOption "Grafana Cloud";
|
2024-08-25 09:45:58 +00:00
|
|
|
password_file = mkOption { type = types.path; };
|
2024-07-31 06:27:03 +00:00
|
|
|
};
|
|
|
|
ruleModules = mkRulesOption;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-25 09:45:58 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
services.tailscale = {
|
|
|
|
enable = true;
|
|
|
|
permitCertUid = config.services.caddy.user;
|
|
|
|
openFirewall = true;
|
|
|
|
};
|
2024-07-31 06:27:03 +00:00
|
|
|
|
2024-08-25 09:45:58 +00:00
|
|
|
services.caddy = {
|
|
|
|
enable = true;
|
|
|
|
virtualHosts."${config.networking.hostName}.coho-tet.ts.net".extraConfig = ''
|
|
|
|
reverse_proxy 127.0.0.1:${toString config.services.prometheus.port}
|
|
|
|
'';
|
|
|
|
};
|
2024-07-31 06:27:03 +00:00
|
|
|
|
2024-08-25 09:45:58 +00:00
|
|
|
services.prometheus = mkIf cfg.enable {
|
|
|
|
enable = true;
|
|
|
|
port = 9091;
|
|
|
|
globalConfig.external_labels = {
|
|
|
|
hostname = config.networking.hostName;
|
|
|
|
};
|
|
|
|
remoteWrite = mkIf cfg.grafana.enable [
|
2024-07-31 06:27:03 +00:00
|
|
|
{
|
2024-08-25 09:45:58 +00:00
|
|
|
name = "grafana";
|
|
|
|
url = "https://prometheus-prod-24-prod-eu-west-2.grafana.net/api/prom/push";
|
|
|
|
basic_auth = {
|
|
|
|
username = "1340065";
|
|
|
|
password_file = cfg.grafana.password_file;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
exporters = {
|
|
|
|
node = {
|
2024-07-31 06:27:03 +00:00
|
|
|
enable = true;
|
2024-08-25 09:45:58 +00:00
|
|
|
enabledCollectors = [
|
|
|
|
"loadavg"
|
|
|
|
"time"
|
|
|
|
"systemd"
|
2024-07-31 06:27:03 +00:00
|
|
|
];
|
2024-08-25 09:45:58 +00:00
|
|
|
listenAddress = "127.0.0.1";
|
|
|
|
port = 9100;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
scrapeConfigs = [
|
|
|
|
{
|
|
|
|
job_name = "prometheus";
|
|
|
|
static_configs = [ { targets = [ "localhost:${toString config.services.prometheus.port}" ]; } ];
|
|
|
|
}
|
|
|
|
{
|
|
|
|
job_name = "node";
|
|
|
|
static_configs = [
|
|
|
|
{ targets = [ "localhost:${toString config.services.prometheus.exporters.node.port}" ]; }
|
2024-07-31 06:27:03 +00:00
|
|
|
];
|
2024-08-25 09:45:58 +00:00
|
|
|
}
|
|
|
|
];
|
2024-07-31 06:27:03 +00:00
|
|
|
|
2024-08-25 09:45:58 +00:00
|
|
|
alertmanager = {
|
|
|
|
enable = true;
|
|
|
|
listenAddress = "127.0.0.1";
|
|
|
|
logLevel = "debug";
|
|
|
|
configuration = {
|
|
|
|
route = {
|
|
|
|
receiver = "ntfy";
|
2024-07-31 06:27:03 +00:00
|
|
|
};
|
2024-08-25 09:45:58 +00:00
|
|
|
receivers = [
|
2024-07-31 06:27:03 +00:00
|
|
|
{
|
2024-08-25 09:45:58 +00:00
|
|
|
name = "ntfy";
|
|
|
|
webhook_configs = [
|
2024-07-31 06:27:03 +00:00
|
|
|
{
|
2024-08-25 09:45:58 +00:00
|
|
|
url = "https://ntfy.xinyang.life/prometheus-alerts?tpl=yes&m=${lib.escapeURL ''
|
|
|
|
Alert {{.status}}
|
|
|
|
{{range .alerts}}-----{{range $k,$v := .labels}}
|
|
|
|
{{$k}}={{$v}}{{end}}
|
|
|
|
{{end}}
|
|
|
|
''}";
|
|
|
|
send_resolved = true;
|
2024-07-31 06:27:03 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2024-08-25 09:45:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
alertmanagers = [
|
2024-07-31 06:27:03 +00:00
|
|
|
{
|
2024-08-25 09:45:58 +00:00
|
|
|
scheme = "http";
|
|
|
|
static_configs = [
|
2024-07-31 06:27:03 +00:00
|
|
|
{
|
2024-08-25 09:45:58 +00:00
|
|
|
targets = [
|
|
|
|
"${config.services.prometheus.alertmanager.listenAddress}:${toString config.services.prometheus.alertmanager.port}"
|
|
|
|
];
|
2024-07-31 07:37:42 +00:00
|
|
|
}
|
2024-07-31 06:27:03 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
2024-08-25 09:45:58 +00:00
|
|
|
rules = [ (lib.generators.toYAML { } { groups = cfg.ruleModules; }) ];
|
2024-07-31 06:27:03 +00:00
|
|
|
};
|
2024-08-25 09:45:58 +00:00
|
|
|
custom.prometheus.ruleModules = [
|
|
|
|
{
|
|
|
|
name = "system_alerts";
|
|
|
|
rules = [
|
|
|
|
{
|
|
|
|
alert = "SystemdFailedUnits";
|
|
|
|
expr = "node_systemd_unit_state{state=\"failed\"} > 0";
|
|
|
|
for = "5m";
|
|
|
|
labels = {
|
|
|
|
severity = "critical";
|
|
|
|
};
|
|
|
|
annotations = {
|
|
|
|
summary = "Systemd has failed units on {{ $labels.instance }}";
|
|
|
|
description = "There are {{ $value }} failed units on {{ $labels.instance }}. Immediate attention required!";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
alert = "HighLoadAverage";
|
|
|
|
expr = "node_load1 > 0.8 * count without (cpu) (node_cpu_seconds_total{mode=\"idle\"})";
|
|
|
|
for = "1m";
|
|
|
|
labels = {
|
|
|
|
severity = "warning";
|
|
|
|
};
|
|
|
|
annotations = {
|
|
|
|
summary = "High load average detected on {{ $labels.instance }}";
|
|
|
|
description = "The 1-minute load average ({{ $value }}) exceeds 80% the number of CPUs.";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
alert = "HighTransmitTraffic";
|
|
|
|
expr = "rate(node_network_transmit_bytes_total{device!=\"lo\"}[5m]) > 100000000";
|
|
|
|
for = "1m";
|
|
|
|
labels = {
|
|
|
|
severity = "warning";
|
|
|
|
};
|
|
|
|
annotations = {
|
|
|
|
summary = "High network transmit traffic on {{ $labels.instance }} ({{ $labels.device }})";
|
|
|
|
description = "The network interface {{ $labels.device }} on {{ $labels.instance }} is transmitting data at a rate exceeding 100 MB/s for the last 1 minute.";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
alert = "NetworkTrafficExceedLimit";
|
|
|
|
expr = ''increase(node_network_transmit_bytes_total{device!="lo",device!~"tailscale.*",device!~"wg.*",device!~"br.*"}[30d]) > 322122547200'';
|
|
|
|
for = "0m";
|
|
|
|
labels = {
|
|
|
|
severity = "critical";
|
|
|
|
};
|
|
|
|
annotations = {
|
|
|
|
summary = "Outbound network traffic exceed 300GB for last 30 day";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
alert = "JobDown";
|
|
|
|
expr = "up == 0";
|
|
|
|
for = "1m";
|
|
|
|
labels = {
|
|
|
|
severity = "critical";
|
|
|
|
};
|
|
|
|
annotations = {
|
|
|
|
summary = "Job {{ $labels.job }} down for 1m.";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2024-07-31 06:27:03 +00:00
|
|
|
}
|