Debian Codegraph @

plugins/logging/file

raw
1 #!perl -w
2
3 =head1 NAME
4
5 file - Simple log-to-file logging for qpsmtpd
6
7 =head1 DESCRIPTION
8
9 The 'file' logging plugin for qpsmtpd records qpsmtpd log messages into a
10 file (or a named pipe, if you prefer.)
11
12 =head1 CONFIGURATION
13
14 To enable the logging plugin, add a line of this form to the qpsmtpd plugins
15 configuration file:
16
17 =over
18
19 logging/file [loglevel I<level>] [reopen] [nosplit] [tsformat I<format>] I<path>
20
21 For example:
22
23 logging/file loglevel LOGINFO /var/log/qpsmtpd.log
24 logging/file /var/log/qpsmtpd.log.%Y-%m-%d
25 logging/file loglevel LOGCRIT reopen |/usr/local/sbin/page-sysadmin
26 logging/file loglevel LOGDEBUG tsformat %FT%T /var/log/qpsmtpd.log
27
28 =back
29
30 Multiple instances of the plugin can be configured by appending :I<N> for any
31 integer(s) I<N>, to log to multiple files simultaneously, e.g. to log critical
32 errors and normally verbose logs elsewhere.
33
34 The filename or command given can include strftime conversion specifiers,
35 which can be used to substitute time and date information into the logfile.
36 The file will be reopened whenever this output changes (for example, with a
37 format of qpsmtpd.log.%Y-%m-%d-%h, the log would be reopened once per hour).
38
39 The list of supported conversion specifiers depends on the strftime()
40 implementation of your C library. See strftime(3) for details. Additionally,
41 %i will be expanded to a (hopefully) unique session-id; if %i is used, a new
42 logfile will be started for each SMTP connection.
43
44 The following optional configuration setting can be supplied:
45
46 =over
47
48 =item nosplit
49
50 If specified, the output file or pipe will be reopened at once once per
51 connection, and only prior to the first log output. This prevents logs for
52 sessions that span log intervals being split across multiple logfiles.
53 Without this option, the log will be reopened only when its output filename
54 changes; if strftime specifiers are not used, the log will not be reopened
55 at all.
56
57 =item reopen
58
59 Forces the log output to be reopened once per connection, as soon as something
60 is available to be logged. This can be combined with a high log severity (see
61 I<loglevel> below) to facilitate SMTP service alarms with Nagios or a similar
62 monitoring agent.
63
64 =item loglevel I<loglevel>
65
66 The internal log level below which messages will be logged. The I<loglevel>
67 given should be chosen from the list below. Priorities count downward (for
68 example, if LOGWARN were selected, LOGERROR, LOGCRIT and LOGEMERG messages
69 would be logged as well).
70
71 =item tsformat I<format>
72
73 By default qpsmtpd will prepend log items with the date and time as given in
74 the format by perl's C<localtime()> function. If you prefer another format then
75 you can specify a tsformat parameter.
76
77 =over
78
79 =item B<LOGDEBUG>
80
81 =item B<LOGINFO>
82
83 =item B<LOGNOTICE>
84
85 =item B<LOGWARN>
86
87 =item B<LOGERROR>
88
89 =item B<LOGCRIT>
90
91 =item B<LOGALERT>
92
93 =item B<LOGEMERG>
94
95 =back
96
97 =back
98
99
100 The chosen I<path> should be writable by the user running qpsmtpd; it will be
101 created it did not already exist, and appended to otherwise.
102
103 =head1 AUTHORS
104
105 Devin Carraway <[email protected]>, with contributions by Peter J.
106 Holzer <[email protected]>.
107
108 =head1 LICENSE
109
110 Copyright (c) 2005-2006, Devin Carraway
111 Copyright (c) 2006, Peter J. Holzer.
112
113 This plugin is licensed under the same terms as the qpsmtpd package itself.
114 Please see the LICENSE file included with qpsmtpd for details.
115
116 =cut
117
118 use strict;
119 use warnings;
120
121 use IO::File;
122 use Sys::Hostname;
123 use POSIX qw(strftime);
124 use Qpsmtpd::Constants;
125
126 sub register {
127 my ($self, $qp, @args) = @_;
128 my %args;
129
130 $self->{_loglevel} = LOGWARN;
131 $self->{_tsformat} = '%a %b %d %T %Y'; # same as scalar localtime
132
133 while (1) {
134 last if !@args;
135 if (lc $args[0] eq 'loglevel') {
136 shift @args;
137 my $ll = shift @args;
138 if (!defined $ll) {
139 warn "Malformed arguments to logging/file plugin";
140 return;
141 }
142 if ($ll =~ /^(\d+)$/) {
143 $self->{_loglevel} = $1;
144 }
145 elsif ($ll =~ /^(LOG\w+)$/) {
146 $self->{_loglevel} = log_level($1);
147 defined $self->{_loglevel} or $self->{_loglevel} = LOGWARN;
148 }
149 }
150 elsif (lc $args[0] eq 'nosplit') {
151 shift @args;
152 $self->{_nosplit} = 1;
153 }
154 elsif (lc $args[0] eq 'reopen') {
155 shift @args;
156 $self->{_reopen} = 1;
157 }
158 elsif (lc $args[0] eq 'tsformat') {
159 shift @args;
160 my $format = shift @args;
161 $self->{_tsformat} = $format;
162 }
163 else { last }
164 }
165
166 unless (@args && $args[0]) {
167 warn "Malformed arguments to syslog plugin";
168 return;
169 }
170
171 my $output = join(' ', @args);
172
173 if ($output =~ /^\s*\|(.*)/) {
174 $self->{_log_pipe} = 1;
175 $self->{_log_format} = $1;
176 }
177 else {
178 $output =~ /^(.*)/; # detaint
179 $self->{_log_format} = $1;
180 }
181 $self->{_current_output} = '';
182 $self->{_session_counter} = 0;
183 1;
184 }
185
186 sub log_output {
187 my ($self, $transaction) = @_;
188 my $output = $self->{_log_format};
189 $output =~ s/%i/($transaction->notes('logging-session-id') || 'parent')/ge;
190 $output = strftime $output, localtime;
191 $output;
192 }
193
194 sub open_log {
195 my ($self, $output, $qp) = @_;
196
197 if ($self->{_log_pipe}) {
198 unless ($self->{_f} = new IO::File "|$output") {
199 warn "Error opening log output to command $output: $!";
200 return undef;
201 }
202 }
203 else {
204 unless ($self->{_f} = new IO::File ">>$output") {
205 warn "Error opening log output to path $output: $!";
206 return undef;
207 }
208 }
209 $self->{_current_output} = $output;
210 $self->{_f}->autoflush(1);
211 1;
212 }
213
214 # Reopen the output iff the interpolated output filename has changed
215 # from the one currently open, or if reopening was selected and we haven't
216 # yet done so during this session.
217 #
218 # Returns true if the file was reopened, zero if not, undef on error.
219 sub maybe_reopen {
220 my ($self, $transaction) = @_;
221
222 my $new_output = $self->log_output($transaction);
223 if (
224 !$self->{_current_output}
225 || $self->{_current_output} ne $new_output
226 || ($self->{_reopen}
227 && !$transaction->notes('file-reopened-this-session'))
228 )
229 {
230 unless ($self->open_log($new_output, $transaction)) {
231 return undef;
232 }
233 $transaction->notes('file-reopened-this-session', 1);
234 return 1;
235 }
236 return 0;
237 }
238
239 sub hook_connect {
240 my ($self, $transaction) = @_;
241
242 $transaction->notes('file-logged-this-session', 0);
243 $transaction->notes('file-reopened-this-session', 0);
244 $transaction->notes(
245 'logging-session-id',
246 sprintf("%08d-%04d-%d",
247 scalar time, $$,
248 ++$self->{_session_counter})
249 );
250 return DECLINED;
251 }
252
253 sub hook_disconnect {
254 my ($self) = @_;
255
256 if ($self->{reopen_} && $self->{_f}) {
257 $self->{_f} = undef;
258 }
259 return DECLINED;
260 }
261
262 sub hook_logging {
263 my ($self, $transaction, $trace, $hook, $plugin, @log) = @_;
264
265 return DECLINED
266 if !defined $self->{_loglevel}
267 or $trace > $self->{_loglevel};
268 return DECLINED if defined $plugin and $plugin eq $self->plugin_name;
269
270 # Possibly reopen the log iff:
271 # - It's not already open
272 # - We're allowed to split sessions across logfiles
273 # - We haven't logged anything yet this session
274 # - We aren't in a session
275 if ( !$self->{_f}
276 || !$self->{_nosplit}
277 || !$transaction
278 || !$transaction->notes('file-logged-this-session'))
279 {
280 unless (defined $self->maybe_reopen($transaction)) {
281 return DECLINED;
282 }
283 $transaction->notes('file-logged-this-session', 1) if $transaction;
284 }
285
286 my $f = $self->{_f};
287 print $f strftime($self->{_tsformat}, localtime), ' ',
288 hostname(), '[', $$, ']: ', @log, "\n";
289 return DECLINED;
290 }
291