Skip to content
Snippets Groups Projects
check_snmp_data 5.75 KiB
#!/usr/bin/env bash
# ======================================================================
#
# NAGIOS / ICINGA CHECK :: check_snmp_data
# this plugin checks snmp performance data
#
# DOCS:
#   - Synology - see docs:
#     https://global.download.synology.com/download/Document/Software/DeveloperGuide/Firmware/DSM/All/enu/Synology_DiskStation_MIB_Guide.pdf
#
#   - IODs uc davis http://oidref.com/1.3.6.1.4.1.2021
#
# ----------------------------------------------------------------------
#
# SYNTAX: 
# label    --> DATA:[method]:label:[text]
# n x data --> DATA:[method]:data:[perf-label]:[oid]:[optional unit]
#
# DATA:cpu:label:CPU usage
# DATA:cpu:data:cpu-user:1.3.6.1.4.1.2021.11.9.0:%
# DATA:cpu:data:cpu-system:1.3.6.1.4.1.2021.11.10.0:%
# DATA:cpu:data:cpu-idle:1.3.6.1.4.1.2021.11.11.0:%
#
# DATA:load:label:System load
# DATA:load:data:load1:1.3.6.1.4.1.2021.10.1.5.1:
# DATA:load:data:load5:1.3.6.1.4.1.2021.10.1.5.2:
# DATA:load:data:load15:1.3.6.1.4.1.2021.10.1.5.3:
#
# DATA:netio:label:Network IO (experimental)
# DATA:netio:data:in:1.3.6.1.2.1.4.3.0:
# DATA:netio:data:out:1.3.6.1.2.1.4.10.0:
#    
# DATA:synotemp:label:Synology NAS temperature
# DATA:synotemp:data:temp:1.3.6.1.4.1.6574.1.2.0:°C
#
# ----------------------------------------------------------------------
# 2020-08-11  <axel.hahn@iml.unibe.ch>  initial version
# 2020-08-13  <axel.hahn@iml.unibe.ch>  add host in label for counter data
# ======================================================================

. `dirname $0`/inc_pluginfunctions

# --- basic vars
ABOUT="SNMP performance data v0.3"
SNMPCOMMUNITY="public"
SNMPVERSION="2c"
SNMPGET=$(which snmpget)

HOSTNAME="127.0.0.1"
METHOD=""

# --- OID declarations
# OID_syno="1.3.6.1.4.1.6574"
# OID_ucdavis="1.3.6.1.4.1.2021" # University of California, Davis - private UCD SNMP MIB extensions

# --- output variables
SYNO=""
out=""

#---------------------------------------------------
# FUNCTIONS
#---------------------------------------------------

# --- write verbose text
_wd()
{
    if [ "$verbose" = "yes" ] ; then 
        out="${out}$*
" ; fi
}

#---------------------------------------------------
# --- get config entries in the DATA comment lines

# get line(s) for config
# param  string  one of label|data
_cfg_reader(){
    grep "^#\ DATA:$METHOD:$1:" $0
}
_cfg_getlabel(){
    _cfg_reader "label" | cut -f 4- -d ":"
}
_cfg_dataitem(){
    _cfg_reader "data" | cut -f $1 -d ":" | while read item
    do
        echo -n "$item "
    done
}

# get a list existing methods
_cfg_getMethods(){
    grep "^#\ DATA:.*:label:" $0 | cut -f 2 -d ":"
}

#---------------------------------------------------
# --- get a value from SNMP output data
# global string  $SYNO  output of snmpget
# param  string  mib    string
_getresult(){
    echo "$SYNO" | grep "${1} " | cut -d "=" -f2 | cut -f 2- -d " "
}

# --- get a value from SNMP output data
# param  string  mib    string
_get(){
    _getresult $1 | cut -d ":" -f2 | cut -f 2- -d " "
}

# is given oid a counter?
# function resturns "true" or "false" 
_iscounter(){
    _getresult $1 | cut -d ":" -f1 | grep -i "Counter" >/dev/null
    if [ $? -eq 0 ]; then
        echo "true"
    else
        echo "false"
    fi
}

#---------------------------------------------------
# --- show usage
usage()
{
    cat <<EOH

$ABOUT

University of Bern * Institute of Medical Education
GNU GPL 3.0

USAGE:
    ./check_snmp_data -h hostname [-C communitystring] -m method

PARAMETERS:
    -h  hostname       as fqdn or ip address
    -C  communitystr   optional: community string for snmp; default is $SNMPCOMMUNITY
    -v                 verbose output
    -m  method         what to show
                       method is one of ...
EOH

    # --- dynamic: add current methods
    _cfg_getMethods | while read line
    do
        METHOD=`echo $line | cut -f 1 -d ":"`
        descr=$(_cfg_getlabel)
        printf "                          %-10s %-50s\n" $METHOD "$descr"
    done

    # --- finish usage
    cat <<EOH2

EXAMPLE:
    ./check_snmp_data -h 192.168.100.12 -v -m cpu

EOH2
    ph.abort ""
}


#---------------------------------------------------
# MAIN
#---------------------------------------------------

while getopts h:m:v OPTNAME; do
        case "$OPTNAME" in

        h)
                HOSTNAME="$OPTARG"
                option_found=1
                ;;
		C)
			    SNMPCOMMUNITY="$OPTARG"
			    ;;
        m)
                METHOD="$OPTARG"
                ;;
        v)
                verbose="yes"
                _wd ""
                ;;
        *)
                usage
                ;;
        esac
done

if [ "$option_found" = "0" ] || [ "$HOSTNAME" = "" ] ; then
    usage
    # remark: script aborts ...
fi

# --- read metadata of the selected METHOD
info=$(_cfg_getlabel)
if [ -z "$info" ]; then
    echo ERROR: unknown METHOD [$METHOD]
    usage
fi

prflist=( $(_cfg_dataitem 4) )
oidlist=( $(_cfg_dataitem 5) )
unitlist=( $(_cfg_dataitem 6) )


# --- SNPGET to all wanted oids
SYNO=`$SNMPGET -One -t 10 -v $SNMPVERSION -c $SNMPCOMMUNITY $HOSTNAME ${oidlist[*]} 2> /dev/null`

if [ $? -ne 0 ] ; then
    $SNMPGET -One -t 10 -v $SNMPVERSION -c $SNMPCOMMUNITY $HOSTNAME ${oidlist[*]}
    ph.abort "Problem with SNMP request"
fi


# --- performance data
for index in ${!oidlist[*]}
do
    label="${prflist[$index]}"
    value=$(_get ${oidlist[$index]})
    unit="${unitlist[$index]}"

    # handle counter data
    if [ "$(_iscounter ${oidlist[$index]})" = "true" ]; then
        value2=`ph.perfdeltaspeed "snmp-data-${HOSTNAME}-${method}-${label}" $value`
        _wd "$( printf '%-14s total: %-14s delta: %6s %s per sec' $label $value $value2 $unit )"
        value=$value2
    else
        _wd "$( printf '%-14s %s %s' $label $value $unit )"
    fi

    ph.perfadd "${label}" "${value}"
done


# --- output
ph.status "SNMP performance data :: $info $out"
ph.exit