From bd99efcb7b89baeccdd0477d310ddbe3088f0d66 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 5 May 2023 14:55:16 +0200
Subject: [PATCH] allow multiple hosts per line in snmp.cfg

---
 check_snmp_data     |  2 +-
 check_snmp_includes | 10 +++++++---
 check_snmp_synology |  2 +-
 inc_pluginfunctions |  4 ++--
 4 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/check_snmp_data b/check_snmp_data
index f9c8641..90ec912 100755
--- a/check_snmp_data
+++ b/check_snmp_data
@@ -186,7 +186,7 @@ $_methods
 
 CONFIG FILE:
     The config file can be multiline and has the syntax
-    [SNMPTARGET]:[auth parameters]
+    [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
diff --git a/check_snmp_includes b/check_snmp_includes
index c54910d..83b8a6b 100644
--- a/check_snmp_includes
+++ b/check_snmp_includes
@@ -35,10 +35,14 @@ 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 -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"
diff --git a/check_snmp_synology b/check_snmp_synology
index 93936f5..c994e19 100755
--- a/check_snmp_synology
+++ b/check_snmp_synology
@@ -96,7 +96,7 @@ OPTIONS:
 
 CONFIG FILE:
     The config file can be multiline and has the syntax
-    [SNMPTARGET]:[auth parameters]
+    [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
diff --git a/inc_pluginfunctions b/inc_pluginfunctions
index 3de16e7..3904103 100644
--- a/inc_pluginfunctions
+++ b/inc_pluginfunctions
@@ -325,8 +325,8 @@ function ph.getFileAge(){
 function ph._getStorefile(){
         local varName=$1
 
-        local mydir="/tmp/icinga_counter_$( whoami )"
-        test -n "$dir_data" && mydir="${dir_data}/_counter_$( whoami )"
+        local mydir="/tmp/icinga_counter_${USER}"
+        test -n "$dir_data" && mydir="${dir_data}/_counter_${USER}"
         local _basename
         _basename=$(basename $0)
 
-- 
GitLab