#!/bin/sh
#
#  check_multiple_file.sh
#
#  Test la presence, la date , la taille de fichiers
#
# $Id: check_multiple_file.sh,v 1.4 2010/05/21 07:31:51 doguete Exp $: Emmanuel Doguet
#

# libs
. `dirname $0`/nagios_libs.sh

#NAGIOS_DEBUG=1

function help()
{
	echo "check_multiple_file.sh [check] -f <file> ...."


	echo "check:"
	echo "   -s 	Le fichier doit avoir au minimum cette taile (file > size)"
	echo "   -S 	Le fichier doit etre plus petit"
	echo "   -e 	Le fichier doit exister"
	echo "   -t 	Age minimum du fichier (fichier doit avoir plus de X minutes)"
	echo "   -T	Age maximum du fichier (fichier doit avoir moins de X minutes)"
	echo ""
	echo "   -f     Fichier"
	echo  ""
	echo "Mettre le check avant le fichier a tester, exemple: "
	echo "  check_multiple_file.sh -e -s2048 -f /etc/sysctl.conf  -e -T60 -f /etc/mtab"
	echo ""
	exit 2
}


function check_file()
{
	file=$1
	NAGIOS_Debug "Check_file $file"	
	OK=1


	if [ "$check_exist" = "1" ]
	then
		if [ ! -e "$file" ]
		then
			NAGIOS_Add "$N_COLOR_YELLOW $file n'existe pas!\n"
			NAGIOS_SetStatus $STATE_WARNING "$file n'existe pas"
		fi
	fi


	# Size
	if [ -e "$file" ]
	then
	file_size=$(stat -c%s $file)

	if [ "$check_min_size" != "" ]
	then

		NAGIOS_Debug "filesize is $file_size >  $check_min_size"

		if [ $check_min_size -gt $file_size  ]
		then
                        NAGIOS_Add "$N_COLOR_YELLOW $file trop petit ($file_size < $check_min_size bytes)\n" 
                        NAGIOS_SetStatus $STATE_WARNING "$file trop petit"
			OK=0
		fi
	fi

	if [ "$check_max_size" != "" ]
	then

		NAGIOS_Debug "filesize is $file_size >  $check_max_size"

		if [ $check_max_size -lt $file_size  ]
		then
                        NAGIOS_Add "$N_COLOR_YELLOW $file trop grand ($file_size > $check_max_size bytes)\n" 
                        NAGIOS_SetStatus $STATE_WARNING "$file trop grand"
			OK=0
		fi
	fi


	# Age
	NOW=`date +%s`
	NOW_file=$(stat -c%Z $file)
	file_age=$(( ($NOW - $NOW_file)/60 )) 

        if [ "$check_min_time" != "" ]
        then

                NAGIOS_Debug "file age is $file_age min >  $check_min_time"

                if [ $check_min_time -gt $file_age  ]
                then
                        NAGIOS_Add "$N_COLOR_YELLOW $file trop recent ($file_age < $check_min_time minutes)\n"
                        NAGIOS_SetStatus $STATE_WARNING "$file trop recent"
                        OK=0
                fi
        fi

        if [ "$check_max_time" != "" ]
        then

                NAGIOS_Debug "file age is $file_age min <  $check_max_time"

                if [ $check_max_time -lt $file_age  ]
                then
                        NAGIOS_Add "$N_COLOR_YELLOW $file trop vieux ($file_age > $check_max_time minutes)\n"
                        NAGIOS_SetStatus $STATE_WARNING "$file trop vieux"
                        OK=0
                fi
        fi

	if [ "$OK" = "1" ]
	then
		NAGIOS_Add "$N_COLOR_GREEN $file is OK (size=$file_size bytes, age=$file_age minutes)\n"
		NAGIOS_SetStatus $STATE_OK "`basename $file` ok"
	fi

	fi	
}


	# Démarrage
        while getopts "et:T:s:S:f:" OPT ;do
                case "$OPT" in

			"e")	check_exist=1
				;;

			"s")
				check_min_size=$OPTARG
				;;
	
			"S")
				check_max_size=$OPTARG
				;;

			"t")
				check_min_time=$OPTARG
				;;
	

			"T")
				check_max_time=$OPTARG
				;;

                        "f")    check_file $OPTARG
				check_min_size=""
				check_max_size=""
				check_exist=0
				check_min_time=""
				check_max_time=""
                                ;;


                        *)      help
                                exit 2
                                ;;

                esac
        done

	if [ $NAGIOS_STATUS_SET -eq 0 ]
	then
		NAGIOS_Add "$N_COLOR_YELLOW aucun fichier a verifier!\n"
                NAGIOS_SetStatus $STATE_WARNING "aucun fichier a verifier\n"
	fi

	NAGIOS_Send
	exit $NAGIOS_STATUS
