#!/bin/sh
# ======================================================================
#
# Include file for SNMP related checks
# Used by
# - check_snmp_data
# - check_snmp_printer
# - check_snmp_synology
#
# ======================================================================

# ----------------------------------------------------------------------
# Variables
# ----------------------------------------------------------------------
SNMPAUTH=
SNMPCONFIG=/etc/icinga2/snmp.cfg
SNMPCOMMUNITY="public"
SNMPVERSION="2c"


SNMPWALK=$(which snmpwalk)
SNMPGET=$(which snmpget)

SNMPTIMEOUT=3

SNMPTARGET="127.0.0.1"


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

# read config data for authentication; it sets the variable SNMPAUTH
# that can be used for snmpwalk / snmpget
#
# no params
#
# used global vars:
#   - SNMPVERSION + SNMPCOMMUNITY to set the default
#   - SNMPCONFIG - filename of the config to read
#   - SNMPTARGET   - SNMPTARGET (ip or fqdn) of target to connect
#
read_config(){
    SNMPAUTH="-v $SNMPVERSION -c $SNMPCOMMUNITY"

    if [ -r "$SNMPCONFIG" ]; then
        SNMPAUTH="$( grep -v "^#" "$SNMPCONFIG" | grep ",*${SNMPTARGET}[,:]" | cut -f 2- -d ':' )"
        if [ -z "$SNMPAUTH" ]; then
            SNMPAUTH="$( grep "^DEFAULT:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
            if [ -z "$SNMPAUTH" ]; then
                ph.setStatus "unknown"
                echo "ERROR: No authentication data were found for [${SNMPTARGET}] in config file [$SNMPCONFIG]."
                ph.exit
            fi
        fi
    else
        ph.setStatus "unknown"
        echo "ERROR: unable to read config file [$SNMPCONFIG]."
        ph.exit
    fi
}


# ----------------------------------------------------------------------