summary refs log tree commit diff
path: root/nixos/modules/programs/atop.nix
diff options
context:
space:
mode:
authorKlemens Nanni <klemens@posteo.de>2022-05-31 06:31:05 +0200
committerKlemens Nanni <klemens@posteo.de>2022-05-31 07:21:01 +0200
commit09350ff7d4243e81fcab99cdd5f2c696075de42e (patch)
tree1d4eae959ca6aa2099abddce72fed4fe300d32b8 /nixos/modules/programs/atop.nix
parentcac45c69ab463c73b012b7236d732e35a0dc3033 (diff)
downloadnixpkgs-09350ff7d4243e81fcab99cdd5f2c696075de42e.tar
nixpkgs-09350ff7d4243e81fcab99cdd5f2c696075de42e.tar.gz
nixpkgs-09350ff7d4243e81fcab99cdd5f2c696075de42e.tar.bz2
nixpkgs-09350ff7d4243e81fcab99cdd5f2c696075de42e.tar.lz
nixpkgs-09350ff7d4243e81fcab99cdd5f2c696075de42e.tar.xz
nixpkgs-09350ff7d4243e81fcab99cdd5f2c696075de42e.tar.zst
nixpkgs-09350ff7d4243e81fcab99cdd5f2c696075de42e.zip
nixos/atop: Convert log format to fix service start
Raw logs are stored in a versioned binary format and must be update with
atopconvert(1) upon atop version updates.

Failure to do so results in atop.service startup failure as I found out
the hard way after the "atop: 2.6.0 -> 2.7.1"[0] bump:
```
May 31 01:49:25 <hostname> sh[2269709]: existing file /var/log/atop/atop_20220531 has incompatible header
May 31 01:49:25 <hostname> sh[2269709]: (created by version 2.6 - current version 2.7)
May 31 01:49:25 <hostname> systemd[1]: atop.service: Main process exited, code=exited, status=7/NOTRUNNING
```

Convert logs in `ExecStartPre` and replace them iff updated.
This is to avoid changing original modification times upon every service
start and thus work against atop's log rotation (see existing
`ExecStartPre`).

0: https://github.com/NixOS/nixpkgs/pull/175180#issuecomment-1141546487
Diffstat (limited to 'nixos/modules/programs/atop.nix')
-rw-r--r--nixos/modules/programs/atop.nix18
1 files changed, 18 insertions, 0 deletions
diff --git a/nixos/modules/programs/atop.nix b/nixos/modules/programs/atop.nix
index ad75ab27666..a31078a891a 100644
--- a/nixos/modules/programs/atop.nix
+++ b/nixos/modules/programs/atop.nix
@@ -136,6 +136,24 @@ in
           packages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
           services =
             mkService cfg.atopService.enable "atop" [ atop ]
+            // lib.mkIf cfg.atopService.enable {
+              # always convert logs to newer version first
+              # XXX might trigger TimeoutStart but restarting atop.service will
+              # convert remainings logs and start eventually
+              atop.serviceConfig.ExecStartPre = pkgs.writeShellScript "atop-update-log-format" ''
+                set -e -u
+                for logfile in "$LOGPATH"/atop_*
+                do
+                  ${atop}/bin/atopconvert "$logfile" "$logfile".new
+                  # only replace old file if version was upgraded to avoid
+                  # false positives for atop-rotate.service
+                  if ! ${pkgs.diffutils}/bin/cmp -s "$logfile" "$logfile".new
+                  then
+                    ${pkgs.coreutils}/bin/mv -v -f "$logfile".new "$logfile"
+                  fi
+                done
+              '';
+            }
             // mkService cfg.atopacctService.enable "atopacct" [ atop ]
             // mkService cfg.netatop.enable "netatop" [ cfg.netatop.package ]
             // mkService cfg.atopgpu.enable "atopgpu" [ atop ];