So proud of my disk size monitoring script, so I'm just going to share it here as-is:

#!/bin/bash

# diskspace.sh

# Set up the mountpoint

MOUNTPOINT="/mnt/somethingsomething"

# Get disk usage and total

DISK_USAGE=$(df -hk | grep "$MOUNTPOINT" | awk '{print $3}')

DISK_TOTAL=$(df -hk | grep "$MOUNTPOINT" | awk '{print $2}')

# Convert from block sizes to GB

DISK_USAGE_GB=$((DISK_USAGE / 1024 / 1024))

DISK_TOTAL_GB=$((DISK_TOTAL / 1024 / 1024))

# How much is allowed to be left

DISK_GB_LIMIT="8"

# How much is left

DISK_GB_LEFT=$((DISK_TOTAL_GB - DISK_USAGE_GB))

if [ "$DISK_GB_LEFT" -lt "$DISK_GB_LIMIT" ]; then

echo "Less than ${DISK_GB_LIMIT} GB left! Only ${DISK_GB_LEFT} GB left!"

else

echo "There is still ${DISK_GB_LEFT} of ${DISK_TOTAL_GB} GB left. No need to be alarmed as the alarming limit is ${DISK_GB_LIMIT} GB."

# Your heartbeat curl here:

curl https://example.com/endpoint;

fi

Set up a cronjob:

# Disk size heartbeat

*/3 * * * * bash /path/to/diskspace.sh >/dev/null 2>&1

#MastoAdmin #Bash #Scripts #Linux #Server #Code

Reply to this note

Please Login to reply.

Discussion

nostr:npub1cmgqvz7xr07euwkum3mjghjqcu4d3k2fcyf6g4uwwe5ggnd6fetq0wrzd2 Neat-o. You can make it even more robust:

1. Check if mountpoint is available with `findmnt --real -rno TARGET "/mnt/somethingsomething" || printf '%s' 'WARNING: No mountpoint found!'`

2. You can probe hb url and then print results, if all is okay: `host example.com/endpoint >/dev/null || printf '%s' 'WARNING: No hostname found'`

Add some logging and profit even more.

nostr:npub1cmgqvz7xr07euwkum3mjghjqcu4d3k2fcyf6g4uwwe5ggnd6fetq0wrzd2 because I feel obliged to rewrite all Bash scripts into Perl:

use v5.36;

use strict;

use warnings;

# cpan Filesys::DfPortable to install or look in your package manager

use Filesys::DfPortable;

my $mountpoint = "/";

my $GB = 1024 ** 3;

my $diskLimit = 8;

my ($diskUsage, $diskTotal, $diskFree);

# I'd use Filesys::DfPortable.

# This actually works for Win95 and above, Linux, *BSD, HP-UX,

# AIX, Solaris, macOS, Iris, Cygwin etc.

my $ref = dfportable($mountpoint, $GB);

$diskUsage = int($ref->{bused});

$diskTotal = int($ref->{blocks});

$diskFree = int($ref->{bavail});

# But if you really *require* parsing df:

my @dfOutput = `df -hk $mountpoint`;

foreach my $line (@dfOutput) {

# Extract the necessary fields using regex.

# I am essentially scanning for the 2nd and 3rd fields.

if ($line =~ /^\S+\s+(\d+)\s+(\d+)/) {

$diskUsage = $1;

$diskTotal = $2;

last;

}

}

$diskFree = $diskTotal - $diskUsage;

if ($diskFree < $diskLimit) {

print "Less than $diskLimit GB left! Only $diskFree GB left!\n";

}

else {

print "There is still $diskFree/$diskTotal GB left. No need to be alarmed as the limit is $diskLimit GB.\n";

my $curl = `curl example.com/endpoint`;

}

nostr:npub1cmgqvz7xr07euwkum3mjghjqcu4d3k2fcyf6g4uwwe5ggnd6fetq0wrzd2 I am learning bash scripting so I will for sure take this script and play with it. Thank you for sharing.