Skip to content
Snippets Groups Projects
check_snmp_printer 4.06 KiB
#!/bin/env bash
# ============================================================
#
# Wrapper for check_snmp_printer plugin
#
# The reason behind this wrapper is detecting the snmp auth
# parameters by check_dnmp_includes which needs a snmp target
# to find its custom config.
# 
# All given params will be sent to
# /usr/lib64/nagios/plugins/check_snmp_printer_v2.0.1
# except "-c <script>"
#
# ------------------------------------------------------------
# 2021-03-22  <axel.hahn@iml.unibe.ch>  v0.1
# 2023-09-18  <axel.hahn@iml.unibe.ch>  v0.2  snmpv3 support
# 2023-10-12  <axel.hahn@iml.unibe.ch>  v0.3  parameter for custom script; detect debian
# 2025-02-12  <axel.hahn@unibe.ch>      v1.0  add help and debug param
# ============================================================

# ------------------------------------------------------------
# CONFIG
# ------------------------------------------------------------

. $( dirname $0 )/inc_pluginfunctions
. $( dirname $0 )/check_snmp_includes

self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=1.0

debug=0

# check='/usr/lib64/nagios/plugins/check_snmp_printer'
# updated script is taken from https://github.com/Tylan/check_snmp_printer/tree/master
nagiosDir='/usr/lib64/nagios/plugins'
myscript="check_snmp_printer_v2.0.1"
case "$( ph.getOS)" in
    debian)
        nagiosDir='/usr/lib/nagios/plugins'
        ;;
esac

#---------------------------------------------------
# --- show usage
function showHelp(){
    _self=$( basename $0 )
cat <<EOH
$( ph.showImlHelpHeader )

This is a wrapper for the check_snmp_printer plugin.
Maybe you want to upgrade it from 
<https://github.com/Tylan/check_snmp_printer/tree/master>

The wrapper does

- scan for snmp auth parameters of given host by check_snmp_includes
- adds snmp timeout
- executes check_snmp_printer with given other params

USAGE:
    $_self [-d] [-c SCRIPT] -H SNMPTARGET [other params]

PARAMETERS:

    -d, --debug (as 1st param)
       Debug mode: shows all generated params.
       "-d" or "--debug" will be removed from params for 
       check_snmp_printer script

    -c SCRIPT
       Set script to execute with full path
       default: $nagiosDir/$myscript

    -H HOSTNAME
       required: as fqdn or ip address; default: localhost

    All other params for check_snmp_printer, eg.
    --consum
    --messages
    --pagecount
    --trays

CONFIG FILE:
    The config file can be multiline and has the syntax
    [SNMPTARGET[,target2]]:[auth parameters]
    The auth parameters set the version and all needed values to connect.
    Snmp v2 uses a community string.
    Snmp v3 is highly recommended (you should disable Snmp v2) and needs
    a user and password.

EXAMPLE:
    $_self -H 192.168.100.12 --consum

EOH
}

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


# show help
if [ "$#" -eq 0 ]; then
    showHelp
    exit 0
fi
if [ "$1" = "--debug" ] || [ "$1" = "-d" ]; then
    debug=1
    shift
fi

sParams="$*"

# find host behind param -H
SNMPTARGET=$( echo $sParams | sed -Ee 's#.*\-H\ ([0-9a-z\.\-]*).*#\1#' )

# detect -c <script>
check=$(      echo $sParams | sed -Ee 's#.*\-c\ ([0-9a-z\.\-_/]*).*#\1#' )
test "$check" = "$sParams" && check="${nagiosDir}/${myscript}"
test "$check" = "$sParams" || sParams=$( echo $sParams | sed -Ee 's#\-c\ ([0-9a-z\.\-_/]*)##' )

# find SNMPTARGET in config to add snmp auth parameters
test -z "$SNMPAUTH" && read_config
sParams+=" $SNMPAUTH"

# find what to show ... it is more for debug message
what=$( echo $sParams | sed -Ee 's#.*\-\-([a-z]*).*#\1#' )
case "$what" in
    consum)
        what="Consumables"
        ;;
    messages)
        what="Messages of the printer"
        ;;
    pagecount)
        what="Number of printed pages"
        ;;
    trays)
        what="Status of trays for paper"
        sParams="$sParams --nofeeder"
        ;;

esac

# add our global snmp timeout
sParams+=" -t $SNMPTIMEOUT"

test "$debug" = "1" && ( echo "$what:"; echo calling $check $sParams ; echo ) 
$check $sParams

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