#!/bin/sh
# Store one hang capture as a zip. Five zips are kept. The oldest is
# removed before the sixth is created. The unpacked directory is deleted.
set -u

STATE=${ZOS_LOG_COLLECTOR_STATE:-/var/lib/zos-log-collector}
DEST_ROOT=${ZOS_LOG_COLLECTOR_DEST:-/var/log/hang-captures}
KEEP=${ZOS_LOG_COLLECTOR_KEEP:-5}
XORG_DIR=${ZOS_LOG_COLLECTOR_XORG_DIR:-/var/log}
LIGHTDM_DIR=${ZOS_LOG_COLLECTOR_LIGHTDM_DIR:-/var/log/lightdm}
PSTORE_DIR=${ZOS_LOG_COLLECTOR_PSTORE_DIR:-/sys/fs/pstore}
PSTORE_ARCHIVE=${ZOS_LOG_COLLECTOR_PSTORE_ARCHIVE:-/var/lib/systemd/pstore}

[ -f "$STATE/collect-pending" ] || exit 0

mkdir -p "$STATE"
lock="$STATE/capture.lock"
if ! : >>"$lock"; then
    logger -t zos-log-collector "cannot create capture lock" || true
    exit 0
fi
exec 9<>"$lock"
if ! flock -n 9; then
    exit 0
fi

# Re-check after the lock. A second login must not write another capture.
[ -f "$STATE/collect-pending" ] || exit 0

mkdir -p "$DEST_ROOT"
chmod 0750 "$DEST_ROOT" 2>/dev/null || true
chgrp adm "$DEST_ROOT" 2>/dev/null || true

while [ "$(find "$DEST_ROOT" -mindepth 1 -maxdepth 1 -type f -name '*.zip' | wc -l)" -ge "$KEEP" ]; do
    oldest=$(find "$DEST_ROOT" -mindepth 1 -maxdepth 1 -type f -name '*.zip' -printf '%f\n' | sort | head -n 1)
    if [ -z "$oldest" ]; then
        break
    fi
    rm -f "$DEST_ROOT/$oldest"
done

stamp=$(date +%Y-%m-%d_%H%M%S)
dest="$DEST_ROOT/$stamp"
n=2
while [ -e "$dest" ] || [ -e "$dest.zip" ]; do
    dest="$DEST_ROOT/${stamp}_$n"
    n=$((n + 1))
done
mkdir -p "$dest"
chmod 0750 "$dest" 2>/dev/null || true

journalctl -b -1 -o short --no-pager >"$dest/journal-b-1.log" 2>/dev/null || true
journalctl -b 0 -o short --no-pager >"$dest/journal-b-0.log" 2>/dev/null || true
# The kernel ring buffer only has this boot. The previous boot kernel
# log is the journal's dmesg for that boot.
journalctl -b -1 -k -o short --no-pager >"$dest/dmesg-b-1.txt" 2>/dev/null || true
journalctl -b 0 -k -o short --no-pager >"$dest/dmesg-b-0.txt" 2>/dev/null || true
journalctl --list-boots --no-pager >"$dest/boots.txt" 2>/dev/null || true

# Every file directly in /var/log whose name starts with Xorg.
for log in "$XORG_DIR"/Xorg*; do
    if [ -f "$log" ]; then
        cp -a "$log" "$dest/" 2>/dev/null || true
    fi
done

mkdir -p "$dest/lightdm"
for name in lightdm.log seat0-greeter.log x-0.log x-1.log; do
    if [ -f "$LIGHTDM_DIR/$name" ]; then
        cp -a "$LIGHTDM_DIR/$name" "$dest/lightdm/" 2>/dev/null || true
    fi
done
rmdir "$dest/lightdm" 2>/dev/null || true

pstore_notes=""
if [ -d "$PSTORE_DIR" ]; then
    mkdir -p "$dest/pstore/sys-fs-pstore"
    cp -a "$PSTORE_DIR"/. "$dest/pstore/sys-fs-pstore/" 2>/dev/null || true
    if [ -n "$(find "$dest/pstore/sys-fs-pstore" -mindepth 1 -print -quit)" ]; then
        pstore_notes="pstore/sys-fs-pstore/ holds files still present in $PSTORE_DIR."
    else
        rmdir "$dest/pstore/sys-fs-pstore" 2>/dev/null || true
    fi
fi

# systemd-pstore moves records out of /sys/fs/pstore at early boot and
# keeps every earlier crash. Copy the whole archive so one capture can
# be compared with previous crashes.
if [ -d "$PSTORE_ARCHIVE" ]; then
    mkdir -p "$dest/pstore/var-lib-systemd-pstore"
    cp -a "$PSTORE_ARCHIVE"/. "$dest/pstore/var-lib-systemd-pstore/" 2>/dev/null || true
    if [ -n "$(find "$dest/pstore/var-lib-systemd-pstore" -mindepth 1 -print -quit)" ]; then
        note="pstore/var-lib-systemd-pstore/ holds every record systemd-pstore archived in $PSTORE_ARCHIVE."
        pstore_notes="${pstore_notes:+$pstore_notes
}$note"
    else
        rmdir "$dest/pstore/var-lib-systemd-pstore" 2>/dev/null || true
    fi
fi
rmdir "$dest/pstore" 2>/dev/null || true
[ -n "$pstore_notes" ] || pstore_notes="No pstore data was present."

{
    echo "Previous boot did not reach an orderly shutdown."
    echo "That is what a long power-button press, a power loss, or a crash before shutdown leaves behind."
    echo "Captured: $(date '+%b %d %H:%M:%S')"
    if [ -r /proc/sys/kernel/random/boot_id ]; then
        echo "Current boot id: $(cat /proc/sys/kernel/random/boot_id)"
    fi
    echo "journal-b-1.log is the complete previous boot (journalctl -b -1 -o short)."
    echo "journal-b-0.log is the complete current boot (journalctl -b 0 -o short)."
    echo "Timestamps look like Oct 08 11:24:58."
    echo "dmesg-b-1.txt is the previous boot kernel log (journalctl -b -1 -k -o short)."
    echo "dmesg-b-0.txt is the current boot kernel log (journalctl -b 0 -k -o short)."
    echo "Files copied from /var/log are every file whose name starts with Xorg."
    echo "lightdm/ holds lightdm.log, seat0-greeter.log, x-0.log, and x-1.log when present."
    echo "$pstore_notes"
} >"$dest/REASON.txt"

find "$dest" -type f -exec chmod 0640 {} + 2>/dev/null || true

archive="$dest.zip"
if (
    cd "$DEST_ROOT" &&
    zip -r -q "$(basename "$archive")" "$(basename "$dest")"
); then
    chmod 0640 "$archive" 2>/dev/null || true
    rm -rf "$dest"
    rm -f "$STATE/collect-pending"
    logger -t zos-log-collector "stored hang capture $archive" || true
else
    logger -t zos-log-collector "failed to zip $dest; unpacked capture left in place" || true
fi
exit 0
