#!/bin/bash

# loggin
logger -p INFO "Nagios syncsort: script started"

# Version 1.5
# Date last change: 14-10-2010

# SUBMIT_CHECK_RESULT rebuild for passive syncsort checks in Nagios.
# Original script Written by Ethan Galstad (egalstad@nagios.org)
# Rewritten for Syncsort by Remco Hage
# RealOpenIT bv. 
# Informaticalaan 7
# 2628ZD DELFT
# The Netherlands
# Phone: +311502568969
#
# Arguments:
#  $1 = host_name (Short name of host that the service is
#       associated with)
#  $2 = svc_description (Description of the service)
#  $3 = return_code (An integer that determines the state
#       of the service check, 0=OK, 1=WARNING, 2=CRITICAL,
#       3=UNKNOWN).
#  $4 = plugin_output (A text string that should be used
#       as the plugin output for the service check)
# 

##########################################
# different inputs that Nagios receives. #
##########################################
#1="A Backup Express job started: Backup Express job iFolder (id 1284626964, type 1) started"
#2"server;SYNCSORT-BACKUP-SERVER;OK;HARD;1;A Backup Express job got the medium: Tape (3996L3) mounted [status 0] on device dev000 (n/a) at node BACKUP-SERVER (0.00.000.0) for job 1284585300 (type 1)"
#3="A Backup Express job needs a medium: Operator on node BACKUP-SERVER (0.00.000.0) please mount tape (3992L3) on device dev000 (n/a) for job 1284535850 (type 3)"
#4="A Backup Express job ended: Backup Express job SERVICE-BASE (id 1284426000, type 1) ended with return code 0."
#5= Backup Express cannot access a node: Backup Express cannot access node ROIT-SQL01 (192.168.2.12)
#6="A Backup Express job cancelled: Backup Express job SERVER_Backup-BASE (id 1284446264, type 1) was cancelled."

###################################################
# For example and script test all types are here: #
###################################################
# 1 OK
	type1=`echo "$4" | grep "A Backup Express job started" | awk '{ print $5 }'`

# 2 OK
	type2=`echo "$4" | grep "A Backup Express job got the medium" |awk '{ print $5 }' `

# 3 OK
	type3=`echo "$4" | grep "A Backup Express job needs a medium" |  awk '{ print $5 }' `

#4 OK OR ERROR
        type4=`echo "$4" | grep "job cancelled" | awk '{ print $5 }'`

#5 ERROR: node not found
	type5=`echo "$4" | grep "cannot access node" | awk '{ print $4$5$6 }'`

#6 WARNING only cancelled jobs
	type6=`echo "$4" | grep "A Backup Express job ended"  | sed -e s/'.*code'/'code'/g | awk '{ print $2 }' | sed s/[\'.]//g`


##############
# VARIABLES: #
##############
hostname=$1
serviceoutput=$4
servicename=${2/-BASE/}
CommandFile="/usr/local/nagios/var/rw/nagios.cmd"
echocmd="/bin/echo"
datetime=`date +%s`
ok=0
nok=8 

process_webpage() 

#logger -p INFO "Nagios syncsort script process webpage"
#echo "ARCH IS : $ARCH"
case "$ARCH" in
 "1")

		logger -p INFO "Syncsort submit_check_result type1 is $type1"
                                 cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$servicename;0;$serviceoutput"
                                `$echocmd $cmdline >> $CommandFile`
                                 exit 0
		;;		
	
"2")

		 # type determined, no error so exit ok
                                logger -p INFO "Syncsort submit_check_result type2 is $type2"
                                cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$servicename;0;$serviceoutput"
                                `$echocmd $cmdline >> $CommandFile`
                                exit 0
		;;			

"3")
                                logger -p INFO "Syncsort submit_check_result type3 is $type3"
                                cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$servicename;0;$serviceoutput"
                                `$echocmd $cmdline >> $CommandFile`
                                exit 0
		;;			

"4")
                                logger -p INFO "Syncsort submit_check_result type4 is $type4"
                                cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$servicename;1;$serviceoutput"
                                `$echocmd $cmdline >> $CommandFile`
                                exit 1
		;;

"5")
                                logger -p INFO "Syncsort submit_check_result type5 is $type5"
                                cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$servicename;2;$serviceoutput"
                                `$echocmd $cmdline >> $CommandFile`
                                exit 2
                ;;


"8")
		  logger -p INFO "Syncsort submit_check_result type6 is $type6 @NOK"
                               cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$servicename;2;$serviceoutput"
                                `$echocmd $cmdline >> $CommandFile`
                                exit 2	
		;;		

"0")
	
		 logger -p INFO "Syncsort submit_check_result type5 is $type5 @OK type3 is $type3"
                                cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$servicename;0;$serviceoutput"
                                `$echocmd $cmdline >> $CommandFile`
                                exit 0
		;;			

	*) 
		logger -p INFO "Syncsort submit_check_result @ ELSE. Service output was $serviceoutput, and the type output was $type or $type4  or $type6 ..."
                cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;$hostname;$servicename;$3;$serviceoutput"
                `$echocmd $cmdline >> $CommandFile`
                exit $3
	;;
	esac


#############################################
# Output vallues after script edit of types #
#############################################
#type1: started: ("job started:" OK)
#type2: got ("got the medium:" OK)
#type3: needs ("needs a medium:" OK)
#type4: cancelled: ("job cancelled:" WARN)
#type5: 0/8 ("0" (ok) OR "8" CRIT)

# NOTE: for type2 and type3 Syncsort somehow calls the backup job SYNCSORT-SYNCSORT. So if you want to see this status, make a service called SYNCSORT-SYNCSORT.

# LOGGING
logger -p INFO "Nagios syncsort: checking on empthy values and wich check"

# Test if type is empty, if not then process results
echo check type1
if [ -n = $type1 ] ; then
	# type1 empty check type2
		logger -p INFO "type1 ($type1) empty, check type2"
		if [ -n = $type2 ] ; then
		# type2 empty check type3
			logger -p INFO "type2 ($type2) empty, check type3"
			if [ -n = $type3 ] ; then
			# type3 empty check type4
				logger -p INFO "type3 ($type3) empty, check type4"
				if [ -n = $type4 ] ; then
				# type4 is empty check5
					logger -p INFO "type4 ($type4) empty, check type5"
					if [ -n = $type5 ] ; then
					logger -p INFO "type5 ($ype5) empty, check 6"
						if [ -n = $type6 ] ; then
						# type6 empty
						logger -p INFO "type6 1/3 ($type6) empty, check type6 2/3"
						#ARCH=$type6
						#process_webpage  $type6
						echo "only empty results received"
							elif [ $type6 == 8 ] ; then
							logger -p INFO "type6 2/3 is ($type6)"
							ARCH=8
							process_webpage 8
								else
								logger -p INFO "type6 3/3 is ($type6)"
								ARCH=$type6
								process_webpage $ARCH
					fi
						else
						echo at arch 5
						ARCH=5
						process_webpage $ARCH
						fi
				else
				echo at arch 4
				ARCH=4
				process_webpage $ARCH
				fi

			else
			echo at arch 3
			ARCH=3
			process_webpage $ARCH
			fi
		else
		echo at arch 2
		ARCH=2
		process_webpage $ARCH
		fi
else
echo at arch 1
ARCH=1
process_webpage $ARCH
fi

# LOGGING
logger -p INFO "Nagios syncsort: end of script reached"
