From 64527ae96ac402080cf5154377a7e84486a2f219 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Sat, 7 Dec 2019 22:13:13 +0100 Subject: [PATCH] Wrote an apt watcher script --- Perso/AptUpdateWatcher.sh | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 Perso/AptUpdateWatcher.sh diff --git a/Perso/AptUpdateWatcher.sh b/Perso/AptUpdateWatcher.sh new file mode 100755 index 0000000..ee0afbe --- /dev/null +++ b/Perso/AptUpdateWatcher.sh @@ -0,0 +1,61 @@ +#! /bin/bash + +# This script updates the apt database if running as root and retrieves the number of upgradable packages. +# If the count is high enough or if some important packages can be upgraded, inform the administrator. + + +while getopts ":c:p:h" option; do + case ${option} in + c ) + trigger_count=$OPTARG + ;; + p ) + # Add each matching option to the array + key_packages+=("$OPTARG") + ;; + h | \? | : ) + echo "Usage : AptUpdateWatcher [-c count] [-p package matching regex] [-h] [-?]" + echo " -c : Change the minimum count of upgradable packages before sending a warning" + echo " -p : Add an expression to watch for important packages. If a package matching this option is found, send a warning" + echo " -h/-? : Prints this message" + exit 1 + ;; + esac +done + +# Use default options if nothing provided + +if [ -z "$trigger_count" ]; then + trigger_count=20 +fi + +if [ -z "$key_packages" ]; then + key_packages=( "ssh" "ssl" "apache" "kernel" ) +fi + + +if [ "$EUID" -eq 0 ]; then + apt-get update +fi + +upgradable_packages="$(apt-get -s -V upgrade | grep -e'=>')" +upgradable_count=$(echo "$upgradable_packages" | wc -l) + +if [ $upgradable_count -gt $trigger_count ]; then + output="There are more than $trigger_count packages ready to upgrade ($upgradable_count packages):\n" + output+="$upgradable_packages\n\n" +fi + +for expression in "${key_packages[@]}"; do + matching_packages=$(echo "$upgradable_packages" | grep -e"$expression") + if [ -n "$matching_packages" ]; then + output+="Packages matching the expression '""$expression""' can be upgraded: \n" + output+="$matching_packages\n\n" + fi +done + +if [ -n "$output" ]; then + output="The Apt Update Watcher has been triggered.\n\n""$output" + + echo -e "$output" # Can be replaced by anything or piped into sendmail +fi