check_sagan.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env bash
  2. # Author: Jon Schipp
  3. # Date: 01-27-2014
  4. ########
  5. # Examples:
  6. # 1.) Check presence of disk queue (buffer)
  7. # $ ./check_sagan.sh -T -q rsyslog -d /var/spool/rsyslog
  8. # Nagios Exit Codes
  9. OK=0
  10. WARNING=1
  11. CRITICAL=2
  12. UNKNOWN=3
  13. # Default rsyslog spool directory
  14. FILE=/var/log/sagan/sagan.stats
  15. usage()
  16. {
  17. cat <<EOF
  18. Checks most recent sagan stats preprocessor line for specified thresholds
  19. Options:
  20. -c <int> Critical number
  21. -w <int> Warning number
  22. -f <file> Set file when using impstats checks
  23. -T <type> Check type (total/dropped/ignored/signatures)
  24. total - Check total processed messages
  25. drop - Check dropped messages
  26. ignore - Check gnore matches
  27. signature - Check signature matches
  28. Usage: $0 -T dropped
  29. EOF
  30. }
  31. compare(){
  32. local msg=$1
  33. local count=$2
  34. if [[ $count -gt $CRIT ]]; then
  35. echo "CRITICAL: $count $msg"
  36. exit $CRITICAL;
  37. elif [[ $count -gt $WARN ]]; then
  38. echo "WARNING: $count $msg"
  39. exit $WARNING;
  40. else
  41. echo "OK: $count $msg"
  42. exit $OK;
  43. fi
  44. }
  45. argcheck(){
  46. # if less than n argument
  47. if [ $ARGC -lt $1 ]; then
  48. echo "Missing arguments! Use \`\`-h'' for help."
  49. exit 1
  50. fi
  51. }
  52. # Initialize variables
  53. ARGC=$#
  54. TOTAL_CHECK=0
  55. DROP_CHECK=0
  56. IGNORE_CHECK=0
  57. SIG_CHECK=0
  58. CRIT=0
  59. WARN=0
  60. argcheck 1
  61. while getopts "hc:w:f:T:" OPTION
  62. do
  63. case $OPTION in
  64. h) usage ;;
  65. c) CRIT=$OPTARG ;;
  66. w) WARN=$OPTARG ;;
  67. f)
  68. if [[ -r $OPTARG ]]; then
  69. FILE="$OPTARG"
  70. else
  71. echo "Does $OPTARG exist and is readable?"
  72. exit $UNKNOWN
  73. fi
  74. ;;
  75. T)
  76. if [[ "$OPTARG" == "total" ]]; then
  77. TOTAL_CHECK=1
  78. elif [[ "$OPTARG" == "drop" ]]; then
  79. DROP_CHECK=1
  80. elif [[ "$OPTARG" == "ignore" ]]; then
  81. IGNORE_CHECK=1
  82. elif [[ "$OPTARG" == "signature" ]]; then
  83. SIG_CHECK=1
  84. else
  85. echo "$OPTARG is not a valid check type"
  86. exit $WARNING
  87. fi
  88. ;;
  89. \?)
  90. exit $WARNING ;;
  91. esac
  92. done
  93. stats_line=$(tail -n 1 $FILE) || { echo "Not able to read $FILE" && exit $UNKNOWN; }
  94. [[ $stats_line =~ ^# ]] && echo "Waiting for stats.." && exit $OK
  95. [[ $TOTAL_CHECK -eq 1 ]] && count=$(echo $stats_line | cut -d , -f2) && compare total $count
  96. [[ $ISG_CHECK -eq 1 ]] && count=$(echo $stats_line | cut -d , -f3) && compare signature $count
  97. [[ $DROP_CHECK -eq 1 ]] && count=$(echo $stats_line | cut -d , -f7) && compare dropped $count
  98. [[ $IGNORE_CHECK -eq 1 ]] && count=$(echo $stats_line | cut -d , -f8) && compare ignored $count