E-mail alert when disk space gets low

The following script alerts you when any of your HDD partitions is almost full. For email sending you need to have mailx installed (in CentOS: yum install -y mailx).

#!/bin/sh
 
MAX_ALLOWED_USAGE_IN_PERCENTAGE=90
ALERT_SEND_TO="nekimail@example.com"
HUMAN_FRIENDLY_SERVER_NAME="MYSERVER - `hostname -I`"
 
if [ -n "$(df -P | awk "{ df=strtonum(\$5); if (df > ${MAX_ALLOWED_USAGE_IN_PERCENTAGE}) print; }")" ] ; then
        echo "$(df -Ph | awk "{ df=strtonum(\$5); if (df > ${MAX_ALLOWED_USAGE_IN_PERCENTAGE}) print; }")" | mail -s "${HUMAN_FRIENDLY_SERVER_NAME} - HDD space problem | Used >${MAX_ALLOWED_USAGE_IN_PERCENTAGE}%" ${ALERT_SEND_TO}
fi
 
exit 0