check_filesystem_stat.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env bash
  2. # Author: Jon Schipp
  3. ########
  4. # Examples:
  5. # 1.) Return critical if stat does not exit successfully
  6. # $ ./check_filesystem_errors.sh -p /mnt -d 2
  7. #
  8. # Nagios Exit Codes
  9. OK=0
  10. WARNING=1
  11. CRITICAL=2
  12. UNKNOWN=3
  13. usage()
  14. {
  15. cat <<EOF
  16. Nagios plug-in that recursively checks for filesystem input/output
  17. errors by directory using stat.
  18. Options:
  19. -p <dir> Directory or mountpoint to begin
  20. -d <int> Depth i.e. level of subdirectories to check (def: 1)
  21. EOF
  22. }
  23. argcheck() {
  24. if [ $ARGC -lt $1 ]; then
  25. echo "Please specify an argument!, try $0 -h for more information"
  26. exit 1
  27. fi
  28. }
  29. DEPTH=1
  30. CHECK=0
  31. COUNT=0
  32. ARGC=$#
  33. # Print warning and exit if less than n arguments specified
  34. argcheck 1
  35. # option and argument handling
  36. while getopts "hp:d:" OPTION
  37. do
  38. case $OPTION in
  39. h)
  40. usage
  41. exit $UNKNOWN
  42. ;;
  43. p)
  44. CHECK=1
  45. DIR=$OPTARG
  46. ;;
  47. d)
  48. DEPTH=$OPTARG
  49. ;;
  50. *)
  51. exit $UNKNOWN
  52. ;;
  53. esac
  54. done
  55. if [ $CHECK -eq 1 ]; then
  56. find $DIR -maxdepth $DEPTH -type d -print0 | xargs -0 -I file sh -c 'stat "file" 1>/dev/null 2>/dev/null || (echo "Error file" && exit 2)'
  57. if [ $? -gt 0 ]; then
  58. echo "CRITICAL: Found filesystem errors"
  59. exit $CRITICAL
  60. else
  61. echo "OK: No filesystem errors found"
  62. exit $OK
  63. fi
  64. fi