Monitoring Linux disk usage and notifying when it’s low

I’ve a couple of Linux servers that I don’t particularly pay much attention to monitoring manually but really don’t want to run out of disk space. The following is a little shell script that sends a message to my Phone via the Android Pushover client.

#!/bin/bash
 
# Set this to be the minimum number of GB before we start notifying
notify=100
 
# Add as many paths to the df command to monitor as you want, or just use /
df /path/to/monitor/ /another/path --block-size=1GB | awk '{if (NR!=1) {print $6,"\t",$4 }}' | {
        while read x
        do
                location=`echo "$x" | awk '{print $1}'`
                remaining=`echo "$x" | awk '{print $2}'`
 
                if [[ $remaining -lt $notify ]]
                then
                        send=true
                        message="$message $location [$remaining GB] "
                fi
        done
 
        if [[ ! -z "$message" ]]
        then
                /usr/bin/php /home/rob/.bin/notify "Disk mounts with under $notify GB remaining. $message"
        fi
}

I use a php script [/home/rob/.bin/notify] to send the notification to Pushover, but there are other APIs available.

<!--?php curl_setopt_array($ch = curl_init(), array(   CURLOPT_URL =--> "https://api.pushover.net/1/messages.json",
  CURLOPT_POSTFIELDS => array(
  "token" => "YOUR_TOKEN",
  "user" => "YOUR_USER",
  "message" => "$argv[1]",
)));
curl_exec($ch);
curl_close($ch);
?>

You can then just stick the script on a CRON schedule at a convenient time you’d like to be notified.

Leave a Reply