minor fixes and improvements
[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 debug=false
17 if [[ $1 ]]; then
18 debug=true
19 fi
20
21
22 d() {
23 if $debug; then
24 printf "%s\n" "$*"
25 fi
26 }
27
28 main() {
29 if [[ ! -s /var/log/exim4/paniclog ]]; then
30 return 0
31 fi
32
33 # seems to randomly be caused by
34 # Starting exim4-base housekeeping, exim4-base.service
35 regex="^[^ ]* 00:00:0.* Failed writing transport results to pipe: Broken pipe$"
36 grep "$regex" /var/log/exim4/paniclog >> /var/log/exim4/paniclog-archive ||:
37 sed -i "/$regex/d" /var/log/exim4/paniclog
38
39 while read -r service regex; do
40 found=false
41 wipe=true
42 d "$service $regex"
43 while read -r d1 d2; do
44 d "$d1 $d2"
45 found=true
46 tmptime=$(date -d "$d1 $d2" +%s)
47 # dont consider every matching line, just those in > 60 second intervals
48 if [[ ! $logtime ]]; then
49 logtime=$tmptime
50 elif (( tmptime > logtime + 60 )); then
51 logtime=$tmptime
52 else
53 continue
54 fi
55 sec_min=$((logtime - 60))
56 sec_max=$((logtime + 60))
57 jmin="$(date -d @$sec_min "+%F %H:%M:%S")"
58 jmax="$(date -d @$sec_max "+%F %H:%M:%S")"
59 description=$(systemctl cat $service | sed -rn 's/^ *Description=(.*)/\1/p')
60 jrregex="^Starting $description"
61 if [[ $service == spamassassin ]]; then
62 jrregex+="\|^spamd: restarting"
63 fi
64 d "jrregex=$jrregex jmin=$jmin jmax=$jmax"
65 # the sed clears out the initial time and process+pid
66 if ! journalctl -u $service -S "$jmin" -U "$jmax" \
67 | sed -r 's/^([^[:space:]]*[[:space:]]+){5}//' | grep "$jrregex" &>/dev/null; then
68 wipe=false
69 break
70 fi
71 done < <(awk "/$regex/ "'{print $1,$2}' /var/log/exim4/paniclog)
72 if $found && $wipe; then
73 d "wiping $regex"
74 if [[ ! -w /var/log/exim4/paniclog-archive ]]; then
75 touch /var/log/exim4/paniclog-archive
76 chgrp adm /var/log/exim4/paniclog-archive
77 chmod 664 /var/log/exim4/paniclog-archive
78 fi
79 grep -E "$regex" /var/log/exim4/paniclog >> /var/log/exim4/paniclog-archive ||:
80 sed -ri "/$regex/d" /var/log/exim4/paniclog
81 fi
82 done <<'EOF'
83 clamav-daemon malware acl condition
84 spamassassin spam acl condition
85 EOF
86 }
87
88 if [[ $INVOCATION_ID ]]; then
89 # this is to prevent systemd from filling up the journal
90 for (( runcount=0; runcount < 100; runcount++ )); do
91 main
92 sleep 30
93 done
94 else
95 main
96 fi