Skip to content
Snippets Groups Projects
megacli2prom.nix 1.83 KiB
Newer Older
  • Learn to ignore specific revisions
  • # MegaCli to Prometheus text format exporter
    #
    # Scope: Gets data from MegaRAID compatible storage controllers and mogrifies
    #        to Prometheus text format, saves to a temp file, to later be scraped
    #        by the node exporter.
    # 
    # Usage: Import this to every server with a MegaRAID card that you want to
    #        include in the central monitoring system
    #
    # See https://nixos.org/manual/nixos/stable/#module-services-prometheus-exporters
    
    
    { config, options, lib, ourpkgs, pkgs, ... }:
    
    
    let
      cfg = config.services.private-storage.monitoring.megacli2prom;
    
    in {
      options.services.private-storage.monitoring.megacli2prom = {
        enable = lib.mkEnableOption "MegaCli2Prom metrics gathering service";
        outFile = lib.mkOption {
    
          type = lib.types.str;
    
          description = "Where to store the temporary file for node exporter to scrape?";
    
          default = "/run/prometheus-node-exporter/megacli.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 = [ ourpkgs.megacli2prom pkgs.megacli ];
    
          systemd.services.megacli2prom = {
            enable = true;
            description = "MegaCli2Prom metrics gathering service";
            startAt = cfg.interval;
    
            path = [ pkgs.megacli ];
    
            # 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 = ''
              "${ourpkgs.megacli2prom}/bin/megacli2prom" > "${cfg.outFile}.tmp"
              mv "${cfg.outFile}.tmp" "${cfg.outFile}"
            '';