Skip to content

Commit

Permalink
Factor out common code in "xymon-report" and "mail-log-on-error"
Browse files Browse the repository at this point in the history
Common code is now stored in "reporting-helpers".

This fixes #2 on GitHub.
  • Loading branch information
xtaran committed May 12, 2016
1 parent 012a690 commit f4f2d81
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 110 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dist_sbin_SCRIPTS += aptitude-robot-session
dist_pkgdata_SCRIPTS = random-delay
dist_pkgdata_SCRIPTS += mail-log-on-error
dist_pkgdata_SCRIPTS += xymon-report
dist_pkgdata_DATA = reporting-helpers
man_MANS = aptitude-robot.8 aptitude-robot-session.8

pkgconfdir = $(sysconfdir)/aptitude-robot
Expand Down
2 changes: 2 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ aptitude-robot (1.5-1) UNRELEASED; urgency=medium
* Version 1.5 (new backwards-compatible features)
+ Add support for filtering log files in reports.
+ Add coloured icons for warning/error/ignored lines in xymon-report.
+ Factor out common code in "xymon-report" and "mail-log-on-error"
into "reporting-helpers. Fixes GH#2.
+ Rename COPYING to LICENSE as it's more common outside the GNU world.
+ Mention Semantic Versioning in README.
* Switch Vcs-Git header from git:// to https://.
Expand Down
63 changes: 16 additions & 47 deletions mail-log-on-error
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
REPORT_MAIL=false
REPORT_FILE=$(mktemp aptitude-run-mailreport.XXXXXXXXXX)
REPORT_RCPT=root
ERROR='\bError\b|\bErr:|\bE:|^aptitude exited with value [^0]'
REPORT_LOG_DROP=''
REPORT_LOG_IGNORE=''

if [ -f /etc/default/aptitude-robot ]; then
. /etc/default/aptitude-robot
REPORTING_HELPERS="$(dirname $0)/reporting-helpers"
if [ -f "$REPORTING_HELPERS" ]; then
. "$REPORTING_HELPERS"
else
echo "E: $REPORTING_HELPERS helpers not present, bailing out." 1>&2
exit 1;
fi

if [ -z "$REPORT_FILE" ]; then
Expand All @@ -17,9 +18,6 @@ if [ -z "$REPORT_FILE" ]; then
exit 1
fi

# TODO: The following contains code very similar to
# xymon-report. Should be factored out.

cat >> $REPORT_FILE << EOF
Aptitude-Robot Report
=====================
Expand All @@ -28,45 +26,17 @@ EOF
#
# check for remaining upgrades
#
UPGRADES_FILE=$(mktemp aptitude-upgrades.XXXXXXXXX)
if [ -z "$UPGRADES_FILE" ]; then
REMAINING_UPGRADES="$(remaining_upgrades)"
if [ -n "$REMAINING_UPGRADES" ]; then
COLOR=red
MSG="${MSG}ERROR: $(basename $0): Can't check for remaining upgrades:
Couldn't create temporary file in `pwd` using mktemp.
"
else
aptitude search '~U !~ahold' > "$UPGRADES_FILE"
if [ -s "$UPGRADES_FILE" ]; then
REPORT_MAIL=true
cat >> "$REPORT_FILE" << EOF
Remaining Upgrades
------------------
Some upgrades need to be looked into manually
EOF
cat "$UPGRADES_FILE" >> "$REPORT_FILE"
fi
rm "$UPGRADES_FILE"
MSG="${MSG}${REMAINING_UPGRADES}"
fi

#
# check if we need to filter stuff
#
cat_or_egrep_v() {
if [ -n "$1" ]; then
egrep -v "$1"
else
cat
fi
}

