From 85acb084c490a29014f55fa94e4126724e7acc80 Mon Sep 17 00:00:00 2001
From: Florian Sesser <florian@private.storage>
Date: Wed, 10 Nov 2021 13:46:03 +0000
Subject: [PATCH] Monitoring: Also have atomicity for megacli2prom

---
 nixos/modules/monitoring/exporters/megacli2prom.nix | 8 +++++++-
 nixos/modules/monitoring/exporters/tahoe.nix        | 4 ++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/nixos/modules/monitoring/exporters/megacli2prom.nix b/nixos/modules/monitoring/exporters/megacli2prom.nix
index b364088a..a38f1ccc 100644
--- a/nixos/modules/monitoring/exporters/megacli2prom.nix
+++ b/nixos/modules/monitoring/exporters/megacli2prom.nix
@@ -42,7 +42,13 @@ in {
         wantedBy = [ "multi-user.target" ];
         startAt = cfg.interval;
         path = [ pkgs.megacli ];
-        script = "${ourpkgs.megacli2prom}/bin/megacli2prom > ${cfg.outFile}";
+        # 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}"
+        '';
       };
   };
 }
diff --git a/nixos/modules/monitoring/exporters/tahoe.nix b/nixos/modules/monitoring/exporters/tahoe.nix
index f63c21e3..9b46adc3 100644
--- a/nixos/modules/monitoring/exporters/tahoe.nix
+++ b/nixos/modules/monitoring/exporters/tahoe.nix
@@ -46,8 +46,8 @@ in {
         startAt = cfg.interval;
         path = [ pkgs.curl ];
 
-        # Curl to a temp file and then move atomically so textfile collector
-        # won't read a partial file.
+        # 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}"
-- 
GitLab