#!/bin/bash 
#########################################################################
# Script:       check_datacore.sh                                       #
# Author:       Roland RIPOLL							                #
# Purpose:      Monitor Datacore sansynphony 9 with Nagios				#
# Description:  Checks Datacore sansynphony 9 via SNMP.					#
#               Can be used to query status and performance info        #
# Tested on:    Datacore sansynphony 9									#
# History:                                                              #
# 20130215 cr�ation														#
# 20130308 am�lioration nombre de requettes SNMP						#
# 20130325 Ajout champ													#
# 20130405 Ajout condition attention warinng critique sur les volumes	#
# 20131226 refonte suite changement OID sur la PSP4    					#
#########################################################################
# Usage: ./check_datacore.sh -H host -C community -t type
#########################################################################
STATE_OK=0              # define the exit code if status is OK
STATE_WARNING=1         # define the exit code if status is Warning
STATE_CRITICAL=2        # define the exit code if status is Critical
STATE_UNKNOWN=3         # define the exit code if status is Unknown
#PATH=/usr/local/bin:/usr/bin:/bin # Set path

# Get user-given variables
#########################################################################
while getopts "H:C:t:w:c:" Input;
do
       case ${Input} in
       H)      host=${OPTARG};;
       C)      community=${OPTARG};;
	   t)      type=${OPTARG};;
       \?)     echo "options -H for host, -C for SNMP-Community, -t for type (Replicat ou Volume)"
               exit 1
               ;;
       esac
done

case ${type} in
Replicat)
	state=0;
	texte=""
	
	SELECTcheck=( $(snmpwalk -v 2c -O n -c  ${community} ${host} 1.3.6.1.4.1.7652.1.1.1 | grep "Monitors the failure status of virtual disk sources." | cut -b 27-124) );
	texte="$texte FailureStatus : <BR>";
	inc=-1;
	for index in "${!SELECTcheck[@]}"; 
		do
			inc=$( expr $inc + 1 );
			OIDState="1.3.6.1.4.1.7652.1.1.1.5.${SELECTcheck[$inc]}";
			OIDCaption="1.3.6.1.4.1.7652.1.1.1.4.${SELECTcheck[$inc]}";
			OIDStMessage="1.3.6.1.4.1.7652.1.1.1.6.${SELECTcheck[$inc]}";
			MState=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDState | cut -d\" -f 2);
			MCaption=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDCaption | cut -d\" -f 2);
			MStMessage=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDStMessage | cut -d\" -f 2);
			if [ "$MState" = "Healthy" ];
			then 
				texte="$texte $MCaption : $MStMessage <BR>"; 
				state=$state; 
			else 
				texte="$texte KO $MCaption : $MStMessage <BR>"; 
				state=$( expr $state + 1 );
			fi
		done 
	
	SELECTcheck=( $(snmpwalk -v 2c -O n -c  ${community} ${host} 1.3.6.1.4.1.7652.1.1.1 | grep "Monitors the data status of virtual disk sources." | cut -b 27-124) );
	texte="$texte DataStatus : <BR>";
	inc=-1;
	for index in "${!SELECTcheck[@]}"; 
		do
			inc=$( expr $inc + 1 );
			OIDState="1.3.6.1.4.1.7652.1.1.1.5.${SELECTcheck[$inc]}";
			OIDCaption="1.3.6.1.4.1.7652.1.1.1.4.${SELECTcheck[$inc]}";
			OIDStMessage="1.3.6.1.4.1.7652.1.1.1.6.${SELECTcheck[$inc]}";
			MState=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDState | cut -d\" -f 2);
			MCaption=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDCaption | cut -d\" -f 2);
			MStMessage=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDStMessage | cut -d\" -f 2);
			if [ "$MState" = "Healthy" ];
			then 
				texte="$texte $MCaption : $MStMessage <BR>"; 
				state=$state; 
			else 
				texte="$texte KO $MCaption : $MStMessage <BR>"; 
				state=$( expr $state + 1 );
			fi
		done
		
	SELECTcheck=( $(snmpwalk -v 2c -O n -c ${community} ${host} 1.3.6.1.4.1.7652.1.1.1 | grep "Monitors the access rights of virtual disk sources." | cut -b 27-124) );
	texte="$texte AccessRights : <BR>";
	inc=-1;
	for index in "${!SELECTcheck[@]}"; 
		do
			inc=$( expr $inc + 1 );
			OIDState="1.3.6.1.4.1.7652.1.1.1.5.${SELECTcheck[$inc]}";
			OIDCaption="1.3.6.1.4.1.7652.1.1.1.4.${SELECTcheck[$inc]}";
			OIDStMessage="1.3.6.1.4.1.7652.1.1.1.6.${SELECTcheck[$inc]}";
			MState=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDState | cut -d\" -f 2);
			MCaption=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDCaption | cut -d\" -f 2);
			MStMessage=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDStMessage | cut -d\" -f 2);
			if [ "$MState" = "Healthy" ];
			then 
				texte="$texte $MCaption : $MStMessage <BR>"; 
				state=$state; 
			else 
				texte="$texte KO $MCaption : $MStMessage <BR>"; 
				state=$( expr $state + 1 );
			fi
		done
	if [ "$state" = "0" ]; 
	then
         echo "$texte";
		exit ${STATE_OK};
	else
        echo "Mirroir FAIL !<BR> $texte";
		exit ${STATE_CRITICAL};
	fi
