? GR0V Shell

GR0V shell

Linux server122.web-hosting.com 4.18.0-513.18.1.lve.el8.x86_64 #1 SMP Thu Feb 22 12:55:50 UTC 2024 x86_64

Path : /lib64/nagios/plugins/nccustom/
File Upload :
Current File : //lib64/nagios/plugins/nccustom/check-bond.sh

#!/bin/bash

## check bond interface
## Copyright (c) 2017, Namecheap.com
## Yuri Muravyov  <yuriy.muravyov@namecheap.com>
## modified by Volodymyr Kuprikov
## modified by Dmitriy Yalanskiy
## modified by Bogdan Kukharskiy (added exclude bond slave interface possibility, bugs fixed)

PROGNAME=check-bond.sh
REVISION=0.4.1
PROGNAME=$(basename $0)

ENV='/usr/bin/env'
GREP=$($ENV which grep)
BC=$($ENV which bc)
AWK=$($ENV which awk)
XARGS=$($ENV which xargs)
SED=$($ENV which sed)
LS=$($ENV which ls)
ECHO=$($ENV which echo)
CP=$($ENV which cp)
DATE=$($ENV which date)
declare -a EXCLUDE_ARRAY

#if $GREP -qiE "CentOS Linux release 7|Red Hat Enterprise Linux Server release 7" /etc/redhat-release ; then
#  SED=/usr/bin/sed
#  LS=/usr/bin/ls
#  ECHO=/usr/bin/echo
#  CP=/usr/bin/cp
#  DATE=/usr/bin/date
#elif $GREP -qiE "CentOS release 6|Red Hat Enterprise Linux Server release 6|CloudLinux Server release 6" /etc/redhat-release ; then
#  SED=/bin/sed
#  LS=/bin/ls
#  ECHO=/bin/echo
#  CP=/bin/cp
#  DATE=/bin/date
#fi

#Function for checking an existance in an array ($1) of an element ($2)
function contains() {
    local n=$#
    local value=${!n}
    for ((i=1;i < $#;i++)) {
        if [ "${!i}" == "${value}" ]; then
            echo "y"
            return 0
        fi
    }
    echo "n"
    return 1
}

print_revision() {
        $ECHO $PROGNAME $REVISION
}

print_usage() {
        $ECHO "Usage: $PROGNAME [options]"
        $ECHO "  e.g. $PROGNAME -b bond0"
        $ECHO "       $PROGNAME -b bond0 -p eth0"
        $ECHO "       $PROGNAME -x eth0"
        $ECHO "       $PROGNAME -b bond0 -w 0.1 -c 0.2"
        $ECHO "       $PROGNAME -b bond0 -p eth0 -w 0.5 -c 3"
        $ECHO "       $PROGNAME -b bond0 --disable-check-errors all --disable-check-drops all"
        $ECHO
        $ECHO "Options:"
        $ECHO -e "\t --bond | -b bond_interface \t\t bond interface name"
        $ECHO -e "\t --exclude | -x bond slave interface \t exclude bond slave interface from check, optional, string (can be a list separated by ',')"
        $ECHO -e "\t --warning | -w NUM \t\t\t warning number of TX/RX errors and drops per second, you can use float numbers (default 0.3)"
        $ECHO -e "\t --critical | -c NUM \t\t\t critical number of TX/RX errors and drops per second, you can use float numbers (default 1)"
        $ECHO -e "\t\t\t\t\t\t if 'Critical' and 'Warning' options aren't defined, only bond interface state will be checked"
        $ECHO -e "\t --primary-interface | -p interface \t primary slave interface name (optional)"
        $ECHO -e "\t --disable-check-errors TYPE-OF-ERRORS \t\t\t may be in value rx, tx or all"
        $ECHO -e "\t --disable-check-drops TYPE-OF-DROPS \t\t\t may be in value rx, tx or all"
        $ECHO -e "\t --help | -h \t\t\t\t print help"
        $ECHO -e "\t --version | -V \t\t\t print version"


}

print_help() {
        print_revision
        $ECHO
        $ECHO "This plugin checks Linux bonding interface status"
        $ECHO
        print_usage
        $ECHO
        exit 3
}

# parse cmd arguments
if [ "$#" -gt 0 ]; then
        while [ "$#" -gt 0 ]; do
                case "$1" in
                        '--help'|'-h')
                                print_help
                                exit 3
                                ;;
                        '--version'|'-V')
                                print_revision
                                exit 3
                                ;;
                        '--bond'|'-b')
                                BOND="$2"
                                shift 2
                                ;;
                        '--exclude'|'-x')
                                IFS=, read -a EXCLUDE_ARRAY <<< "$2" unset IFS
                                shift 2
                                ;;
                        '--warning'|'-w')
                                WARN="$2"
                                shift 2
                                ;;
                        '--critical'|'-c')
                                CRIT="$2"
                                shift 2
                                ;;
                        '--primary-interface'|'-p')
                                PIFACE="$2"
                                shift 2
                                ;;
                        '--disable-check-errors')
                                DISABLED_ERRORS="$2"
                                shift 2
                                ;;
                        '--disable-check-drops')
                                DISABLED_DROPS="$2"
                                shift 2
                                ;;
                        *)
                                $ECHO "Unknown option!"
                                print_usage
                                exit 3
                                ;;
                esac
        done
