patch2mail for SLES10

Posted on April 21st, 2008 by Christian in Life

Well, there is this “nifty” tool called patch2mail, which basically converts the XML for the updates to a more readable format. But you’re screwed if you want to do the same on SLES10. Since it ain’t shipping with the zypper xml wrapper thing, you need to do it a bit different.

So I ended up writing a small (and yet, ugly) shell script to generate me a mail of my liking ..

#!/bin/bash
 
# Checks the output of `zypper pch` for security/recommended/optional
# updates and prepares a detailed report to be mailed to the
# administrators
 
TO="admin-addr@localhost"
CLASSES="security recommended optional"
 
# Temporary files
ZYPP_LIST="$( mktemp /tmp/zypper-list.XXXXXX )"
ZYPP_DETAILS="$( mktemp /tmp/zypper-details.XXXXXX )"
TMP="$( mktemp /tmp/zypper-report.XXXXXX )"
zypper pch 2>/dev/null > $ZYPP_LIST
 
# Figure out how much updates are still pending
PENDING="$( cat $ZYPP_LIST | grep "| Needed" | wc -l )"
 
if [ $PENDING -eq 0 ] ; then
  exit 0
fi
 
echo > $TMP
echo " Pending updates for $( domainname -f ) on $( date )" >> $TMP
 
for severity in $CLASSES; do
  PACKAGES="$( cat $ZYPP_LIST | egrep "${severity}(.*)\| Needed" | \
    cut -d\| -f2 | sed "s,^ ,," )"
  echo
  echo "  Category: $severity"
  for package in $PACKAGES; do
    zypper patch-info $package 2>/dev/null > $ZYPP_DETAILS
    echo "  * $package ($( cat $ZYPP_DETAILS | grep "Version: " | \
      sed "s,Version,version," ))"
    echo "    $( cat $ZYPP_DETAILS | grep "Summary: ")"
    echo "    $( cat $ZYPP_DETAILS | grep "Reboot Required:" )"
    echo
  done
  echo
done >> $TMP
 
cat $TMP | \
  mail -s "[$( date +%F )] Update report for $( domainname -f )" $TO
trap 'rm -f "$TMP" "$ZYPP_LIST" "$ZYPP_DETAILS" >/dev/null 2>&1' 0
trap "exit 2" 1 2 3 15
 
# vim: set tw=80

Leave a Reply