qpsmtpd/plugins/stats

74 lines
1.6 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl -w
use Qpsmtpd::Stats;
use Time::HiRes qw(time);
my $START_TIME = time;
our $MAILS_RECEIVED = 0;
our $MAILS_REJECTED = 0;
our $MAILS_TEMPFAIL = 0;
sub register {
my ($self) = @_;
$self->register_hook('deny', 'increment_deny');
$self->register_hook('queue', 'increment_mails');
}
sub get_stats {
my $uptime = Qpsmtpd::Plugin::stats->uptime;
my $recvd = Qpsmtpd::Plugin::stats->mails_received;
my $reject = Qpsmtpd::Plugin::stats->mails_rejected;
my $soft = Qpsmtpd::Plugin::stats->mails_tempfailed;
my $rate = Qpsmtpd::Plugin::stats->mails_per_sec;
return sprintf(" Uptime: %0.2f sec\n".
" Mails Received: % 10d\n".
" 5xx: % 10d\n".
" 4xx: % 10d\n".
"Mails per second: %0.2f\n",
$uptime, $recvd, $reject, $soft, $rate);
}
sub increment_deny {
my ($self, $tran, $plugin, $level) = @_;
if ($level == DENY or $level == DENY_DISCONNECT) {
$MAILS_REJECTED++;
}
elsif ($level == DENYSOFT or $level == DENYSOFT_DISCONNECT) {
$MAILS_TEMPFAIL++;
}
return DECLINED;
}
sub increment_mails {
my $self = shift;
$MAILS_RECEIVED++;
return DECLINED;
}
sub uptime {
return (time() - $START_TIME);
}
sub mails_received {
return $MAILS_RECEIVED;
}
sub mails_rejected {
return $MAILS_REJECTED;
}
sub mails_tempfailed {
return $MAILS_TEMPFAIL;
}
sub mails_per_sec {
return ($MAILS_RECEIVED / uptime());
}