Created
October 15, 2021 00:38
-
-
Save comprofix/45f5cec4aacb04b5ba347e27d5bb7717 to your computer and use it in GitHub Desktop.
Check Updates with Discord Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script Name: check_updates_deb | |
# Author Name: Matt McKinnon | |
# Date: 7th June 2016 | |
# Description: For use on Debian Based Systems | |
# This script will: | |
# Clean up the local apt repository of retrieved packages (apt-get clean) | |
# Resync the package index (apt-get update) | |
# If called with AUTOUPDATE set to yes then updates will be downloaded and applied with no feed back (not recommended) | |
# If called without AUTOUPDATE then packages are downloaded and an email is sent informing which packages are to be updated. | |
# And more ;-) | |
# NOTE: Perl is needed for this script to work. | |
# | |
# Make user configuration changes in this section | |
# | |
THISSERVER=$(hostname -f) | |
DISCORD="<REPLACE WITH YOUR DISCORD WEBHOOK>" | |
AUTOUPDATE="no" | |
LOGFILE="/var/log/server_maint.log" | |
THISSERVER=$(hostname -f) | |
# | |
# End of user configuration section | |
# | |
DASHES="---------------------------------------------------------------------------------" | |
DASHES2="=================================================================================" | |
# Check if the script is being run as root exit if it is not. | |
if [ $(id -u) -ne 0 ] | |
then | |
echo "You need to be root to run this script." | |
exit 1 | |
fi | |
startlogging() { | |
echo $DASHES2 >> $LOGFILE | |
echo "$0 started running at $(date)" >> $LOGFILE | |
echo $DASHES2 >> $LOGFILE | |
} | |
stoplogging() { | |
echo "$(date) [MESSAGE] $0 finished runnning" >> $LOGFILE | |
echo $DASHES >> $LOGFILE | |
} | |
check_return() { | |
if [ "$?" -ne "0" ] | |
then | |
echo "$(date) [ERROR] $1 failed to run" >> $LOGFILE | |
send_error_email $1 | |
stoplogging | |
exit 1 | |
fi | |
echo "$(date) [SUCCESS] $1 ran without error" >> $LOGFILE | |
} | |
send_error_email() { | |
sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "[$THISSERVER] There was an error whilst running $0" -m " | |
Hello, | |
Whilst running the update script ($0) on $THISSERVER there was a problem. | |
[ERROR] "$1" failed to run | |
Please log in via ssh (e.g. ssh root@$(hostname -f)) and check the log file: | |
vim $LOGFILE | |
Regards." -q | |
} | |
startlogging | |
apt-get clean > /dev/null | |
check_return "apt-get clean" | |
apt-get update > /dev/null | |
check_return "apt-get update" | |
if [[ "$AUTOUPDATE" == "yes" ]] | |
then | |
apt-get -yqq upgrade > /dev/null | |
check_return "apt-get -yq upgrade" | |
else | |
PACKAGES_TO_BE_UPGRADED=$(apt-get -Vs upgrade) #| perl -ne 'print if /upgraded:/ .. /upgraded,/') | |
apt-get -yqd upgrade > /dev/null | |
check_return "apt-get -yqd upgrade" | |
fi | |
if [[ -z $PACKAGES_TO_BE_UPGRADED ]] | |
then | |
echo "$(date) [MESSAGE] No packages need updating." >> $LOGFILE | |
else | |
PACKAGES=$(apt list --upgradeable | awk '{print $1}' | jq -Rs . | awk -F\" '{print $2}') | |
./discord.sh/discord.sh \ | |
--webhook-url="$DISCORD" \ | |
--username "Servers" \ | |
--color "0xBD1E13" \ | |
--title "Servers require updates!" \ | |
--field "HOSTNAME;$THISSERVER;false" \ | |
--field "Packages;$PACKAGES" \ | |
--timestamp | |
echo " | |
Hello, | |
Packages have been downloaded onto $THISSERVER. | |
$PACKAGES_TO_BE_UPGRADED | |
To update the server log in via ssh (e.g. ssh root@$(hostname -f)) and run the following command: | |
apt-get upgrade | |
See the logfile for more info: vim $LOGFILE | |
Regards. " >/tmp/servermail.msg | |
#sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "[$THISSERVER] server may need some updates applied" -m "$(cat /tmp/servermail.msg)" -q | |
echo "$(date) [MESSAGE] Packages need updating email sent to $MAILTO" >> $LOGFILE | |
fi | |
stoplogging | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment