2014-11-20 23:24:10 +10:00
|
|
|
#!/bin/bash
|
2016-06-07 20:42:19 +10:00
|
|
|
# Script Name: diskalert
|
|
|
|
# Author: Matt McKinnon
|
|
|
|
# Date: 7th June 2016
|
|
|
|
# Description:
|
|
|
|
# This script will email when diskspace is high.
|
|
|
|
|
2017-06-15 21:06:29 +10:00
|
|
|
MAILTO="support@comprofix.com"
|
|
|
|
MAILFROM="support@comprofix.com"
|
|
|
|
THISSERVER=$(hostname -f)
|
|
|
|
SMTP="mail.comprofix.com"
|
2017-04-04 23:49:53 +10:00
|
|
|
|
2015-06-13 14:17:16 +10:00
|
|
|
LOGFILE="/var/log/diskalert.log"
|
2016-06-07 20:42:19 +10:00
|
|
|
THISSERVER=$(hostname -f)
|
2017-04-04 23:49:53 +10:00
|
|
|
|
2015-06-13 14:17:16 +10:00
|
|
|
|
|
|
|
startlogging() {
|
|
|
|
echo $DASHES2 >> $LOGFILE
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$0 started running at $(date)" >> $LOGFILE
|
2015-06-13 14:17:16 +10:00
|
|
|
echo $DASHES2 >> $LOGFILE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stoplogging() {
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [MESSAGE] $0 finished runnning" >> $LOGFILE
|
2015-06-13 14:17:16 +10:00
|
|
|
echo $DASHES >> $LOGFILE
|
|
|
|
}
|
|
|
|
|
|
|
|
DASHES="---------------------------------------------------------------------------------"
|
|
|
|
DASHES2="================================================================================="
|
|
|
|
|
|
|
|
declare -a DEVICES
|
|
|
|
index=0
|
|
|
|
|
|
|
|
startlogging
|
|
|
|
|
|
|
|
for i in $( df -h | grep "/dev/" | grep -v tmpfs | awk '{print $1}' );
|
|
|
|
do
|
|
|
|
DEVICES[$index]=$i
|
|
|
|
let "index += 1"
|
|
|
|
done
|
|
|
|
|
|
|
|
for i in ${DEVICES[@]};
|
|
|
|
do
|
|
|
|
let space=`df -Pk $i | grep -v ^File | awk '{printf ("%i", $5) }'`
|
|
|
|
if [ $space -le 89 ]
|
|
|
|
then
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [MESSAGE] Disk space usage on $i acceptable. $space% currently in use." >> $LOGFILE
|
2015-06-13 14:17:16 +10:00
|
|
|
fi
|
|
|
|
if [ $space -ge 90 ]
|
|
|
|
then
|
|
|
|
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [WARNING] $i is running out of disk space. $space% currently in use." >> $LOGFILE
|
2015-06-13 14:17:16 +10:00
|
|
|
echo "
|
|
|
|
Hello,
|
|
|
|
|
|
|
|
You are running out of disk space on $THISSERVER.
|
|
|
|
|
|
|
|
Your $i is currently using $space% of disk space.
|
|
|
|
|
|
|
|
See the logfile for more info: vim $LOGFILE
|
|
|
|
|
|
|
|
Regards, " >/tmp/diskalertmail.msg
|
|
|
|
|
2017-06-15 21:06:29 +10:00
|
|
|
sendemail -o tls=no -s $SMTP -t $MAILTO -f "$THISSERVER <$MAILFROM>" -u "[$THISSERVER] is running out of disk space" -m "$(cat /tmp/diskalertmail.msg)" -q
|
|
|
|
|
2016-03-28 18:55:16 +10:00
|
|
|
echo "$(date) [MESSAGE] Running out of disk space email sent to $MAILTO" >> $LOGFILE
|
2015-06-13 14:17:16 +10:00
|
|
|
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
stoplogging
|