#!/bin/bash
#
# check_apache2logs.sh
#
# Program: Apache2 Logs check for Nagios
# License : GPL
# Copyright (c) 2005 Timo Dotzauer (tdotzauer@online.de)
#
# check_apache2logs.sh Version 1.0 2005/04/25
#
# Description :
#
# This plugin checks, if the apache logs exist
#
#
# BUGS : Nothing - at this time ;-)


if test -f /tmp/check_apache2logs_r.tmp ; then
	export r=`cat /tmp/check_apache2logs_r.tmp`
	else
	echo "0" > /tmp/check_apache2logs_r.tmp
	export r=`cat /tmp/check_apache2logs_r.tmp`
fi

if test -f /tmp/check_apache2logs.tmp ; then
	export lv=`cat /tmp/check_apache2logs.tmp`
	else
	echo "0" > /tmp/check_apache2logs.tmp
fi

if [ -z "$1" ] ; then
	echo "Usage:"
	echo "check_apache2logs -d | --directory <STRING> -r | --rotation-interval <INTEGER> -re | --recurrences <INTEGER>"
	exit 3
fi

case "$1" in
        -h|--help)
        echo "This plugin will check if the httpd write into the log"
        echo ""
        echo "Required Arguments:"
        echo " -d, --directory STRING"
        echo "		Specified the logfile wich the httpd use (includes path)."
        echo " -r, --rotation-interval INTEGER"
        echo "		If you activated the logrotation with a rotation-interval, the plugin will check the rotation."
        echo " -h, --help"
        echo "    Print detailed help screen"
        echo " -v, --version INTEGER.INTEGER or INTERGER:INTEGER:INTEGER"
        echo "    	Print version information"
        echo ""
        echo ""
        echo "Usage:"
        echo "check_apache2logs -d | --directory <STRING> -r | --rotation-interval <INTEGER|DIR>"
        echo "check_apache2logs -h | --help for detailed help"
        echo "check_apache2logs -v | --version for version information"
        ;;
        -v|--version)
        echo "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute, "
        echo "copies of the plugins under the terms of the GNU General Public License. "
        echo "This Version was only tested under SuSE Linux."
        ;;
        -d|--directory)
        if [ "$1" = "-d" ] | [ "$1" = "--directory" ] & [ -z "$2" ] ; then
                echo "Warning: Threshold must be STRING" 
                echo ""
                echo "Usage:"
        	echo "check_apache2logs -d | --directory <STRING> -r | --rotation-interval <INTEGER>"
        	echo "check_apache2logs -h | --help for detailed help"
        	echo "check_apache2logs -v | --version for version information"
                exit 3
        fi
        ;;
        -r|--rotation-interval)
        if [ "$3" = "-r" ] | [ "$3" = "--rotation-interval" ] & [ -z "$4" ] ; then
                echo "Warning: Threshold must be INTEGER"
                echo ""
		echo "Usage:"
        	echo "check_apache2logs -d | --directory <STRING> -r | --rotation-interval <INTEGER>"
        	echo "check_apache2logs -h | --help for detailed help"
        	echo "check_apache2logs -v | --version for version information"
		echo "1=Everday, 30=all 30 Days, 365=One Year, etc..."
		echo "Example: -r 30 /var/log/apache2/"
	      	echo "Do not forget the logrotate destination directory!"
		exit 3
        fi
        ;;
        -re|--recurrence)
        if [ "$6" = "-re" ] | [ "$6" = "--recurence" ] & [ -z "$7" ] ; then
                echo "Warning: Threshold must be INTEGER"
                echo ""
		echo "Usage:"
        	echo "check_apache2logs -d | --directory <STRING> -r | --rotation-interval <INTEGER>"
        	echo "check_apache2logs -h | --help for detailed help"
        	echo "check_apache2logs -v | --version for version information"
		echo "Example: -re 30 | e.g: check_apache2logs -d  /var/log/apache2/access_log -r 5 /var/log/apache2/ -re 10"
	      	echo "Do not forget the logrotate destination directory!"
		exit 3
        fi
        ;;
	*)
                if [ "$1"x != "x" ] ; then
                echo "Unknown argument. Usage check_apache2logs -h for detailed information."
                exit 3
                fi
        ;;
esac

if [ "$1" != "-d" ] ; then
	echo "Sorry. The directory must be specified."
exit 3
fi
if [ "$1" = "-d" ] ; then
	if test -f $2 ; then
	export pt=$2	
	else
        	echo "Sorry. Directory or file $2 not found."
      	 	exit 3
	fi
fi
if [ "$3" != "-r" ] ; then
        echo "Unknown rotation-interval value: $3."
	echo "Example: 30"
        echo "Note: Value 0 will be deactivate the rotation-interval-check."
	exit 3
fi
if [ -z $4 ] ; then
        echo "Unknown rotation-interval value: $4."
        exit 3
fi
if test -f /tmp/check_apache2logs.tmp ; then
	export lv=`cat /tmp/check_apache2logs.tmp`
	else
	echo "0" > /tmp/check_apache2logs.tmp
fi
if [ $4 != "0" ] ; then
	if [ -z $5 ] ; then
	echo "No logrotate destination directory specified!"
	echo "Example: -r 30 /var/log/apache2/"
	fi
fi
if [ "$6" != "-re" ] ; then
        echo "Unknown recurrence: $6."
	echo "Example: 30"
	exit 3
fi
if [ -z $7 ] ; then
        echo "Unknown recurrence value: $7."
        exit 3
fi
export cv=`ls -al $2 | awk '{print $5}'`
echo $cv > /tmp/check_apache2logs.tmp 
export mt=`find $5 -mtime $4`
if [ "$cv" -gt "$lv" ] ; then
echo "0" > /tmp/check_apache2logs_r.tmp
fi
if [ "$lv" -le "$cv" ] ; then
	if [ -z "$mt" ] ; then	
		echo "LOGROTATION ERROR: Return: $mt Check: $4 Dir: $5"
		exit 2
	fi
	if [ "$r" -ge "$7" ] ; then
		echo "LOGROTATION ERROR: $r recurrences occurred. Please check the logfiles!"
		exit 2
	else
		echo "$r + 1" |bc > /tmp/check_apache2logs_r.tmp
	fi
	echo "APACHELOG OK: File: $2 Lv: $lv By Cv: $cv By"
	exit 0
fi