check_file_growth.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env bash
  2. # Author: Jon Schipp
  3. # Date: 11-07-2013
  4. ########
  5. # Examples:
  6. # 1.) Check if file has grown in the last 30 seconds
  7. # $ ./check_file_growth.sh -f /var/log/system.log -M stat -i 30
  8. # File grew by 118 bytes
  9. #
  10. # 2.) If file has grown by more than (c)ritical or (w)arning bytes in 30 seconds exit with critical or warning status
  11. # $ ./check_file_growth -f big.log -M stat -T bigger -c 1000000 -w 5000000 -i 30
  12. # Nagios Exit Codes
  13. OK=0
  14. WARNING=1
  15. CRITICAL=2
  16. UNKNOWN=3
  17. usage()
  18. {
  19. cat <<EOF
  20. Check the level of byte growth of a file for a time interval.
  21. Also, check that a file is growing.
  22. Options:
  23. -f Specify filename as full path
  24. -i <int> Interval in seconds
  25. -M <command> Command to use for the checks
  26. "wc/stat" wc is portable but slower, stat is faster but less portable
  27. -T <type> Type of concern for thresholds
  28. "bigger/smaller" than thresholds
  29. -c <int> Critical threshold in bytes
  30. -w <int> Warning threshold in bytes
  31. Modifiers:
  32. The following path modifiers can be used in \`\`-f''.
  33. %m - min, %h - hour, %d - day, %m - month, %Y - full year
  34. Usage: $0 -f /logs/%d/big.log -M stat -T bigger -c 1000000 -w 5000000 -i 30
  35. EOF
  36. }
  37. expand_path(){
  38. local file
  39. file="$1"
  40. min=$(date +"%M")
  41. hour=$(date +"%H")
  42. day=$(date +"%d")
  43. month=$(date +"%m")
  44. year=$(date +"%Y")
  45. file_sub_min="${file/\%M/$min}"
  46. file_sub_hour="${file_sub_min/\%H/$hour}"
  47. file_sub_day="${file_sub_hour/\%d/$day}"
  48. file_sub_month="${file_sub_day/\%m/$month}"
  49. file_sub_year="${file_sub_month/\%Y/$year}"
  50. FILE="$file_sub_year"
  51. }
  52. argcheck(){
  53. local num
  54. num=$1
  55. [[ $ARGC -lt $num ]] && { usage && exit 1; }
  56. }
  57. # Define now to prevent expected number errors
  58. FILE=/dev/null
  59. TYPE=bigger
  60. CONCERN=0
  61. TIME=0
  62. CRIT=0
  63. WARN=0
  64. NEW=0
  65. OLD=0
  66. GROWTH=0
  67. PROG=wc
  68. OS=$(uname)
  69. ARGC=$#
  70. argcheck 4
  71. while getopts "hc:f:i:M:T:w:" OPTION
  72. do
  73. case $OPTION in
  74. h)
  75. usage;;
  76. c)
  77. CRIT="$OPTARG";;
  78. f)
  79. FILE="$OPTARG";;
  80. i)
  81. TIME="$OPTARG";;
  82. M)
  83. PROG="$OPTARG"
  84. if [[ "$OPTARG" == stat ]]; then
  85. PROG="$OPTARG"
  86. elif [[ "$OPTARG" == wc ]]; then
  87. PROG="$OPTARG"
  88. else
  89. echo "Unknown argument to \`\`-M''! Choose wc or stat."
  90. exit 1
  91. fi
  92. ;;
  93. T)
  94. CONCERN=1
  95. if [[ "$OPTARG" == bigger ]]; then
  96. TYPE="$OPTARG"
  97. elif [[ "$OPTARG" == smaller ]]; then
  98. TYPE="$OPTARG"
  99. else
  100. echo "Unknown type!"
  101. exit 1
  102. fi;;
  103. w)
  104. WARN="$OPTARG";;
  105. \?)
  106. exit 1;;
  107. esac
  108. done
  109. expand_path $FILE
  110. ls $FILE 1>/dev/null 2>/dev/null || { echo $FILE doesn\'t exist or is not a regular file\! && exit $CRITICAL; }
  111. if [[ $PROG == stat ]] && [[ $OS != AIX ]]; then
  112. if [[ $OS == Linux ]]; then
  113. OLD=$(stat -c %s $FILE)
  114. sleep $TIME
  115. NEW=$(stat -c %s $FILE)
  116. else
  117. OLD=$(stat -f %z $FILE)
  118. sleep $TIME
  119. NEW=$(stat -f %z $FILE)
  120. fi
  121. else
  122. OLD=$(wc -c $FILE | awk '{ print $1}')
  123. sleep $TIME
  124. NEW=$(wc -c $FILE | awk '{ print $1}')
  125. fi
  126. GROWTH=$(($NEW-$OLD))
  127. if [[ $CONCERN -eq 0 ]]; then
  128. if [[ $GROWTH -gt 0 ]]; then
  129. echo "File grew by $GROWTH bytes"
  130. exit $OK
  131. else
  132. echo "File hasn't grown"
  133. exit $CRITICAL
  134. fi
  135. fi
  136. if [[ $CONCERN -eq 1 ]]; then
  137. if [[ $TYPE == "bigger" ]]; then
  138. if [ $GROWTH -ge $CRIT ]; then
  139. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  140. exit $CRITICAL
  141. elif [[ $GROWTH -ge $WARN ]]; then
  142. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  143. exit $WARNING
  144. else
  145. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  146. exit $OK
  147. fi
  148. fi
  149. if [[ $TYPE == "smaller" ]]; then
  150. if [[ $GROWTH -le $CRIT ]]; then
  151. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  152. exit $CRITICAL
  153. elif [[ $GROWTH -le $WARN ]]; then
  154. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  155. exit $WARNING
  156. else
  157. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  158. exit $OK
  159. fi
  160. fi
  161. fi