Files
VSC/Icinga2/_Test/check_snmpwalk.sh
T
claudio 368d6fafea Issue
Code backup
2026-05-10 16:59:01 +02:00

454 lines
15 KiB
Bash

#!/bin/bash
# startup checks
if [ -z "$BASH" ]; then
echo "Please use BASH."
exit 3
fi
if [ ! -e "/usr/bin/which" ]; then
echo "/usr/bin/which is missing."
exit 3
fi
snmp=$(which snmpwalk)
if [ $? -ne 0 ]; then
echo "Please install snmpwalk"
exit 3
fi
# Global Variables
APPNAME=$(basename $0)
AUTHOR="Claudio Boggian"
VERSION="0.1"
# Default settings
COMMUNITY="public"
HOST_NAME="localhost"
SNMPVERSION="2c"
# State Variables
STATE_OK=0
STATE_WARN=1
STATE_CRIT=2
STATE_UNK=3
# Default Outputs
STATE=$STATE_OK
STATE_STRING=""
PERFDATA=""
# Default WARNING / CRITICAL for temperature checks
WARNING=80
CRITICAL=90
# Change this to modify the script's handling of how it
# separates each consumable/tray when multiple checks
# are output.
SEPARATOR="\n"
# This is the character that tokenizes multiple arguments
# to the port check. I have this here so it's easy to
# change if I find the current character I have is buggy
# or a bad choice
ARG_TOKEN=','
# WATCHGUARD SNMP OIDS
OID_wgSystemTotalSendBytes="1.3.6.1.4.1.3097.6.3.8"
OID_wgSystemTotalRecvBytes="1.3.6.1.4.1.3097.6.3.9"
OID_wgSystemTotalSendPackets="1.3.6.1.4.1.3097.6.3.10"
OID_wgSystemTotalRecvPackets="1.3.6.1.4.1.3097.6.3.11"
OID_wgSystemCpuUtil1="1.3.6.1.4.1.3097.6.3.77"
OID_wgSystemCurrActiveConns="1.3.6.1.4.1.3097.6.3.80"
OID_wgIpsecTunnelNum="1.3.6.1.4.1.3097.6.5.1.1"
OID_wgInfoGavService="1.3.6.1.4.1.3097.6.1.3.0"
OID_wgInfoIpsService="1.3.6.1.4.1.3097.6.1.4.0"
# print_version
print_version() {
echo "$APPNAME $VERSION"
echo "$AUTHOR"
echo ''
}
# print_usage
print_usage(){
echo ''
echo 'Usage for SNMP v1/2c:'
echo " $APPNAME -H <host/IP> -t <type of check> [-C <SNMP community>] [-p <ports>] [-S <character>] [-w <warning value>] [-c <critical value>]"
echo ''
echo 'Usage for SNMP v3:'
echo " $APPNAME -H <host/IP> -t <check> -u <user> -x <protocol> -X <password> -a <protocol> -A <password> -l <security mode> [-p <ports>] [-S <character>]>"
echo ''
}
# print_help Function
function print_help () {
print_version
echo 'Description:'
echo "$APPNAME is a Nagios plugin to check the status of various components of Dell PowerConnect switches."
echo ''
echo 'This plugin is not developped by the Nagios Plugin group.'
echo 'Please do not e-mail them for support on this plugin.'
echo ''
echo 'For contact info, please read the plugin script file.'
print_usage
echo "---------------------------------------------------------------------"
echo ''
echo 'OPTIONS:'
echo ' -H|--host'
echo ' Host name or IP address to check. Default is: localhost. REQUIRED OPTION'
echo ' -v|--snmpversion { 1 | 2c | 3 }'
echo " Specifies the SNMP version to use. Default is '2c'. REQUIRED OPTION"
echo ' -C|--community'
echo " SNMP v2 community string with Read access. Default is 'public'. REQUIRED OPTION"
echo ' -u|--user'
echo ' SNMP v3 username'
echo ' -l|--privlevel { noAuthNoPriv | authNoPriv | authPriv }'
echo ' SNMP v3 privilege level'
echo ' -x|--privprotocol { DES | AES }'
echo ' SNMP v3 privacy protocol'
echo ' -X|--privpassword'
echo ' SNMP v3 privacy password'
echo ' -a|--authprotocol { MD5 | SHA }'
echo ' SNMP v3 authentication protocol'
echo ' -A|--authpassword'
echo ' SNMP v3 authentication password'
echo ' -t|--type { assets | description | uptime | ports | port | health | temps | fans | psus }'
echo ' The check you want to perform for the switch(es).'
echo ' REQUIRED OPTION.'
echo ' -p|--port <port(s)>'
echo " Specify port number, or list of ports (comma-separated) to be checked (only for the '-t port' option!)"
echo ' -S|--separator <text string>'
echo ' Assign a particular string as the separator for consumables.'
echo ' Default is " " to conform to Nagios plugin development guidelines'
echo ' -w|--warning'
echo ' Assign WARNING value, in degrees C, for temperature checks'
echo ' -c|--critical'
echo ' Assign CRITICAL value, in degrees C, for temperature checks'
echo ' -h|--help'
echo ' Show this help screen'
echo ' -V|--version'
echo ' Show the current version of the plugin'
echo ''
echo 'CHECK TYPES:'
echo ' assets:'
echo ' Returns serial number and service tag of unit. '
echo ''
echo ' description:'
echo ' Returns make and model of unit. Equivalent to checking sysDescr.'
echo ''
echo ' uptime:'
echo ' How long the unit has been powered on. Equivalent to checking sysUptime.'
echo ''
echo ' ports:'
echo ' Checks admin status of all ports. Reports number of ports UP and DOWN. Equivalent to checking ifOperStatus'
echo ''
echo ' port:'
echo ' Admin status of specified ports. Reports number of ports UP and DOWN. '
echo ' Ports to check can be specified as a range (x-y), as well as a comma-separated list (3,5,19).'
echo ''
echo ' health:'
echo " Global health status of the device(s). Will let you know something is wrong, but won't check what."
echo ''
echo ' temps:'
echo ' Ambient temperature of the device(s).'
echo ' WARNING and CRITICAL values are required! Default WARNING is 65 deg F and CRITICAL is 70 deg F.'
echo ''
echo ' fans:'
echo ' Fan status of the device(s).'
echo ''
echo ' psus:'
echo ' Power supply status of the device(s).'
echo ''
echo 'EXAMPLES:'
echo " $APPNAME -H hostname -C public -t port -p 42"
echo ' to check status of port 42'
echo " $APPNAME -H hostname -C public -t psus"
echo ' to check status of power supplies'
echo " $APPNAME -H hostname -C public -t temps -w 60 -c 70"
echo ' to check thermal temperatures'
echo ''
exit $STATE_UNK
}
# Check System Statistics Send/Recv
#$OID_wgSystemTotalSendBytes
#$OID_wgSystemTotalSendPackets
#$OID_wgSystemTotalRecvBytes
#$OID_wgSystemTotalRecvPackets
function CheckTransfer () {
TRANSFER_STATE=$STATE_OK
TOTSENDB="walk_snmp $OID_wgSystemTotalSendBytes"
check_snmp_error "$?" "$TOTSENDB"
SBYTE=$(echo "$TOTSENDB" | cut -d " " -f 2 | sed 's/(//g'| sed 's/)//g')
return $TRANSFER_STATE
}
#########################################################
# Subroutine: walk_snmp #
#########################################################
walk_snmp(){
if [ $2 = true ]; then
OUTPUT_OPTIONS="-Oavq"
else
OUTPUT_OPTIONS="-Oa"
fi
if [[ $SNMPVERSION = 3 ]]; then
RESULT_TEXT="snmpwalk -v $SNMPVERSION $OUTPUT_OPTIONS -u $SNMPUSER -l $PRIVILEGELEVEL -x $PRIVACYPROTOCOL -X $PRIVACYPASSWORD -a $AUTHPROTOCOL -A $AUTHPASSWORD $HOST_NAME $1"
RESULT_CODE=$?
else
# Check if community was also set
RESULT_TEXT="snmpwalk -v $SNMPVERSION $OUTPUT_OPTIONS -c $COMMUNITY $HOST_NAME $1"
RESULT_CODE=$?
fi
if [[ $RESULT_CODE -ne 0 ]]; then
echo "Plugin $APPNAME failure - snmpwalk command error."
echo "$RESULT_TEXT"
exit $STATE_UNK
fi
if [ $2 = true ]; then
echo "$RESULT_TEXT" | sed -e 's/^[[:space:]]*//' | tr -d "\""
else
echo "$RESULT_TEXT"
fi
}
#########################################################
# Subroutine: get_snmp #
#########################################################
get_snmp(){
if [[ $SNMPVERSION = 3 ]]; then
RESULT_TEXT="snmpget -v $SNMPVERSION -Oavq -u $SNMPUSER -l $PRIVILEGELEVEL -x $PRIVACYPROTOCOL -X $PRIVACYPASSWORD -a $AUTHPROTOCOL -A $AUTHPASSWOR"
RESULT_CODE=$?
else
# Check if community was also set
RESULT_TEXT="snmpget -v $SNMPVERSION -Oavq -c $COMMUNITY $HOST_NAME $1"
RESULT_CODE=$?
fi
if [[ $RESULT_CODE -ne 0 ]]; then
echo "Plugin $APPNAME failure - snmpget command error."
echo "$RESULT_TEXT | tr -d "\"
exit $STATE_UNK
fi
echo "$RESULT_TEXT | tr -d "\"
}
#########################################################
# Subroutine: check_snmp_error #
#########################################################
# Tests errors returned by function operations #
#########################################################
check_snmp_error(){
if [[ $1 -ne 0 ]]; then
echo $2
exit $STATE_UNK
fi
}
#########################################################
## MAIN CODE ##
#########################################################
# Check that all required binaries for the script are available
# EXIT with an UNKNOWN status if not
binaries="snmpwalk snmpget cut tr sed grep awk wc"
for required_binary in $binaries
do
which $required_binary > /dev/null
if [ "$?" != '0' ];then
echo "UNKNOWN: $APPNAME: No usable '$required_binary' binary in '$PATH'"
exit $STATE_UNK
fi
done
# Check to see if any parameters were passed
if [[ $# -eq 0 ]]; then
print_usage
exit $STATE_UNK
fi
# Parse our options as passed, and make sure things are peachy
while test -n "$1"; do
case "$1" in
--host|-H)
HOST_NAME=$2
shift
;;
--comunity|-C)
COMMUNITY=$2
shift
;;
--snmpversion|-v)
SNMPVERSION=$2
shift
;;
--user|-u)
SNMPUSER=$2
shift
;;
--privelegelevel|-l)
PRIVILEGELEVEL=$2
shift
;;
--authprotocol|-a)
AUTHPROTOCOL=$2
shift
;;
--authpassword|-A)
AUTHPASSWORD=$2
shift
;;
--privacyprotocol|-x)
PRIVACYPROTOCOL=$2
shift
;;
--privacypassword|-X)
PRIVACYPASSWORD=$2
shift
;;
--type|-t)
CHECK_TYPE=$2
shift
;;
--port|-p)
PORT_LIST=$2
shift
;;
--separator|-S) # Assign separator
SEPARATOR="$2"
shift
;;
--warning|-w) # Assign WARNING threshold
WARNING=$2
shift
;;
--critical|-c) # Assign CRITICAL threshold
CRITICAL=$2
shift
;;
--help|-h)
print_help
;;
--version|-V)
print_version
exit $STATE
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNK
;;
esac
shift
done
# Make sure all necessary arguments were given; EXIT with an UNKNOWN status if not
if [ ! -z $CHECK_TYPE ]; then
# Determine the check to perform
case "$CHECK_TYPE" in
"assets")
CheckAssetInfo
;;
"description")
CheckDescription
;;
"uptime")
CheckUptime
STATE=$?
;;
"ports")
CheckPorts
STATE=$?
;;
"port")
# be sure CHECK_TYPE is not null first!!!
for CURRENT_PORT in $(echo "$PORT_LIST" | tr "$ARG_TOKEN" "\n")
do
CheckPort "$CURRENT_PORT"
CURRENT_PORT_STATUS=$?
if [ "$CURRENT_PORT_STATUS" -gt "$STATE" ]; then
STATE="$CURRENT_PORT_STATUS"
fi
done
;;
"health")
GLOBALHEALTH="get_snmp $OID_productStatusGlobalStatus"
check_snmp_error "$?"
case "$GLOBALHEALTH" in
3)
STATE_STRING="OK: System hardware is okay."
STATE=$STATE_OK
;;
4)
STATE_STRING="WARNING: At least one power supply is not functional, or hardware reported error."
STATE=$STATE_WARN
;;
5)
STATE_STRING="CRITICAL: At least one fan is not functional, or hardware has fatal errors."
STATE=$STATE_CRIT
;;
esac
;;
"temps")
CheckTemps
STATE=$?
;;
"fans")
CheckFans
STATE=$?
;;
"psus")
CheckPSUs
STATE=$?
;;
*)
STATE_STRING="UNKNOWN: No valid check type specified to -t parameter"
STATE=$STATE_UNK
;;
esac
else
echo "Command incomplete!"
STATE=$STATE_UNK
fi
# If the program hasn't exited already, then a check was run okay and we can quit.
if [ "$PERFDATA" == "" ]; then
echo "$STATE_STRING"
else
echo "$STATE_STRING|$PERFDATA"
fi
exit $STATE