#!/usr/bin/perl

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp-apache-logger 3228 2010-08-27 00:02:48Z nuxwin $
# Author: Laurent Declercq <laurent.declercq@ispcp.net>
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is "ispCP ω (OMEGA) a Virtual Hosting Control Panel".
#
# The Initial Developer of the Original Code is ispCP Team.
# Portions created by Initial Developer are Copyright (C) 2006-2010 by
# isp Control Panel. All Rights Reserved.
#
# The ispCP ω Home Page is:
#
#    http://isp-control.net
#

################################################################################
## Script description:                                                        ##
##  ispcp-apache-logger - ispCP apache logfiles handling                      ##
##                                                                            ##
## I. CustomLog handler:                                                      ##
##                                                                            ##
## When called without the loggingType arguments or with logginType argument  ##
## and the value of it set to 'access', this script receives log lines        ##
## through a pipe and outputs their information into three separate files,    ##
## depending on the origin virtual host:                                      ##
##                                                                            ##
##  - /var/log/apache2/domain.tld-combined.log                                ##
##  - /var/log/apache2/domain.tld-traf log                                    ##
##  - /var/log/apache2/users/domain.tld-access.log                            ##
##                                                                            ##
## The program assumes that the first field of each log line is the virtual   ##
## host identity (See the %v logging directive from Apache mod_log_config).   ##
##                                                                            ##
## The log format is currently recognized as follows:                         ##
##                                                                            ##
##  "%v %... %I %O"                                                           ##
##                                                                            ##
## Where %... represents any other logging directives of your choice.         ##
## Additionally, %I and %O correspond to apache's mod_logio directives for    ##
## Input and Output bytes, used to generate the content of the traffic log    ##
## files.                                                                     ##
##                                                                            ##
## II. ErrorLog handler:                                                      ##
##                                                                            ##
## When called with the loggingType argument and the value is set to 'error', ##
## this script takes error log lines pipe and outputs their information into  ##
## a separate file depending on the originating virtual host :                ##
##                                                                            ##
##  - /var/log/apache2/users/domain.tld-error.log                             ##
##                                                                            ##
## Due to the fixed log format of error log lines, the script uses a          ##
## workaround to retrieve the virtual host identity, by looking for any       ##
## virtual host information inside the line itself. If the script is unable   ##
## to identify the virtual host with this method, errors are written to       ##
## the fallback log file, named "default-error.log".                          ##
##                                                                            ##
## See http://httpd.apache.org/docs/current/logs.html#piped for more info.    ##
################################################################################

# Only for dev
#use strict;
#no strict 'refs';
#use warnings;

use IO::Handle;
use POSIX qw/strftime/;
use FileCache maxopen => 55;
use Getopt::Long;
use sigtrap 'handler' => \&signalHandler, 'normal-signals';

my %opt = (
	loggingType => 'access', # Default logging type
	logDir => '/var/log/ispcp' # Default script event log directory
);

Getopt::Long::Configure(qw(no_ignore_case));
GetOptions(\%opt, 'loggingType|t=s', 'logDir|l=s');

%main::cfg = ();

################################################################################
#                                 Subroutines                                  #
################################################################################


################################################################################
#
# Load configuration from ispCP configuration file
#
# @return int 0 on success, negative int otherwise
#
sub getConf {

	my $file;

	if(-e '/usr/local/etc/ispcp/ispcp.conf'){
		$file = '/usr/local/etc/ispcp/ispcp.conf';
	} elsif(-e '/etc/ispcp/ispcp.conf') {
		$file = '/etc/ispcp/ispcp.conf';
	} else {
		return -1;
	}

	return -1 if ! open F, '<', $file;

	%main::cfg = join('', <F>) =~ /^\s*(\w+)\s*=\s*(.*)$/gm;
	close F;

	%main::cfg ? 0 : 1;
}

################################################################################
# Create/update all custom log files
#
# This subroutine is the handler that is responsible to grab and parse all error
# log lines from apache and create the ispCP errors log files from them.
#
# @return void
#
sub ErrorLogHandler {

	eventLog(
		'notice', 'ispCP Apache Logger started ErrorLog Handler -- ' .
			'resuming normal operations'
	);

	while (<STDIN>) {
		my $vhost = 'default';

		# Trying to retrieve the virtual host identity
		$vhost = $1 if /
			(?:$main::cfg{'APACHE_WWW_DIR'}|$main::cfg{'PHP_STARTER_DIR'})
			\/([\w\d.-]+)
		/x;

		# Error from ispCP Frontend ?
		$vhost = $main::cfg{'BASE_SERVER_VHOST'} if ($vhost eq 'master');

		# Write the error log line in the default|domain.tld-error.log
		my $fh;

		if($vhost eq 'default') {
			$fh = cacheout '>>', "$main::cfg{'APACHE_LOG_DIR'}/$vhost-error.log";
		} else {
			$fh = cacheout '>>',
				"$main::cfg{'APACHE_USERS_LOG_DIR'}/$vhost-error.log";
		}

		$fh->autoflush(1);
		print $fh $_;
	}

	# Occurs when apache breaks the communication for the reason exposed here:
	# https://issues.apache.org/bugzilla/show_bug.cgi?id=49800
	signalHandler('eol');
}

################################################################################
# Create/update all custom log files
#
# This subroutine is the handler that is responsible to grab and parse all
# custom log lines from apache and create the ispCP custom logfiles from them.
#
# @return void
#
sub CustomLoghandler {

	eventLog(
		'notice', 'ispCP Apache Logger started CustomLog Handler -- ' .
			'resuming normal operations'
	);

	while (<STDIN>) {
		my ($vhost, $inBytes, $outBytes) = /^(\S+|\s) .* (\d+) (\d+)$/;

		# Normalize the virtual host name to all lowercase. If it's blank, the
		# request was handled by the default server, so supply a default name.
		# This shouldn't happen, but caution rocks.
		$vhost = lc ($vhost) || 'default';

		# If the vhost contains a '/' or '\', it is illegal so just use the
		# default log to avoid any security issues due if it is interpreted as a
		# directory separator.
		$vhost = 'default' if $vhost =~ m@[/\\]@;

		# Back the log line to the NCSA extended/combined log format by stripped
		# the %v %I and %O variables.
		# Note: That allows to not define a custom log format in Awstats
		# configuration template.
		s/^\S*\s+(.*)\s\d+\s\d+$/$1/;

		# Write the log line in the domain.tld-combined.log file
		my $fh = cacheout '>>',
			"$main::cfg{'APACHE_LOG_DIR'}/$vhost-combined.log";
		$fh->autoflush(1);
		print $fh $_;

		# Write the traffic value in the default|domain.tld-traf.log file
		$fh = cacheout '>>', "$main::cfg{'APACHE_LOG_DIR'}/$vhost-traf.log";
		$fh->autoflush(1);
		print $fh $inBytes + $outBytes, "\n";

		# Write the log line in the default|domain.tld-access.log file
		$fh = cacheout '>>',
			"$main::cfg{'APACHE_USERS_LOG_DIR'}/$vhost-access.log";
		$fh->autoflush(1);
		print $fh $_;
	}
}

################################################################################
#
# Convenience subroutines to log events from this script
#
# @return void
#
sub eventLog {

	($priority, $message) = @_;

	my $date = strftime "%a %b %e %H:%M:%S %Y", localtime;

	if(-d $opt{'logDir'}) {
		if($priority ne 'debug' && $priority ne 'info' && $priority ne 'notice') {
			*STDERR = cacheout '>>',
				"$opt{'logDir'}/ispcp-apache-logger.stderr";
			STDERR->autoflush(1);

			print STDERR "[$date] [$priority] $message\n";
		} elsif($priority eq 'debug' && !$main::cfg{'DEBUG'}) {
			return 0;
		} else {
			*STDOUT = cacheout '>>',
				"$opt{'logDir'}/ispcp-apache-logger.stdout";
			STDOUT->autoflush(1);

			print STDOUT "[$date] [$priority] $message\n";
		}
	} else { # Logging on STDERR
		STDERR->autoflush(1);

		print STDERR "[$date] [$priority] $message\n";
	}
}

################################################################################
#
# This subroutine is the handler that is responsible to handle some signals that
# can be received from Apache. This handler ensure that all filehandles are
# correctly closed before shutting down.
#
# This method is also called by the errorLog handler in condition exposed here:
# https://issues.apache.org/bugzilla/show_bug.cgi?id=49800
#
sub signalHandler() {

	my $signal =  shift;
	my $loggingType = ($opt{'loggingType'} eq 'access') ? 'Access' : 'Error';

	eventLog('debug', "Ending $loggingType log processing...");

	if($signal eq 'eol') {
		eventLog('notice', "No more lines received, shutting down (pid $$)");
	} else {
		eventLog('notice', "Caught SIG$signal, shutting down (pid $$)");
	}

	# Close all filehandles
	cacheout_close();

	exit;
}

################################################################################
#                              Main program                                    #
################################################################################

eventLog('notice', "Starting ispCP Apache logger (pid $$)");

# Get ispCP configuration file
if(getConf() != 0) {
	eventLog(
		'err',
		"Unable to load ispCP configuration from the ispcp.conf file!"
	);

} elsif (!-d $main::cfg{'APACHE_LOG_DIR'}) {
	eventLog(
		'err',
		"Apache $main::cfg{'APACHE_LOG_DIR'} directory does not exist!"
	);

} elsif (!-d $main::cfg{'APACHE_USERS_LOG_DIR'}) {
	eventLog(
		'err',
		"Apache $main::cfg{'APACHE_USERS_LOG_DIR'} directory does not exist!"
	);

} elsif ($opt{'loggingType'} eq 'access') {
	# Stating Apache access log processing
	CustomLoghandler();
} else {
	# Stating Apache error log processing
	ErrorLogHandler();
}

# This is the rescue part of this script that is run when an error occurs.
# This allows to not kill the current process and prevents Apache to trying to
# re-spawn the process again and again. Instead, the script will acts as
# /dev/null. It's really better to act like this to avoid too much processor
# resources consumption. But the side effect of this is that the administrator
# will not be able to see the problem unless if he reads the log file.

eventLog(
	'notice', "Acts as /dev/null to avoid too many re-spawn attempts by " .
		"Apache (pid $$)"
);

while(<STDIN>) {}

__END__
