#!/bin/bash

APACHE2CTL=$( which apache2ctl )
[ $? -ne 0 ] && echo "UNKNOWN - apache2ctl not found" && exit 3

[ ! -d /etc/apache2/sites-enabled/ ] && echo "UNKNOWN - Apache2 does not seem to be installed." && exit 3

[ $( ls -1 /etc/apache2/sites-enabled/|wc -l ) -eq 0 ] && echo "UNKNOWN - No VirtualHost seems enabled." && exit 3

DEBUG=0
[ "$1" == "-d" ] && DEBUG=1


ERRORS=""
EXITCODE=0
VHOSTS=0

# Detect Apache version
VERSION=$( $APACHE2CTL -v 2> /dev/null|grep '^Server version'|sed 's|^.*Apache/\([0-9]\.[0-9]\).*$|\1|' )
[ $DEBUG -eq 1 ] && echo "Apache version deteced : $VERSION"
if [ "$VERSION" != "2.2" -a "$VERSION" != "2.4" ]
then
	echo "UNKNOWN - Fail to detect Apache version (or unsupported version)"
	exit 3
fi

# Check configuration
$APACHE2CTL configtest 2> /dev/null
if [ $? -ne 0 ]
then
	EXITCODE=1
	ERRORS="$ERRORS - configtest notice problems"
elif [ $DEBUG -eq 1 ]
then
	echo "No configuration problem detected with 'apache2ctl configtest'"
fi

IFS="
"
for file in /etc/apache2/sites-enabled/*
do
	[ $DEBUG -eq 1 ] && echo "Site $file :"

	if [ ! -h $file ]
	then
		[ $DEBUG -eq 1 ] && echo "  File '$file' is not a symbolic link"
		ERRORS="$ERRORS - Site file '$file' is not a symbolic link"
		[ $EXITCODE -ne 2 ] && EXITCODE=1
		continue
	fi
	[ $DEBUG -eq 1 ] && echo "  File '$file' is a symbolic link"

	if [ "$VERSION" == "2.4" ]
	then
		if [ $( echo "$file"|grep -cE '\.conf$' ) -ne 1 ]
		then
			[ $DEBUG -eq 1 ] && echo "  Filename '$file' not ended with '.conf'"
			ERRORS="$ERRORS - Site filename '$file' not ended with .conf"
			[ $EXITCODE -ne 2 ] && EXITCODE=1
			continue
		fi
		[ $DEBUG -eq 1 ] && echo "  Site filename '$file' ended with .conf"
	fi

	LINES=0
	for line in $( grep -iE '^[[:space:]]*DocumentRoot[[:space:]]' "$file" )
	do
		let LINES=LINES+1
		[ $DEBUG -eq 1 ] && echo -e "  DocumentRoot lines :\n$( echo "$line"|sed 's/^/    /' )"
		dir=$( echo -e "$line"|sed 's/^[[:space:]]*DocumentRoot[[:space:]]*\(.*\)[[:space:]]*$/\1/i' )
		[ $DEBUG -eq 1 ] && echo "  DocumentRoot : $dir"
		vhost=$( basename "$file" )
		[ $DEBUG -eq 1 ] && echo "  VirtualHost : $vhost"
		if [ -z "$dir" -o "$dir" == "$line" ]
		then
			ERRORS="$ERRORS - Fail to detect DocumentRoot of VirtualHost $vhost"
			[ $EXITCODE -ne 2 ] && EXITCODE=3
		elif [ "$dir" == "/var/www" -o "$dir" == "/var/www/" ]
		then
			ERRORS="$ERRORS - VirtualHost $vhost DocumentRoot is '$dir'"
			EXITCODE=2
		fi
	done
	if [ $LINES -eq 0 ]
	then
		if [ $( grep -iEc '^[[:space:]]*]<VirtualHost[[:space:]]' "$file" ) -gt 0 ]
		then
			ERRORS="$ERRORS - No DocumentRoot defined in VirtualHost $vhost"
			EXITCODE=2
		elif [ $DEBUG -eq 1 ]
		then
			echo "  No DocumentRoot and VirtualHost detected in this file"
		fi
	elif [ $DEBUG -eq 1 ]
	then
		echo "  $LINES DocumentRoot lines checked"
	fi
done

case $EXITCODE in
	0)
		echo "OK - No problem detected"
		exit 0
	;;
	1)
		STATUS="WARNING"
	;;
	2)
		STATUS="CRITICAL"
	;;
	3)
		STATUS="UNKNOWN"
	;;
esac
echo "${STATUS}${ERRORS}"
exit $EXITCODE
