Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions check_dir_files.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
#
# ## Plugin for Nagios to monitor directory size
# ## Written by Gerd Stammwitz (http://www.enbiz.de/)
Expand Down Expand Up @@ -134,16 +134,29 @@ done

##### Get size of specified directory

error=""
duresult=`$FIND $dirpath -type f |$WC -l || error="Error"`
# Path exists?
if [ ! -d $dirpath ]; then
echo "Directory '$dirpath' does not exist!"
exit $STATE_CRITICAL
fi

if [ ! "$error" = "" ]; then
errtext=`echo $duresult`
echo "$error:$errtext"
exit $STATE_UNKNOWN
# Should generally be run as root to avoid "Permission denied" errors.
# Nagios is not able to see the error messages whether std is redirected or not as wc is catching the output.
# Helping the user diagnose the problem in Nagios itself is done below on checking the return code.
# stderr is redirected here to not get duplicate output when manually running the script.
dirsize=$($FIND $dirpath -type f 2>&1 | $WC -l ; exit "${PIPESTATUS[0]}")
returnCode=$?

if [ $returnCode != 0 ]; then
# For the user to be able to see the error message in Nagios, this needs to be printed out
# on stdout (which is piped to wc before). Therefore, we repeat find here (without wc) to hopefully
# get the same error as before. stdout is ignored, afterwards stderr is redirected to stdout, so
# only the error messages are printed out.
error=$($FIND $dirpath -type f 2>&1 >/dev/null)
echo "Error: find failed with \"$error\""
exit $STATE_UNKNOWN
fi

dirsize=`echo $duresult`
result="OK"
exitstatus=$STATE_OK

Expand Down