Skip to content

ntfy.sh Low Disk Alert

Usage

This script is designed to be run using either a SystemD timer, or a CronJob. It is a BASH script that runs when specified and notifies using ntfy.sh when disk space is low.

Script

This script is taken from here

#!/bin/bash

mingigs=10
avail=$(df | awk '$6 == "/" && $4 < '$mingigs' * 1024*1024 { print $4/1024/1024 }')
topicurl=https://ntfy.sh/mytopic

if [ -n "$avail" ]; then
  curl \
    -d "IMPORTANT | Only $avail GB available on the root disk." \
    -H "Title: Low disk space alert on $(hostname)" \
    -H "Priority: high" \
    -H "Tags: warning,cd" \
    $topicurl
fi

Script Setup

Using the above script, you can put this into a script file.

Create a script using your text editor of choice.

nano /opt/disk-space-checker-ntfy/disk-space-ntfy.sh

Input the above script, and modify this as needed.

  • Replace the topicurl
  • Edit text if required

Set the script to be executable.

sudo chmod +x /opt/disk-space-checker-ntfy/disk-space-ntfy.sh

Cronjob Setup

Tip: Use crontab.guru to create an expression for a cronjob.

Enter /etc/crontab to create a cronjob.

crontab -e

Input the following (or similar).

0 12 * * * /opt/disk-space-checker-ntfy/disk-space-ntfy.sh

This should run the script stored at /opt/disk-space-checker-ntfy/disk-space-ntfy.sh every 12 hours, everyday.

Customised Script

Here is a customised script showing the last-run date in a file using touch.

  • This is useful to see when the script last ran a check.
#!/bin/bash

mingigs=20
avail=$(df | awk '$6 == "/" && $4 < '$mingigs' * 1024*1024 { print $4/1024/1024 }')
topicurl=http://192.168.0.5/servermon

now=$(date +"%Y-%m-%d")

if [ -n "$avail" ]; then
  curl \
    -d "IMPORTANT | Only $avail GB available on the root disk." \
    -H "Title: Low disk space alert on $(hostname)" \
    -H "Priority: high" \
    -H "Tags: warning,disk,server" \
    $topicurl
fi

touch /opt/disk-space-checker-ntfy/last-run-date-$now