Add service for exporting journalctl logs

- Export logs every five minutes to /output/logs
- Attempt to export logs on shutdown, not fully functional yet
- Change audio-recorder service unitConfig after target to multi-user

Signed-off-by: Satu Koskinen <satu.a.koskinen@gmail.com>
This commit is contained in:
Satu Koskinen 2023-10-29 14:14:47 +02:00
parent 000299ba3f
commit 19fe225a4f
No known key found for this signature in database
GPG Key ID: A6210BB600E94883
4 changed files with 68 additions and 1 deletions

View File

@ -42,6 +42,7 @@
modules = [
./targets/raspberry-pi-4
./modules/audio-recorder
./modules/logging
./modules/real-time-clock/i2c-rtc.nix
./modules/shutdown-button/service.nix
nixos-hardware.nixosModules.raspberry-pi-4

View File

@ -73,7 +73,7 @@
Restart = "always";
};
unitConfig = {
After = "sound.target";
After = ["multi-user.target"];
};
startLimitIntervalSec = 0;
};

View File

@ -0,0 +1,61 @@
{
config,
pkgs,
lib,
...
}: {
options = with lib; {
services.journalctl-log-export = {
enable = mkEnableOption "Enable exporting journalctl logs to a file.";
output-folder = mkOption {
type = types.str;
default = "/output/logs";
description = "The folder to save logs to.";
};
};
};
config = lib.mkIf config.services.journalctl-log-export.enable {
systemd.services.journalctl-log-export-on-shutdown = {
description = "Journalctl log export on shutdown service";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = "root";
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/mkdir -p ${config.services.journalctl-log-export.output-folder}";
ExecStop = ''${pkgs.bash}/bin/bash -c "echo 'Exporting logs at shutdown' && journalctl --boot 0 > ${config.services.journalctl-log-export.output-folder}/$(date +\"%Y_%m_%d-%H_%M_%S\")_journalctl_on_shutdown.txt"'';
};
};
systemd.services.journalctl-log-export = {
description = "Journalctl log export service";
script = ''
#!/usr/bin/env bash
set -x
${pkgs.coreutils}/bin/echo "Exporting journalctl logs"
${pkgs.coreutils}/bin/mkdir -p ${config.services.journalctl-log-export.output-folder}
# if environment variable for output file name is not set, set it with systemctl set-environment
if [ -z "$JOURNALCTL_LOG_EXPORT_OUTPUT_FILE" ]; then
JOURNALCTL_LOG_EXPORT_OUTPUT_FILE=$(${pkgs.coreutils}/bin/date +"%Y_%m_%d-%H_%M_%S")_journalctl.txt
${pkgs.systemd}/bin/systemctl set-environment JOURNALCTL_LOG_EXPORT_OUTPUT_FILE=$JOURNALCTL_LOG_EXPORT_OUTPUT_FILE
fi
${pkgs.systemd}/bin/journalctl --boot 0 > ${config.services.journalctl-log-export.output-folder}/$JOURNALCTL_LOG_EXPORT_OUTPUT_FILE
'';
serviceConfig = {
User = "root";
Type = "oneshot";
};
};
systemd.timers.journalctl-log-export = {
wantedBy = [ "timers.target" ];
partOf = [ "journalctl-log-export.service" ];
timerConfig = {
OnCalendar = "*:0/5"; # every five minutes
Unit = "journalctl-log-export.service";
};
};
};
}

View File

@ -45,6 +45,11 @@ in {
max-file-time-secs = 60;
};
services.journalctl-log-export = {
enable = true;
output-folder = "/output/logs";
};
services.gps-recorder = {
enable = true;
output-path = "/output/gps";