check_fs_write.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env bash
  2. # Author: Jon Schipp
  3. # Date: 09-10-2014
  4. ########
  5. # Examples:
  6. # 1.) Check if sshd has restarted since last check
  7. # $ ./check_fs_write.sh -o /var,/tmp
  8. # Nagios Exit Codes
  9. OK=0
  10. WARNING=1
  11. CRITICAL=2
  12. UNKNOWN=3
  13. usage()
  14. {
  15. cat <<EOF
  16. Notify if a filesystem is read-only by writing a file to it.
  17. Options:
  18. -f Specify custom filename (optional)
  19. -o Specify destinations to write, comma separated
  20. Usage: $0 -o /var,/home,/tmp,/nfs
  21. EOF
  22. }
  23. if [ $# -lt 1 ];
  24. then
  25. usage
  26. exit 1
  27. fi
  28. # Define now to prevent expected number errors
  29. CHECK=0
  30. CRIT=0
  31. FILE=68b329da9893e34099c7d8ad5cb9c940
  32. while getopts "hf:o:" OPTION
  33. do
  34. case $OPTION in
  35. h)
  36. usage
  37. ;;
  38. f)
  39. FILE="$OPTARG"
  40. ;;
  41. o)
  42. DIR=$(echo "$OPTARG" | sed 's/,/ /g');
  43. CHECK=1
  44. ;;
  45. \?)
  46. exit 1
  47. ;;
  48. esac
  49. done
  50. if [ $CHECK -eq 1 ]; then
  51. for directory in $DIR
  52. do
  53. timeout 3s touch $directory/$FILE 2>/dev/null
  54. if [ $? -ne 0 ]; then
  55. echo "Failure to write file to ${directory}!"
  56. CRIT=1
  57. else
  58. rm -f $directory/$FILE
  59. echo "Success writing file to $directory"
  60. fi
  61. done
  62. if [ $CRIT -eq 1 ]; then
  63. echo "State: CRITICAL"
  64. exit $CRITICAL
  65. else
  66. echo "State: OK"
  67. exit $OK
  68. fi
  69. fi