From 8149f2ef2361cbbc025dc4afa4ff5a161afe5eb9 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 5 May 2023 12:33:58 +0200
Subject: [PATCH] Snmp Synology: use include file

---
 check_snmp_includes | 51 ++++++++++++++++++++++++++++++++++++++++++
 check_snmp_synology | 54 ++++++++++++++-------------------------------
 2 files changed, 68 insertions(+), 37 deletions(-)
 create mode 100644 check_snmp_includes

diff --git a/check_snmp_includes b/check_snmp_includes
new file mode 100644
index 0000000..c54910d
--- /dev/null
+++ b/check_snmp_includes
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+# ----------------------------------------------------------------------
+# 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
+        if grep "^${SNMPTARGET}:" "$SNMPCONFIG" >/dev/null; then
+            SNMPAUTH="$( grep "^${SNMPTARGET}:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
+        else
+            SNMPAUTH="$( grep "^DEFAULT:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
+        fi
+    else
+        ph.setStatus "unknown"
+        echo "ERROR: unable to read config file [$SNMPCONFIG]."
+        ph.exit
+    fi
+}
+
+
+# ----------------------------------------------------------------------
diff --git a/check_snmp_synology b/check_snmp_synology
index f36a9df..93936f5 100755
--- a/check_snmp_synology
+++ b/check_snmp_synology
@@ -17,20 +17,13 @@
 #---------------------------------------------------
 
 . $(dirname $0)/inc_pluginfunctions
+. $(dirname $0)/check_snmp_includes
 
 # --- basic vars
 
 self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
 self_APPVERSION=1.2
 
-SNMPCOMMUNITY="public"
-SNMPVERSION="2c"
-SNMPWALK=$(which snmpwalk)
-SNMPGET=$(which snmpget)
-
-SNMPCONFIG=/etc/icingaclient/snmp.cfg
-
-HOSTNAME="localhost"
 option_found=0
 healthString=""
 verbose="no"
@@ -88,20 +81,22 @@ On higher cpu usage it can show processes that cause cpu waits and
 with most cpu consumption.
 
 SYNTAX:
-    $_self [options]
+    $_self [options] -h TARGET
 
 OPTIONS:
-    -a   authentication params for snmpwalk/ snmpget to connect to target; 
-         default: \"-v2c -c public\" (Snmpv2 with community string \"public\")
-    -h   host to connect as hostname or ip address
-    -f FILE  
-         read authentication from config file
-         default: \"/etc/icingaclient/snmp.cfg\"
-    -v   detailed output
+    -a STRING
+        authentication params for snmpwalk/ snmpget to connect to target; 
+        default: \"-v2c -c public\" (Snmpv2 with community string \"public\")
+    -h SNMPTARGET
+        as fqdn or ip address; default: localhost
+    -f FILE
+        read authentication from config file
+        default: \"/etc/icinga2/snmp.cfg\"
+    -v  detailed output
 
 CONFIG FILE:
     The config file can be multiline and has the syntax
-    [hostname]:[auth parameters]
+    [SNMPTARGET]:[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
@@ -114,7 +109,7 @@ CONFIG FILE:
 EXAMPLE:
 
     $_self -h server-01.example.com -v
-        Show Synology status of server-01 using connect data from /etc/icingaclient/snmp.cfg
+        Show Synology status of server-01 using connect data from /etc/icinga2/snmp.cfg
 
     $_self -h server-01.example.com -v -f /opt/somewhere/snmp.conf
         Show Synology status of server-01 using connect data from custom config
@@ -144,21 +139,6 @@ usage()
         ph.abort "$USAGE"
 }
 
-read_config(){
-    SNMPAUTH="-v $SNMPVERSION -c $SNMPCOMMUNITY"
-
-    if [ -r "$SNMPCONFIG" ]; then
-        if grep "^${HOSTNAME}:" "$SNMPCONFIG" >/dev/null; then
-            SNMPAUTH="$( grep "^${HOSTNAME}:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
-        else
-            SNMPAUTH="$( grep "^DEFAULT:" "$SNMPCONFIG" | cut -f 2- -d ':' )"
-        fi
-    else
-        ph.setStatus "unknown"
-        echo "ERROR: unable to read config file [$SNMPCONFIG]."
-        ph.exit
-    fi
-}
 
 #---------------------------------------------------
 # MAIN
@@ -173,7 +153,7 @@ while getopts a:h:f:v OPTNAME; do
             SNMPCONFIG="$OPTARG"
             ;;
         h)
-            HOSTNAME="$OPTARG"
+            SNMPTARGET="$OPTARG"
             option_found=1
             ;;
         v)
@@ -191,8 +171,8 @@ done
 test -z "$SNMPAUTH" && read_config
 
 # --- read raid and disks to get its single OIDs
-nbDisk=$($SNMPWALK -OQne -t 10 ${SNMPAUTH} $HOSTNAME $OID_diskID   2> /dev/null | wc -l )
-nbRAID=$($SNMPWALK -OQne -t 10 ${SNMPAUTH} $HOSTNAME $OID_RAIDName 2> /dev/null | wc -l)
+nbDisk="$( $SNMPWALK -OQne -t $SNMPTIMEOUT ${SNMPAUTH} $SNMPTARGET $OID_diskID   2> /dev/null | wc -l )"
+nbRAID="$( $SNMPWALK -OQne -t $SNMPTIMEOUT ${SNMPAUTH} $SNMPTARGET $OID_RAIDName 2> /dev/null | wc -l )"
 
 for i in $(seq 1 $nbDisk);
 do
@@ -204,7 +184,7 @@ do
 done
 
 # --- SNPGET to all wanted oids
-syno=$($SNMPGET -OQne -t 10 ${SNMPAUTH} $HOSTNAME $OID_model $OID_serialNumber $OID_DSMVersion $OID_DSMUpdateAvailable $OID_systemStatus $OID_powerStatus $OID_systemFanStatus $OID_CPUFanStatus $OID_temp $OID_disk $OID_RAID 2> /dev/null | sed 's/^[ \t]*//;s/[ \t]*$//')
+syno=$($SNMPGET -OQne -t $SNMPTIMEOUT ${SNMPAUTH} $SNMPTARGET $OID_model $OID_serialNumber $OID_DSMVersion $OID_DSMUpdateAvailable $OID_systemStatus $OID_powerStatus $OID_systemFanStatus $OID_CPUFanStatus $OID_temp $OID_disk $OID_RAID 2> /dev/null | sed 's/^[ \t]*//;s/[ \t]*$//')
 
 if [ "$?" != "0" ] ; then
     ph.abort "Problem with SNMP request"
-- 
GitLab