#
# check for errors in log file
#
LOGFILE="$1"
if [ -f "$LOGFILE" ]; then
if cat "$LOGFILE" | cat_or_egrep_v "$REPORT_LOG_IGNORE" | egrep -q -i "$ERROR" >/dev/null 2>/dev/null; then
ERRORS_IN_LOGFILE=$(errors_in_logfile "$1")
if [ "$ERRORS_IN_LOGFILE" = "errors" ]; then
REPORT_MAIL=true
cat >> "$REPORT_FILE" << EOF
Expand All @@ -75,19 +45,18 @@ Log File Errors
Please investigate the errors mentioned in the log file
EOF
cat "$LOGFILE" | cat_or_egrep_v "$REPORT_LOG_DROP" "$LOGFILE" >> "$REPORT_FILE"
fi
else
print_logfile "$1" >> "$REPORT_FILE"
elif [ "$ERRORS_IN_LOGFILE" = "notfound" ]; then
REPORT_MAIL=true
cat >> "$REPORT_FILE" << EOF
Missing Log File
----------------
aptitude-robot produces no log file. Please investigate.
aptitude-robot produced no log file. Please investigate.
EOF
fi

if [ $REPORT_MAIL = true ]; then
mail -s "aptitude-robot report on $(hostname)" $REPORT_RCPT < "$REPORT_FILE"
if [ "$REPORT_MAIL" = true ]; then
mail -s "aptitude-robot report on $(hostname)" "$REPORT_RCPT" < "$REPORT_FILE"
fi
rm "$REPORT_FILE"
97 changes: 97 additions & 0 deletions reporting-helpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# -*- sh -*-
#
# This file is no shell script but to be sourced by other shell
# scripts as a library of common helper functions, etc.

WARN='\bWarn|\bW:|^The following packages have unmet dependencies'
ERROR='\bError\b|\bErr:|\bE:|^aptitude exited with value [^0]'
REPORT_LOG_DROP=''
REPORT_LOG_IGNORE=''

MSG="
Aptitude-Robot Report
=====================
"

if [ -f /etc/default/aptitude-robot ]; then
. /etc/default/aptitude-robot
fi

#
# check for remaining upgrades
#
remaining_upgrades() {
UPGRADES_FILE=$(mktemp aptitude-upgrades.XXXXXXXXX)
if [ -z "$UPGRADES_FILE" ]; then
echo "ERROR: $(basename $0): Can't check for remaining upgrades:
Couldn't create temporary file in `pwd` using mktemp.
"
else
aptitude search '~U !~ahold' > "$UPGRADES_FILE"
if [ -s "$UPGRADES_FILE" ]; then
echo "Remaining Upgrades:
$(cat "$UPGRADES_FILE")
"
fi
rm "$UPGRADES_FILE"
fi
}

#
# check if we need to filter stuff
#
cat_or_egrep_v() {
if [ -n "$1" ]; then
egrep -v "$1"
else
cat
fi
}

#
# mangle filters from egrep syntax to sed syntax, probably incomplete
#
egrep_to_sed() {
printf '%s' "$1" | sed -e 's/[()|%]/\\&/g'
}

#
# check for errors in log file
#
errors_in_logfile() {
LOGFILE="$1"
if [ -f "$LOGFILE" ]; then
if cat "$LOGFILE" | cat_or_egrep_v "$REPORT_LOG_IGNORE" | egrep -q -i "$ERROR" >/dev/null 2>/dev/null; then
echo errors
return 2;
fi
if cat "$LOGFILE" | cat_or_egrep_v "$REPORT_LOG_IGNORE" | egrep -q -i "$WARN" >/dev/null 2>/dev/null; then
echo warnings
return 1
fi
else
echo notfound
return 3
fi
return 0
}

#
# print log file, but filtered
#
print_logfile() {
cat "$1" | cat_or_egrep_v "$REPORT_LOG_DROP"
}