;;
Volume)
	state=0;
	texte=""
	
	SELECTcheck=( $(snmpwalk -v 2c -O n -c ${community} ${host} 1.3.6.1.4.1.7652.1.1.1 | grep "Monitors the disk pools available space percentage." | cut -b 27-124) );
	texte="$texte Etat des Espaces sur les volumes :<br>";
	inc=-1;
	for index in "${!SELECTcheck[@]}"; 
		do
			inc=$( expr $inc + 1 );
			OIDState="1.3.6.1.4.1.7652.1.1.1.5.${SELECTcheck[$inc]}";
			OIDCaption="1.3.6.1.4.1.7652.1.1.1.4.${SELECTcheck[$inc]}";
			OIDStMessage="1.3.6.1.4.1.7652.1.1.1.6.${SELECTcheck[$inc]}";
			MState=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDState | cut -d\" -f 2);
			MCaption=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDCaption | cut -d\" -f 2);
			MStMessage=$(snmpget -v 2c -O vq -c ${community} ${host} $OIDStMessage | cut -d\" -f 2);
			if [ "$MState" = "Healthy" ];
			then 
				texte="$texte $MCaption : $MStMessage <BR>"; 
				state=$state; 
			fi
			if [ "$MState" = "Attention" ];
			then 
				texte="$texte $MCaption : $MStMessage <BR>"; 
					if [ $state -gt 0 ]; 
					then 
						state=$state;
					else	
						state=1;
					fi
			fi
			if [ "$MState" = "Warning" ];
			then 
				texte="$texte $MCaption : $MStMessage <BR>"; 
					if [ $state -gt 1 ]; 
					then 
						state=$state;
					else	
						state=2;
					fi
			fi
			if [ "$MState" = "Critical" ];
			then 
				texte="$texte $MCaption : $MStMessage <BR>"; 
					if [ $state -gt 2 ]; 
					then 
						state=$state;
					else	
						state=3;
					fi 
			fi					
		done

	if [ "$state" = "0" ]; 
	then
       	echo "$texte";
		exit ${STATE_OK};
	fi
	if [ "$state" = "1" ]; 
	then
		echo "Attention ESPACE !<BR> $texte";
       	exit ${STATE_WARNING};
	fi
	if [ "$state" = "2" ]; 
	then
		echo "Warning ESPACE !<BR> $texte";
       	exit ${STATE_WARNING};
	fi
	if [ "$state" = "3" ]; 
	then
		echo "Alerte ESPACE !<BR> $texte";
       	exit ${STATE_CRITICAL};
	fi 
;;
esac	
echo "type non reconnu"
exit ${STATE_UNKNOWN}
