From 864cdab35e200a2f1b13e65ffa7ab00b729e43e5 Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Sun, 7 Jun 2026 17:54:41 +0200 Subject: [PATCH] prometheus for dibbler --- .../monitoring/prometheus/default.nix | 1 + .../monitoring/prometheus/dibbler.nix | 91 +++++++++++++++++++ secrets/ildkule/ildkule.yaml | 7 +- 3 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 hosts/ildkule/services/monitoring/prometheus/dibbler.nix diff --git a/hosts/ildkule/services/monitoring/prometheus/default.nix b/hosts/ildkule/services/monitoring/prometheus/default.nix index e71a3b9..72a83b5 100644 --- a/hosts/ildkule/services/monitoring/prometheus/default.nix +++ b/hosts/ildkule/services/monitoring/prometheus/default.nix @@ -8,6 +8,7 @@ in { ./matrix-synapse.nix ./mysqld.nix ./postgres.nix + ./dibbler.nix ]; services.prometheus = { diff --git a/hosts/ildkule/services/monitoring/prometheus/dibbler.nix b/hosts/ildkule/services/monitoring/prometheus/dibbler.nix new file mode 100644 index 0000000..745abef --- /dev/null +++ b/hosts/ildkule/services/monitoring/prometheus/dibbler.nix @@ -0,0 +1,91 @@ +{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.services.prometheus.exporters.sql; + configFile = + if cfg.configFile != null then + cfg.configFile + else + let + nameInline = lib.mapAttrsToList (k: v: v // { name = k; }); + renameStartupSql = j: removeAttrs (j // { startup_sql = j.startupSql; }) [ "startupSql" ]; + configuration = { + jobs = map renameStartupSql ( + nameInline (lib.mapAttrs (k: v: (v // { queries = nameInline v.queries; })) cfg.configuration.jobs) + ); + }; + in + builtins.toFile "config.yaml" (builtins.toJSON configuration); +in +{ + sops.secrets."config/postgresql_dibbler_password" = { }; + + services.prometheus.scrapeConfigs = [ + { + job_name = "sql_exporter"; + scrape_interval = "1m"; + scheme = "http"; + + static_configs = [ + { + targets = [ "localhost:9237" ]; + } + ]; + } + ]; + + services.prometheus.exporters.sql = { + enable = true; + configuration = { + jobs.dibbler = { + interval = "1m"; + + queries."daily_purchase_sum" = { + help = "Sum of purchases for the current day."; + labels = [ "thing" ]; + values = [ "sum" ]; + query = "SELECT SUM(price) FROM purchases GROUP BY DATE(time) ORDER BY DATE(time) DESC LIMIT 1"; + }; + + queries."total_purchase_sum" = { + help = "Sum of all purchases."; + labels = [ "thing" ]; + values = [ "sum" ]; + query = "SELECT SUM(price) FROM purchases"; + }; + + queries."total_stock_value" = { + help = "The value of all stock in dibbler."; + labels = [ "thing" ]; + values = [ "sum" ]; + query = "SELECT SUM(price * stock) FROM products"; + }; + + queries."user_credit_sum" = { + help = "The sum of all user credit."; + labels = [ "thing" ]; + values = [ "sum" ]; + query = "SELECT SUM(credit) FROM users"; + }; + }; + }; + }; + + systemd.services."prometheus-sql-exporter".serviceConfig = { + RuntimeDirectory = "prometheus-sql-exporter"; + LoadCredential = "postgresql_dibbler_password:${ + config.sops.secrets."config/postgresql_dibbler_password".path + }"; + ExecStartPre = ''/bin/sh -c '${lib.getExe' pkgs.coreutils "cat"} ${configFile} | ${lib.getExe' pkgs.jq "jq"} -c \".jobs[0].connections[0]=\\\"postgres://pvv_vv:$(${lib.getExe' pkgs.coreutils "cat"} %d/postgresql_dibbler_password)@postgres.pvv.ntnu.no\\\"\" > /run/prometheus-sql-exporter/config.yaml' ''; + ExecStart = lib.mkForce '' + ${pkgs.prometheus-sql-exporter}/bin/sql_exporter \ + -web.listen-address ${cfg.listenAddress}:${toString cfg.port} \ + -config.file /run/prometheus-sql-exporter/config.yaml \ + ${lib.concatStringsSep " \\\n " cfg.extraFlags} + ''; + }; +} diff --git a/secrets/ildkule/ildkule.yaml b/secrets/ildkule/ildkule.yaml index 3544145..5d1b30c 100644 --- a/secrets/ildkule/ildkule.yaml +++ b/secrets/ildkule/ildkule.yaml @@ -1,5 +1,6 @@ config: mysqld_exporter_password: ENC[AES256_GCM,data:I9K+QMqaN3FOOVKzeOR9Q6UERStXX0P8WEHyN1jzzbM=,iv:UxvIdlfAyJvNuxPkU4+guKPa0fiD0vVLzHOTYktcmso=,tag:ltnIqEwESYx9HBu8UN0ZLw==,type:str] + postgresql_dibbler_password: ENC[AES256_GCM,data:wP4CVz9qRE3CJrblWWYqOIkcH3LM5H81,iv:j8zr1TRNlPPqIppYlWhDoKlL7m2Ph2wQlU6bvFj8R9A=,tag:Cfp8zbYkoLqI7DTDpOBlJw==,type:str] keys: grafana: secret_key: ENC[AES256_GCM,data:+WoAJbDBEgKs0RoHT+7oEELAVQ+/2Xt+5RTMSXg23moCqVRx+Gzll9P5Drw=,iv:AkRn/Y20iEe5i1T+84wAgLCTFtAox2G3giyawAkltAw=,tag:BZbt5Wb5lYLIJBm/pfP4GQ==,type:str] @@ -72,8 +73,8 @@ sops: dC9meDZlc3d3aUJEVjc4REF0Y1BLcGcK79LbJzc5KVgEgyJR11crGuX8YcVoJBbT Fin7Zoon06L7qx0Zw5u27wV7RKMnYT7hOMiWs6660ZTLcYJ5M1aEZQ== -----END AGE ENCRYPTED FILE----- - lastmodified: "2025-03-16T20:08:18Z" - mac: ENC[AES256_GCM,data:C2tpWppc13jKJq5d4nmAKQOaNWHm27TKwxAxm1fi2lejN1lqUaoz5bHfTBA7MfaWvuP5uZnfbtG32eeu48mnlWpo58XRUFFecAhb9JUpW9s5IR3/nbzLNkGU7H5C0oWPrxI4thd+bAVduIgBjjFyGj1pe6J9db3c0yUWRwNlwGU=,iv:YpoQ4psiFYOWLGipxv1QvRvr034XFsyn2Bhyy39HmOo=,tag:ByiCWygFC/VokVTbdLoLgg==,type:str] + lastmodified: "2026-06-07T12:51:04Z" + mac: ENC[AES256_GCM,data:otGwzc3Bme1PGRU4zWRRf3kmAf5EDjT8sPkDK/zDrO0ve+d3x99Qls4DBZV8G6FsFtUXbeKVo70I5VpqUgaIef+nyMl6zzWkW1wpbKIBYjh5fkaP0xxhLnw+shrB088PAXT0wkP7hJBLO0ZJgTrpprJKhmeqVj2HEXBSJ1wSjk8=,iv:cjqtdbWBVLUfpQdykb3vKDKa/VC/kGBybwYtG/eStqc=,tag:remOj4bX/H/LlAPgcXHAdA==,type:str] pgp: - created_at: "2026-05-20T17:35:58Z" enc: |- @@ -96,4 +97,4 @@ sops: -----END PGP MESSAGE----- fp: F7D37890228A907440E1FD4846B9228E814A2AAC unencrypted_suffix: _unencrypted - version: 3.9.4 + version: 3.12.2 -- 2.54.0