Collection of scripts used to monitor bird sessions and route limits
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.3KB

  1. #!/bin/sh
  2. # Copyright 2017 alarig <alarig@grifon.fr>
  3. #
  4. # BSD-3-Clause licence
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. #
  9. # 1. Redistributions of source code must retain the above copyright notice,
  10. # this list of conditions and the following disclaimer.
  11. #
  12. # 2. Redistributions in binary form must reproduce the above copyright notice,
  13. # this list of conditions and the following disclaimer in the documentation
  14. # and/or other materials provided with the distribution.
  15. #
  16. # 3. Neither the name of the copyright holder nor the names of its contributors
  17. # may be used to endorse or promote products derived from this software without
  18. # specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  24. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. # POSSIBILITY OF SUCH DAMAGE.
  31. # For debuggin purpose, uncomment the next line and `tail -F` the file.
  32. #exec 2> /tmp/check_bgp.log
  33. usage() {
  34. printf "Usage:\n"
  35. printf "check_bgp [-4|-6] -p session\n"
  36. printf "Must be run with user account able to use birdc/birdc6 or use"
  37. printf "-s to use birdc with sudo.\n"
  38. }
  39. args=$(getopt 46hsp: $*)
  40. if [ $? -ne 0 ]; then
  41. usage
  42. exit 3
  43. fi
  44. set -- $args
  45. while :; do
  46. case "$1" in
  47. -h)
  48. usage
  49. exit 0
  50. shift
  51. ;;
  52. -4)
  53. VERSION="IPv4"
  54. shift
  55. ;;
  56. -6)
  57. VERSION="IPv6"
  58. shift
  59. ;;
  60. -s)
  61. SUDO='sudo'
  62. shift
  63. ;;
  64. -p)
  65. SESSION="$2"
  66. shift; shift
  67. ;;
  68. --)
  69. shift; break
  70. ;;
  71. esac
  72. done
  73. # path expansion for birdc and sudo of FreeBSD
  74. PATH="$PATH:/usr/local/sbin/:/usr/local/bin/"
  75. if [ "${VERSION}" = "IPv4" ]; then
  76. birdc="${SUDO} birdc -r"
  77. elif [ "${VERSION}" = "IPv6" ]; then
  78. birdc="${SUDO} birdc6 -r"
  79. else
  80. printf "CRITICAL: Invalid IP version, use -4 for IPv4 and -6 for IPv6\n"
  81. exit 2
  82. fi
  83. if [ -z ${SESSION} ]; then
  84. printf "CRITICAL: No session name supplied, use -p \$session.\n"
  85. exit 2
  86. fi
  87. STATE="$($birdc show protocol ${SESSION} | \
  88. awk -v session=${SESSION} '$0 ~ session { print $6 }')"
  89. ROUTE_LIMIT_IN="$($birdc show protocol all ${SESSION} | \
  90. awk '/Receive limit/ { print $3 }')"
  91. # Test if we are on a bird2
  92. if [ -z ${ROUTE_LIMIT_IN} ]; then
  93. ROUTE_LIMIT_IN="$($birdc show protocol all ${SESSION} | \
  94. awk '/Import limit/ { print $3 }')"
  95. fi
  96. # There is no import limit, so no ratio either
  97. if [ -z ${ROUTE_LIMIT_IN} ]; then
  98. RATIO_IN=0
  99. fi
  100. ROUTES_IMPORTED="$($birdc show protocol all ${SESSION} | grep 'Routes:' | \
  101. sed -Ee '/imported/{ s/^.* ([0-9]+) imported.*$/\1/; p;}' -e d)"
  102. ROUTE_HIT_IN="$($birdc show protocol all ${SESSION} | \
  103. grep -E 'Receive limit:.*HIT')"
  104. if [ ! -z ${ROUTE_LIMIT_IN} ]; then
  105. RATIO_IN=$(echo "(${ROUTES_IMPORTED}*100)/${ROUTE_LIMIT_IN}" | bc)
  106. fi
  107. ROUTE_HIT_OUT="$($birdc show protocol all ${SESSION} | \
  108. grep -E 'Export limit:.*HIT')"
  109. # Session states: https://gitlab.labs.nic.cz/labs/bird/blob/master/proto/bgp/bgp.c#L1465
  110. # Plugin return codes: https://nagios-plugins.org/doc/guidelines.html#AEN78
  111. case ${STATE} in
  112. Established)
  113. if [ "${ROUTE_HIT_IN}" != "" ]; then
  114. printf "CRITICAL: ${SESSION} has hit import route "
  115. printf "limit\n"
  116. exit 2
  117. elif [ "${ROUTE_HIT_OUT}" != "" ]; then
  118. printf "CRITICAL: ${SESSION} has hit export route "
  119. printf "limit\n"
  120. exit 2
  121. elif [ ${RATIO_IN} -gt 92 ]; then
  122. printf "WARNING: ${SESSION} import route limit is "
  123. printf "over threshold\n"
  124. exit 1
  125. else
  126. printf "OK: ${SESSION} is ${STATE}\n"
  127. exit 0
  128. fi
  129. ;;
  130. OpenSent | OpenConfirm)
  131. printf "WARNING: ${SESSION} is in ${STATE} state\n"
  132. exit 1
  133. ;;
  134. Idle | Connect | Active)
  135. printf "CRITICAL: ${SESSION} is in ${STATE} state\n"
  136. exit 2
  137. ;;
  138. *)
  139. printf "Unknown state: ${STATE}\n"
  140. exit 3
  141. ;;
  142. esac