#!/bin/bash
# SYNOPSIS
#	check_spma
#
# DESCRIPTION
#	This NAGIOS plugin checks whether the last run spma client front
#        end for invoking components returned an error or not
#
# AUTHOR
#	ctheodos@grid.auth.gr

OK=0
WARN=1
CRITICAL=2
UNKNOWN=3
PATH="/bin:/sbin:/usr/bin:/usr/sbin"

MY_TMP_FILE=`mktemp`

(for i in `seq 10 -1 1`; do zcat /var/log/spma.log.$i.gz 2> /dev/null; done; cat /var/log/spma.log) > $MY_TMP_FILE

last_summary=""
STATUS=`cat $MY_TMP_FILE| tail -1 |awk '{print $2}'|awk '{gsub(/[[:punct:]]/,"")}1'`

case $STATUS in
  'OK')
    RESULT=$OK ;;
  'INFO')
    RESULT=$WARN ;;
  'ERROR')
    RESULT=$CRITICAL ;;
  *) RESULT=$UNKNOWN ;;
esac

echo -en `tail -n 1 $MY_TMP_FILE`"\n"

tac $MY_TMP_FILE| awk '{ printf "%sNEWLINE", $0 }'|awk -F'-----------------------------------------------------NEWLINE' '{print $1}'|sed "s#NEWLINE#\n#g"
rm -f $MY_TMP_FILE 
exit $RESULT
