modules/prometheus: refactor
This commit is contained in:
parent
ebf69d94dd
commit
ec63c8b6cc
26 changed files with 579 additions and 407 deletions
|
@ -1,93 +0,0 @@
|
|||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.custom.prometheus;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (cfg.enable && cfg.exporters.blackbox.enable) {
|
||||
services.prometheus.exporters.blackbox = {
|
||||
enable = true;
|
||||
listenAddress = "127.0.0.1";
|
||||
configFile = pkgs.writeText "blackbox.config.yaml" (
|
||||
lib.generators.toYAML { } {
|
||||
modules = {
|
||||
tcp4_connect = {
|
||||
prober = "tcp";
|
||||
tcp = {
|
||||
ip_protocol_fallback = false;
|
||||
preferred_ip_protocol = "ip4";
|
||||
tls = false;
|
||||
};
|
||||
timeout = "15s";
|
||||
};
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
services.prometheus.scrapeConfigs = [
|
||||
{
|
||||
job_name = "blackbox";
|
||||
scrape_interval = "1m";
|
||||
metrics_path = "/probe";
|
||||
params = {
|
||||
module = [ "tcp4_connect" ];
|
||||
};
|
||||
static_configs = [
|
||||
{
|
||||
targets = [
|
||||
"tok-00.namely.icu:8080"
|
||||
"la-00.video.namely.icu:8080"
|
||||
"auth.xinyang.life:443"
|
||||
"home.xinyang.life:8000"
|
||||
];
|
||||
}
|
||||
];
|
||||
relabel_configs = [
|
||||
{
|
||||
source_labels = [ "__address__" ];
|
||||
target_label = "__param_target";
|
||||
}
|
||||
{
|
||||
source_labels = [ "__param_target" ];
|
||||
target_label = "instance";
|
||||
}
|
||||
{
|
||||
target_label = "__address__";
|
||||
replacement = "127.0.0.1:${toString config.services.prometheus.exporters.blackbox.port}";
|
||||
}
|
||||
];
|
||||
}
|
||||
{
|
||||
job_name = "blackbox_exporter";
|
||||
static_configs = [
|
||||
{ targets = [ "127.0.0.1:${toString config.services.prometheus.exporters.blackbox.port}" ]; }
|
||||
];
|
||||
}
|
||||
];
|
||||
|
||||
custom.prometheus.ruleModules = [
|
||||
{
|
||||
name = "probe_alerts";
|
||||
rules = [
|
||||
{
|
||||
alert = "HighProbeLatency";
|
||||
expr = "probe_duration_seconds > 0.5";
|
||||
for = "2m";
|
||||
labels = {
|
||||
severity = "warning";
|
||||
};
|
||||
annotations = {
|
||||
summary = "High request latency on {{ $labels.instance }}";
|
||||
description = "95th percentile of request latency is above 0.5 seconds for the last 2 minutes.";
|
||||
};
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.custom.prometheus;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (cfg.enable && cfg.exporters.caddy.enable) {
|
||||
services.caddy.globalConfig = lib.mkIf cfg.exporters.caddy.enable ''
|
||||
servers {
|
||||
metrics
|
||||
}
|
||||
'';
|
||||
|
||||
services.prometheus.scrapeConfigs = [
|
||||
{
|
||||
job_name = "caddy";
|
||||
static_configs = [ { targets = [ "127.0.0.1:2019" ]; } ];
|
||||
}
|
||||
];
|
||||
|
||||
custom.prometheus.ruleModules = [
|
||||
{
|
||||
name = "caddy_alerts";
|
||||
rules = [
|
||||
{
|
||||
alert = "UpstreamHealthy";
|
||||
expr = "caddy_reverse_proxy_upstreams_healthy != 1";
|
||||
for = "5m";
|
||||
labels = {
|
||||
severity = "critical";
|
||||
};
|
||||
annotations = {
|
||||
summary = "Upstream {{ $labels.unstream }} not healthy";
|
||||
};
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
}
|
|
@ -1,21 +1,16 @@
|
|||
{
|
||||
config,
|
||||
pkgs,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
inherit (lib)
|
||||
mkEnableOption
|
||||
mkOption
|
||||
mkIf
|
||||
types
|
||||
;
|
||||
cfg = config.custom.prometheus;
|
||||
mkExporterOption =
|
||||
enableOption:
|
||||
(mkOption {
|
||||
type = types.bool;
|
||||
default = enableOption;
|
||||
description = "Enable this exporter";
|
||||
});
|
||||
|
||||
mkRulesOption = mkOption {
|
||||
type = types.listOf (
|
||||
|
@ -30,38 +25,36 @@ let
|
|||
in
|
||||
{
|
||||
imports = [
|
||||
./blackbox.nix
|
||||
./caddy.nix
|
||||
./gotosocial.nix
|
||||
./immich.nix
|
||||
./miniflux.nix
|
||||
./ntfy-sh.nix
|
||||
./restic.nix
|
||||
./exporters.nix
|
||||
./grafana.nix
|
||||
];
|
||||
|
||||
options = {
|
||||
custom.monitoring = {
|
||||
grafana = {
|
||||
enable = mkEnableOption "grafana with oauth only";
|
||||
};
|
||||
};
|
||||
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;
|
||||
blackbox.enable = mkExporterOption false;
|
||||
caddy.enable = mkExporterOption config.services.caddy.enable;
|
||||
gotosocial.enable = mkExporterOption config.services.gotosocial.enable;
|
||||
immich.enable = mkExporterOption config.services.immich.enable;
|
||||
miniflux.enable = mkExporterOption config.services.miniflux.enable;
|
||||
ntfy-sh.enable = mkExporterOption config.services.ntfy-sh.enable;
|
||||
};
|
||||
grafana = {
|
||||
enable = mkEnableOption "Grafana Cloud";
|
||||
password_file = mkOption { type = types.path; };
|
||||
};
|
||||
ruleModules = mkRulesOption;
|
||||
exporters = {
|
||||
enable = mkEnableOption "prometheus exporter on all supported and enable guarded services";
|
||||
node = {
|
||||
enable = mkEnableOption "node exporter";
|
||||
listenAddress = mkOption {
|
||||
type = types.str;
|
||||
default = "${config.networking.hostName}.coho-tet.ts.net";
|
||||
};
|
||||
};
|
||||
blackbox = {
|
||||
enable = mkEnableOption "blackbox exporter";
|
||||
listenAddress = mkOption {
|
||||
type = types.str;
|
||||
default = "${config.networking.hostName}.coho-tet.ts.net";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -78,46 +71,18 @@ in
|
|||
reverse_proxy 127.0.0.1:${toString config.services.prometheus.port}
|
||||
'';
|
||||
};
|
||||
|
||||
services.prometheus = mkIf cfg.enable {
|
||||
enable = true;
|
||||
port = 9091;
|
||||
globalConfig.external_labels = {
|
||||
hostname = config.networking.hostName;
|
||||
};
|
||||
remoteWrite = mkIf cfg.grafana.enable [
|
||||
{
|
||||
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 = {
|
||||
enable = true;
|
||||
enabledCollectors = [
|
||||
"loadavg"
|
||||
"time"
|
||||
"systemd"
|
||||
];
|
||||
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}" ]; }
|
||||
];
|
||||
}
|
||||
];
|
||||
|
||||
alertmanager = {
|
||||
|
@ -163,55 +128,8 @@ in
|
|||
};
|
||||
custom.prometheus.ruleModules = [
|
||||
{
|
||||
name = "system_alerts";
|
||||
name = "prometheus_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";
|
||||
|
|
65
modules/nixos/prometheus/exporters.nix
Normal file
65
modules/nixos/prometheus/exporters.nix
Normal file
|
@ -0,0 +1,65 @@
|
|||
{
|
||||
config,
|
||||
pkgs,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
inherit (lib) mkIf;
|
||||
cfg = config.custom.prometheus.exporters;
|
||||
in
|
||||
{
|
||||
config = {
|
||||
services.prometheus.exporters.node = mkIf cfg.node.enable {
|
||||
enable = true;
|
||||
enabledCollectors = [
|
||||
"loadavg"
|
||||
"time"
|
||||
"systemd"
|
||||
];
|
||||
listenAddress = cfg.node.listenAddress;
|
||||
port = 9100;
|
||||
};
|
||||
|
||||
services.prometheus.exporters.blackbox = mkIf cfg.blackbox.enable {
|
||||
enable = true;
|
||||
listenAddress = cfg.blackbox.listenAddress;
|
||||
configFile = pkgs.writeText "blackbox.config.yaml" (
|
||||
lib.generators.toYAML { } {
|
||||
modules = {
|
||||
tcp4_connect = {
|
||||
prober = "tcp";
|
||||
tcp = {
|
||||
ip_protocol_fallback = false;
|
||||
preferred_ip_protocol = "ip4";
|
||||
tls = false;
|
||||
};
|
||||
timeout = "15s";
|
||||
};
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
services.gotosocial.settings = {
|
||||
metrics-enabled = true;
|
||||
};
|
||||
|
||||
services.immich.environment = {
|
||||
IMMICH_TELEMETRY_INCLUDE = "all";
|
||||
};
|
||||
|
||||
services.restic.server.prometheus = true;
|
||||
systemd.services.miniflux.environment.METRICS_COLLECTOR = "1";
|
||||
services.ntfy-sh.settings.enable-metrics = true;
|
||||
|
||||
services.caddy.globalConfig = ''
|
||||
servers {
|
||||
metrics
|
||||
}
|
||||
|
||||
admin ${config.networking.hostName}.coho-tet.ts.net:2019 {
|
||||
}
|
||||
'';
|
||||
};
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.custom.prometheus;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (cfg.enable && cfg.exporters.gotosocial.enable) {
|
||||
services.gotosocial.settings = {
|
||||
metrics-enabled = true;
|
||||
};
|
||||
services.prometheus.scrapeConfigs = [
|
||||
{
|
||||
job_name = "gotosocial";
|
||||
static_configs = [ { targets = [ "localhost:8080" ]; } ];
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
43
modules/nixos/prometheus/grafana.nix
Normal file
43
modules/nixos/prometheus/grafana.nix
Normal file
|
@ -0,0 +1,43 @@
|
|||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.custom.monitoring.grafana;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf cfg.enable {
|
||||
sops.templates."grafana.env".content = ''
|
||||
GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET=${config.sops.placeholder."grafana/oauth_secret"}
|
||||
'';
|
||||
services.grafana = {
|
||||
enable = true;
|
||||
settings = {
|
||||
server = {
|
||||
http_addr = "127.0.0.1";
|
||||
http_port = 3003;
|
||||
root_url = "https://grafana.xinyang.life";
|
||||
domain = "grafana.xinyang.life";
|
||||
};
|
||||
"auth.generic_oauth" = {
|
||||
enabled = true;
|
||||
name = "Kanidm";
|
||||
client_id = "grafana";
|
||||
scopes = "openid,profile,email,groups";
|
||||
auth_url = "https://auth.xinyang.life/ui/oauth2";
|
||||
token_url = "https://auth.xinyang.life/oauth2/token";
|
||||
api_url = "https://auth.xinyang.life/oauth2/openid/grafana/userinfo";
|
||||
use_pkce = true;
|
||||
use_refresh_token = true;
|
||||
allow_sign_up = true;
|
||||
login_attribute_path = "preferred_username";
|
||||
groups_attribute_path = "groups";
|
||||
role_attribute_path = "contains(grafana_role[*], 'GrafanaAdmin') && 'GrafanaAdmin' || contains(grafana_role[*], 'Admin') && 'Admin' || contains(grafana_role[*], 'Editor') && 'Editor' || 'Viewer'";
|
||||
allow_assign_grafana_admin = true;
|
||||
auto_login = true;
|
||||
};
|
||||
"auth" = {
|
||||
disable_login_form = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
systemd.services.grafana.serviceConfig.EnvironmentFile = config.sops.templates."grafana.env".path;
|
||||
};
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.custom.prometheus;
|
||||
immichEnv = config.services.immich.environment;
|
||||
metricPort =
|
||||
if builtins.hasAttr "IMMICH_API_METRICS_PORT" immichEnv then
|
||||
immichEnv.IMMICH_API_METRICS_PORT
|
||||
else
|
||||
8081;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (cfg.enable && cfg.exporters.immich.enable) {
|
||||
services.immich.environment = {
|
||||
IMMICH_METRICS = "true";
|
||||
};
|
||||
|
||||
services.prometheus.scrapeConfigs = [
|
||||
{
|
||||
job_name = "immich";
|
||||
static_configs = [ { targets = [ "127.0.0.1:${toString metricPort}" ]; } ];
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.custom.prometheus;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (cfg.enable && cfg.exporters.miniflux.enable) {
|
||||
systemd.services.miniflux.environment.METRICS_COLLECTOR = "1";
|
||||
services.prometheus.scrapeConfigs = [
|
||||
{
|
||||
job_name = "miniflux";
|
||||
static_configs = [ { targets = [ config.systemd.services.miniflux.environment.LISTEN_ADDR ]; } ];
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.custom.prometheus;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (cfg.enable && cfg.exporters.ntfy-sh.enable) {
|
||||
services.ntfy-sh.settings.enable-metrics = true;
|
||||
services.prometheus.scrapeConfigs = [
|
||||
{
|
||||
job_name = "ntfy-sh";
|
||||
static_configs = [ { targets = [ "ntfy.xinyang.life" ]; } ];
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
|
@ -3,17 +3,26 @@ let
|
|||
cfg = config.custom.prometheus;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (cfg.enable && cfg.exporters.restic.enable) {
|
||||
config = {
|
||||
services.restic.server.prometheus = true;
|
||||
|
||||
services.prometheus.scrapeConfigs = [
|
||||
(lib.mkIf cfg.exporters.restic.enable {
|
||||
job_name = "restic";
|
||||
static_configs = [ { targets = [ config.services.restic.server.listenAddress ]; } ];
|
||||
})
|
||||
];
|
||||
custom.prometheus.templates.scrape.mkResticScrapes =
|
||||
{
|
||||
address,
|
||||
port ? null,
|
||||
...
|
||||
}:
|
||||
let
|
||||
portStr = if port then ":${toString port}" else "";
|
||||
in
|
||||
[
|
||||
(lib.mkIf cfg.exporters.restic.enable {
|
||||
job_name = "restic";
|
||||
static_configs = [ { targets = [ "${address}${portStr}" ]; } ];
|
||||
})
|
||||
];
|
||||
|
||||
custom.prometheus.ruleModules = [
|
||||
custom.prometheus.templates.rules.mkResticRules = [
|
||||
{
|
||||
name = "restic_alerts";
|
||||
rules = [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue