Enhance mailtrace script: add support for specifying a single log file and improve error handling for log file options

This commit is contained in:
2026-06-16 11:36:07 +02:00
parent a26b854cca
commit 3cb450ada8
+16 -5
View File
@@ -73,6 +73,7 @@ set -euo pipefail
FOLLOW=0
ALL_LOGS=0
LOG_FILE=""
FAIL_ONLY=0
SUCCESS_ONLY=0
@@ -140,6 +141,7 @@ Debug:
Input / Zeitraum:
--follow tail -F /var/log/mail.log (live)
--all-logs /var/log/mail.log* inkl. .gz (historisch)
--log-file <pfad> einzelne Logdatei (auch .gz); nicht kombinierbar mit --follow/--all-logs
--since <prefix> Timestamp prefix-match (z.B. 2026-01-13T14:47)
--until <prefix> stoppe sobald ts >= prefix (ISO8601 string compare)
USAGE
@@ -150,6 +152,7 @@ while [[ $# -gt 0 ]]; do
case "$1" in
--follow) FOLLOW=1 ;;
--all-logs) ALL_LOGS=1 ;;
--log-file) LOG_FILE="${2:-}"; shift ;;
--success) SUCCESS_ONLY=1 ;;
--fail) FAIL_ONLY=1 ;;
--addr) ADDR_FILTER="${2:-}"; shift ;;
@@ -185,6 +188,14 @@ if [[ "$DEDUP_PREFER" != "final" && "$DEDUP_PREFER" != "amavis" && "$DEDUP_PREFE
echo "Ungültiges --dedup-prefer: $DEDUP_PREFER (final|amavis|first|last)" >&2
exit 2
fi
if [[ -n "$LOG_FILE" && ($FOLLOW -eq 1 || $ALL_LOGS -eq 1) ]]; then
echo "--log-file kann nicht zusammen mit --follow oder --all-logs verwendet werden." >&2
exit 2
fi
if [[ -n "$LOG_FILE" && ! -r "$LOG_FILE" ]]; then
echo "Datei nicht lesbar: $LOG_FILE" >&2
exit 2
fi
if [[ $SUCCESS_ONLY -eq 1 && $FAIL_ONLY -eq 1 ]]; then
echo "Bitte nur eines von --success oder --fail verwenden." >&2
exit 2
@@ -201,12 +212,12 @@ fi
# --- Input selection ---------------------------------------------------------
if [[ $FOLLOW -eq 1 ]]; then
INPUT_CMD=(sudo tail -F /var/log/mail.log)
elif [[ -n "$LOG_FILE" ]]; then
INPUT_CMD=(sudo zcat -f "$LOG_FILE")
elif [[ $ALL_LOGS -eq 1 ]]; then
INPUT_CMD=(sudo zcat -f /var/log/mail.log*)
else
if [[ $ALL_LOGS -eq 1 ]]; then
INPUT_CMD=(sudo zcat -f /var/log/mail.log*)
else
INPUT_CMD=(sudo cat /var/log/mail.log)
fi
INPUT_CMD=(sudo cat /var/log/mail.log)
fi
###############################################################################