Debian Codegraph @

reporting-helpers

raw
1 # -*- sh -*-
2 #
3 # This file is no shell script but to be sourced by other shell
4 # scripts as a library of common helper functions, etc.
5
6 # These variables can be overriden, but this should be used with care
7 WARN='\bWarn|\bW:|^The following packages have unmet dependencies'
8 ERROR='(^|[^-/])(\bError\b|\bErr:|\bE:|aptitude exited with value [^0])'
9 ERROR_FALSE_POSITIVE='\b[Nn]o *[Ee]rror\b'
10
11 # These variables are meant to be optionally set by the administrator.
12 REPORT_LOG_DROP=''
13 REPORT_LOG_IGNORE=''
14
15 MSG="
16 Aptitude-Robot Report
17 =====================
18
19 "
20
21 if [ -f /etc/default/aptitude-robot ]; then
22 . /etc/default/aptitude-robot
23 fi
24
25 #
26 # check for remaining upgrades
27 #
28 remaining_upgrades() {
29 UPGRADES_FILE=$(mktemp --tmpdir aptitude-upgrades.XXXXXXXXX)
30 if [ -z "$UPGRADES_FILE" ]; then
31 echo "ERROR: $(basename $0): Can't check for remaining upgrades:
32 Couldn't create temporary file $UPGRADES_FILE using mktemp.
33 "
34 else
35 aptitude search '~U !~ahold' > "$UPGRADES_FILE"
36 if [ -s "$UPGRADES_FILE" ]; then
37 echo "Remaining Upgrades:
38
39 $(cat "$UPGRADES_FILE")
40
41 "
42 fi
43 rm "$UPGRADES_FILE"
44 fi
45 }
46
47 #
48 # check if we need to filter stuff
49 #
50 cat_or_egrep_v() {
51 if [ -n "$1" ]; then
52 egrep -v "$1|$ERROR_FALSE_POSITIVE"
53 else
54 egrep -v "$ERROR_FALSE_POSITIVE"
55 fi
56 }
57
58 #
59 # mangle filters from egrep syntax to sed syntax, probably incomplete
60 #
61 egrep_to_sed() {
62 printf '%s' "$1" | sed -e 's/[()|%]/\\&/g'
63 }
64
65 #
66 # check for errors in log file
67 #
68 errors_in_logfile() {
69 LOGFILE="$1"
70 if [ -f "$LOGFILE" ]; then
71 if cat "$LOGFILE" | cat_or_egrep_v "$REPORT_LOG_IGNORE" | egrep -q -i "$ERROR" >/dev/null 2>/dev/null; then
72 echo errors
73 return 2;
74 fi
75 if cat "$LOGFILE" | cat_or_egrep_v "$REPORT_LOG_IGNORE" | egrep -q -i "$WARN" >/dev/null 2>/dev/null; then
76 echo warnings
77 return 1
78 fi
79 else
80 echo notfound
81 return 3
82 fi
83 return 0
84 }
85
86 #
87 # print log file, but filtered
88 #
89 print_logfile() {
90 cat "$1" | cat_or_egrep_v "$REPORT_LOG_DROP"
91 }
92
93 print_logfile_for_xymon() {
94 print_logfile "$1" | \
95 sed -e "s%$(egrep_to_sed "$WARN")%\&yellow &%gi;s%$(egrep_to_sed "$ERROR")%\1\&red \2%gi" | \
96 sed -e "s%\&\(yellow\|red\) \(.*\($(egrep_to_sed "$ERROR_FALSE_POSITIVE")\)\)%\2%g" | \
97 if [ -n "$REPORT_LOG_IGNORE" ]; then
98 sed -e "s%&\(yellow\|red\) \(.*\($(egrep_to_sed "$REPORT_LOG_IGNORE")\)\)%\&clear \2%g";
99 else
100 cat
101 fi
102 }