Server : Apache System : Linux host44.registrar-servers.com 4.18.0-513.18.1.lve.2.el8.x86_64 #1 SMP Sat Mar 30 15:36:11 UTC 2024 x86_64 User : vapecompany ( 2719) PHP Version : 7.4.33 Disable Function : NONE Directory : /proc/self/root/proc/self/root/proc/self/root/lib64/nagios/plugins/nccustom/ |
Upload File : |
#!/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) REVISION=0.5.0 PROGNAME=${0##*/} declare -a EXCLUDE_ARRAY=() WARN=0.3 CRIT=1 BOND="" PIFACE="" OK_STATUS=$'\t' WARN_STATUS="" CRIT_STATUS="" #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 may be in value rx, tx or all" echo -e "\t --disable-check-drops TYPE-OF-DROPS \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 ;; '--version'|'-V') print_revision exit 3 ;; '--bond'|'-b') BOND="$2" shift 2 ;; '--exclude'|'-x') IFS=, read -r -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 [[ -z "${BOND}" ]]; then BONDS="$(ls -1 /proc/net/bonding/|xargs)" else BONDS="${BOND}" fi if [[ -z "$WARN" || -z "$CRIT" ]]; then echo "Both 'Warning' and 'Critical' options must be set" exit 3 fi # Check if 'Critical' is not less than 'Warning' if (( $(echo "$WARN > $CRIT" | bc -l) )); then echo "'Critical' option cannot be less than 'Warning' option" exit 3 fi DISABLED_ERRORS="${DISABLED_ERRORS:-none}" DISABLED_DROPS="${DISABLED_DROPS:-none}" 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=$(grep 'Slave Interface' /proc/net/bonding/${BOND} | cut -d ' ' -f 3 | tr '\n' ' ' | sed 's/ $//') 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" != '' && "$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 && -n "$WARN" && -n "$CRIT" ]]; then #Used suffixes # _P - previous # _C - current # _D - delta # _A - average TIME_P="$(/usr/bin/stat --format='%Y' /tmp/nagios-proc-net-dev.tmp)" TIME_C="$("date" +%s)" TIME_D=$(( $TIME_C-$TIME_P )) for IFACE in $BOND $INTERFACES; do #echo "now for =${IFACE}=" STR="$(awk -v iface="$IFACE" '$1 == iface ":" {print $4"-"$5"-"$12"-"$13}' /proc/net/dev)" IFS=- set $STR RX_ERR_C="$1" RX_DRP_C="$2" TX_ERR_C="$3" TX_DRP_C="$4" unset IFS STR="$(awk -v iface="$IFACE" '$1 == iface ":" {print $4"-"$5"-"$12"-"$13}' /tmp/nagios-proc-net-dev.tmp)" IFS=- set $STR RX_ERR_P="$1" RX_DRP_P="$2" TX_ERR_P="$3" TX_DRP_P="$4" unset IFS 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 done fi bonding_mode=$(grep 'Bonding Mode: ' /proc/net/bonding/${BOND}) slave_interfaces="Slave Interfaces: ${INTERFACES[*]}" if [[ -n "$BSTATE" ]]; then active_slave="Currently Active Slave: $BSTATE" else active_slave="" fi OK_STATUS="${OK_STATUS}${BOND}'s ${bonding_mode}, ${slave_interfaces}" # Append active slave if present if [[ -n "$active_slave" ]]; then OK_STATUS="${OK_STATUS}, $active_slave" fi OK_STATUS="${OK_STATUS}"$'\n\t' done cp -f /proc/net/dev /tmp/nagios-proc-net-dev.tmp if [ $? -eq 1 ]; then echo "Can't rewrite last state file (/tmp/nagios-proc-net-dev.tmp), 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" exit 0 else echo "OK: $OK_STATUS" if [[ ${#EXCLUDE_ARRAY[@]} -gt 0 ]]; then echo "Excluded from check: ${EXCLUDE_ARRAY[*]}" fi exit 0 fi echo "Something wrong with check" exit 3