check_file_growth.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Usage: $0 -f big.log -M stat -T bigger -c 1000000 -w 5000000 -i 30
  32. EOF
  33. }
  34. if [ $# -lt 4 ];
  35. then
  36. usage
  37. exit 1
  38. fi
  39. # Define now to prevent expected number errors
  40. FILE=/dev/null
  41. TYPE=bigger
  42. CONCERN=0
  43. TIME=0
  44. CRIT=0
  45. WARN=0
  46. NEW=0
  47. OLD=0
  48. GROWTH=0
  49. PROG=wc
  50. OS=$(uname)
  51. while getopts "hc:f:i:M:T:w:" OPTION
  52. do
  53. case $OPTION in
  54. h)
  55. usage
  56. ;;
  57. c)
  58. CRIT="$OPTARG"
  59. ;;
  60. f)
  61. FILE="$OPTARG"
  62. ;;
  63. i)
  64. TIME="$OPTARG"
  65. ;;
  66. M)
  67. PROG="$OPTARG"
  68. if [[ "$OPTARG" == stat ]]; then
  69. PROG="$OPTARG"
  70. elif [[ "$OPTARG" == wc ]]; then
  71. PROG="$OPTARG"
  72. else
  73. echo "Unknown argument to \`\`-M''! Choose wc or stat."
  74. exit 1
  75. fi
  76. ;;
  77. v)
  78. FILE="$OPTARG"
  79. ;;
  80. T)
  81. CONCERN=1
  82. if [[ "$OPTARG" == bigger ]]; then
  83. TYPE="$OPTARG"
  84. elif [[ "$OPTARG" == smaller ]]; then
  85. TYPE="$OPTARG"
  86. else
  87. echo "Unknown type!"
  88. exit 1
  89. fi
  90. ;;
  91. w)
  92. WARN="$OPTARG"
  93. ;;
  94. \?)
  95. exit 1
  96. ;;
  97. esac
  98. done
  99. if [ ! -f $FILE ]; then
  100. echo "File doesn't exist or is not a regular file!"
  101. exit $UNKNOWN
  102. fi
  103. if [ $PROG == stat ] && [[ $OS != AIX ]]; then
  104. if [[ $OS == Linux ]]; then
  105. OLD=$(stat -c %s $FILE)
  106. sleep $TIME
  107. NEW=$(stat -c %s $FILE)
  108. else
  109. OLD=$(stat -f %z $FILE)
  110. sleep $TIME
  111. NEW=$(stat -f %z $FILE)
  112. fi
  113. else
  114. OLD=$(wc -c $FILE | awk '{ print $1}')
  115. sleep $TIME
  116. NEW=$(wc -c $FILE | awk '{ print $1}')
  117. fi
  118. GROWTH=$(($NEW-$OLD))
  119. if [ $CONCERN -eq 0 ]; then
  120. if [ $GROWTH -gt 0 ]; then
  121. echo "File grew by $GROWTH bytes"
  122. exit $OK
  123. else
  124. echo "File hasn't grown"
  125. exit $CRITICAL
  126. fi
  127. fi
  128. if [ $CONCERN -eq 1 ]; then
  129. if [ $TYPE == "bigger" ]; then
  130. if [ $GROWTH -ge $CRIT ]; then
  131. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  132. exit $CRITICAL
  133. elif [ $GROWTH -ge $WARN ]; then
  134. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  135. exit $WARNING
  136. else
  137. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  138. exit $OK
  139. fi
  140. fi
  141. if [ $TYPE == "smaller" ]; then
  142. if [ $GROWTH -le $CRIT ]; then
  143. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  144. exit $CRITICAL
  145. elif [ $GROWTH -le $WARN ]; then
  146. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  147. exit $WARNING
  148. else
  149. echo "File grew by $GROWTH bytes in ${TIME} seconds"
  150. exit $OK
  151. fi
  152. fi
  153. fi