Apache2-LogNotify

 view release on metacpan or  search on metacpan

lib/Apache2/LogNotify.pm  view on Meta::CPAN

package Apache2::LogNotify;

use strict;
use Apache2::Const -compile => qw(OK HTTP_UNAUTHORIZED SERVER_ERROR);
use Apache2::Const qw(:common :log :cmd_how :http HTTP_BAD_REQUEST);
use Apache2::Connection qw(get_remote_host);
use Apache2::RequestUtil qw(dir_config);
use Apache2::RequestRec ();
use Apache2::ServerRec;
use Apache2::ServerUtil qw(server get_server_version);
use Apache2::Log;
use Apache2::Process;
use APR::Table;
use APR::Finfo ();
use Mail::Mailer;
use IPC::Cache;
use Data::Dumper;

our $VERSION = 0.10;
my $cache = new IPC::Cache( { namespace  => 'LogNotify',
			      expires_in => 86400 } );
$cache->purge();

sub handler {
    my $r = shift;
    my $s = $r->server();
    my $args = $r->args;
    my $user = $r->user;
    my $debug = undef;
    my $dir_cfg = getConfig($r->server , $r->per_dir_config);

    $debug = 1 if ( $dir_cfg->{Debug} eq "On" );
    $r->log_error(__PACKAGE__.": ", ' Debug: ['. $dir_cfg->{Debug} . ']');
    foreach my $key ( keys %{$dir_cfg} ) {
	my $value = $dir_cfg->{$key};
	if ( $debug ) {
	    if ( ref($value ) eq "SCALAR" ){
		$r->log_error(__PACKAGE__.": ", $key, ': ['. $value .']');
	    }
	    if ( ref($value ) eq "ARRAY" ){
		$r->log_error(__PACKAGE__.": ", $key, ': ['. join(", ", @{$value} ) .']');
	    }
	}
    }
    # LogError On
    $r->log_error(__PACKAGE__.':  LogError CODE: ['. $dir_cfg->{LogError}->[0]  .']') if ( $debug );
    return Apache2::Const::OK unless ( $dir_cfg->{LogError} !~ /^[Oo][Nn]$/ );

    #ErrorType 300 400 500
    #my @errorType = split( /\s+,\s+/, join(' ',@{$dir_cfg->{ErrorType}}) );
    my @errorType = split( /\s+/, join(' ',@{$dir_cfg->{ErrorType}}) );
    $r->log_error(__PACKAGE__. "==========: ErrorType:  ".  Dumper ($dir_cfg->{ErrorType}) ) if ( $debug );
    $r->log_error(__PACKAGE__.'==========:  ERROR TYPE: ['. @{$dir_cfg->{ErrorType}}  .']') if ( $debug );
    $r->log_error(__PACKAGE__.'==========:  ERROR TYPE 2: ['. join(", ", @errorType )  .']') if ( $debug );
#    $r->log_error(__PACKAGE__.'==========:  HOSTNAME: ['. $s->server_hostname() .']') if ( $debug );

    foreach my $errorTest ( @errorType ) {
	#$r->log_error(__PACKAGE__.'==========: errorType: Test: ['. $errorTest  .']') if ( $debug );
	if ( $r->status() == $errorTest ) {
	    $r->log_error(__PACKAGE__.'==========: PURGE CACHE: ') if ( $debug );
	    $cache->purge();
	    my $seen = $cache->get( $r->uri() );
	    my $seenCount = $cache->get("SeenCount");
	    $r->log_error(__PACKAGE__.'==========: SEEN: <' . $seenCount . '>') if ( $debug );
	    if ( ! $seen || $seenCount % 10 == 0 ) {
		$cache->set($r->uri(), 1, 30 );
		$cache->set("SeenCount", 1, 30 );
		$r->log_error(__PACKAGE__.'==========: SENDING MAIL ON: <'. $r->uri().'>' ) if ( $debug );
		&sendMail($r);
	    }
	    else {
		$r->log_error(__PACKAGE__.'==========: NO MAIL MAIL <' . $seen . '> <'. $r->uri().'>') if ( $debug );
		$cache->set("SeenCount", $seenCount + 1, 30 );
	    }
	}
    }



( run in 2.079 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )