| 1 |
#!perl -w |
| 2 |
# Adaptive logging plugin - logs at one level for successful messages and |
| 3 |
# one level for DENY'd messages |
| 4 |
|
| 5 |
sub register { |
| 6 |
my ($self, $qp, %args) = @_; |
| 7 |
|
| 8 |
$self->{_minlevel} = LOGERROR; |
| 9 |
if (defined($args{accept})) { |
| 10 |
if ($args{accept} =~ /^\d+$/) { |
| 11 |
$self->{_minlevel} = $args{accept}; |
| 12 |
} |
| 13 |
else { |
| 14 |
$self->{_minlevel} = log_level($args{accept}); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
$self->{_maxlevel} = LOGWARN; |
| 19 |
if (defined($args{reject})) { |
| 20 |
if ($args{reject} =~ /^\d+$/) { |
| 21 |
$self->{_maxlevel} = $args{reject}; |
| 22 |
} |
| 23 |
else { |
| 24 |
$self->{_maxlevel} = log_level($args{reject}); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
$self->{_prefix} = '`'; |
| 29 |
if (defined $args{prefix} and $args{prefix} =~ /^(.+)$/) { |
| 30 |
$self->{_prefix} = $1; |
| 31 |
} |
| 32 |
|
| 33 |
# If you want to capture this log entry with this plugin, you need to |
| 34 |
# wait until after you register the plugin |
| 35 |
$self->log(LOGINFO, 'Initializing logging::adaptive plugin'); |
| 36 |
} |
| 37 |
|
| 38 |
sub hook_logging { # wlog |
| 39 |
my ($self, $transaction, $trace, $hook, $plugin, @log) = @_; |
| 40 |
|
| 41 |
# Don't log your own log entries! If this is the only logging plugin |
| 42 |
# then these lines will not be logged at all. You can safely comment |
| 43 |
# out this line and it will not cause an infinite loop. |
| 44 |
return DECLINED if defined $plugin and $plugin eq $self->plugin_name; |
| 45 |
|
| 46 |
if (defined $self->{_maxlevel} && $trace <= $self->{_maxlevel}) { |
| 47 |
warn join( |
| 48 |
" ", |
| 49 |
$$ |
| 50 |
. ( |
| 51 |
defined $plugin ? " $plugin plugin:" |
| 52 |
: defined $hook ? " running plugin ($hook):" |
| 53 |
: "" |
| 54 |
), |
| 55 |
@log |
| 56 |
), |
| 57 |
"\n" |
| 58 |
unless $log[0] =~ /logging::adaptive/; |
| 59 |
push @{$transaction->{_log}}, [$trace, $hook, $plugin, @log] |
| 60 |
if (defined $self->{_minlevel} && $trace <= $self->{_minlevel}); |
| 61 |
} |
| 62 |
|
| 63 |
return DECLINED; |
| 64 |
} |
| 65 |
|
| 66 |
sub hook_deny { # dlog |
| 67 |
my ($self, $transaction, $prev_hook, $return, $return_text) = @_; |
| 68 |
$self->{_denied} = 1; |
| 69 |
} |
| 70 |
|
| 71 |
sub hook_reset_transaction { # slog |
| 72 |
|
| 73 |
# fires when a message is accepted |
| 74 |
my ($self, $transaction, @args) = @_; |
| 75 |
|
| 76 |
return DECLINED if $self->{_denied}; |
| 77 |
|
| 78 |
foreach my $row (@{$transaction->{_log}}) { |
| 79 |
next unless scalar @$row; # skip over empty log lines |
| 80 |
my ($trace, $hook, $plugin, @log) = @$row; |
| 81 |
warn join( |
| 82 |
" ", $$, |
| 83 |
$self->{_prefix} |
| 84 |
. ( |
| 85 |
defined $plugin ? " $plugin plugin:" |
| 86 |
: defined $hook ? " running plugin ($hook):" |
| 87 |
: "" |
| 88 |
), |
| 89 |
@log |
| 90 |
), |
| 91 |
"\n" |
| 92 |
if ($trace <= $self->{_minlevel}); |
| 93 |
} |
| 94 |
|
| 95 |
return DECLINED; |
| 96 |
} |
| 97 |
|
| 98 |
=head1 NAME |
| 99 |
|
| 100 |
adaptive - An adaptive logging plugin for qpsmtpd |
| 101 |
|
| 102 |
=head1 DESCRIPTION |
| 103 |
|
| 104 |
A qpsmtpd plugin for logging at different levels depending on success or |
| 105 |
failure of any given message. |
| 106 |
|
| 107 |
=head1 INSTALL AND CONFIG |
| 108 |
|
| 109 |
Place this plugin in the plugin/logging directory beneath the standard |
| 110 |
qpsmtpd installation. Edit the config/logging file and add a line like |
| 111 |
this: |
| 112 |
|
| 113 |
logging/adaptive [accept minlevel] [reject maxlevel] [prefix char] |
| 114 |
|
| 115 |
where the optional parameters are: |
| 116 |
|
| 117 |
=over 4 |
| 118 |
|
| 119 |
=item B<accept> |
| 120 |
|
| 121 |
This is the level at which messages which are accepted will be logged. You |
| 122 |
can use either the loglevel number (as shown in config.sample/loglevels) or |
| 123 |
you can use the text form (from the same file). Typically, you would set |
| 124 |
this to LOGERROR (4) so that the FROM and TO lines would be logged (with the |
| 125 |
default installation). If absent, it will be set to LOGERROR (4). |
| 126 |
|
| 127 |
=item B<reject> |
| 128 |
|
| 129 |
This is the level which messages which are rejected for any reason will be |
| 130 |
logged. This would typically be set as high as reasonable, to document why a |
| 131 |
message may have been rejected. If absent, it defaults to LOGWARN (5), which |
| 132 |
is probably not high enough for most sites. |
| 133 |
|
| 134 |
=item B<prefix> |
| 135 |
|
| 136 |
In order to visually distinguish the accepted from rejected lines, all |
| 137 |
log lines from a accepted message will be prefixed with the character |
| 138 |
listed here (directly after the PID). You can use anything you want as |
| 139 |
a prefix, but it is recommended that it be short (preferably just a single |
| 140 |
character) to minimize the amount of bloat in the log file. If absent, the |
| 141 |
prefix defaults to the left single quote (`). |
| 142 |
|
| 143 |
=back |
| 144 |
|
| 145 |
=head1 TYPICAL USAGE |
| 146 |
|
| 147 |
If you are using multilog to handle your logging, you can replace the system |
| 148 |
provided log/run file with something like this: |
| 149 |
|
| 150 |
#! /bin/sh |
| 151 |
export LOGDIR=./main |
| 152 |
mkdir -p $LOGDIR/failed |
| 153 |
exec multilog t n10 \ |
| 154 |
'-*` *' $LOGDIR/detailed \ |
| 155 |
'-*' '+*` *' $LOGDIR/accepted |
| 156 |
|
| 157 |
which will have the following effects: |
| 158 |
|
| 159 |
=over 4 |
| 160 |
|
| 161 |
=item 1. All lines will be logged into the ./mail/detailed folder |
| 162 |
|
| 163 |
=item 2. Log lines for messages that are accepted will go to ./main/accepted |
| 164 |
|
| 165 |
=back |
| 166 |
|
| 167 |
You may want to use the s####### option to multilog to ensure that the log |
| 168 |
files are large enough to maintain a proper amount of history. Depending on |
| 169 |
your site load, it is useful to have at least a week and preferrably three |
| 170 |
weeks of accepted messages. You can also use the n## option to have more |
| 171 |
log history files maintained. |
| 172 |
|
| 173 |
=head1 AUTHOR |
| 174 |
|
| 175 |
John Peacock <[email protected]> |
| 176 |
|
| 177 |
=head1 COPYRIGHT AND LICENSE |
| 178 |
|
| 179 |
Copyright (c) 2005 John Peacock |
| 180 |
|
| 181 |
This plugin is licensed under the same terms as the qpsmtpd package itself. |
| 182 |
Please see the LICENSE file included with qpsmtpd for details. |
| 183 |
|
| 184 |
=cut |
| 185 |
|