#!/bin/bash # ------------------------------------------------------------------------- # MONITORING FILESYSTEM CHECK FOR THE LINUX/UNIX MACHINES. # THE SCRIPT DETECTS THE MOUNTPOINTS AUTOMATICALLY. # SCRIPT WORK WITH NAGIOS/ICINGA BUT COULD BE USED WITH ANY OTHER MONITORING TOOL # ---------------------------------------------------------------------- # Piotr Mozdzynski p i o t v [at] k c n . p l # # Exclude list , separated with the "|" - An example: EXCLUDE="/dev/hdd1|/dev/hdc5" EXCLUDE="/boot/efi|/dev/sxa" ## THRESHOLD ## # Accept external threshold input in decimal ( without % ) , for example: # ./check_disk 50 75 # above will set the the warning to 50% and criticat to 75% # if the imput was not specified , set the defaut warning to 90% and critical to 95% if [ -z "$1" ] || [ -z "$2" ] then WARNING=90 CRITICAL=95 else WARNING=$1 CRITICAL=$2 fi # clean previous results from temporary file and set the new one rm -rf .disk.check.result touch .disk.check.result # exit codes exit_ok() { echo "OK , the filesystem on $(hostname) as on $(date)" && exit 0 } exit_warning() { cat .disk.check.result | while read line do echo "$line" done exit 1 } exit_critical() { cat .disk.check.result | while read line do echo "$line" done exit 2 } # Store the temporary output into control file. In case of both conditions found store warnings on the second line of the file. # critical messages will aways overwrite warnings, warnings will be stored on second line till the critical are fixed. save_warning() { # output warning into the control file echo "WARNING Out of space \"$partition ($usep%)\" on $(hostname) as on $(date) " >> .disk.check.result } save_critical() { # always overwrite the warning with the critical on the control file. ( after cleared critical , the warning will show on the next check ) echo "CRITICAL Out of space \"$partition ($usep%)\" on $(hostname) as on $(date) " > .disk.check.result } # loop function and output to control file function loop() { while read output; do usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1) partition=$(echo $output | awk '{print $2}') if [ "$usep" -gt "$WARNING" ] && [ "$usep" -lt "$CRITICAL" ] ; then save_warning elif [ $usep -ge $CRITICAL ] ; then save_critical fi done } # run the loop function with the excluded from checking partitions if [ "$EXCLUDE" != "" ] ; then df -hP | grep -vE "^[^/]|tmpfs|cdrom|${EXCLUDE}" | awk '{print $5 " " $6}' | loop else df -hP | grep -vE "^[^/]|tmpfs|cdrom"| awk '{print $5 " " $6}' | loop fi # read the .disk.check.result to display correct exit status. value=$(cat .disk.check.result) case $value in *CRITICAL*) exit_critical ;; *WARNING*) exit_warning ;; *) exit_ok ;; esac #save the OK ( not functionally needed , only for debug ) echo "OK" > .disk.check.result