print_logfile_for_xymon() {
print_logfile "$1" | \
sed -e "s%$(egrep_to_sed "$WARN")%\&yellow &%gi;s%$(egrep_to_sed "$ERROR")%\&red &%gi" | \
if [ -n "$REPORT_LOG_IGNORE" ]; then
sed -e "s%&\(yellow\|red\) \(.*\($(egrep_to_sed "$REPORT_LOG_IGNORE")\)\)%\&clear \2%g";
else
cat
fi
}
82 changes: 19 additions & 63 deletions xymon-report
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,41 @@

COLUMN=upd
COLOR=green
WARN='\bWarn|\bW:|^The following packages have unmet dependencies'
ERROR='\bError\b|\bErr:|\bE:|^aptitude exited with value [^0]'
REPORT_LOG_DROP=''
REPORT_LOG_IGNORE=''

MSG="
Aptitude-Robot Report
=====================
"

if [ -f /etc/default/aptitude-robot ]; then
. /etc/default/aptitude-robot
REPORTING_HELPERS="$(dirname $0)/reporting-helpers"
if [ -f "$REPORTING_HELPERS" ]; then
. "$REPORTING_HELPERS"
else
echo "E: $REPORTING_HELPERS helpers not present, bailing out." 1>&2
exit 1;
fi

# TODO: The following contains code very similar to
# mail-log-on-error. Should be factored out.

#
# check for remaining upgrades
#
UPGRADES_FILE=$(mktemp aptitude-upgrades.XXXXXXXXX)
if [ -z "$UPGRADES_FILE" ]; then
REMAINING_UPGRADES="$(remaining_upgrades)"
if [ -n "$REMAINING_UPGRADES" ]; then
COLOR=red
MSG="${MSG}ERROR: $(basename $0): Can't check for remaining upgrades:
Couldn't create temporary file in `pwd` using mktemp.
"
else
aptitude search '~U !~ahold' > "$UPGRADES_FILE"
if [ -s "$UPGRADES_FILE" ]; then
COLOR=red
MSG="${MSG}
Remaining Upgrades:
$(cat "$UPGRADES_FILE")
"
fi
rm "$UPGRADES_FILE"
MSG="${MSG}${REMAINING_UPGRADES}"
fi

#
# check if we need to filter stuff
#
cat_or_egrep_v() {
if [ -n "$1" ]; then
egrep -v "$1"
else
cat
fi
}

#
# mangle filters from egrep syntax to sed syntax, probably incomplete
#
egrep_to_sed() {
printf '%s' "$1" | sed -e 's/[()|%]/\\&/g'
}

#
# check for errors in log file
#
LOGFILE="$1"
if [ -f "$LOGFILE" ]; then
if cat "$LOGFILE" | cat_or_egrep_v "$REPORT_LOG_IGNORE" | egrep -q -i "$WARN" >/dev/null 2>/dev/null; then
COLOR=yellow
fi
if cat "$LOGFILE" | cat_or_egrep_v "$REPORT_LOG_IGNORE" | egrep -q -i "$ERROR" >/dev/null 2>/dev/null; then
COLOR=red
fi
ERRORS_IN_LOGFILE=$(errors_in_logfile "$1")
if [ "$ERRORS_IN_LOGFILE" = "warnings" ]; then
COLOR=yellow
MSG="${MSG}
$(print_logfile_for_xymon "$1")
"
elif [ "$ERRORS_IN_LOGFILE" = "errors" ]; then
COLOR=red
MSG="${MSG}
$(cat "$LOGFILE" | \
cat_or_egrep_v "$REPORT_LOG_DROP" | \
sed -e "s%$(egrep_to_sed "$WARN")%\&yellow &%gi;s%$(egrep_to_sed "$ERROR")%\&red &%gi" | \
sed -e "s%&\(yellow\|red\) \(.*\($(egrep_to_sed "$REPORT_LOG_IGNORE")\)\)%\&clear \2%g" )
$(print_logfile_for_xymon "$1")
"
else
elif [ "$ERRORS_IN_LOGFILE" = "notfound" ]; then
COLOR=red
MSG="${MSG}
Expand Down

0 comments on commit f4f2d81

Please sign in to comment.