Skip to content
Snippets Groups Projects
tahoe.nix 2.45 KiB
Newer Older
  • Learn to ignore specific revisions
  • # 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, ... }:
    
      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" ];
    
            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}"
            '';
          };
    
          systemd.timers.tahoe-metrics-collector = {
            after = [ "tahoe.storage.service" ];
          };