Script HA supersivion Nagios routeur actif par SNMP

check_snmp_active_router.sh

#! /bin/bash # Creative Commons CC0 Public Domain Licence set -euo pipefail IFS=$' \t\n' export LC_ALL=C OID_HOSTNAME=.1.3.6.1.2.1.1.5.0 LABEL="ACTIVE_ROUTER" ALL_ARGS=("$@") exec 6>&1 # Link file descriptor #6 with stdout. Saves stdout. # FIX IGNORED ~/.snmp/snmp.conf WHEN 'sudo -u nagios check_snmp_host.sh' HOME=$(getent passwd "$(whoami)" | cut -d: -f6) PATH="/sbin:/usr/sbin:$PATH" exit_malfunction() { exec 1>&6 6>&- # Restore stdout and close file descriptor #6 echo "${LABEL} ${STATUS_MSG} - ERROR $*" exit 3 } # NAGIOS EXIT STATUS # STATE_OK=0 ; STATE_WARNING=1 ; STATE_CRITICAL=2 ; STATE_UNKNOWN=3; STATE_DEPENDENT=4 STATUS_MSG=UNKNOWN typeset -i STATUS_CODE=3 trap 'if [ $STATUS_CODE -eq 3 ] && [ $? -ne 0 ] ; then exit_malfunction "Unexpected error" fi' EXIT ERR command_exists() { command -v "$1" >/dev/null 2>&1 || ( exit_malfunction "I require $1 but it's not installed. Aborting." ) } command_exists snmpget get_defaultgw() { ip -4 route show default | awk '/^default/ { print $3 }' || exit_malfunction "Can't get default gateway" } get_hostname() { local REMOTE_HOST=$1 ROUTER_NAME=$(snmpget -r 2 "${ALL_ARGS[@]}" "$REMOTE_HOST" "${OID_HOSTNAME}" | cut -d= -f2 | cut -d: -f2 | xargs) || exit_malfunction "SNMP failed" } main() { local DEFAULTGW local -i ROUTER_NB DEFAULTGW=$(get_defaultgw) get_hostname "$DEFAULTGW" ROUTER_NB=$(echo "$ROUTER_NAME" | grep -o -e '[0-9]\+$') || exit_malfunction "Hostname error in '$ROUTER_NAME' for $DEFAULTGW" if [ "$ROUTER_NB" -eq 1 ]; then STATUS_CODE=0 STATUS_MSG=OK elif [ "$ROUTER_NB" -eq 2 ]; then STATUS_CODE=1 STATUS_MSG=WARNING else STATUS_CODE=2 STATUS_MSG=CRITICAL fi echo "${LABEL} ${STATUS_MSG} - $ROUTER_NAME is active router" } main exit $STATUS_CODE