bunch of updates and fixes
[distro-setup] / epanic-clean
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 # The panic log regularly gets some stuff in it we dont want to fix.
6 # Detect it and wipe it out.
7
8 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
9
10 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
11 set -eE -o pipefail
12 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
13
14 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
15
16
17 wipe=false
18 while read -r d1 d2; do
19 tmptime=$(date -d "$d1 $d2" +%s)
20 # dont consider every matching line, just those in > 60 second intervals
21 if [[ ! $logtime ]]; then
22 logtime=$tmptime
23 elif (( tmptime > logtime + 60 )); then
24 logtime=$tmptime
25 else
26 continue
27 fi
28 sec_min=$((logtime - 60))
29 sec_max=$((logtime + 60))
30 jmin="$(date -d @$sec_min "+%F %H:%M:%S")"
31 jmax="$(date -d @$sec_max "+%F %H:%M:%S")"
32 if journalctl -S "$jmin" -U "$jmax" \
33 | awk '$6 == "spamd:" && $7 == "restarting"' | grep . &>/dev/null; then
34 wipe=true
35 break
36 fi
37 done < <(awk '/spam acl condition/ {print $1,$2}' /var/log/exim4/paniclog)
38 if $wipe; then
39 regex="^$(date -d @$logtime "+%F %H:%M" )|^${jmin%:*}|^${jmax%:*}"
40 grep -E "$regex" /var/log/exim4/paniclog >> /var/log/exim4/paniclog-archive
41 sed -ri "/$regex/d" /var/log/exim4/paniclog
42 fi