check_bro.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #!/usr/bin/env bash
  2. # Author: Jon Schipp
  3. # Date: 11-08-2013
  4. ########
  5. # Examples:
  6. # 1.) Check status of all Bro workers
  7. # $ ./check_bro.sh -f /usr/local/bro-2.2/bin/broctl -T status
  8. # 2.) Return average packet loss for the 3 named bro workers
  9. # $ ./check_bro.sh -T loss -i "nids0,nids1,nids2"
  10. # 3.) Check average packet loss of all bro workers against warning and critical thresholds i.e > 10% or 20% packet loss.
  11. # $ ./check_bro.sh -T loss -i all -w 10 -c 20
  12. # 4.) Check packet loss percentage for the last most recent interval from Bro's capture_loss.log above 10% loss.
  13. # $ ./check_bro.sh -f /usr/local/bro-2.2/logs/current/capture_loss.log -T capture_loss -c 10
  14. # 5.) Check average packet loss reported by Myricom's SnifferG driver for each Bro node.
  15. # $ ./check_bro.sh -T myricom -i "192.168.1.254,192.168.1.253" -u bro
  16. # Nagios Exit Codes
  17. OK=0
  18. WARNING=1
  19. CRITICAL=2
  20. UNKNOWN=3
  21. # Default location of broctl
  22. # Set this to the proper location if your installation differs or use ``-f''
  23. BROCTL=/usr/local/bro/bin/broctl
  24. # Default location of myri_counters
  25. # Set this to the proper location if your installation differs
  26. MYRI_COUNTERS=/opt/snf/bin/myri_counters
  27. # Default location of capture_loss.log
  28. # Set this to the proper location if your installation differs
  29. CAPTURE_LOG=/usr/local/bro/logs/current/capture_loss.log
  30. usage()
  31. {
  32. cat <<EOF
  33. Check status of Bro and Bro workers.
  34. This script should be run on the Bro manager.
  35. Options:
  36. -c <int> Critical threshold as percent of packet loss
  37. -f <path> Set optional absolute path for broctl, myri_counters, or capture_loss.log (Use as first option)
  38. (def: $BROCTL, $MYRI_COUNTERS, $CAPTURE_LOG)
  39. -i <node/worker> Identifier for Bro instance(s). IP, FQDN, or name depending on the check. (sep:, )
  40. -p <name> Print the value of data from Bro e.g. Notice::suppressing or capture_filter
  41. -T <type> Check type, "status/loss/capture_loss/myricom/print"
  42. status - Check status of all Bro workers
  43. loss - Average packet loss by name for a single-
  44. (\`\`-i nids01''), set (\`\`-i "nids01,nids02"), or all workers (\`\`-i all'').
  45. capture_loss - Checks for packet loss in capture_loss.log
  46. myricom - Average Myricom Sniffer driver packet loss by IP or FQDN for a single-
  47. (\`\`-i 192.168.1.1'') or set (\`\`-i "192.168.1.1,192.168.1.2") of Bro nodes
  48. Connects to nodes via SSH (pub-key auth). If username is not root use ``-u''.
  49. print - Print Bro values
  50. -u <user> Username for the myricom check (def: root)
  51. -w <int> Warning threshold as percent of packet loss
  52. Usage: $0 -f /usr/local/bro-dev/bin/brotcl -T status
  53. $0 -f /usr/local/bro-2.2/logs/current/capture_loss.log -T capture_loss -c 20
  54. EOF
  55. }
  56. argcheck() {
  57. # if less than n argument
  58. if [ $ARGC -lt $1 ]; then
  59. echo "Missing arguments! Use \`\`-h'' for help."
  60. exit 1
  61. fi
  62. }
  63. # Initialize variables
  64. CRIT=0
  65. WARN=0
  66. LIST_NODES=0
  67. LOSS_CHECK=0
  68. CAPTURE_LOSS_CHECK=0
  69. STATUS_CHECK=0
  70. PRINT_CHECK=0
  71. MYRI_CHECK=0
  72. MYRI_STATS=0
  73. USER=root
  74. LOSS=0
  75. RECV=0
  76. RUNNING=0
  77. STOPPED=0
  78. CRASHED=0
  79. UNKNOWN_WORKER=0
  80. WORKERS=all
  81. EXCLUDE=none
  82. ARGC=$#
  83. argcheck 1
  84. while getopts "hfc:i:lm:p:T:u:w:" OPTION
  85. do
  86. case $OPTION in
  87. h)
  88. usage
  89. ;;
  90. f)
  91. shift
  92. if [[ $1 == *broctl ]]; then
  93. BROCTL="$1"
  94. elif [[ $1 == *myri_counters ]]; then
  95. MYRI_COUNTERS=$1
  96. elif [[ $1 == *capture_loss.log ]]; then
  97. CAPTURE_LOG=$1
  98. else
  99. echo "File name appears to be incorrect, maybe try setting the approprate variable in $0."
  100. fi
  101. ;;
  102. c)
  103. CHECK_THRESHOLD=1
  104. CRIT="$OPTARG"
  105. ;;
  106. l)
  107. LIST_NODES=1
  108. ;;
  109. i)
  110. if [ $LOSS_CHECK -eq 1 ] && [[ "$OPTARG" == all ]]; then
  111. WORKERS=".*"
  112. elif [ $LOSS_CHECK -eq 1 ]; then
  113. WORKERS=$(echo "$OPTARG" | sed 's/,/:\\|/g')
  114. elif [ $MYRI_CHECK -eq 1 ]; then
  115. NODE=$(echo "$OPTARG" | sed 's/,/ /g')
  116. else
  117. echo "ERROR: Argument is in incorrect format or \`\`-T <type>'' was not specified first"
  118. exit $UNKOWN
  119. fi
  120. ;;
  121. p)
  122. PRINT="$OPTARG"
  123. ;;
  124. T)
  125. if [[ "$OPTARG" == status ]]; then
  126. STATUS_CHECK=1
  127. elif [[ "$OPTARG" == myricom ]]; then
  128. MYRI_CHECK=1
  129. elif [[ "$OPTARG" == loss ]]; then
  130. LOSS_CHECK=1
  131. elif [[ "$OPTARG" == capture_loss ]]; then
  132. CAPTURE_LOSS_CHECK=1
  133. elif [[ "$OPTARG" == print ]]; then
  134. PRINT_CHECK=1
  135. else
  136. echo "Unknown argument type"
  137. exit $UNKNOWN
  138. fi
  139. ;;
  140. u)
  141. USER="$OPTARG"
  142. ;;
  143. w)
  144. WARN="$OPTARG"
  145. ;;
  146. \?)
  147. exit 1
  148. ;;
  149. esac
  150. done
  151. if [ $LOSS_CHECK -eq 1 ] || [ $STATUS_CHECK -eq 1 ] || [ $LIST_NODES -eq 1 ] || [ $PRINT_CHECK -eq 1 ] ; then
  152. if [ ! -f $BROCTL ];
  153. then
  154. echo "ERROR: Broctl has not been found. Update the BROCTL variable in $0 or specify the path with \`\`-f''"
  155. exit 1
  156. fi
  157. fi
  158. if [ $LIST_NODES -eq 1 ]; then
  159. $BROCTL nodes
  160. exit $OK
  161. fi
  162. if [ $LOSS_CHECK -eq 1 ]; then
  163. # Total average packet loss as a percent for specified workers
  164. FLOAT_LOSS=$($BROCTL netstats | grep "$WORKERS" | sed 's/[a-z]*=//g' | awk '{ drop += $4 ; link += $5 } END { printf("%f\n", ((drop/NR) / (link/NR))* 100) }')
  165. LOSS=$(/usr/bin/printf "%d\n" $FLOAT_LOSS 2>/dev/null)
  166. if [ $LOSS -gt $CRIT ] ;then
  167. echo "Average packet loss is: $FLOAT_LOSS"
  168. exit $CRITICAL
  169. elif [ $LOSS -gt $WARN ]; then
  170. echo "Average packet loss is: $FLOAT_LOSS"
  171. exit $WARNING
  172. else
  173. echo "Average packet loss is: $FLOAT_LOSS"
  174. exit $OK
  175. fi
  176. fi
  177. # Check status of Bro workers
  178. if [ $STATUS_CHECK -eq 1 ]; then
  179. # Broctl stderr is whitespace separated and we need to match on entire line
  180. IFS=$'\n'
  181. for line in $($BROCTL status 2>&1 | grep -v 'Name\|waiting')
  182. do
  183. NAME=$(echo "$line" | awk '{ print $1 }')
  184. case "$line" in
  185. *stop*)
  186. echo "$NAME has stopped"
  187. STOPPED=$((STOPPED+1))
  188. ;;
  189. *fail*)
  190. echo "$NAME has crashed"
  191. CRASHED=$((CRASHED+1))
  192. ;;
  193. *run*)
  194. echo "$NAME is running"
  195. RUNNING=$((RUNNING+1))
  196. ;;
  197. *)
  198. echo "Unknown status of worker: $NAME"
  199. UNKNOWN_WORKER=$((UNKNOWN_WORKER+1))
  200. ;;
  201. esac
  202. done
  203. if [ $STOPPED -gt 0 ] || [ $CRASHED -gt 0 ] || [ $UNKNOWN_WORKER -gt 0 ]; then
  204. echo "-> $STOPPED stopped workers, $CRASHED crashed workers, $RUNNING running workers, and $UNKNOWN_WORKER workers with an unknown status"
  205. exit $CRITICAL
  206. else
  207. echo "All $RUNNING instances are running!"
  208. exit $OK
  209. fi
  210. fi
  211. if [ $CAPTURE_LOSS_CHECK -eq 1 ]; then
  212. if [ ! -f $CAPTURE_LOG ]; then
  213. echo "capture_loss.log cannot be found, modify CAPTURE_LOG in $0 or use \`\`-f''"
  214. exit $UNKNOWN
  215. fi
  216. INTERVAL=$(awk 'NR == 9 { printf("%d\n", $2) }' $CAPTURE_LOG)
  217. TIME=$(date +"%s")
  218. RECENT=$(echo $((TIME-INTERVAL)))
  219. awk -v recent=$RECENT -v crit=$CRIT -v loss=0 -v threshold=0 '! /^#/ && $1 > recent && $4 > 0 \
  220. {
  221. loss++; decimal=sprintf("%d", $6);
  222. if ( strtonum(decimal) > crit ) {
  223. threshold++
  224. print "Peer: "$3,"\t","Loss:", $6;
  225. }
  226. }
  227. END {
  228. if ( loss >= 1 ) {
  229. print "\n--------------------\n"loss,"instances of loss with",threshold,"exceeding the threshold ("crit"%).";
  230. if ( threshold > 0 ) {
  231. exit 2
  232. }
  233. exit 0
  234. }
  235. else
  236. print "\nNo loss detected"; }' $CAPTURE_LOG
  237. if [ $? -eq 2 ]; then
  238. exit $CRITICAL
  239. else
  240. exit $OK
  241. fi
  242. fi
  243. if [ $PRINT_CHECK -eq 1 ]; then
  244. $BROCTL print $PRINT
  245. exit $OK
  246. fi
  247. if [ $MYRI_CHECK -eq 1 ]; then
  248. LOSS=0
  249. RECV=0
  250. COUNT=0
  251. LOSS_TOT=0
  252. RECV_TOT=0
  253. AVERAGE=0
  254. if [ ! -f $MYRI_COUNTERS ]; then
  255. echo "ERROR: myri_counters has not been found. Update the MYRI_COUNTERS variable in $0 or specify the path with \`\`-f''"
  256. exit $UNKOWN
  257. fi
  258. for node in $NODE
  259. do
  260. MYRI_STATS=$(ssh -l $USER $node $MYRI_COUNTERS | awk -F : '/SNF drop ring full|SNF recv pkts/ { print $2 }' | sed 's/[ \t]*//')
  261. RECV=$(echo $MYRI_STATS | awk '{ print $1 }')
  262. LOSS=$(echo $MYRI_STATS | awk '{ print $2 }')
  263. LOSS_TOT=$((LOSS+LOSS_TOT))
  264. RECV_TOT=$((RECV+RECV_TOT))
  265. COUNT=$((COUNT+1))
  266. echo "$node: Lost $LOSS of $RECV"
  267. done
  268. echo "--------------------"
  269. # Average
  270. FLOAT_LOSS=$(echo "(($LOSS_TOT / $COUNT) / ($RECV_TOT / $COUNT)) * 100" | bc -l)
  271. LOSS=$(/usr/bin/printf "%d\n" $FLOAT_LOSS 2>/dev/null)
  272. if [ $LOSS -gt $CRIT ]; then
  273. echo "Average packet loss is: $FLOAT_LOSS"
  274. exit $CRITICAL
  275. elif [ $LOSS -gt $WARN ]; then
  276. echo "Average packet loss is: $FLOAT_LOSS"
  277. exit $WARNING
  278. else
  279. echo "Average packet loss is: $FLOAT_LOSS"
  280. exit $OK
  281. fi
  282. fi