fi

if [ ! "$BOND" ]; then
   BONDS="$($LS -1 /proc/net/bonding/|$XARGS)"
else
   BONDS="$BOND"
fi

if [ "$WARN" -a "$CRIT" ]; then
   if [ "$($ECHO $WARN'>'$CRIT | $BC -l)" -eq 1 ]; then
        $ECHO "'Critical' option cannot be less than 'Warning' option"
        exit 3
  fi
fi

if [ "$WARN" -a -z "$CRIT" ]; then
    $ECHO "Set 'Critical' option if you set 'Warning' option"
    exit 3
fi

if [ -z "$WARN" -a "$CRIT" ]; then
    $ECHO "Set 'Warning' option if you set 'Critical' option"
    exit 3
fi

if [ ! "$WARN" -a ! "$CRIT" ]; then
    WARN=0.3
    CRIT=1
fi

if [ ! "$DISABLED_ERRORS" ]; then
    DISABLED_ERRORS="none"
fi

if [ ! "$DISABLED_DROPS" ]; then
    DISABLED_DROPS="none"
fi

declare -i COUNTIF

for BOND in $BONDS; do

    if [ ! -f "/proc/net/bonding/$BOND" ]; then
        $ECHO "No bond interface with name $BOND found"
        exit 3
    fi

    INTERFACES="$($AWK '/Slave Interface/ {print $3}' /proc/net/bonding/$BOND|$XARGS)"

    for IFACE in $INTERFACES; do
            COUNTIF+=1
            if [ $(contains "${EXCLUDE_ARRAY[@]}" "${IFACE}") == "y" ]; then  #checking that IFACE is in excluded array (n means NOT found in exclude_array, y is for found)
                continue
            fi
            IFSTATE="$($GREP "Slave Interface: $IFACE" -A1 /proc/net/bonding/$BOND|$AWK '/MII Status:/ {print $3}')"
            [ "$IFSTATE" != 'up' ] && CRIT_STATUS+="$IFACE from $BOND is $IFSTATE; "
    done;

    #for fault-tolerance mode only
    BSTATE="$($AWK '/Currently Active Slave/ {print $4}' /proc/net/bonding/$BOND)"
    [ "$BSTATE" = 'None' ] && CRIT_STATUS+="$BOND interface have no active slaves. "
    [ "$PIFACE" != '' -a "$BSTATE" != "$PIFACE" ] && WARN_STATUS+="$BOND interface have unexpected primary slave interface $BSTATE. "

    [[ $COUNTIF -lt 2 ]] && CRIT_STATUS+="$BOND interface use less than 2 slave interfaces. "

    if [ -f /tmp/nagios-proc-net-dev.tmp -a "$WARN" -a "$CRIT" ]; then
    #Used suffixes
    # _P - previous
    # _C - current
    # _D - delta
    # _A - average
    TIME_P="$($LS -l --time-style='+%s' /tmp/nagios-proc-net-dev.tmp|$AWK {' print $6 '})"
    TIME_C="$("$DATE" +%s)"
    TIME_D=$(( $TIME_C-$TIME_P ))

        for IFACE in $BOND $INTERFACES; do
            STR="$($GREP "$IFACE:" /proc/net/dev |$AWK  {'print $4"-"$5"-"$12"-"$13'})"
            IFS=-
            set $STR
            RX_ERR_C="$1"
            RX_DRP_C="$2"
            TX_ERR_C="$3"
            TX_DRP_C="$4"

            STR="$($GREP "$IFACE:" /tmp/nagios-proc-net-dev.tmp |$AWK  {'print $4"-"$5"-"$12"-"$13'})"
            IFS=-
            set $STR
            RX_ERR_P="$1"
            RX_DRP_P="$2"
            TX_ERR_P="$3"
            TX_DRP_P="$4"

            RX_ERR_D=$(( $RX_ERR_C-$RX_ERR_P ))
            RX_DRP_D=$(( $RX_DRP_C-$RX_DRP_P ))
            TX_ERR_D=$(( $TX_ERR_C-$TX_ERR_P ))
            TX_DRP_D=$(( $TX_DRP_C-$TX_DRP_P ))

            RX_ERR_A=$($ECHO "scale=2; $RX_ERR_D/$TIME_D"|$BC|$SED 's/^\./0./' )
            RX_DRP_A=$($ECHO "scale=2; $RX_DRP_D/$TIME_D"|$BC|$SED 's/^\./0./' )
            TX_ERR_A=$($ECHO "scale=2; $TX_ERR_D/$TIME_D"|$BC|$SED 's/^\./0./' )
            TX_DRP_A=$($ECHO "scale=2; $TX_DRP_D/$TIME_D"|$BC|$SED 's/^\./0./' )

            if [ "$DISABLED_ERRORS" != "rx" ] && [ "$DISABLED_ERRORS" != "all" ]; then
                [ "$($ECHO $RX_ERR_A'>'$CRIT | $BC -l)" -eq 1 ] && CRIT_STATUS+="$IFACE RX ERRORS: $RX_ERR_A per sec. "
            fi
            if [ "$DISABLED_DROPS" != "rx" ] && [ "$DISABLED_DROPS" != "all" ]; then
                [ "$($ECHO $RX_DRP_A'>'$CRIT | $BC -l)" -eq 1 ] && CRIT_STATUS+="$IFACE RX DROPS: $RX_DRP_A per sec. "
            fi
            if [ "$DISABLED_ERRORS" != "tx" ] && [ "$DISABLED_ERRORS" != "all" ]; then
                [ "$($ECHO $TX_ERR_A'>'$CRIT | $BC -l)" -eq 1 ] && CRIT_STATUS+="$IFACE TX ERRORS: $TX_ERR_A per sec. "
            fi
            if [ "$DISABLED_DROPS" != "tx" ] && [ "$DISABLED_DROPS" != "all" ]; then
                [ "$($ECHO $TX_DRP_A'>'$CRIT | $BC -l)" -eq 1 ] && CRIT_STATUS+="$IFACE TX DROPS: $TX_DRP_A per sec. "
            fi

            if [ "$DISABLED_ERRORS" != "rx" ] && [ "$DISABLED_ERRORS" != "all" ]; then
                [ "$($ECHO $RX_ERR_A'>'$WARN | $BC -l)" -eq 1 ] && WARN_STATUS+="$IFACE RX ERRORS: $RX_ERR_A per sec. "
            fi
            if [ "$DISABLED_DROPS" != "rx" ] && [ "$DISABLED_DROPS" != "all" ]; then
                [ "$($ECHO $RX_DRP_A'>'$WARN | $BC -l)" -eq 1 ] && WARN_STATUS+="$IFACE RX DROPS: $RX_DRP_A per sec. "
            fi
            if [ "$DISABLED_ERRORS" != "tx" ] && [ "$DISABLED_ERRORS" != "all" ]; then
                [ "$($ECHO $TX_ERR_A'>'$WARN | $BC -l)" -eq 1 ] && WARN_STATUS+="$IFACE TX ERRORS: $TX_ERR_A per sec. "
            fi
            if [ "$DISABLED_DROPS" != "tx" ] && [ "$DISABLED_DROPS" != "all" ]; then
                [ "$($ECHO $TX_DRP_A'>'$WARN | $BC -l)" -eq 1 ] && WARN_STATUS+="$IFACE TX DROPS: $TX_DRP_A per sec. "
            fi

            IFS=' '
        done
    fi
OK_STATUS+="$OK_STATUS $BOND's $($GREP "Bonding Mode: " /proc/net/bonding/$BOND), Slave Interfaces: $INTERFACES$([ "$BSTATE" != '' ] && $ECHO ", Currently Active Slave: $BSTATE")$([ ${#EXCLUDE_ARRAY[@]} != 0 ] && $ECHO ", Excluded from check: ${EXCLUDE_ARRAY[*]}")"
done

$CP -f /proc/net/dev  /tmp/nagios-proc-net-dev.tmp
if [ $? -eq 1 ]; then
   $ECHO "Can't rewrite last state file, check file permissions"
   exit 3
fi

[ "$CRIT_STATUS" != '' ] && $ECHO Critical: $CRIT_STATUS && exit 2
[ "$WARN_STATUS" != '' ] && $ECHO Warning: $WARN_STATUS && exit 1

if [ "$OK_STATUS" = '' ]; then
$ECHO "OK: No bonding interface found"
else
$ECHO "OK: $OK_STATUS"
fi

exit 0

T1KUS90T
  root-grov@198.54.114.191:~$