From 720c78d83b44720a809f0bf16b309353ed2eadfe Mon Sep 17 00:00:00 2001
From: Fulvio Galeazzi <fulvio.galeazzi@garr.it>
Date: Tue, 6 Mar 2018 01:30:23 +0100
Subject: [PATCH] 2018-03-06:  FG;  Consistently changed (script/config)
 behaviour of -r switch.

---
 .../Config/userparameter_dellmd38xx.conf      |  3 +-
 .../Script/zbxDellStorageDeviceStatus.sh      | 59 ++++++++++++++-----
 2 files changed, 47 insertions(+), 15 deletions(-)

diff --git a/Dell_MD38XX/Config/userparameter_dellmd38xx.conf b/Dell_MD38XX/Config/userparameter_dellmd38xx.conf
index f551190..d8b64a9 100644
--- a/Dell_MD38XX/Config/userparameter_dellmd38xx.conf
+++ b/Dell_MD38XX/Config/userparameter_dellmd38xx.conf
@@ -1,5 +1,6 @@
+# $2 is left as a placeholder, just in case...
 UserParameter=custom.dellmd38xx.array.discovery[*],/etc/zabbix/scripts/zbxDellStorageDiscover.sh $1 $2
-UserParameter=custom.dellmd38xx.array.exec[*],/etc/zabbix/scripts/zbxDellStorageDeviceStatus.sh $1 --raid $2
+UserParameter=custom.dellmd38xx.array.exec[*],/etc/zabbix/scripts/zbxDellStorageDeviceStatus.sh $1 --raid $2,$3
 # UserParameter=custom.dellmd38xx.device.discovery[*],/etc/zabbix/scripts/zbxDellStorageDiscover.sh $1 $2 $3 --devices
 #---> Workaround, as above command takes too long to execute: make Zabbix happy by reading a file, produced with crontab by above script.
 UserParameter=custom.dellmd38xx.device.discovery[*],cat /tmp/zbxDellStorageDiscover.out
diff --git a/Dell_MD38XX/Script/zbxDellStorageDeviceStatus.sh b/Dell_MD38XX/Script/zbxDellStorageDeviceStatus.sh
index 170ab08..51ef610 100755
--- a/Dell_MD38XX/Script/zbxDellStorageDeviceStatus.sh
+++ b/Dell_MD38XX/Script/zbxDellStorageDeviceStatus.sh
@@ -49,8 +49,8 @@ Options:
   -m, --match=String    Filter Storage Array name according to String
 
   -r, --raid=String     Skip initial SMcli query and assume argument is 
-                        comma-separated list of controllers belonging 
-                        to the same Storage Array
+                        comma-separated list of controller-name followed
+                        by relevant controller addresses
 
   -w, --want=String1[,String2[,String3]]
                         List of words identifying storage objects
@@ -103,7 +103,7 @@ Options:
 ZABBIXSERVER=localhost
 SENDER=/usr/bin/zabbix_sender
 DEBUG=0
-TEST=1
+TEST=0
 ZABBIXPORT=10051
 ZABBIXSOURCE=localhost
 CHECKSYNTAX="-e"
@@ -232,6 +232,7 @@ if [[ -z $CONFIGFILE ]]; then
     echo "$DEFAULTCONFIG" > $CONFIGFILE
 fi
 
+hasFailed=0
 if [[ -z "$RAIDCTRLLIST" ]]; then
     KNOWNARRAYS=`mktemp`
     if [ -z "$PATHBIN" ]; then
@@ -270,7 +271,8 @@ if [[ -z "$RAIDCTRLLIST" ]]; then
 	done < $KNOWNARRAYS
 	rm $KNOWNARRAYS
     else
-	echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.device.failed[${raidNameThis}]\" 2" >> $SENDERFILESTATUS
+	hasFailed=2
+	echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.device.failed[${raidNameThis}]\" $hasFailed" >> $SENDERFILESTATUS
 	SENDER_CMD="$SENDER -v --zabbix-server $ZABBIXSERVER --port $ZABBIXPORT --input-file $SENDERFILESTATUS"
 	if [[ $DEBUG = 1 ]]; then
 	    cat $SENDERFILESTATUS >> $SENDERFILEDEBUG
