Script to notify via email when low on disk space

I found the below mentioned script on the internet, which notifies via email when server gets low on storage or as per defined. I run it every 5 minutes, if i am low on storage it keeps notifying me with email after 5 minutes. What i need is to get only 1 email notification when storage limit set exceeds because 1 notification is enough. I will clean up storage and if it again exceeds i again get notified once and so on. i run script with crontab.

#!/bin/sh
# Purpose: Monitor Linux disk space and send an email alert to $ADMIN
ALERT=90 # alert level 
ADMIN="your@email.here" # dev/sysadmin email ID
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read -r output;
do
  echo "$output"
  usep=$(echo "$output" | awk '{ print $1}' | cut -d'%' -f1 )
  partition=$(echo "$output" | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "Running out of space "$partition ($usep%)" on $(hostname) as on $(date)" |
    mail -s "Alert: Almost out of disk space $usep%" "$ADMIN"
  fi
done
Asked By: Blohsh

||

I have modified your script – It will now create a lock file for every partition you got notified and stops mailing for this partition. If the partition is in clean state, the lockfile will get removed and you can get notified again if the partition goes in bad state again:

#!/bin/bash
# Purpose: Monitor Linux disk space and send an email alert to $ADMIN
ALERT=90 # alert level
ADMIN="your@email.here" # dev/sysadmin email ID
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read -r output;
do
  echo "$output"
  usep=$(echo "$output" | awk '{ print $1}' | cut -d'%' -f1 )
  partition=$(echo "$output" | awk '{ print $2 }' )
  lockname=$(echo $partition | tr / _);
  if [ $usep -ge $ALERT ]; then
   if test -f "./$lockname.lock";
    then echo "Found lockfile - Cancel mail";
    else
   touch "./$lockname.lock"
   echo "Running out of space "$partition ($usep%)" on $(hostname) as on $(date)" |
   mail -s "Alert: Almost out of disk space $usep%" "$ADMIN"
   fi
  else
   if test -f "./$lockname.lock"; then rm "./$lockname.lock"
   fi
 fi
done
Answered By: Eekiig