check_traffic.sh 2.5 KB

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