161 lines
4.3 KiB
Bash
Executable file
161 lines
4.3 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
# Copyright 2017 alarig <alarig@grifon.fr>
|
|
#
|
|
# BSD-3-Clause licence
|
|
#
|
|
# Redistribution and use in source and binary forms, with or without
|
|
# modification, are permitted provided that the following conditions are met:
|
|
#
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
# this list of conditions and the following disclaimer.
|
|
#
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
# and/or other materials provided with the distribution.
|
|
#
|
|
# 3. Neither the name of the copyright holder nor the names of its contributors
|
|
# may be used to endorse or promote products derived from this software without
|
|
# specific prior written permission.
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
# For debuggin purpose, uncomment the next line and `tail -F` the file.
|
|
#exec 2> /tmp/check_bgp.log
|
|
|
|
usage() {
|
|
printf "Usage:\n"
|
|
printf "check_bgp [-4|-6] -p session\n"
|
|
printf "Must be run with user account able to use birdc/birdc6 or use"
|
|
printf "-s to use birdc with sudo.\n"
|
|
}
|
|
|
|
args=$(getopt 46hsp: $*)
|
|
|
|
if [ $? -ne 0 ]; then
|
|
usage
|
|
exit 3
|
|
fi
|
|
set -- $args
|
|
|
|
while :; do
|
|
case "$1" in
|
|
-h)
|
|
usage
|
|
exit 0
|
|
shift
|
|
;;
|
|
-4)
|
|
VERSION="IPv4"
|
|
shift
|
|
;;
|
|
-6)
|
|
VERSION="IPv6"
|
|
shift
|
|
;;
|
|
-s)
|
|
SUDO='sudo'
|
|
shift
|
|
;;
|
|
-p)
|
|
SESSION="$2"
|
|
shift; shift
|
|
;;
|
|
--)
|
|
shift; break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# path expansion for birdc and sudo of FreeBSD
|
|
PATH="$PATH:/usr/local/sbin/:/usr/local/bin/"
|
|
|
|
if [ "${VERSION}" = "IPv4" ]; then
|
|
birdc="${SUDO} birdc -r"
|
|
elif [ "${VERSION}" = "IPv6" ]; then
|
|
birdc="${SUDO} birdc6 -r"
|
|
else
|
|
printf "CRITICAL: Invalid IP version, use -4 for IPv4 and -6 for IPv6\n"
|
|
exit 2
|
|
fi
|
|
|
|
if [ -z ${SESSION} ]; then
|
|
printf "CRITICAL: No session name supplied, use -p \$session.\n"
|
|
exit 2
|
|
fi
|
|
|
|
STATE="$($birdc show protocol ${SESSION} | \
|
|
awk -v session=${SESSION} '$0 ~ session { print $6 }')"
|
|
|
|
ROUTE_LIMIT_IN="$($birdc show protocol all ${SESSION} | \
|
|
awk '/Receive limit/ { print $3 }')"
|
|
|
|
# Test if we are on a bird2
|
|
if [ -z ${ROUTE_LIMIT_IN} ]; then
|
|
ROUTE_LIMIT_IN="$($birdc show protocol all ${SESSION} | \
|
|
awk '/Import limit/ { print $3 }')"
|
|
fi
|
|
|
|
# There is no import limit, so no ratio either
|
|
if [ -z ${ROUTE_LIMIT_IN} ]; then
|
|
RATIO_IN=0
|
|
fi
|
|
|
|
ROUTES_IMPORTED="$($birdc show protocol all ${SESSION} | grep 'Routes:' | \
|
|
sed -Ee '/imported/{ s/^.* ([0-9]+) imported.*$/\1/; p;}' -e d)"
|
|
ROUTE_HIT_IN="$($birdc show protocol all ${SESSION} | \
|
|
grep -E 'Receive limit:.*HIT')"
|
|
|
|
if [ ! -z ${ROUTE_LIMIT_IN} ]; then
|
|
RATIO_IN=$(echo "(${ROUTES_IMPORTED}*100)/${ROUTE_LIMIT_IN}" | bc)
|
|
fi
|
|
|
|
ROUTE_HIT_OUT="$($birdc show protocol all ${SESSION} | \
|
|
grep -E 'Export limit:.*HIT')"
|
|
|
|
# Session states: https://gitlab.labs.nic.cz/labs/bird/blob/master/proto/bgp/bgp.c#L1465
|
|
# Plugin return codes: https://nagios-plugins.org/doc/guidelines.html#AEN78
|
|
|
|
case ${STATE} in
|
|
Established)
|
|
if [ "${ROUTE_HIT_IN}" != "" ]; then
|
|
printf "CRITICAL: ${SESSION} has hit import route "
|
|
printf "limit\n"
|
|
exit 2
|
|
elif [ "${ROUTE_HIT_OUT}" != "" ]; then
|
|
printf "CRITICAL: ${SESSION} has hit export route "
|
|
printf "limit\n"
|
|
exit 2
|
|
elif [ ${RATIO_IN} -gt 92 ]; then
|
|
printf "WARNING: ${SESSION} import route limit is "
|
|
printf "over threshold\n"
|
|
exit 1
|
|
else
|
|
printf "OK: ${SESSION} is ${STATE}\n"
|
|
exit 0
|
|
fi
|
|
;;
|
|
OpenSent | OpenConfirm)
|
|
printf "WARNING: ${SESSION} is in ${STATE} state\n"
|
|
exit 1
|
|
;;
|
|
Idle | Connect | Active)
|
|
printf "CRITICAL: ${SESSION} is in ${STATE} state\n"
|
|
exit 2
|
|
;;
|
|
*)
|
|
printf "Unknown state: ${STATE}\n"
|
|
exit 3
|
|
;;
|
|
esac
|