Skip to content

Instantly share code, notes, and snippets.

@CorentinGC
Last active November 18, 2024 22:11
Show Gist options
  • Save CorentinGC/19b20057edfa15b9536c07c8a4a03dd8 to your computer and use it in GitHub Desktop.
Save CorentinGC/19b20057edfa15b9536c07c8a4a03dd8 to your computer and use it in GitHub Desktop.
Send mail when no more disk space left (msmtp)
#!/bin/bash
# Purpose: Monitor Linux disk space and send an email alert to $ADMIN
DIRNAME=$(dirname "$0")
ALERT=90 # alert level
ADMIN="[email protected]"
MAIL_CONTENT="Subject:[$(uname -n)] No more space left\\r\\n"
df -H | grep -vE '^Filesystem|tmpfs|cdrom|udev|efivars' | awk '{ print $5 " " $1 }' | {
while read -r OUTPUT;
do
USED=$(echo "$OUTPUT" | awk '{ print $1}' | cut -d'%' -f1 )
PARTITION=$(echo "$OUTPUT" | awk '{ print $2 }' )
LOCKNAME=$(echo $PARTITION | tr / _);
if [ $USED -ge $ALERT ]; then
if test -f "$DIRNAME/$LOCKNAME.lock";
then echo "Found lockfile for $PARTITION";
else
touch "$DIRNAME/$LOCKNAME.lock"
echo "No more space on $PARTITION ($USED%)"
ALERT_TEXT="- No more space left on $PARTITION ($USED%)\\r\\n\\r\\n"
MAIL_CONTENT="${MAIL_CONTENT}${ALERT_TEXT}"
fi
else
if test -f "$DIRNAME/$LOCKNAME.lock"; then rm "$DIRNAME/$LOCKNAME.lock"
fi
fi
done
echo -e $MAIL_CONTENT | msmtp $ADMIN
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment