From e4b296212e615aa3d390d4bde2d75ced107343e9 Mon Sep 17 00:00:00 2001
From: Fulvio Galeazzi <fulvio.galeazzi@garr.it>
Date: Tue, 27 Feb 2018 12:02:51 +0100
Subject: [PATCH] 2018-02-27:  FG;  Discovery and setting item values for Dell
 MD38XX.

---
 .../zbxDellStorageDeviceDiscover.sh}          | 121 +++++++-----------
 .../zbxDellStorageStatusCheck.sh}             |  50 +++++++-
 2 files changed, 88 insertions(+), 83 deletions(-)
 rename Dell_MD38XX/{zbxDellStorageDeviceDiscover => Script/zbxDellStorageDeviceDiscover.sh} (68%)
 rename Dell_MD38XX/{zbxDellStorageStatusCheck => Script/zbxDellStorageStatusCheck.sh} (86%)

diff --git a/Dell_MD38XX/zbxDellStorageDeviceDiscover b/Dell_MD38XX/Script/zbxDellStorageDeviceDiscover.sh
similarity index 68%
rename from Dell_MD38XX/zbxDellStorageDeviceDiscover
rename to Dell_MD38XX/Script/zbxDellStorageDeviceDiscover.sh
index b01f859..d57fffa 100755
--- a/Dell_MD38XX/zbxDellStorageDeviceDiscover
+++ b/Dell_MD38XX/Script/zbxDellStorageDeviceDiscover.sh
@@ -2,7 +2,7 @@
 #
 # Name: zbxDellStorageDeviceDiscover
 #
-# Get Dell MD38XX storage server metrics and send to Zabbix using zabbix_sender
+# Discover Dell MD38XX storage objects: Storage, RAID, Disk (Group and Pool)
 #
 # Author: Fulvio Galeazzi
 #
@@ -12,7 +12,7 @@
 # ApacheStatsForZabbix: https://github.com/gpmidi/zabbix-apache-stats
 #
  
-name="zbxDellStorageStatusCheck"
+name="zbxDellStorageDeviceDiscover"
 version="0.1"
 
 # Test  if  your  getopt(1)  is this enhanced version or an old version
@@ -129,28 +129,23 @@ while true; do
 done
 
 # Temporary files
-STATUSFILE=`mktemp`
-SENDERFILE=`mktemp`
-if [[ -z $CONFIGFILE ]]; then
-    CONFIGFILE=`mktemp`
-    tmpCONFIGFILE=1
-    echo "$DEFAULTCONFIG" > $CONFIGFILE
-fi
 KNOWNARRAYS=`mktemp`
 
 GET_CMD="$PATHBIN -d -i > $KNOWNARRAYS"
+if [[ $DEBUG = 1 ]]; then
+    echo -n -e "\n########################################\n"
+    echo -n -e "Command line to collect data\n"
+    echo -n -e "$GET_CMD\n"
+fi
 eval $GET_CMD
 RET_VAL=$?
 if [[ $RET_VAL = 0 ]]; then
-
-    cat $KNOWNARRAYS
-
     index=0
-    # 1 is with ','
+    # 1: list of IP separated by ','
     raidCtrlArray1=()
-    # 2 is with ' '
+    # 2: list of IP separated by ' '
     raidCtrlArray2=()
-    devNameArray=()
+    raidNameArray=()
     while read aLine ; do
 	# Strip leading and trailing space, replace blanks with comma
 	aNiceLine=`echo $aLine | sed -e 's/^\s\+//' -e 's/\s\+$//' -e 's/\s\+/,/g'`
@@ -158,36 +153,40 @@ if [[ $RET_VAL = 0 ]]; then
 	if [[ -z "$firstWord" || ${firstWord} == *"SMcli"* ]]; then
 	    continue
 	fi
-	echo "First: -->${firstWord}<--"
 	if [[ ! -z "$MATCHSTR" && ! "$aLine" == *"$MATCHSTR"* ]]; then
 	    continue
 	fi
-	echo "Line: ${aNiceLine}"
-
-	devNameArray[$index]="$firstWord"
+	raidNameArray[$index]="$firstWord"
 	tmpString="${aNiceLine#*,}"
 	raidCtrlArray1[$index]="$tmpString"
 	raidCtrlArray2[$index]="${tmpString//,/ }"
+	if [[ $DEBUG = 1 ]]; then
+	    echo -n -e "\tGot:\t"
+	    echo -e "raidName: ${raidNameArray[$index]} raidArray1: --${raidCtrlArray1[$index]}-- raidArray2: --${raidCtrlArray2[$index]}--"
+	fi
 	index=$(($index+1))
     done < $KNOWNARRAYS
-    echo "Dev:   ${devNameArray[@]}"
-    echo "Raid1:  ${raidCtrlArray1[@]}"
-    echo "Raid2:  ${raidCtrlArray2[@]}"
+    rm $KNOWNARRAYS
 
     IFS=',' read -a wantObjArr <<< "$WANTOBJECTS"
