summaryrefslogtreecommitdiff
path: root/nixos
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2025-08-30 19:27:25 +0200
committerMartin Fischer <martin@push-f.com>2025-08-30 20:03:15 +0200
commit80a98992584487a389900ea9e78963c01ea3d812 (patch)
treee5b254c6d7956e2d181375847904766b2a3af586 /nixos
parent0c1ab2a61849a92f8df4170b934a3cdc68ec98da (diff)
refactor: make Prometheus & Loki config reusable
Diffstat (limited to 'nixos')
-rw-r--r--nixos/hosts/tente/default.nix13
-rw-r--r--nixos/hosts/tente/exporters.nix55
-rw-r--r--nixos/hosts/tente/grafana.nix63
-rw-r--r--nixos/shared/monitoring.nix (renamed from nixos/hosts/tente/monitoring.nix)94
4 files changed, 130 insertions, 95 deletions
diff --git a/nixos/hosts/tente/default.nix b/nixos/hosts/tente/default.nix
index e63b9bc..b9ac389 100644
--- a/nixos/hosts/tente/default.nix
+++ b/nixos/hosts/tente/default.nix
@@ -18,11 +18,13 @@ rec {
<top/profiles/server>
<top/shared/postgresql.nix>
<top/shared/tailscale.nix>
+ <top/shared/monitoring.nix>
./web-personal.nix
./git.nix
./headscale.nix
./matrix.nix
- ./monitoring.nix
+ ./exporters.nix
+ ./grafana.nix
"${sources.my-lex-surf}/service.nix"
"${sources.my-osm-proposals}/service.nix"
"${sources.my-geopos-link}/service.nix"
@@ -115,17 +117,16 @@ rec {
};
};
- monitoring.grafanaUiPort = 3000;
+ grafana.port = 3000;
+ grafana.matrixForwarderPort = 3002;
+ grafana.matrixServerUrl = "http://localhost:${toString matrix.port}";
monitoring.alloyUiPort = 3001;
- monitoring.grafanaMatrixForwarderPort = 3002;
monitoring.lokiPort = 3030;
monitoring.prometheusNodeExporterPort = 9002;
- monitoring.prometheusSqlExporterPort = 9003;
+ exporters.sqlExporterPort = 9003;
headscale.port = 8080;
matrix.port = 8008;
- monitoring.matrixServerUrl = "http://localhost:${toString matrix.port}";
-
# Use the GRUB 2 boot loader.
boot.loader.grub.enable = true;
# boot.loader.grub.efiSupport = true;
diff --git a/nixos/hosts/tente/exporters.nix b/nixos/hosts/tente/exporters.nix
new file mode 100644
index 0000000..1ed53e5
--- /dev/null
+++ b/nixos/hosts/tente/exporters.nix
@@ -0,0 +1,55 @@
+{ config, lib, pkgs, ... }:
+
+let
+ cfg = config.exporters;
+in
+{
+ options.exporters = {
+ sqlExporterPort = lib.mkOption {
+ type = lib.types.int;
+ };
+ };
+
+ imports = [
+ <top/shared/prometheus-sql-exporter/service.nix>
+ ];
+
+ config = {
+ services.prometheus-sql-exporter = {
+ enable = true;
+ port = cfg.sqlExporterPort;
+ config = {
+ target = {
+ # This URL should be postgresql:///postgres?host=/run/postgresql
+ # but sql_exporter uses xo/dburl which isn't spec-compliant: https://github.com/xo/dburl/issues/46
+ data_source_name = "postgresql:/run/postgresql:/postgres";
+ collectors = ["db-sizes"];
+ };
+ collectors = [
+ {
+ collector_name = "db-sizes";
+ metrics = [
+ {
+ metric_name = "pg_db_size_bytes";
+ help = "disk space used by the database";
+ type = "gauge";
+ key_labels = ["database_name"];
+ values = ["size"];
+ query = "SELECT datname AS database_name, pg_database_size(datname) as size from pg_database";
+ }
+ ];
+ }
+ ];
+ };
+ };
+
+ monitoring.prometheusScrapeConfigs = [
+ {
+ job_name = "sql";
+ static_configs = [{
+ targets = [ "localhost:${toString cfg.sqlExporterPort}" ];
+ }];
+ }
+ ];
+ };
+}
diff --git a/nixos/hosts/tente/grafana.nix b/nixos/hosts/tente/grafana.nix
new file mode 100644
index 0000000..e6fb7af
--- /dev/null
+++ b/nixos/hosts/tente/grafana.nix
@@ -0,0 +1,63 @@
+{ config, lib, pkgs, ... }:
+
+let
+ cfg = config.grafana;
+in
+{
+ options.grafana = {
+ port = lib.mkOption {
+ type = lib.types.int;
+ };
+ matrixForwarderPort = lib.mkOption {
+ type = lib.types.int;
+ };
+ prometheusSqlExporterPort = lib.mkOption {
+ type = lib.types.int;
+ };
+ matrixServerUrl = lib.mkOption {
+ type = lib.types.str;
+ };
+ };
+
+ imports = [
+ <top/shared/prometheus-sql-exporter/service.nix>
+ <top/shared/grafana-matrix-forwarder/service.nix>
+ ];
+
+ config = {
+ age.secrets.grafana-matrix-forwarder-env.file = <top/secrets/grafana-matrix-forwarder-env.age>;
+
+ services.grafana = {
+ enable = true;
+ settings = {
+ server = {
+ http_addr = "0.0.0.0";
+ http_port = cfg.port;
+ };
+ };
+
+ provision = {
+ enable = true;
+ datasources.settings.datasources = [
+ {
+ name = "Prometheus Tente";
+ type = "prometheus";
+ url = "http://${config.services.prometheus.listenAddress}:${toString config.services.prometheus.port}";
+ }
+ {
+ name = "Loki Tente";
+ type = "loki";
+ access = "proxy";
+ url = "http://127.0.0.1:${toString config.services.loki.configuration.server.http_listen_port}";
+ }
+ ];
+ };
+ };
+ services.grafana-matrix-forwarder = {
+ enable = true;
+ port = cfg.matrixForwarderPort;
+ homeserver = cfg.matrixServerUrl;
+ environmentFile = config.age.secrets.grafana-matrix-forwarder-env.path;
+ };
+ };
+}
diff --git a/nixos/hosts/tente/monitoring.nix b/nixos/shared/monitoring.nix
index f6ed7cf..8711630 100644
--- a/nixos/hosts/tente/monitoring.nix
+++ b/nixos/shared/monitoring.nix
@@ -6,12 +6,6 @@ let
in
{
options.monitoring = {
- grafanaUiPort = lib.mkOption {
- type = lib.types.int;
- };
- grafanaMatrixForwarderPort = lib.mkOption {
- type = lib.types.int;
- };
lokiPort = lib.mkOption {
type = lib.types.int;
};
@@ -21,55 +15,12 @@ in
prometheusNodeExporterPort = lib.mkOption {
type = lib.types.int;
};
- prometheusSqlExporterPort = lib.mkOption {
- type = lib.types.int;
- };
- matrixServerUrl = lib.mkOption {
- type = lib.types.str;
+ prometheusScrapeConfigs = lib.mkOption {
+ type = lib.types.listOf lib.types.attrs;
+ default = [];
};
};
-
- imports = [
- <top/shared/prometheus-sql-exporter/service.nix>
- <top/shared/grafana-matrix-forwarder/service.nix>
- ];
-
config = {
- age.secrets.grafana-matrix-forwarder-env.file = <top/secrets/grafana-matrix-forwarder-env.age>;
-
- services.grafana = {
- enable = true;
- settings = {
- server = {
- http_addr = "0.0.0.0";
- http_port = cfg.grafanaUiPort;
- };
- };
-
- provision = {
- enable = true;
- datasources.settings.datasources = [
- {
- name = "Prometheus";
- type = "prometheus";
- url = "http://${config.services.prometheus.listenAddress}:${toString config.services.prometheus.port}";
- }
- {
- name = "Loki";
- type = "loki";
- access = "proxy";
- url = "http://127.0.0.1:${toString cfg.lokiPort}";
- }
- ];
- };
- };
- services.grafana-matrix-forwarder = {
- enable = true;
- port = cfg.grafanaMatrixForwarderPort;
- homeserver = cfg.matrixServerUrl;
- environmentFile = config.age.secrets.grafana-matrix-forwarder-env.path;
- };
-
services.prometheus = {
enable = true;
@@ -82,13 +33,7 @@ in
targets = [ "localhost:${toString cfg.prometheusNodeExporterPort}" ];
}];
}
- {
- job_name = "sql";
- static_configs = [{
- targets = [ "localhost:${toString cfg.prometheusSqlExporterPort}" ];
- }];
- }
- ];
+ ] ++ cfg.prometheusScrapeConfigs;
exporters.node = {
enable = true;
@@ -97,34 +42,6 @@ in
};
};
- services.prometheus-sql-exporter = {
- enable = true;
- port = cfg.prometheusSqlExporterPort;
- config = {
- target = {
- # This URL should be postgresql:///postgres?host=/run/postgresql
- # but sql_exporter uses xo/dburl which isn't spec-compliant: https://github.com/xo/dburl/issues/46
- data_source_name = "postgresql:/run/postgresql:/postgres";
- collectors = ["db-sizes"];
- };
- collectors = [
- {
- collector_name = "db-sizes";
- metrics = [
- {
- metric_name = "pg_db_size_bytes";
- help = "disk space used by the database";
- type = "gauge";
- key_labels = ["database_name"];
- values = ["size"];
- query = "SELECT datname AS database_name, pg_database_size(datname) as size from pg_database";
- }
- ];
- }
- ];
- };
- };
-
services.loki = {
enable = true;
configuration = {
@@ -174,8 +91,7 @@ in
serviceConfig = {
SupplementaryGroups = [
"systemd-journal"
- "www-data"
- ];
+ ] ++ lib.optional config.services.nginx.enable config.services.nginx.group;
};
};