From 399ab2f3b8d6e6802f7f02d580cc5ad17515b1cf Mon Sep 17 00:00:00 2001 From: alarig Date: Fri, 18 May 2018 11:33:03 +0200 Subject: [PATCH] Adding script --- check_bgp | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100755 check_bgp diff --git a/check_bgp b/check_bgp new file mode 100755 index 0000000..4784b30 --- /dev/null +++ b/check_bgp @@ -0,0 +1,119 @@ +#!/bin/sh + +# Copyright 2017 alarig +# +# 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 }')" + +# 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) + printf "OK: ${SESSION} is ${STATE}\n" + exit 0 + ;; + 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