Find out the percentage of space:
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }'
Output:
71% /dev/hdb1 98% /dev/hdb5
Write a shell script
Above command displays field 5 and 1 of df command. Now all you need to do is write a script to see if the percentage of space is >= 90% :
#!/bin/sh df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output; do echo $output usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) partition=$(echo $output | awk '{ print $2 }' ) if [ $usep -ge 90 ]; then echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | mail -s "Alert: Almost out of disk space $usep%" [email protected] fi done
Save and install script as cronjob. Copy script to /etc/cron.daily/
# cp diskAlert /etc/cron.daily/ # chmod +x /etc/cron.daily/diskAlert
OR install as cronjob:
crontab -e
Write cronjob as per your requirement
10 0 * * * /path/to/diskAlert
Updated script version
Updated script – You can exclude selected filesystem in case you don’t want monitor all filesystems.
#!/bin/sh # set -x # Shell script to monitor or watch the disk space # It will send an email to $ADMIN, if the (free available) percentage of space is >= 90%. # ------------------------------------------------------------------------- # Set admin email so that you can get email. ADMIN="root" # set alert level 90% is default ALERT=90 # Exclude list of unwanted monitoring, if several partions then use "|" to separate the partitions. # An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5" EXCLUDE_LIST="/auto/ripper" # #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: # function main_prog() { while read 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 server $(hostname), $(date)" | \ mail -s "Alert: Almost out of disk space $usep%" $ADMIN fi done } if [ "$EXCLUDE_LIST" != "" ] ; then df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | main_prog else df -H | grep -vE "^Filesystem|tmpfs|cdrom" | awk '{print $5 " " $6}' | main_prog fi