SHARE
TWEET

Untitled

clmbv Feb 8th, 2016 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3. # check a WebDAV server
  4. # connect using a username and password and upload & delete a test file
  5.  
  6. #Exit codes
  7. STATE_OK=0
  8. STATE_WARNING=1
  9. STATE_CRITICAL=2
  10. STATE_UNKNOWN=3
  11.  
  12. FILENAME=test_monitor
  13. FQFN=/tmp/$FILENAME
  14.  
  15. # Verify the type of input and number of values
  16. # Display an error message if the (input) is not correct
  17. # Exit the shell script with a warning status
  18. [ $# -ne 4 ] && {
  19.     echo "Usage: $0 <hostname> <username> <password> <webdav_dir>"
  20.     exit $STATE_UNKNOWN
  21. }
  22.  
  23. HOSTNAME=$1
  24. USERNAME=$2
  25. PASSWORD=$3
  26. WEBDAVDIR=$4
  27. BASEURL=http://$HOSTNAME/$WEBDAVDIR
  28.  
  29. # create temp file
  30. rm -rf $FQFN
  31. truncate -s 1M $FQFN
  32. if [ ! -f $FQFN ]; then
  33.     echo "Test file not found!";
  34.     exit $STATE_UNKNOWN;
  35. fi
  36.  
  37. # check if curl is installed
  38. command -v curl >/dev/null 2>&1 || { echo >&2 "Curl is not installed. Script cannot run."; exit $STATE_UNKNOWN; }
  39.  
  40. _curl() {
  41.     curl --fail -u $USERNAME:$PASSWORD "$@"
  42. }
  43.  
  44. # connect to server and upload file
  45. #curl --fail -s -T $FQFN -u $USERNAME:$PASSWORD http://$HOSTNAME/$WEBDAVDIR/ > /dev/null
  46. #_curl -s -T $FQFN $BASEURL/ > /dev/null
  47.  
  48. _check_status() {
  49. status=$?
  50.  
  51. case $status in
  52.     0)
  53.         echo "OK - File uploaded on WebDAV Server: $HOSTNAME"
  54.         # remove test file
  55.         #curl --fail -X DELETE -u $USERNAME:$PASSWORD http://$HOSTNAME/$WEBDAVDIR/$FILENAME
  56.         #_curl -X DELETE $BASEURL/$FILENAME
  57.         exit $STATE_OK
  58.         ;;
  59.     1)
  60.         echo "CRITICAL - Cannot "$@" file on $HOSTNAME"
  61.         exit $STATE_CRITICAL
  62.         ;;
  63.     6)
  64.         echo "CRITICAL - Could not resolve host $HOSTNAME"
  65.         exit $STATE_CRITICAL
  66.         ;;
  67.     26)
  68.         echo "CRITICAL - Cannot open $FQFN for upload"
  69.         exit $STATE_CRITICAL
  70.         ;;
  71.     22)
  72.         echo "CRITICAL - The requested URL returned error"
  73.         exit $STATE_CRITICAL
  74.         ;;
  75.     127)
  76.         echo "CRITICAL - Command not found"
  77.         exit $STATE_CRITICAL
  78.         ;;
  79.     *)
  80.         echo "UNKNOWN - "
  81.         exit $STATE_UNKNOWN
  82. esac
  83. }
  84.  
  85. _curl -s -T $FQFN $BASEURL/ > /dev/null
  86. _check_status upload
  87.  
  88. _curl -X DELETE $BASEURL/$FILENAME
  89. _check_status delete
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top