@@ -282,13 +284,24 @@ if [[ -z "$RAIDCTRLLIST" ]]; then
 	else
 	    [[ $TEST = 0 ]] && eval $SENDER_CMD > /dev/null
 	fi
-	echo 2
     fi
     raidList="$raidCtrlThis"
 else
-    raidList="${RAIDCTRLLIST//,/ }"
+    raidNameThis="${RAIDCTRLLIST%%,*}"
+    tmpString="${RAIDCTRLLIST#*,}"
+    raidList="${tmpString//,/ }"
 fi
 
+if [[ $hasFailed > 0 ]]; then
+    if [[ $TEST = 0 ]]; then
+	if [[ $DEBUG = 1 ]]; then
+	    rm -f $SENDERFILEDEBUG
+	fi
+	rm -f $SENDERFILESTATUS
+    fi
+    echo $hasFailed
+    exit $hasFailed
+fi
 IFS=',' read -a wantObjArr <<< "$WANTOBJECTS"
 
 GET_CMD="$SUDOCMD $PATHBIN $raidList $CHECKSYNTAX -c 'set session performanceMonitorInterval=$MONINTERVAL performanceMonitorIterations=1 ; save storageArray performanceStats file=\"$STATUSFILE\";' > /dev/null"
@@ -365,7 +378,8 @@ if [[ $RET_VAL = 0 ]]; then
 	rm -f $SENDERFILE
     done < $STATUSFILE
 else
-    echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.device.failed[${raidNameThis}]\" 1" >> $SENDERFILESTATUS
+    hasFailed=1
+    echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.device.failed[${raidNameThis}]\" $hasFailed" >> $SENDERFILESTATUS
     SENDER_CMD="$SENDER -v --zabbix-server $ZABBIXSERVER --port $ZABBIXPORT --input-file $SENDERFILESTATUS"
     if [[ $DEBUG = 1 ]]; then
         cat $SENDERFILESTATUS >> $SENDERFILEDEBUG
@@ -377,9 +391,22 @@ else
     else
         [[ $TEST = 0 ]] && eval $SENDER_CMD > /dev/null
     fi
-    echo 1
 fi
 
+if [[ $hasFailed > 0 ]]; then
+    if [[ $TEST = 0 ]]; then
+	if [[ $DEBUG = 1 ]]; then
+            rm -f $SENDERFILEDEBUG
+	fi  
+	rm -f $SENDERFILESTATUS
+	rm -f $STATUSFILE
+	if [[ -n $tmpCONFIGFILE ]]; then
+	    rm -f $CONFIGFILE
+	fi
+    fi
+    echo $hasFailed
+    exit $hasFailed
+fi
 
 # DEBUG
 if [[ $DEBUG = 1 ]]; then
@@ -408,6 +435,7 @@ if [[ $DEBUG = 1 ]]; then
     echo "--zabbixport=$ZABBIXPORT"
     echo "--zabbixsource=$ZABBIXSOURCE"
     echo "--timeout=$TIMEOUT"
+    echo "--test=$TEST"
     
     echo -e "\n  wantObjects=${wantObjArr[@]}\n"
     
@@ -419,11 +447,14 @@ if [[ $DEBUG = 1 ]]; then
 fi
 
 # Housekeeping
-rm -f $STATUSFILE
-rm -f $SENDERFILEDEBUG
-rm -f $SENDERFILESTATUS
-if [[ -n $tmpCONFIGFILE ]]; then
-    rm -f $CONFIGFILE
+if [[ $TEST = 0 ]]; then
+    if [[ $DEBUG = 1 ]]; then
+	rm -f $SENDERFILEDEBUG
+    fi
+    rm -f $SENDERFILESTATUS
+    rm -f $STATUSFILE
+    if [[ -n $tmpCONFIGFILE ]]; then
+	rm -f $CONFIGFILE
+    fi
 fi
-
 echo 0
-- 
GitLab