-
-    for (( idx=0; idx<${#devNameArray[@]}; idx++)); do
+    
+    DISCOJSON=`mktemp`
+    echo -e '{\n  "data":[' >> $DISCOJSON
+    firstIter=1
+    for (( idx=0; idx<${#raidNameArray[@]}; idx++)); do
 	raidList="${raidCtrlArray2[$idx]}"
 	STATUSFILE=`mktemp`
-	GET_CMD="$PATHBIN $raidList -c 'set session performanceMonitorInterval=3 performanceMonitorIterations=1 ; save storageArray performanceStats file=\"$STATUSFILE\";' > /dev/null"
 
-	echo "Execute: $GET_CMD"
+	GET_CMD="$PATHBIN $raidList $CHECKSYNTAX -c 'set session performanceMonitorInterval=3 performanceMonitorIterations=1 ; save storageArray performanceStats file=\"$STATUSFILE\";' > /dev/null"
+	if [[ $DEBUG = 1 ]]; then
+	    echo -n -e "\n########################################\n"
+	    echo -n -e "Command line to collect data\n"
+	    echo -n -e "$GET_CMD\n"
+	fi
 	# get data from server
 	eval $GET_CMD
 	RET_VAL=$?
-
 	if [[ $RET_VAL = 0 ]]; then
-
 	    while read STATUSLINE
 	    do
 		# read the status file to get all relevant statistics
@@ -201,73 +200,43 @@ if [[ $RET_VAL = 0 ]]; then
 		if [[ -z "$firstWord" || ! "${wantObjArr[@]}" =~ "$firstWord" ]]; then
 		    continue
 		fi
-		
 		tmpString=`echo $firstField | awk '{print $1" "$2}'`
 		deviceType=${tmpString// /_}
 		deviceName=${firstField:${#tmpString}+1}
 		deviceName=${deviceName// /_}
-
-		echo "RAID: ${raidCtrlArray1[$idx]}  Want: $WANTOBJECTS  Type: $deviceType   Name: $deviceName"
+		
+		if [[ $firstIter = 1 ]]; then
+		    firstIter=0
+		else
+		    echo "," >> $DISCOJSON
+		fi
+		echo -n -e "    {\n\t\"{#RAIDNAME}\":\"${raidNameArray[$idx]}\", " >> $DISCOJSON
+		echo -n -e "\"{#DEVTYPE}\":\"${deviceType}\", \"{#DEVNAME}\":\"${deviceName}\"\n    }" >> $DISCOJSON
+		if [[ $DEBUG = 1 ]]; then
+		    echo -n -e "\tGot:\t"
+		    echo -e "raidName: ${raidNameArray[$idx]}  deviceType: ${deviceType} \t deviceName: ${deviceName}"
+		fi
 	    done < $STATUSFILE
-
-
 	fi
 	rm $STATUSFILE
     done
+    echo -e '\n  ]\n}' >> $DISCOJSON
 fi
 
-
 # DEBUG
 if [[ $DEBUG = 1 ]]; then
     echo -e "\n########################################\n"
-    echo -e "Statistics available on your server:\n"
-    cat $STATUSFILE | awk -F: '{print $1}'
-    
-    echo -e "\n########################################\n"
-    echo -e "Statistics that I am collecting:\n"
-    cat $CONFIGFILE
     
-    echo -e "\n########################################\n"
-    echo -e "Metrics to be sent\n"
-    SENDERFILEDEBUG=`mktemp`
-    echo '"SERVER" "METRIC" VALUE' >> $SENDERFILEDEBUG
-    cat $SENDERFILE >> $SENDERFILEDEBUG
-    column -t -s'\"' $SENDERFILEDEBUG
-    rm -f $SENDERFILEDEBUG
-    
-    echo -e "\n########################################\n"
     echo -e "Parameters\n"
     echo "--check=$CHECKSYNTAX"
     echo "--path=$PATHBIN"
-    echo "--raid=$raidList"
+    echo "--match=$MATCHSTR"
     echo "--interval=$MONINTERVAL"
-    echo "--zabbixserver=$ZABBIXSERVER"
-    echo "--sender=$SENDER"
-    echo "--zabbixport=$ZABBIXPORT"
-    echo "--zabbixsource=$ZABBIXSOURCE"
     echo "--timeout=$TIMEOUT"
-    
-    echo -e "\n  wantObjects=${wantObjArr[@]}\n"
-    
+    echo -e "  wantObjects: ${wantObjArr[@]}\n"
     echo -e "\n########################################\n"
-    echo -e "Command line to collect data\n"
-    echo -e "$GET_CMD\n"
-    echo -e "Command line to send data to Zabbix\n"
-    echo -e "$SENDER_CMD\n"
-    
-    if [[ $TEST = 0 ]]; then
-	echo -e "\n########################################\n"
-	echo -e "zabbix_sender output\n"
-	eval $SENDER_CMD
-	echo -e "\n"
-    fi
-else
-    [[ $TEST = 0 ]] && eval $SENDER_CMD > /dev/null
 fi
 
-# Housekeeping
-rm -f $STATUSFILE
-# rm -f $SENDERFILE
-if [[ -n $tmpCONFIGFILE ]]; then
-    rm -f $CONFIGFILE
-fi
+cat $DISCOJSON
+rm $DISCOJSON
+exit 0
diff --git a/Dell_MD38XX/zbxDellStorageStatusCheck b/Dell_MD38XX/Script/zbxDellStorageStatusCheck.sh
similarity index 86%
rename from Dell_MD38XX/zbxDellStorageStatusCheck
rename to Dell_MD38XX/Script/zbxDellStorageStatusCheck.sh
index 94073ce..31522e2 100755
--- a/Dell_MD38XX/zbxDellStorageStatusCheck
+++ b/Dell_MD38XX/Script/zbxDellStorageStatusCheck.sh
@@ -46,8 +46,7 @@ Options:
   -p PATHBIN, --path=PATHBIN
                         Full path of SMcli executable
 
-  -r, --raid IP1[,IP2]  List of IPs or DNS names: all addresses
-                        should belong to the same controller
+  -r, --raid String     Name (or IP of one of the controllers) of the Storage Array
 
   -w, --want String1[,String2[,String3]]
                         List of words identifying storage objects
@@ -155,7 +154,7 @@ while true; do
             shift 2
             ;;
         -r|--raid)
-            RAIDCTRL="$2"
+            RAIDNAME="$2"
             shift 2
             ;;
         -w|--want)
@@ -222,7 +221,44 @@ if [[ -z $CONFIGFILE ]]; then
     echo "$DEFAULTCONFIG" > $CONFIGFILE
 fi
 
-raidList=`echo $RAIDCTRL | sed -e 's/,/ /'`
+KNOWNARRAYS=`mktemp`
+GET_CMD="$PATHBIN -d -i > $KNOWNARRAYS"
+if [[ $DEBUG = 1 ]]; then
+    echo -n -e "\n########################################\n"
+    echo -n -e "Command line to collect data\n"
+    echo -n -e "$GET_CMD\n"
+fi
+eval $GET_CMD
+RET_VAL=$?
+if [[ $RET_VAL = 0 ]]; then
+    index=0
+    raidCtrlThis=""
+    raidNameThis=""
+    while read aLine ; do
+	# Strip leading and trailing space, replace blanks with comma
+	aNiceLine=`echo $aLine | sed -e 's/^\s\+//' -e 's/\s\+$//' -e 's/\s\+/,/g'`
+	firstWord=${aNiceLine%%,*}
+	if [[ -z "$firstWord" || ${firstWord} == *"SMcli"* ]]; then
+	    continue
+	fi
+	if [[ ! "$aLine" == *"$RAIDNAME"* ]]; then
+	    continue
+	fi
+	raidNameThis="$firstWord"
+	tmpString="${aNiceLine#*,}"
+	raidCtrlThis="${tmpString//,/ }"
+	if [[ $DEBUG = 1 ]]; then
+	    echo -n -e "\tGot:\t"
+	    echo -e "raidName: ${raidNameThis} raidCtrl: --${raidCtrlThis}--"
+	fi
+	break
+    done < $KNOWNARRAYS
+    rm $KNOWNARRAYS
+else
+    echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.failed[${raidNameThis}]\" 2" >> $SENDERFILE
+fi
+raidList="$raidCtrlThis"
+
 IFS=',' read -a wantObjArr <<< "$WANTOBJECTS"
 
 GET_CMD="$PATHBIN $raidList $CHECKSYNTAX -c 'set session performanceMonitorInterval=$MONINTERVAL performanceMonitorIterations=1 ; save storageArray performanceStats file=\"$STATUSFILE\";' > /dev/null"
@@ -234,7 +270,7 @@ RET_VAL=$?
 # in case of error, just send [failed] = 1
 # otherwise send all collected data
 if [[ $RET_VAL = 0 ]]; then
-    echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx[failed]\" 0" >> $SENDERFILE
+    echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.failed[${raidNameThis}]\" 0" >> $SENDERFILE
     
     index=0
     zbxKey=()
@@ -269,12 +305,12 @@ if [[ $RET_VAL = 0 ]]; then
 	    if [ ! "$zbxWant[$idx]" ]; then
 		continue
 	    fi
-	    echo -n "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.${zbxKey[$idx]}[${deviceType},${deviceName}]\" " >> $SENDERFILE
+	    echo -n "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.${zbxKey[$idx]}[${raidNameThis},${deviceType},${deviceName}]\" " >> $SENDERFILE
 	    echo ${arrayLine[$idx]} >> $SENDERFILE
 	done
     done < $STATUSFILE
 else
-    echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx[failed]\" 1" >> $SENDERFILE
+    echo "\"$ZABBIXSOURCE\" \"custom.dellmd38xx.failed[${raidNameThis}]\" 1" >> $SENDERFILE
 fi
 
 # Send all information to Zabbix using zbbix_sender
-- 
GitLab