Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am starting to use nagios to monitor some of our systems, and i was wondering if anyone had ever come across a way to make it monitor directories for files.

I want to look in a directory and say there are x number of files in there that are over x mins old, so warn me.

Cheers

share|improve this question

1 Answer 1

up vote 3 down vote accepted

i found a plugin for this (can't remember where), which seems to work well in production.

define command{
    command_name    check_numfiles
    command_line    $USER1$/check_numoffiles.sh $ARG1$
    }

and

check_command           check_numfiles!-d /var/spool/sms/failed -w 1 -c 2

it goes warning if the directory contains one or more files, critical if two or more. the plugin uses find, so modifying it with a -mtime xxx to check only for files over a certain age would be trivial.

the plugin itself is:

#!/bin/sh
#
# ## Plugin for Nagios to monitor how man files a directory contain
# ## Written by Bernd Mueller (http://www.lisega.de/)
# ##
# ## - 20070426 coded and tested for Linux
# ## - no jet published on NagiosExchange
#
#
# ## You are free to use this script under the terms of the Gnu Public License.
# ## No guarantee - use at your own risc.
#
#
# Usage: ./check_nomoffiles -d <path> -w <warn> -c <crit>
#
# ## Description:
#
# This plugin determines the number of files in a directory
# and compares it with the supplied thresholds.
#
# ## Output: 
#
# The plugin prints the Count of Files in the directory followed by "ok" or
# either "warning" or "critical" if the corresponing threshold is reached.
#
# Exit Codes
# 0 OK       Directory Count of files checked and everything is ok
# 1 Warning  Directory Count of files above "warning" threshold
# 2 Critical Directory Count of files above "critical" threshold
# 3 Unknown  Invalid command line arguments or could not determine directory size
#
# Example: check_numoffiles -d . -w 1000 -c 1400
#
# 121 Files - ok         (exit code 0)
# 1234 Files - warning   (exit code 1)
# 1633 Files - critical  (exit code 2)


# Paths to commands used in this script.  These
# may have to be modified to match your system setup.

PATH=""

find="/usr/bin/find"
xargs="/usr/bin/xargs"
tail="/usr/bin/tail"
awk="/usr/bin/awk"
cut="/usr/bin/cut"
wc="/usr/bin/wc"

PROGNAME=`/bin/basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="Revision 1.0"
AUTHOR="(c) 2007 Bernd Mueller (http://www.lisega.de/)"

# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

print_revision() {
    echo "$REVISION $AUTHOR"
}

print_usage() {
    echo "Usage: $PROGNAME -d <path> -w <warn> -c <crit>"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}

print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    echo "Directory Files monitor plugin for Nagios"
    echo ""
    print_usage
    echo ""
}

# Make sure the correct number of command line
# arguments have been supplied

if [ $# -lt 1 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi

# Grab the command line arguments

thresh_warn=""
thresh_crit=""
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $VERSION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $VERSION
            exit $STATE_OK
            ;;
        --dirname)
            dirpath=$2
            shift
            ;;
        -d)
            dirpath=$2
            shift
            ;;
        --warning)
            thresh_warn=$2
            shift
            ;;
        -w)
            thresh_warn=$2
            shift
            ;;
        --critical)
            thresh_crit=$2
            shift
            ;;
        -c)
            thresh_crit=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

##### Get size of specified directory

error=""
statresult=`$find $dirpath -maxdepth 1 -type f | $wc -l |$tail -1`
dirsize=`echo $statresult`
result="ok"
exitstatus=$STATE_OK

##### Compare with thresholds

if [ "$thresh_warn" != "" ]; then
    if [ $dirsize -ge $thresh_warn ]; then
        result="warning"
        exitstatus=$STATE_WARNING
    fi
fi
if [ "$thresh_crit" != "" ]; then
    if [ $dirsize -ge $thresh_crit ]; then
        result="critical"
        exitstatus=$STATE_CRITICAL
    fi
fi

echo "$dirsize Files - $result"
exit $exitstatus
share|improve this answer
    
cool that looks promising, i'll give it a go tomorow –  beakersoft Oct 18 '10 at 21:01
    
That worked a treat thanks a lot –  beakersoft Oct 21 '10 at 16:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.