Damit wir über die neusten Debian Pakete informiert werden richten wir uns das folgende Skript ein und aktivieren ein Cronjob für täglich 01:00.
root:~# ( cat <<HERE5
#!/bin/bash
# $File: debpackages.sh $
# $Author: mschulz $ - $Date: 2018-02-21 12:10:59 +0100 (Mi, 21 Feb 2018) $
# $HeadURL: http://svn.tuxnet24.de/operations/scripts/debpackages.sh $ - $Revision: 55 $
# $Description: Script to get a list of debian packages, witch has to be updated. $
# ______________________________________________________
# Get the real hostname
hostname=$( [ -x /home/sysadmin/bin/hostname ] && /home/sysadmin/bin/hostname || hostname )
# The path to the temporary logfile for this program
logfile="/tmp/debpackages.log"
# The notify mail address
notify="it-monitoring@assona.net"
# The subject of the notify message
subject="[${LOGNAME}@$hostname] Debian Package Report"
# ##
# This function send a notify message and exit the program.
#
# @param string $1 The state (ERROR|OK)
# @param string $2 The notify mail address
# @param string $3 The notify subject
# @param string $4 The path to the report logfile
# @param string $5 The notify message
# @return void
#
function f_notify () {
local count="$1"
local notify="$2"
local subject="$3"
local logfile="$4"
local msg="$5"
# Create content of the mail body
local header="$( sed -n -e '2,7p' $0 | sed 's/^# //g' )"
local logtrace=$( [ -f "$logfile" ] && cat $logfile 2>/dev/null || echo -e "$logfile" )
# Send notify message
echo -e "${header}\n\n*${msg}*\n\n${logtrace}\n\n-- \n" | mail -s "$subject [${count}]" $notify
}
# ##
# MAIN
# Get a clean exit
trap "rm -rf ${logfile} 2>/dev/null" EXIT
touch ${logfile}
# Write a temporary upgrade list by executing the command apt-get -s dist-upgrade
apt-get update >/dev/null 2>&1 && apt-get -s dist-upgrade >${logfile}
# Get only the upgrade list
upgrades="$(perl -ne 'print $_ if ( $_ =~ m/(?:he following packages will|ie folgenden Pakete werden)/io .. $_ =~ m/(?:upgraded|aktualisiert)\./io );' ${logfile})"
if [ -n "${upgrades}" ]; then
# Get the count of the packages witch will be upgraded
count="$(perl -ne 'print $1 if ($_ =~ m/^(\d+).+(?:upgraded|aktualisiert)\./ig)' ${logfile})"
# Sent a notify message
f_notify "${count}" "${notify}" "${subject}" "${upgrades}" "On ${hostname} are currently ${count} packages pending an upgrade"
fi
# vim: syntax=bash ts=4 sw=4 sts=4 sr noet
# EOF
HERE5
) >/usr/local/sbin/debpackages.sh
Nachdem wir das Skript erstellt haben, machen wir es ausführbar.
root:~# chmod 744 /usr/local/sbin/debpackages.sh
Nun tragen wir noch den Cronjob für täglich 01:00 Uhr ein.
root:~# cat <(crontab -l) <(echo -e "# mschulz - $(date +%Y%m%d) - Notify about new debian packages\n0 1 * * * /usr/local/sbin/debpackages.sh >/dev/null >&1") | crontab -