Blog

Low Disk Storage Slack Notification

image

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:

  • Ubuntu Web server

  • An application that allows one to SSH into a server

  • WINSCP (optional)

  • A Slack account

 

Step 1 Creating the Cron job:

  • In your server go to the /etc/cron.daily/ directory

  • Create a file named “diskstatus” (do not put any extension)

  • Within the file place the following code:

#!/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