Search Exchange

Search All Sites

Nagios Live Webinars

Let our experts show you how Nagios can help your organization.

Contact Us

Phone: 1-888-NAGIOS-1
Email: sales@nagios.com

Login

Remember Me

Directory Tree

CheckDirSize

Rating
9 votes
Favoured:
0
Hits
171084
Files:
FileDescription
check_dirsize.shCheckDirSize V1.0
check_dirsize11.shCheckDirSize V1.1 (with perfdata option)
Network Monitoring Software - Download Nagios XI
Log Management Software - Nagios Log Server - Download
Netflow Analysis Software - Nagios Network Analyzer - Download
Monitor size of directory and notify if size exceedes threshold. Shell script (sh), GPL
## Description

This plugin determines the size of a directory (including sub dirs) and compares it with the supplied thresholds. It might also be used to monitor a regular file instead of a directory.

Usage: check_dirsize -d -w -c -f

## Output:

The plugin prints the size of the directory in KB followed by "ok" or either "warning" or "critical" if the corresponing threshold is reached.

Exit Codes
0 = OK (Directory size checked and everything is ok)
1 = Warning (Directory size above "warning" threshold)
2 = Critical (Directory size above "critical" threshold)
3 = Unknown (Invalid command line arguments or could not determine directory size)

## Example

check_dirsize -d . -w 1000 -c 1400

121 KB - ok (exit code 0)
1234 KB - warning (exit code 1)
1633 KB - critical (exit code 2)

## Changes in V1.1
CheckDirSize now generates perfdata if desired (option -f). Thanks to Jose Vicente Mondejar.
Reviews (6)
There is a bug in the script.

the command line option -perfdata should be --perfdata in the code.

--perfdata is not recognized.
bynishith, December 30, 2019
I'm using NRPE on Client Side. Below is the configuration.

Upload "check_dirsize11.sh" script on Nagios Client Node under "/usr/lib64/nagios/plugins/" directory.

Open nrpe.cfg file & append new line as follows.
vim /etc/nagios/nrpe.cfg
command[check_syslog_dir]=/usr/lib64/nagios/plugins/check_dirsize11.sh -d /var/log/

Restart NRPE Service.
systemctl restart nrpe

Now on the Nagios Server Side, configure remote host as follows:

define service{
use generic-service
host_name linuxbox1.example.local
service_description SYSLOG Directory Size
check_command check_nrpe!check_syslog_dir
}

Save & Exit.

You can also run the NRPE Command from Nagios Server to get the output. The command is,

/usr/local/nagios/libexec/check_nrpe -H 172.16.1.96 -c check_syslog_dir

Important Note:

If you're getting sudo error to access "DU" command, append below line inside Nagios client sudo file.
%nagios ALL=(ALL) NOPASSWD:ALL

Now, modify "check_dirsize11.sh" plugin, line no:46
DU="/usr/bin/sudo /usr/bin/du"

If you want directory size output in "GB", then change line no. 159 as follows.

duresult=`$DU -sh $dirpath 2>&1` || error="Error"

(Replace -sk with -sh)

That's it.
byAposwolf, December 27, 2017
I modified the script, now there is also a -m or --metric flag, where you can select which format the warn and crit input and the echo output should be (KB, MB, GB or TB)
Here it is:

#!/bin/sh
#
# ## Plugin for Nagios to monitor directory size
# ## Written by Gerd Stammwitz (http://www.enbiz.de/)
# ##
# ## - 20040727 coded and tested for Solaris and Linux
# ## - 20041216 published on NagiosExchange
# ## - 20070710 modified by Jose Vicente Mondejar to add perfdata option
#
#
# ## You are free to use this script under the terms of the Gnu Public License.
# ## No guarantee - use at your own risc.
#
#
# Usage: ./check_dirsize -d -w -c
#
# ## Description:
#
# This plugin determines the size of a directory (including sub dirs)
# and compares it with the supplied thresholds.
# It might also be used to monitor a regular file instead of a directory.
#
# ## Output:
#
# The plugin prints the size of the directory in KB followed by "ok" or
# either "warning" or "critical" if the corresponing threshold is reached.
#
# Exit Codes
# 0 OK Directory size checked and everything is ok
# 1 Warning Directory size above "warning" threshold
# 2 Critical Directory size above "critical" threshold
# 3 Unknown Invalid command line arguments or could not determine directory size
#
# Example: check_dirsize -d . -w 1000 -c 1400
#
# 121 KB - ok (exit code 0)
# 1234 KB - warning (exit code 1)
# 1633 KB - critical (exit code 2)


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

PATH=""

DU="/usr/bin/du"
CUT="/usr/bin/cut"
WC="/usr/bin/wc"

PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="Revision 1.1"
AUTHOR="(c) 2004,2007 Gerd Stammwitz (http://www.enbiz.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|--dirname [-w|--warning ] [-c|--critical ] [-f|--perfdata] [-m|--metric ]"
echo "Usage: $PROGNAME -h|--help"
echo "Usage: $PROGNAME -V|--version"
echo ""
echo " must be KB, MB, GB or TB"
}

print_help() {
print_revision $PROGNAME $REVISION
echo ""
echo "Directory size 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=""
metric="KB"
perfdata=0
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
;;
--metric)
metric=$2
shift
;;
-m)
metric=$2
shift
;;
-f)
perfdata=1
;;
-perfdata)
perfdata=1
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done

##### Get size of specified directory
error=""
duresult=`$DU -s $dirpath 2>&1` || error="Error"
if [ ! "$error"="" ]; then
errtext=`echo $duresult | $CUT -f3 -d":"`
echo "$error:$errtext"
exit $STATE_UNKNOWN
fi

dirsize=`echo $duresult | $CUT -f1 -d" "`
result="ok"
exitstatus=$STATE_OK

##### Compare with thresholds
calc_value=1
case $metric in
"MB")
calc_value=$((dirsize / 1024))
thresh_warn=$((thresh_warn * 1024))
thresh_crit=$((thresh_crit * 1024))
;;
"GB")
calc_value=$((dirsize / 1024 / 1024))
thresh_warn=$((thresh_warn * 1024 * 1024))
thresh_crit=$((thresh_crit * 1024 * 1024))
;;
"TB")
calc_value=$((dirsize / 1024 / 1024 / 1024))
thresh_warn=$((thresh_warn * 1024 * 1024 * 1024))
thresh_crit=$((thresh_crit * 1024 * 1024 * 1024))
;;
"KB")
calc_value=$dirsize
;;
*)
echo "Wrong Metric! Must be KB, MB, GB or TB!"
exit $STATE_UNKNOWN
;;
esac

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
if [ $perfdata -eq 1 ]; then
result="$result|'size'=${calc_value};${thresh_warn};${thresh_crit}"
fi

echo "$calc_value $metric - $result"
exit $exitstatus
byErick678, March 23, 2014
Is it possible to implement the remote dirsize check?
Thanks shawnbrito,
i have changed the permission settings in the script as you told but it doesn't work. the error is just "Error:" i have tried chmod u+r on the directory (/etc) i need to monitor but the trick failed to work. any idea please?
This is a must have plugin.. Really appreciate it.. Thanks...

In order to read the folder that have root permission, you will need to modify line:46 with sudo as follows....
DU="/usr/bin/sudo /usr/bin/du"

On Fedora based systems, basename command is located in the /bin/ folder.. (Modify line:50)
PROGNAME=`/bin/basename $0`

I also had to visudo, and #comment the Default requiretty... and add the following line to the list of allowed commands...(Fedora Linux)

nagios ALL=(ALL) NOPASSWD: /usr/local/nagios/libexec/
nagios ALL=(ALL) NOPASSWD: /usr/bin/