Low Disk Storage Slack Notification

The Engagement 4Cast

Low Disk Storage Slack Notification

Avatar

Blog

08/27/2018

Grant Commodore

It is important to know when your disk is running low on storage for a particular server. This step by step guide will ensure that you get a daily slack notification whenever your server usage is at or above 90%

Prerequisites:

Step 1 Creating the Cron job:

#!/bin/bash

# Parse df selected output

df -h|egrep -v 'File|tmpfs|docker|udev|loop'| 

while read LINE; do

        USED_NUMBER=`echo $LINE |awk '{print $5}'|sed 's/%//'|sed 's/ //g'`

       USED_PERCENT=`echo $LINE |awk '{print $5}'|sed 's/ //g'`

        MOUNT_POINT=`echo $LINE |awk '{print $6}'|sed 's/ //g'`

       if [ $USED_NUMBER -gt 90 ]; then

               # Create message without spaces

               MESSAGE=`echo WARNING On $HOSTNAME disk $MOUNT_POINT is full at $USED_PERCENT usage WARNING|sed 's/ /_/g'`

               # Post message

               curl -X POST --data-urlencode 

               'payload={"channel": "#YOUR CHANNEL NAME", "username": "webhookbot", "text": "'$MESSAGE'", "icon_emoji": ":ghost:"}' 

               YOUR SLACK URL GOES HERE

       fi

done

– Modify the code as needed

– Make sure that the file is executable (you may need to change the permissions)

– To check if the cron job has been added use the command “run-parts –test /etc/cron.daily” it should be listed along with the other cron jobs

-Alternatively, you can place a diskspace.sh file in the /usr/local/bin directory and create a crontab in the /etc directory to have the notification run whenever you want 

Subscribe & stay ahead of the crowd with sage marketing tips and predictions.