Sendmail: Difference between revisions
From Federal Burro of Information
Jump to navigationJump to search
(Created page with "== Logchop == <pre> while (<>) { chomp; if ( /(\w{3})\s(\d{2})\s(\d{2}:\d{2}:\d{2})\s(\w+)\s(\w+)\[\d+\]/ ) { $month = $1; $d...") |
No edit summary |
||
Line 2: | Line 2: | ||
<pre> | <pre> | ||
#!/usr/bin/perl -w | |||
use strict; | |||
my $data; | |||
my $debug = 0; | |||
while (<>) { | while (<>) { | ||
chomp; | chomp; | ||
if ( /(\w{3})\s(\d{2})\s(\d{2}:\d{2}:\d{2})\s(\w+)\s(\w+)\[\d+\]/ ) { | if ( /(\w{3})\s(\d{2})\s(\d{2}:\d{2}:\d{2})\s(\w+)\s(\w+)\[\d+\]:\s\[.*\]\s(.*):\s(.*)/ ) { | ||
$month = $1; | my $month = $1; | ||
$date = $2; | my $date = $2; | ||
$time = $3; | my $time = $3; | ||
$host = $4; | my $host = $4; | ||
$process = $5; | my $process = $5; | ||
print "Match $_\n"; | my $msgid = $6; | ||
print " | my $rest = $7; | ||
if ( $debug ) { | |||
print "Match $_\n"; | |||
print "\tMonth: $1\n"; | |||
print "\tDate: $date\n"; | |||
print "\tTime: $time\n"; | |||
print "\tHost: $host\n"; | |||
print "\tProcess: $process\n"; | |||
print "\tMsgid: $msgid\n"; | |||
print "\tRest: $rest\n"; | |||
} | |||
foreach my $tuple ( split ( /,\s/ , $rest ) ) { | |||
$debug && print "\tTuple : $tuple\n"; | |||
my ( $name , $value ) = split ( /=/ , $tuple ) ; | |||
#if ( $name ) { print "\tName: $name\n";} else { print "No Name\n";} | |||
#if ( $value ) { print "\tValue: $value\n";} else { print "No Value\n";} | |||
if ( $name && $value ) { | |||
$data->{"$name"}->{"$value"}++; | |||
} | |||
} | |||
} else { | } else { | ||
print "No: ".$_."\n"; | $debug && print "No: ".$_."\n"; | ||
} | } | ||
} | } | ||
print "#########################\n# Report \n##################\n"; | |||
foreach my $name ( keys( %$data )) { | |||
next if $name eq "msgid"; | |||
next if $name eq "stat"; | |||
next if $name eq "pri"; | |||
if ( $name eq "size" ) { | |||
print "Size:\n"; | |||
foreach my $count ( sort {$data->{$name}->{$b} <=> $data->{$name}->{$a}} keys %{$data->{$name}} ) { | |||
print "\t".$count . " " . $data->{$name}->{$count}."\n"; | |||
} | |||
} elsif ( $name eq "from" || $name eq "to" ) { | |||
print $name ."\n"; | |||
foreach my $count ( sort {$data->{$name}->{$b} <=> $data->{$name}->{$a}} keys %{$data->{$name}} ) { | |||
print "\t".$count . " " . $data->{$name}->{$count}."\n"; | |||
} | |||
} else { | |||
print "$name\n"; | |||
foreach my $count ( sort keys %{$data->{$name}} ) { | |||
print "\t".$count . " " . $data->{$name}->{$count}."\n"; | |||
} | |||
} | |||
} | |||
</pre> | </pre> | ||
[[Category:Perl]] | [[Category:Perl]] |
Latest revision as of 21:03, 25 June 2012
Logchop
#!/usr/bin/perl -w use strict; my $data; my $debug = 0; while (<>) { chomp; if ( /(\w{3})\s(\d{2})\s(\d{2}:\d{2}:\d{2})\s(\w+)\s(\w+)\[\d+\]:\s\[.*\]\s(.*):\s(.*)/ ) { my $month = $1; my $date = $2; my $time = $3; my $host = $4; my $process = $5; my $msgid = $6; my $rest = $7; if ( $debug ) { print "Match $_\n"; print "\tMonth: $1\n"; print "\tDate: $date\n"; print "\tTime: $time\n"; print "\tHost: $host\n"; print "\tProcess: $process\n"; print "\tMsgid: $msgid\n"; print "\tRest: $rest\n"; } foreach my $tuple ( split ( /,\s/ , $rest ) ) { $debug && print "\tTuple : $tuple\n"; my ( $name , $value ) = split ( /=/ , $tuple ) ; #if ( $name ) { print "\tName: $name\n";} else { print "No Name\n";} #if ( $value ) { print "\tValue: $value\n";} else { print "No Value\n";} if ( $name && $value ) { $data->{"$name"}->{"$value"}++; } } } else { $debug && print "No: ".$_."\n"; } } print "#########################\n# Report \n##################\n"; foreach my $name ( keys( %$data )) { next if $name eq "msgid"; next if $name eq "stat"; next if $name eq "pri"; if ( $name eq "size" ) { print "Size:\n"; foreach my $count ( sort {$data->{$name}->{$b} <=> $data->{$name}->{$a}} keys %{$data->{$name}} ) { print "\t".$count . " " . $data->{$name}->{$count}."\n"; } } elsif ( $name eq "from" || $name eq "to" ) { print $name ."\n"; foreach my $count ( sort {$data->{$name}->{$b} <=> $data->{$name}->{$a}} keys %{$data->{$name}} ) { print "\t".$count . " " . $data->{$name}->{$count}."\n"; } } else { print "$name\n"; foreach my $count ( sort keys %{$data->{$name}} ) { print "\t".$count . " " . $data->{$name}->{$count}."\n"; } } }