#!/bin/bash # -*- Mode: sh -*- # check_disks_space.sh ----------------------------------------------------- # Author : paissad (paissad_temp-spam@yahoo.fr) # Created On : Fri Oct 29 13:10:16 CEST 2010 # Revision N° : 3 # Status : Stable # Description : Monitor or watch the disk space. # Send an email if used disk space is >= 90% by default. # Or if disk space is >= $Alert # Depends : # Licence : GPLv3 # __ __ # .-----..---.-.|__|.-----..-----..---.-..--| | # | _ || _ || ||__ --||__ --|| _ || _ | # | __||___._||__||_____||_____||___._||_____| # |__| # --------------------------------------------------------------------------- # Example of use: # mailTo="user@mail.com user2@localhost" Alert=95 ./check_disks_space.sh # This will set threshold alert to 95% and send email to user@mail.com and # user2@localhost # Set an admin that will receive the mails (default: webmaster) mailTo=${mailTo:-"$USER"} # Set the threshold (default: 90%) Alert=${Alert:-"90"} # If 'Lock' is set to yes (default) ... the script avoids to send another # email alert for the same partition. (May be useful when the script is used # in cron jobs. # The lock will be removed automatically if the partition retreive a normal # disk space (not reach the alert !) Lock=${Lock:-"yes"} LockFile=${LockFile:-"$HOME/.check_disks_space.lock"} # Send another mail 720 minutes (12 hours) after the previous one if the # related disk is still in the lockfile ! # A null or negative value will deactivate this feature ! reminder=${reminder:-"720"} # in minutes # Partitions and/or filesystems to exclude # Must match correct regular expressions for grep Exclude=${Exclude:-^Filesystem|/?tmpfs|/?cdrom[0-9]*|/?loop[0-9]*|/?dvd[0-9]*} # ============================================================================ # You should not change anything below this line, except you know what you are # doing # ============================================================================ set -e umask 0077 trap "if (($?)); then echo \"Error during the run of the script\";fi" EXIT if [[ "$Lock" = "yes" ]]; then test -f $LockFile || { mkdir -p $(dirname $LockFile); touch $LockFile; } fi # $1 -> message # $1 -> subject # $3 -> admin(s) who receive the mail function sendEmail(){ echo -e "$1" | mail -s "$2" "$3" } LANG=C df -HP \ | grep -vE "$Exclude" \ | awk '{ print $5 " " $1 }' \ | while read output do usep=$(echo "$output" | awk '{ print $1}' | cut -d'%' -f1) partition=$(echo "$output" | awk '{ print $2 }') AlertSubject="Alert: Almost out of disk space for \"$partition ($usep%)\"" AlertMessage=" ----------------------------------------------------------------------------- * The following partition has reached the threshold alert of $Alert%. - Date : $(date +"%a %d-%b-%Y %T (%Z)") - Partition : $partition - Disk space used : $usep% - Machine hostname : $(hostname -f) ----------------------------------------------------------------------------- " if [[ "$usep" -ge "$Alert" ]]; then echo >&2 -e "$AlertMessage" if [[ "$Lock" = "yes" ]]; then # Add partition to the lockfile if not present yet ! grep -q -E -x "$partition" $LockFile 2>&1 > /dev/null \ || { echo "$partition" >> "$LockFile"; \ sendEmail "$AlertMessage" "$AlertSubject" "$mailTo"; } # Send a mail as reminder even if the disk has already been added # into the lockfile ! if (($reminder > 0)) && [[ -n "$mailTo" ]]; then now=$(date +%s) lastMtime=$(date -r "$LockFile" +%s) ((diffTime=($now-$lastMtime)/60)) # in minutes ! if (($diffTime >= $reminder)); then grep -q -E -x "$partition" $LockFile 2>&1 > /dev/null && \ sendEmail "$AlertMessage" "$AlertSubject" "$mailTo" && \ touch "$LockFile" fi fi else # Send email in any case (because there is no lock file) sendEmail "$AlertMessage" "$AlertSubject" "$mailTo" fi else # Remove the partition from the$ lock file if it has retreived a # normal disk space if [[ -f "$LockFile" ]]; then if [ "$Lock" = "yes" ]; then partition_nn="$partition" ## partition normal name partition=$(echo "$partition" | sed "s,\/,\\\/,g") grep -q -E -x "$partition_nn" "$LockFile" && \ sed -i"_$$.bak" -e "/$partition/d" "$LockFile" rm -f "${LockFile}"_$$.bak fi fi fi done