Send email notification through mailjet when disk usage reaches 90 percent

Create a script file

cd ~

nano check-disk-space-send-email-when-reach-90percent.sh

Copy this to file

#!/bin/bash
# https://gaelbillon.com
# Mix of https://gist.github.com/fduran/1870429 and https://dev.mailjet.com/email/guides/send-api-v31/
# script that will send an email to EMAIL when disk use in partition PART is bigger than %MAX
# adapt these 3 parameters to your case
MAX=90
PART=vda1

USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`
if [ $USE -gt $MAX ]; then
    # echo "Percent used: $USE" | mail -s "Orpheogroup server running out of disk space" $EMAIL
    # This call sends a message to one recipient.
    curl -s \
    -X POST \
    --user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
    https://api.mailjet.com/v3.1/send \
    -H 'Content-Type: application/json' \
    -d '{
        "Messages":[
            {
                "From": {
                    "Email": "johndoe@gmail.com",
                    "Name": "John Doe"
                },
                "To": [
                    {
                        "Email": "person.to.notify@gmail.com",
                        "Name": "Person to notify"
                    }
                ],
                "Subject": "ServerName server running out of disk space",
                "TextPart": "Over 90% on /dev/vda1"
            }
        ]
    }'
fi

Test the script

Make it executable

chmod +x check-disk-space-send-email-when-reach-90percent.sh

You may want to try the scipt first by changin the MAX variable to 10.

Run it from command line

./check-disk-space-send-email-when-reach-90percent.sh

If it runs ok you'll receive an email and see this:

user@vps1111:~$ ./check-disk-space-send-email-when-reach-90percent.sh
{"Messages":[{"Status":"success","CustomID":"","To":[{"Email":"person.to.notify@gmail.com","MessageUUID":"d9c46508-50da-2b12-b8db-16bac6c9aa7e","MessageID":576480754939158643,"MessageHref":"https://api.mailjet.com/v3/REST/message/576480754939158643"}],"Cc":[],"Bcc":[]}]}

Add a cron job

Add cron job every 3 days

crontab -e

0 */72 * * * /home/user/check-disk-space-send-email-when-reach-90percent.sh > /dev/null 2>&1

For testing cron, try first with a set time. For example, for it's 11:24 and you want to see if the job will be run next minute (at 11:25), use this : 25 11 * * *