connection_time:
had single positional argument for loglevel, switched to named args which inherits the more flexible loglevel shortened logging line before: connection_time: Connection time from 66.118.151.187: 3.046 sec. after: connection_time: 3.046 s.
This commit is contained in:
parent
1f6e2b0408
commit
74125300da
@ -2,58 +2,68 @@
|
|||||||
|
|
||||||
=head1 NAME
|
=head1 NAME
|
||||||
|
|
||||||
connection_time - log the duration of a connection
|
connection_time - log the duration of a connection
|
||||||
|
|
||||||
=head1 DESCRIPTION
|
=head1 DESCRIPTION
|
||||||
|
|
||||||
The B<connection_time> plugin records the time of a connection between the
|
The B<connection_time> plugin records the time of a connection between the
|
||||||
first and the last possible hook in qpsmtpd (I<pre-connection> and
|
first and the last possible hook in qpsmtpd (I<pre-connection> and
|
||||||
I<post-connection>) and writes a C<LOGNOTICE> (default, see below) line to
|
I<post-connection>) and writes a C<LOGINFO> (default, see below) line to
|
||||||
the log.
|
the log.
|
||||||
|
|
||||||
=head1 CONFIG
|
=head1 CONFIG
|
||||||
|
|
||||||
One optional argument: the name of the log level (e.g. C<LOGNOTICE>,
|
=head2 loglevel
|
||||||
C<LOGDEBUG>, ...) the message should be logged with. Defaults to C<LOGNOTICE>.
|
|
||||||
|
Adjust the quantity of logging for this plugin. See docs/logging.pod
|
||||||
|
|
||||||
|
connection_time loglevel +1 (less logging)
|
||||||
|
|
||||||
|
connection_time loglevel -1 (more logging)
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
|
||||||
use Time::HiRes qw(gettimeofday tv_interval);
|
use Time::HiRes qw(gettimeofday tv_interval);
|
||||||
use Qpsmtpd::Constants;
|
use Qpsmtpd::Constants;
|
||||||
|
|
||||||
sub register {
|
sub register {
|
||||||
my ($self, $qp, @args) = @_;
|
my ($self, $qp) = shift, shift;
|
||||||
die "too many arguments"
|
if ( @_ == 1 ) { # backwards compatible
|
||||||
if @args > 1;
|
$self->{_args}{loglevel} = shift;
|
||||||
$self->{_level} = shift @args;
|
if ( $self->{_args}{loglevel} =~ /\D/ ) {
|
||||||
$self->{_level} = 'LOGNOTICE'
|
$self->{_args}{loglevel} = Qpsmtpd::Constants::log_level($self->{_args}{loglevel});
|
||||||
unless defined $self->{_level};
|
};
|
||||||
$self->{_level} = Qpsmtpd::Constants::log_level($self->{_level});
|
$self->{_args}{loglevel} ||= 6;
|
||||||
$self->{_level} = LOGNOTICE
|
}
|
||||||
unless defined $self->{_level};
|
elsif ( @_ % 2 ) {
|
||||||
|
$self->log(LOGERROR, "invalid arguments");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$self->{_args} = { @_ }; # named args, inherits loglevel
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub hook_pre_connection {
|
sub hook_pre_connection {
|
||||||
my ($self, @foo) = @_;
|
my ($self, @foo) = @_;
|
||||||
$self->{_connection_start} = [gettimeofday];
|
$self->{_connection_start} = [gettimeofday];
|
||||||
|
$self->log(LOGDEBUG, "started at " . $self->{_connection_start} );
|
||||||
return (DECLINED);
|
return (DECLINED);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub hook_post_connection {
|
sub hook_post_connection {
|
||||||
my ($self, @foo) = @_;
|
my ($self, @foo) = @_;
|
||||||
if ($self->{_connection_start}) {
|
|
||||||
my $remote = $self->connection->remote_ip;
|
if ( ! $self->{_connection_start} ) {
|
||||||
my $elapsed = sprintf(
|
$self->log(LOGERROR, "Start time not set?!");
|
||||||
"%.3f",
|
return (DECLINED);
|
||||||
tv_interval(
|
};
|
||||||
$self->{_connection_start},
|
|
||||||
[gettimeofday]
|
my $elapsed = tv_interval( $self->{_connection_start}, [gettimeofday] );
|
||||||
)
|
|
||||||
);
|
$self->log(LOGINFO, sprintf "%.3f s.", $elapsed );
|
||||||
$self->log($self->{_level},
|
|
||||||
"Connection time from $remote: $elapsed sec.");
|
|
||||||
}
|
|
||||||
return (DECLINED);
|
return (DECLINED);
|
||||||
}
|
}
|
||||||
|
|
||||||
# vim: ts=4 sw=4 expandtab syn=perl
|
|
||||||
|
Loading…
Reference in New Issue
Block a user