fix vpn host naming
[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 if [[ ! -s /var/log/exim4/paniclog ]]; then
17 exit 0
18 fi
19
20 wipe=false
21 while read -r d1 d2; do
22 tmptime=$(date -d "$d1 $d2" +%s)
23 # dont consider every matching line, just those in > 60 second intervals
24 if [[ ! $logtime ]]; then
25 logtime=$tmptime
26 elif (( tmptime > logtime + 60 )); then
27 logtime=$tmptime
28 else
29 continue
30 fi
31 sec_min=$((logtime - 60))
32 sec_max=$((logtime + 60))
33 jmin="$(date -d @$sec_min "+%F %H:%M:%S")"
34 jmax="$(date -d @$sec_max "+%F %H:%M:%S")"
35 if journalctl -S "$jmin" -U "$jmax" \
36 | awk '$6 == "spamd:" && $7 == "restarting"' | grep . &>/dev/null; then
37 wipe=true
38 break
39 fi
40 done < <(awk '/spam acl condition/ {print $1,$2}' /var/log/exim4/paniclog)
41 if $wipe; then
42 regex="^$(date -d @$logtime "+%F %H:%M" )|^${jmin%:*}|^${jmax%:*}"
43 grep -E "$regex" /var/log/exim4/paniclog >> /var/log/exim4/paniclog-archive
44 sed -ri "/$regex/d" /var/log/exim4/paniclog
45 fi