Skip to content
Snippets Groups Projects
tahoe.nix 2.68 KiB
Newer Older
# Tahoe Prometheus metrics collector
#
# Scope: Retrieve metrics from Tahoe and put them where Prometheus'
#        node-exporter's 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, pkgs, ... }:
  cfg = config.services.private-storage.monitoring.exporters.tahoe;
  inherit (config.services.private-storage.monitoring.exporters.node) textfiles-directory;
  options.services.private-storage.monitoring.exporters.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 = "${textfiles-directory}/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 {
Florian Sesser's avatar
Florian Sesser committed
          assertion = config.services.private-storage.monitoring.exporters.node.enable;
Florian Sesser's avatar
Florian Sesser committed
            services.private-storage.monitoring.tahoe requires services.private-storage.monitoring.exporters.node to provide the textfile prometheus collector.
      environment.systemPackages = [ pkgs.curl ];
      systemd.services.tahoe-metrics-collector = {
        enable = true;
        description = "Tahoe metrics gathering service";
        after = [ "tahoe.storage.service" ];
        path = [ pkgs.coreutils pkgs.findutils pkgs.curl ];
        # Save to a temp file and then move atomically so the
        # textfile collector won't read a partial file.
        # See https://github.com/prometheus/node_exporter#textfile-collector
        script = ''
          NUM_CORRUPTION_ADVISORIES=$(find /storage/corruption-advisories/ -type f | wc -l)
          echo "tahoe_corruption_advisories_total $NUM_CORRUPTION_ADVISORIES" > "${cfg.outFile}.tmp"

          curl --silent --show-error --fail-with-body "${cfg.scrapeEndpoint}" >> "${cfg.outFile}.tmp"
          mv "${cfg.outFile}.tmp" "${cfg.outFile}"
        '';
      };
      systemd.timers.tahoe-metrics-collector = {
        after = [ "tahoe.storage.service" ];
      };