#!/bin/sh

# Custom script for Nagios to monitor rel1 log files

# Data that we want to extract from the logs:
# 	start and end time
#	any errors

# Define possible exit status.
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3

# Make sure we get the right input.
if [ $# -ne 1 ]
then
        echo "Usage: $0 log_file_name."
        exit $UNKNOWN
fi

LOG_FILE=/home/admin/logs/$1

# Check if file exists.
if [ ! -f $LOG_FILE ]
then
	echo "File does not exist."
	exit $CRITICAL
fi

# Get start and end time of log.
START_TIME=`grep "started at" $LOG_FILE`
END_TIME=`grep "finished at" $LOG_FILE`

if [ "$START_TIME" = "" ] || [ "$END_TIME" = "" ]
then
	echo "No start or end time."
	exit $CRITICAL
fi

# Find errors.
ERROR=`grep --ignore-case -e "ERROR" -e "Exception" $LOG_FILE`
if [ "$ERROR" != "" ]
then
	echo "Error found: $ERROR"
	exit $CRITICAL
fi

# All is OK & send start & end time.
echo "$START_TIME : $END_TIME"
exit $OK
