系统管理工具包: 监视邮件的使用情况
2008-11-12 08:29:40 来源:WEB开发网Nov 17 03:17:34 narcissus postfix/pipe[14283]:
4F4CB1109404: to=<dev@gendarme.example.com>,
relay=cyrus, delay=0, status=sent (gendarme.example.com)
唯一的 ID 是十六进制值 4F4CB1109404,并且您还可以从这一行内容中确定接收者的地址。可以在包含相同的嵌入 ID 的另一行内容中找到这封电子邮件的发送者:
Nov 17 03:17:34 narcissus postfix/qmgr[104]:
4F4CB1109404: from=
<zfs-discuss-bounces@opensolaris.org>, size=7632, nrcpt=1 (queue active)
这些信息不一定是顺序的,因为 MTA 可能同时处理多封电子邮件,并且在处理过程的不同部分完成时,会将信息写入到日志中。
从上面的一行内容中,您还可以看到电子邮件的总计大小(7632 字节)以及接收者的数目(一个)。
清单 3 显示了一个 Perl 脚本,该脚本可以对信息进行整理,然后输出电子邮件数量及其总计大小的汇总统计信息。
清单 3. 对日志进行解析以获得一些有用的统计信息
#!/usr/bin/perl
#
# Script to extract email statistics from log files
# Time::ParseDate will be used parse the time into an epoch
# value, and then DateTime can be used to reformat the date
# again
use Time::ParseDate;
use DateTime;
# Parse the first file on the command line
open(MAIL,$ARGV[0]) or die "Couldn't open $ARGV[0]: $!n";
# Create a structure to hold the stats
my $mails = {};
# Parse each line of the file
while(<MAIL>)
{
chomp;
my $mailid = 0;
# Look for the 12 digit hex mail ID
if (m/: ([A-Z0-9]{12}):/)
{
$mailid = $1;
}
# Extract the date and parse it into an Epoch value
if (m/(S+ d+ d{2}:d{2}:d{2}) .*? $mailid/)
{
$mails->{$mailid}->{date} = parsedate($1);
}
# Extract the sender address and email size
if (m/$mailid: from=<(.*?)>, size=(d+),/)
{
$mails->{$mailid}->{from} = $1;
$mails->{$mailid}->{size} = $2;
}
# Extract the recipient
if (m/$mailid: to=<(.*?)>/)
{
$mails->{$mailid}->{to} = $1;
}
}
close(MAIL);
# Compile together the stats by parsing the formatted
# information into another summary structure
my $mailstats = {};
foreach my $mailid (keys %{$mails})
{
# Don't create a summary entry if we don't have enough information
# (sender/recipient is empty)
if (!defined($mails->{$mailid}->{to}) ||
!defined($mails->{$mailid}->{from}) ||
$mails->{$mailid}->{to} !~ m/[a-z]/ ||
$mails->{$mailid}->{from} !~ m/[a-z]/)
{
next;
}
# Count the number of emails to each recipient
$mailstats->{$mails->{$mailid}->{to}}->{count}++;
# Sum up the email size to each recipient
$mailstats->{$mails->{$mailid}->{to}}->{size} +=
$mails->{$mailid}->{size};
# Count the number of emails from each sender
$mailstats->{$mails->{$mailid}->{from}}->{count}++;
# Sum up the email size from each sender
$mailstats->{$mails->{$mailid}->{from}}->{size} +=
$mails->{$mailid}->{size};
# Sum up the same information, but organized on a date by date basis
if (defined($mails->{$mailid}->{date}))
{
my $dt = DateTime->from_epoch(
epoch => $mails->{$mailid}->{date})->ymd('');
my $mailto = $mails->{$mailid}->{to};
my $mailfrom = $mails->{$mailid}->{from};
$mailstats->{$mailto}->{_date}->{$dt}->{count}++;
$mailstats->{$mailto}->{_date}->{$dt}->{size} +=
$mails->{$mailid}->{size};
$mailstats->{$mailfrom}->{_date}->{$dt}->{count}++;
$mailstats->{$mailfrom}->{_date}->{$dt}->{size} +=
$mails->{$mailid}->{size};
}
}
# Dump out the information show mail counts and mail sizes
# on a mail address basis
foreach my $address (sort keys %{$mailstats})
{
# Only show information from email addresses that are
# local
if ($address =~ m/@.*example.com$/)
{
printf('%-40s %5d %9d',
$address,
$mailstats->{$address}->{count},
$mailstats->{$address}->{size});
print("n");
}
}
更多精彩
赞助商链接