2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2009-03-02 19:53:33 +01:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
2012-05-21 05:04:36 +02:00
|
|
|
connection_time - log the duration of a connection
|
2009-03-02 19:53:33 +01:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
The B<connection_time> plugin records the time of a connection between the
|
2012-05-21 05:04:36 +02:00
|
|
|
first and the last possible hook in qpsmtpd (I<pre-connection> and
|
|
|
|
I<post-connection>) and writes a C<LOGINFO> (default, see below) line to
|
2009-03-02 19:53:33 +01:00
|
|
|
the log.
|
|
|
|
|
|
|
|
=head1 CONFIG
|
|
|
|
|
2012-05-21 05:04:36 +02:00
|
|
|
=head2 loglevel
|
|
|
|
|
|
|
|
Adjust the quantity of logging for this plugin. See docs/logging.pod
|
|
|
|
|
|
|
|
connection_time loglevel +1 (less logging)
|
|
|
|
|
|
|
|
connection_time loglevel -1 (more logging)
|
2009-03-02 19:53:33 +01:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2012-05-21 05:04:36 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2009-03-02 19:53:33 +01:00
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
|
2012-06-04 03:39:32 +02:00
|
|
|
use Time::HiRes qw(gettimeofday tv_interval);
|
|
|
|
|
2009-03-02 19:53:33 +01:00
|
|
|
sub register {
|
2012-05-21 05:04:36 +02:00
|
|
|
my ($self, $qp) = shift, shift;
|
|
|
|
if ( @_ == 1 ) { # backwards compatible
|
|
|
|
$self->{_args}{loglevel} = shift;
|
|
|
|
if ( $self->{_args}{loglevel} =~ /\D/ ) {
|
|
|
|
$self->{_args}{loglevel} = Qpsmtpd::Constants::log_level($self->{_args}{loglevel});
|
|
|
|
};
|
|
|
|
$self->{_args}{loglevel} ||= 6;
|
|
|
|
}
|
|
|
|
elsif ( @_ % 2 ) {
|
|
|
|
$self->log(LOGERROR, "invalid arguments");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->{_args} = { @_ }; # named args, inherits loglevel
|
2012-06-04 03:39:32 +02:00
|
|
|
};
|
2009-03-02 19:53:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub hook_pre_connection {
|
2012-06-04 03:39:32 +02:00
|
|
|
my $self = shift;
|
|
|
|
$self->{_connection_start} = [gettimeofday];
|
|
|
|
$self->log(LOGDEBUG, "started at " . $self->{_connection_start} );
|
|
|
|
return (DECLINED);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub hook_connect {
|
|
|
|
my $self = shift;
|
|
|
|
# this method is needed to function with the tcpserver deployment model
|
|
|
|
return (DECLINED) if defined $self->{_connection_start};
|
2009-03-02 19:53:33 +01:00
|
|
|
$self->{_connection_start} = [gettimeofday];
|
2012-05-21 05:04:36 +02:00
|
|
|
$self->log(LOGDEBUG, "started at " . $self->{_connection_start} );
|
2009-03-02 19:53:33 +01:00
|
|
|
return (DECLINED);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub hook_post_connection {
|
2012-06-04 03:39:32 +02:00
|
|
|
my $self = shift;
|
2012-05-21 05:04:36 +02:00
|
|
|
|
|
|
|
if ( ! $self->{_connection_start} ) {
|
|
|
|
$self->log(LOGERROR, "Start time not set?!");
|
|
|
|
return (DECLINED);
|
|
|
|
};
|
|
|
|
|
|
|
|
my $elapsed = tv_interval( $self->{_connection_start}, [gettimeofday] );
|
|
|
|
|
|
|
|
$self->log(LOGINFO, sprintf "%.3f s.", $elapsed );
|
2009-03-02 19:53:33 +01:00
|
|
|
return (DECLINED);
|
|
|
|
}
|
|
|
|
|