Quick Links

You probably want to know when your server is running out of disk space. An easy solution is to configure a cron job to run daily and send you notifications over Slack if there are any issues.

Get Disk Space With The df Command

The command for viewing disk space on Linux is df -h (for output in a human readable format):

This lists each mounted filesystem, how big the physical disk is, and how much is being used.

To narrow the output down to a specific filesystem (in this case, root) you can run

        df /
    

. However, this returns the header row, so you can filter that off with

        grep
    

:

Then, select the fifth column (the percentage) with

        awk
    

, and remove the percent symbol with

        sed
    

. This gives you one command that returns a number representing how much of your main disk is full:

df / | grep / | awk '{ print $5}' | sed 's/%//g'

Which, in this case, would return "39". If you want to change the disk this command monitors, you'll need to edit the first df statement.

Configure Command Line Slack Notifications

The simplest way to get Slack notifications working is with webhooks. Webhooks allow you to send notifications as POST requests to a URL without having to manage bot users or OAuth. You can format messages as JSON and POST them using curl.

From Slack's API portal, create a new app, select your workspace to add it to, and give it a name. This is what will show up when it messages you, so pick something interesting.

Once you have your app, find the "Incoming Webhooks" tab under "Add features and functionality."

This will let you create a new webhook, and you'll have to choose which channel it should post to. You can create a new channel for bot notifications, or add it to your own DMs.

Once you have the URL, copy the sample request to verify it works. You should see "Hello World!" in the channel you selected. If so, Slack is good to go.

Putting it All Together

To make your server actually notify you, you'll create a script that you can run with a daily cron job. Here's the shell of the script, minus the notification command:

#!/bin/bash

CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')

THRESHOLD=90

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then

fi

This script sets two variables, CURRENT (to the output of the df command from above) and THRESHOLD (to a value you can configure). It then checks if the current value is greater than the threshold, and runs a command if it is.

You can set the notification to be whatever you'd like, but here's an example---not including the webhook URL, which you'll need to paste at the end (no extra flag required):

curl -X POST -H 'Content-type: application/json' --data "{"text":"Your server `$(hostname)` is currently at ${CURRENT}% disk capacity."}"

This will send a nicely formatted notification to your webhook's designated channel, including the server's hostname and the current disk capacity.

You can paste this command in the if statement of the script above, making the whole thing come together like so:

#!/bin/bash

CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')

THRESHOLD=90

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then

curl -X POST -H 'Content-type: application/json' --data "{"text":"Your server `$(hostname)` is currently at ${CURRENT}% disk capacity."}"

fi

Save this as diskspace.sh in your home directory, and make sure it has execute permissions with chmod +x diskpace.sh. Open up your crontab with crontab -e, and add the following line to the end:

0 * * * * ~/diskspace.sh

This will run your script every hour to check if your disk is full. But you'll only get notifications if it is full, so you can test if the script works by setting THRESHOLD below the current disk usage and running it manually.

For more detailed notifications, you can make the script send you the results of df -h, formatted nicely in a code box, just by updating the curl statement:

curl -X POST -H 'Content-type: application/json' --data "{"text":"Your server `$(hostname)` is currently at ${CURRENT}% disk capacity.n ```$(df -h)``` "}"

Which will show up in Slack as:

Since the command the script runs can be anything you want it to be, you could also configure email notifications using a mail handler like Postfix, or any other feed that supports webhooks.