From 787e85373dadce37f265178dec445fb43fe35647 Mon Sep 17 00:00:00 2001
From: Florian Sesser <florian@private.storage>
Date: Wed, 10 Nov 2021 13:36:15 +0000
Subject: [PATCH] Monitoring: Add regular scraping for Tahoe OpenMetrics
 statistics

---
 morph/lib/storage.nix                        |  4 ++
 nixos/modules/monitoring/exporters/tahoe.nix | 59 ++++++++++++++++++++
 2 files changed, 63 insertions(+)
 create mode 100644 nixos/modules/monitoring/exporters/tahoe.nix

diff --git a/morph/lib/storage.nix b/morph/lib/storage.nix
index 86e14228..15e23737 100644
--- a/morph/lib/storage.nix
+++ b/morph/lib/storage.nix
@@ -43,8 +43,12 @@ in {
     ../../nixos/modules/monitoring/vpn/client.nix
     # Expose base system metrics over the monitoringvpn.
     ../../nixos/modules/monitoring/exporters/node.nix
+    # Collect Tahoe OpenMetrics statistics.
+    ../../nixos/modules/monitoring/exporters/tahoe.nix
   ];
 
+  services.private-storage.monitoring.tahoe.enable = true;
+
   # Turn on the Private Storage (Tahoe-LAFS) service.
   services.private-storage = {
     # Yep.  Turn it on.
diff --git a/nixos/modules/monitoring/exporters/tahoe.nix b/nixos/modules/monitoring/exporters/tahoe.nix
new file mode 100644
index 00000000..f63c21e3
--- /dev/null
+++ b/nixos/modules/monitoring/exporters/tahoe.nix
@@ -0,0 +1,59 @@
+# Tahoe Prometheus metrics collector
+#
+# Scope: Retrieves OpenMetrics from Tahoe and puts them
+#        where textfile collector can find them.
+#
+# Usage: Import this to every server running Tahoe.
+#
+# See https://nixos.org/manual/nixos/stable/#module-services-prometheus-exporters
+
+{ config, options, lib, ourpkgs, pkgs, ... }:
+
+let
+  cfg = config.services.private-storage.monitoring.tahoe;
+
+in {
+  options.services.private-storage.monitoring.tahoe = {
+    enable = lib.mkEnableOption "Tahoe OpenMetrics collecting service";
+    scrapeEndpoint = lib.mkOption {
+      type = lib.types.str;
+      description = "Where to get our metrics from?";
+      default = "http://localhost:3456/statistics?t=openmetrics";
+    };
+    outFile = lib.mkOption {
+      type = lib.types.str;
+      description = "Where to store the temporary file for node exporter to scrape?";
+      default = "/run/prometheus-node-exporter/tahoe.prom";
+    };
+    interval = lib.mkOption {
+      type = lib.types.str;
+      description = ''
+        How often to do it?
+        See https://www.freedesktop.org/software/systemd/man/systemd.time.html#Calendar%20Events
+      '';
+      # Every five minutes.
+      default = "*:0/5";
+    };
+  };
+
+  config =
+    lib.mkIf cfg.enable {
+      environment.systemPackages = [ pkgs.curl ];
+      systemd.services.tahoe-metrics-collector = {
+        enable = true;
+        description = "Tahoe metrics gathering service";
+        wantedBy = [ "multi-user.target" ];
+        startAt = cfg.interval;
+        path = [ pkgs.curl ];
+
+        # Curl to a temp file and then move atomically so textfile collector
+        # won't read a partial file.
+        # See https://github.com/prometheus/node_exporter#textfile-collector
+        script = ''
+          curl --silent --show-error --fail-with-body --output "${cfg.outFile}.tmp" "${cfg.scrapeEndpoint}"
+          mv "${cfg.outFile}.tmp" "${cfg.outFile}"
+        '';
+      };
+  };
+}
+
-- 
GitLab