From 28d87af8b24ce01fd8692d844e34c48ddfda503d Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 12 Feb 2025 11:11:45 +0100
Subject: [PATCH] snmp_printer: add help and debug param

---
 check_snmp_printer | 125 +++++++++++++++++++++++++++++++++------------
 1 file changed, 92 insertions(+), 33 deletions(-)

diff --git a/check_snmp_printer b/check_snmp_printer
index 811b544..0d07fab 100755
--- a/check_snmp_printer
+++ b/check_snmp_printer
@@ -11,24 +11,11 @@
 # /usr/lib64/nagios/plugins/check_snmp_printer_v2.0.1
 # except "-c <script>"
 #
-# TODO: IML like help
-#
-# USAGE
-#   check_snmp_printer -H <host|ip>             --<what>
-#   check_snmp_printer -H <host|ip> -c <script> --<what>
-#
-#     <host>|<ip>   target to connect
-#     <what>        what information will be shown
-#                   --consum
-#                   --messages
-#                   --pagecount
-#                   --trays
-#     <script>      script to execute with full path
-#
 # ------------------------------------------------------------
 # 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
 # ============================================================
 
 # ------------------------------------------------------------
@@ -38,6 +25,9 @@
 . $( 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'
@@ -45,15 +35,80 @@ debug=0
 nagiosDir='/usr/lib64/nagios/plugins'
 myscript="check_snmp_printer_v2.0.1"
 case "$( ph.getOS)" in
-        debian)
-                nagiosDir='/usr/lib/nagios/plugins'
-                ;;
+    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
@@ -64,29 +119,33 @@ 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
+# 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"
-                ;;
+    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 -t $SNMPTIMEOUT
+$check $sParams
 
 # ------------------------------------------------------------
-- 
GitLab