Created
May 17, 2009 19:05
-
-
Save antonlindstrom/113111 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# | |
# Program to parse .csv file from mail.log | |
# and write out top mailers based on number | |
# and size of mails. | |
# | |
# Author: Anton Lindström | |
# [email protected] | |
# | |
use strict; | |
use warnings; | |
my $csvfile = "logmail.csv"; | |
# Open and assign $csvfile to @csv | |
open(CSV, $csvfile); | |
my @csv = <CSV>; | |
close(CSV); | |
# Global variables. | |
my (%topnum, %topsize, %toprecnum, %toprecsize); | |
foreach (@csv) { | |
# Split csv. | |
my ($from, $to, $date, $size, $status) = split(/,/, $_); | |
## Note: Small sizes of mails will cause the kB-function to | |
## make them 0kB in size. Due to heavy mailing in this example | |
## it was nicer to make it this way. It is intended! | |
# For every occurance of $from (user email) +1. | |
if (exists($topnum{$from})) { $topnum{$from} += 1; } | |
else { $topnum{$from} = 1; } | |
# For every occurance of $from (user email) add $size in kB of message. | |
if (exists($topsize{$from})) { $topsize{$from} += int($size/1024); } | |
else { $topsize{$from} = int($size/1024); } | |
# For every occurance of $to (user email) +1. | |
if (exists($toprecnum{$to})) { $toprecnum{$to} += 1; } | |
else { $toprecnum{$to} = 1; } | |
# For every occurance of $to (user email) add $size in kB of message. | |
if (exists($toprecsize{$to})) { $toprecsize{$to} += int($size/1024); } | |
else { $toprecsize{$to} = int($size/1024); } | |
} | |
# Call sub print | |
print "Top 10 mailers (based on number of emails sent):\n"; | |
&print(%topnum); | |
print "Top 10 mailers (based on size in kB of emails sent):\n"; | |
&print(%topsize); | |
print "Top 10 recievers (based on number of emails recieved):\n"; | |
&print(%toprecnum); | |
print "Top 10 recievers (based on size in kB of emails recieved):\n"; | |
&print(%toprecsize); | |
# Print hashes. | |
sub print { | |
my %hash = @_; | |
# Count every foreach to only print 10. | |
my $i=0; | |
foreach my $key( sort { $hash{$b} <=> $hash{$a} } keys(%hash) ) { | |
# Print hash. | |
print "\t $hash{$key}\t$key\n"; | |
# Increment | |
$i++; | |
# Jump to last if $i=10. | |
last if($i == 10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment