All pastes #405561 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-03-21 20:41 UTC
rendered paste body
use strict;
use warnings;

use Digest::MD5 qw(md5_hex);

# takes a log4j log file with ISO date format (YYYY-MM-DD) and finds all
# exceptions in the log, counts the occurances and emails a summary to
# the last arguments.
#
# a log4j string is in this format:
#
# YYYY-MM-DD HH:MM:SEC,MS [area_of_app] SEVERITY - javacmd
#
# example:
#
# 2005-12-02 06:44:34,417 [PS Agent Runner] ERROR  - e.getMessage()
#
# exception_finder.pl <logfile> <email1> <email2> ...
#
#
#
#alternate iplmentation
# read in each exception to an array
# md5 each exception body
# compare md5s
# discard dupes
# parse the rest into this hash
my %exception_hash = ();
my %severity_counts = ();
my %area_counts = ();

my $logfile = shift;
defined $logfile or die "No log file specified: $!";

my $line;
my $error_msg = "";
my $severity = "";
my $area = "";
my $new_exception = 0;
my $exception_start = "";
my $exception_text = "";

open LOGFILE, "<$logfile";
while ($line = <LOGFILE>) {

        if ($line =~ /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} \[(.*)\] (.*) - (.*)/) {
                # $1 is the area of the application
                # $2 is the severity of the error
                # $3 is the error message

                $area = $1;
                $severity = $2;
        if ($severity eq "") {
            $severity = "(undefined)";
        }
                $error_msg = $3;
                $new_exception = 1;
       } else {
                # we can assume we are in an exception
                if ($new_exception) {
                        if ($exception_text ne "") {
                            $exception_hash{$area}{$severity}{$error_msg}{$exception_start}{ md5_hex($exception_text) } = $exception_text;
                        }
                        $new_exception = 0;
                        chomp $line;

                        $exception_start = $line;
                        $exception_text = "";
                } else {
                         $exception_text .= "$line";
                }

        }

}

$exception_hash{$area}{$severity}{$error_msg}{$exception_start}{ md5_hex($exception_text) } = $exception_text;

# my $final_report = "";

my %totals_hash = ();

# get a count of the total number of areas affected.
my $report() = " A total of ",
        scalar keys %exception_hash,
        $report .= " areas of the application had exceptions.\n";
foreach my $area (keys %exception_hash) {
    foreach my $severity (keys %{$exception_hash{$area}}) {

        # for each severity, how many errors?
        $report .= "-" x 78, "\n";
        $report .= "  Area: $area\n",
        $report .= "    Severity: $severity\n",
        $report .= "    Unique error types: ",
        $report .= scalar keys %{$exception_hash{$area}{$severity}},
        $report .= "\n";

        foreach my $error (keys %{$exception_hash{$area}{$severity}}) {

        $report .= "    The following error message appears ",
                    scalar keys %{$exception_hash{$area}{$severity}{$error}},
                    " time(s):\n\t$error\n";
        }
    }
print $report;
}