Email a Low Disk Space Alert With Mailgun API


Here’s how to monitor disk space on your server and use the Mailgun API to send an email alert:

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90
MAILGUN_DOMAIN="X"
MAILGUN_APIKEY="Y"
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    curl -s --user "api:$MAILGUN_APIKEY" \
        https://api.mailgun.net/v3/$MAILGUN_DOMAIN/messages \
        -F from="Server <do-not-reply@$mailgun_domain>" \
        -F to=me@somewhere.com \
        -F subject='Disk Space Alert' \
        -F text="Root partition is low! Used: $CURRENT%"
fi

Thanks: https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-via-api
Thanks: https://www.linuxjournal.com/content/tech-tip-send-email-alert-when-your-disk-space-gets-low