Advertisement
Guest User

Untitled

a guest
Feb 4th, 2016
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  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 ] && { echo "Usage: $0 <hostname> <username> <password> <webdav_dir>"; exit $STATE_UNKNOWN; }
  19.  
  20. HOSTNAME=$1
  21. USERNAME=$2
  22. PASSWORD=$3
  23. WEBDAVDIR=$4
  24. BASEURL=http://$HOSTNAME/$WEBDAVDIR
  25.  
  26. # create temp file
  27. truncate -s 1M $FQFN
  28. if [ ! -f $FQFN ]; then
  29. echo "Test file not found!"; exit $STATE_UNKNOWN;
  30. fi
  31.  
  32. # check if curl is installed
  33. command -v curl >/dev/null 2>&1 || { echo >&2 "Curl is not installed. Script cannot run."; exit $STATE_UNKNOWN; }
  34.  
  35. function _curl {
  36. curl --fail -u $USERNAME:$PASSWORD "$@"
  37. }
  38.  
  39. # connect to server and upload file
  40. _curl -s -T $FQFN $BASEURL/ > /dev/null
  41.  
  42. status=$?
  43.  
  44. case $status in
  45. 0)
  46. echo "OK - File uploaded on WebDAV Server: $HOSTNAME"
  47. # remove test file
  48. _curl -X DELETE $BASEURL/$FILENAME
  49. exit $STATE_OK
  50. ;;
  51. 1)
  52. echo "CRITICAL - Cannot upload file on $HOSTNAME"
  53. exit $STATE_CRITICAL
  54. ;;
  55. 6)
  56. echo "CRITICAL - Could not resolve host $HOSTNAME"
  57. exit $STATE_CRITICAL
  58. ;;
  59. 26)
  60. echo "CRITICAL - Cannot open $FQFN for upload"
  61. exit $STATE_CRITICAL
  62. ;;
  63. 22)
  64. echo "CRITICAL - The requested URL returned error"
  65. exit $STATE_CRITICAL
  66. ;;
  67. 127)
  68. echo "CRITICAL - Command not found"
  69. exit $STATE_CRITICAL
  70. ;;
  71. *)
  72. echo "UNKNOWN - "
  73. exit $STATE_UNKNOWN
  74. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement