Nagios plugin webdav check 2 Original post: nagios plugin webdav check

According to the good advice I have received I've made some modifications to the code:

#!/bin/bash
#
# check a WebDAV server
# connect using a username and password and upload &amp; delete a test file

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

FILENAME=test_monitor
FQFN=/tmp/$FILENAME

# Verify the type of input and number of values
# Display an error message if the (input) is not correct
# Exit the shell script with a warning status
[ $# -ne 4 ] &amp;&amp; { echo "Usage: $0 &lt;hostname&gt; &lt;username&gt; &lt;password&gt; &lt;webdav_dir&gt;"; exit $STATE_UNKNOWN; }

HOSTNAME=$1
USERNAME=$2
PASSWORD=$3
WEBDAVDIR=$4
BASEURL=http://$HOSTNAME/$WEBDAVDIR

# create temp file
truncate -s 1M $FQFN
if [ ! -f $FQFN ]; then
    echo "Test file not found!"; exit $STATE_UNKNOWN;
fi

# check if curl is installed
command -v curl &gt;/dev/null 2&gt;&amp;1 || { echo &gt;&amp;2 "Curl is not installed. Script cannot run."; exit $STATE_UNKNOWN; }

function _curl {
curl --fail -u $USERNAME:$PASSWORD "$@"
}

# connect to server and upload file
_curl -s -T $FQFN $BASEURL/ &gt; /dev/null

status=$?

case $status in
    0)
        echo "OK - File uploaded on WebDAV Server: $HOSTNAME"
        # remove test file
        _curl -X DELETE $BASEURL/$FILENAME
        exit $STATE_OK
        ;;
    1)
        echo "CRITICAL - Cannot upload file on $HOSTNAME"
        exit $STATE_CRITICAL
        ;;
    6)
        echo "CRITICAL - Could not resolve host $HOSTNAME"
        exit $STATE_CRITICAL
        ;;
    26)
        echo "CRITICAL - Cannot open $FQFN for upload"
        exit $STATE_CRITICAL
        ;;
    22)
        echo "CRITICAL - The requested URL returned error"
        exit $STATE_CRITICAL
        ;;
    127)
        echo "CRITICAL - Command not found"
        exit $STATE_CRITICAL
        ;;
    *)
        echo "UNKNOWN - "
        exit $STATE_UNKNOWN
esac


Do you have any more recommendations regarding syntax or script functionality?

I am thinking about the removal of the test file. It is done if the upload is successful, but can it be assumed that if the upload is successful, the delete will be also? In my tests this has always been the case.

Is there a chance for the delete command to fail? And should I modify the script somehow to accommodate for that?
Thanks for incorporating the various suggestions and posting a new code review.  This is looking really excellent.  Some nits and answers to your concerns:


creating a function for curling is good.  "$@" is the right choice as well.  But it would be nice if the stuff within squiggly brackets was indented like:

function _curl {
    curl --fail -u $USERNAME:$PASSWORD "$@"
}

I would stick with deleting the temporary file each time so the test is complete each time.  One scenario that might happen would be for the disk to fill up, and so no WebDAV writes are possible, but your test succeeds because it has the space of the existing file to work with or maybe there's code so it doesn't rewrite the file for performance reasons.  Either way leads to a false negative check which is best avoided.
It would be more thorough and nice to check the return value from the delete as well.  Doing this with a nested case statement could get unwieldy and certainly duplicates a lot of code.  If you can turn the case statement into its own function that could bucket the return values for creates or deletes that might be easier to maintain.  There might be other nagios checks where you want to check the output of curl similarly which would be good to keep in mind when designing this curl return evaluator.
Aren't you linking to the wrong prior question?  nagios plugin webdav check was how I got here.  :)