Debian Codegraph @

plugins/connection_time

raw
1 #!perl -w
2
3 =head1 NAME
4
5 connection_time - log the duration of a connection
6
7 =head1 DESCRIPTION
8
9 The B<connection_time> plugin records the time of a connection between the
10 first and the last possible hook in qpsmtpd (I<pre-connection> and
11 I<post-connection>) and writes a C<LOGINFO> (default, see below) line to
12 the log.
13
14 =head1 CONFIG
15
16 =head2 loglevel
17
18 Adjust the quantity of logging for this plugin. See docs/logging.pod
19
20 connection_time loglevel +1 (less logging)
21
22 connection_time loglevel -1 (more logging)
23
24 =cut
25
26 use strict;
27 use warnings;
28
29 use Qpsmtpd::Constants;
30
31 use Time::HiRes qw(gettimeofday tv_interval);
32
33 sub register {
34 my ($self, $qp) = (shift, shift);
35 if (@_ == 1) { # backwards compatible
36 $self->{_args}{loglevel} = shift;
37 if ($self->{_args}{loglevel} =~ /\D/) {
38 $self->{_args}{loglevel} =
39 Qpsmtpd::Constants::log_level($self->{_args}{loglevel});
40 }
41 $self->{_args}{loglevel} ||= 6;
42 }
43 elsif (@_ % 2) {
44 $self->log(LOGERROR, "invalid arguments");
45 }
46 else {
47 $self->{_args} = {@_}; # named args, inherits loglevel
48 }
49
50 # pre-connection is not available in the tcpserver deployment model.
51 # duplicate the handler, so it works both ways with no redudant methods
52 $self->register_hook('pre-connection', 'connect_handler');
53 $self->register_hook('connect', 'connect_handler');
54 }
55
56 sub connect_handler {
57 my $self = shift;
58 return DECLINED
59 if ($self->hook_name eq 'connect' && defined $self->{_connection_start});
60 $self->{_connection_start} = [gettimeofday];
61 $self->log(LOGDEBUG, "started at " . scalar gettimeofday);
62 return (DECLINED);
63 }
64
65 sub hook_post_connection {
66 my $self = shift;
67
68 if (!$self->{_connection_start}) {
69 $self->log(LOGERROR, "Start time not set?!");
70 return (DECLINED);
71 }
72
73 my $elapsed = tv_interval($self->{_connection_start}, [gettimeofday]);
74
75 $self->log(LOGINFO, sprintf "%.3f s.", $elapsed);
76 return (DECLINED);
77 }
78