check_traffic.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env bash
  2. # Author: Jon Schipp
  3. # Nagios Exit Codes
  4. OK=0
  5. WARNING=1
  6. CRITICAL=2
  7. UNKNOWN=3
  8. usage()
  9. {
  10. cat <<EOF
  11. Nagios plug-in that checks packet rate for traffic specified with a bpf
  12. Options:
  13. -i Network interface
  14. -f <bpf> Filter in libpcap syntax
  15. -t <int> Time interval in seconds (def: 1)
  16. -w <int> Warning threshold
  17. -c <int> Critical threshold
  18. EOF
  19. }
  20. argcheck() {
  21. if [ $ARGC -lt $1 ]; then
  22. echo "Please specify an argument!, try $0 -h for more information"
  23. exit 1
  24. fi
  25. }
  26. depend_check(){
  27. bin=$(which tcpdump)
  28. [[ -f $bin ]] || { echo "UNKNOWN: $bin not found in ${PATH}" && exit $UNKNOWN; }
  29. [[ -d /tmp ]] && DIR=/tmp && return
  30. [[ -d /var/tmp ]] && DIR=/var/tmp && return
  31. DIR=.
  32. }
  33. check_bpf () {
  34. [ "$1" ] || { echo "No BPF specified, use \`\`-f''" && exit $UNKNOWN; }
  35. exp='\0324\0303\0262\0241\02\0\04\0\0\0\0\0\0\0\0\0\0377\0377\0\0\01\0\0\0'
  36. echo -en "$exp" | tcpdump -r - "$*" >/dev/null 2>&1 || { echo "UNKNOWN: Invalid BPF" && exit $UNKNOWN; }
  37. }
  38. get_packets() {
  39. timeout -s SIGINT $TIME tcpdump -nni $INT "$FILTER" 2>/dev/null > $BEFORE
  40. timeout -s SIGINT $TIME tcpdump -nni $INT "$FILTER" 2>/dev/null > $AFTER
  41. ! [ -f $BEFORE ] && echo "UNKNOWN: $BEFORE doesn't exist!" && exit $UNKNOWN
  42. ! [ -f $AFTER ] && echo "UNKNOWN: $AFTER doesn't exist!" && exit $UNKNOWN
  43. }
  44. get_counts() {
  45. START=$(cat $BEFORE | wc -l)
  46. STOP=$(cat $AFTER | wc -l)
  47. [[ $START -gt $STOP ]] && RESULT=$((START-STOP))
  48. [[ $STOP -gt $START ]] && RESULT=$((STOP-START))
  49. }
  50. traffic_calculation() {
  51. if [ $1 -gt $CRIT ]; then
  52. exit $CRITICAL
  53. elif [ $1 -gt $WARN ]; then
  54. exit $WARNING
  55. else
  56. exit $OK
  57. fi
  58. }
  59. PPS=0
  60. BPS=0
  61. LINERATE=0
  62. TIME=1
  63. WARN=0
  64. CRIT=0
  65. ARGC=$#
  66. BEFORE=$DIR/check_traffic1.txt
  67. AFTER=$DIR/check_traffic2.txt
  68. # Print warning and exit if less than n arguments specified
  69. argcheck 1
  70. depend_check
  71. # option and argument handling
  72. while getopts "hi:c:f:t:w:" OPTION
  73. do
  74. case $OPTION in
  75. h)
  76. usage
  77. exit
  78. ;;
  79. i)
  80. INT=$OPTARG
  81. ;;
  82. f)
  83. FILTER="$OPTARG"
  84. ;;
  85. t)
  86. TIME=$OPTARG
  87. ;;
  88. c)
  89. CRIT=$OPTARG
  90. ;;
  91. w)
  92. WARN=$OPTARG
  93. ;;
  94. *)
  95. exit $UNKNOWN
  96. ;;
  97. esac
  98. done
  99. [ -d /sys/class/net/$INT ] || { "UNKNOWN: $INT does not exist" && exit $UNKNOWN; }
  100. [ -d /proc ] && check_bpf "$FILTER"
  101. get_packets
  102. get_counts
  103. echo "Traffic rate is ~${RESULT}/${TIME}"
  104. traffic_calculation $RESULT