Skip to content
Snippets Groups Projects
tahoe.nix 1.88 KiB
Newer Older
# 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, 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 ];

        # 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 = ''
          curl --silent --show-error --fail-with-body --output "${cfg.outFile}.tmp" "${cfg.scrapeEndpoint}"
          mv "${cfg.outFile}.tmp" "${cfg.outFile}"
        '';
      };
  };
}