#!/bin/sh
#
# Recherche de patterns (classe erreur ou warning) dans des logs
#


. `dirname $0`/nagios_libs.sh

# Option
DEBUG=0


function debug()
{
        if [ "$DEBUG" = "1" ]
        then
                echo -e "$@"
        fi
}


function Usage
{
	echo -e "check_logs\n"
	echo -e "Search pattern in files, for display error or warning in Nagios/Centreon\n"
	echo -e "\t-l:  dir or log (/var/log/*log)"
	echo -e "\t-e:  error pattern (regexp)"
	echo -e "\t-w:  warning pattern (regexp)"

	exit 1
}

	LOG_DIR=""
	STR_ERROR=""
	STR_WARNING=""

        OPT=`getopt "l:e:w:" "$@"`

        if [ $? -ne 0 ] || [ "$#" -eq 0 ]
        then
                Usage
        fi

        set -- $OPT

        while [ ! -z "$1" ]
        do
                case "$1" in

                        -l)     LOG_DIR=$2
                                shift
				;;

			-e)	STR_ERROR=$2
				shift
				;;

			-w)	STR_WARNING=$2
				shift
				;;

		esac

		shift
	done

	if [ "$LOG_DIR" = "" ]
	then
		echo "Need a log directory"
		Usage
	fi

	if [ "$STR_ERROR" = "" ] && [ "$STR_WARNING" = "" ]
	then
		echo "Need at least an error or warning string to match"
		Usage
	fi


	file_err_cnt=0
	#file_cnt=0
	file_err_ok=0
	NAGIOS_Add "<br>"

	LOG_DIR_BASE=`dirname $LOG_DIR`
	LOG_REGEX=`basename $LOG_DIR`

	for file in `find $LOG_DIR_BASE -type f -iregex "$LOG_DIR"`
	do
		debug "file: $file"
		file_err_ok=0

		NAGIOS_Add "<hr><b>Fichier: $file</b><hr>"

		# Errors
		if [ "$STR_ERROR" != "" ]
		then
			ERR=`grep -E "$STR_ERROR" $file 2>&1`
			ERR_CNT=`echo -e "$ERR" | wc -l`

			if [ "$ERR" = "" ]
			then
				debug "Green"
				NAGIOS_Add "$N_COLOR_GREEN : pas d'erreur(s)\n\n"
				NAGIOS_SetStatus $STATE_OK

			else
				file_err_ok=1
				file_err_cnt=$(( file_err_cnt + 1 ))
				debug "$RED $ERR_CNT erreur(s) [$ERR]"
				NAGIOS_Add "<b>$N_COLOR_RED  $ERR_CNT erreur(s) :</b>\n$ERR\n\n"
				NAGIOS_SetStatus $STATE_CRITICAL  
			fi
		fi

		# Warnings
                if [ "$STR_WARNING" != "" ]
                then
                        ERR=`grep -E "$STR_WARNING" $file 2>&1`
                        ERR_CNT=`echo -e "$ERR" | wc -l`

                        if [ "$ERR" = "" ]
                        then
                                debug "Green"
                                NAGIOS_Add "$N_COLOR_GREEN : pas de warning\n\n"
				NAGIOS_SetStatus $STATE_OK
                        else
				if [ $file_err_ok -eq 0 ]
				then
					file_err_cnt=$(( file_err_cnt + 1 ))
				fi
                                debug "$YELLOW $ERR_CNT erreur(s) [$ERR]"
                                NAGIOS_Add "<b>$N_COLOR_YELLOW  $ERR_CNT erreur(s) :</b>\n$ERR\n\n"
                                NAGIOS_SetStatus $WARNING 
                        fi
                fi


                #file_cnt=$(( file_cnt + 1 ))


	done

	if [ $file_err_cnt -ne 0 ]
	then
	#	NAGIOS_Send "LOG CRITICAL - $file_err_cnt files in errors"
		NAGIOS_SetStatus $NAGIOS_STATUS "$file_err_cnt fichier(s) en erreur"
	else
		#NAGIOS_Send "LOG OK - no files in error !"
		NAGIOS_SetStatus $NAGIOS_STATUS "LOG OK - no files in error !"
	fi

	NAGIOS_Send
	exit $NAGIOS_STATUS
