File: Rotate.pm

package info (click to toggle)
liblog-agent-rotate-perl 0.104-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 112 kB
  • ctags: 43
  • sloc: perl: 790; makefile: 2; ansic: 1
file content (594 lines) | stat: -rw-r--r-- 15,960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
###########################################################################
# $Id: Rotate.pm,v 1.2 2002/05/14 04:47:50 wendigo Exp $
###########################################################################
#
# Log::Agent::File::Rotate
#
# RCS Revision: $Revision: 1.2 $
# Date: $Date: 2002/05/14 04:47:50 $
#
# Copyright (c) 2000 Raphael Manfredi.
# Copyright (c) 2002 Mark Rogaski, mrogaski@cpan.org; all rights reserved.
#
# See the README file included with the
# distribution for license information.
#
# $Log: Rotate.pm,v $
# Revision 1.2  2002/05/14 04:47:50  wendigo
# added support for file_perm
#
# Revision 1.1  2002/05/12 17:33:43  wendigo
# Initial revision
#
# Revision 0.1.1.3  2001/04/11 15:58:34  ram
# patch3: mark rotation in the logfile before rotating it
#
# Revision 0.1.1.2  2000/11/12 14:53:27  ram
# patch2: untaint data read or rename() complains under -T
# patch2: use new -single_host parameter to configure LockFile::Simple
#
# Revision 0.1.1.1  2000/11/06 19:57:24  ram
# patch1: removed reference on driver
#
# Revision 0.1  2000/03/05 22:15:40  ram
# Baseline for first alpha release.
#
###########################################################################

use strict;

###########################################################################
package Log::Agent::File::Rotate;

#
# A rotating logfile set
#

use File::stat;
use Fcntl;
use Symbol;
use Compress::Zlib;
require LockFile::Simple;

use Log::Agent; # We're using logerr() ourselves when safe to do so

my $DEBUG = 0;

#
# ->make
#
# Creation routine.
#
# Attributes initialized by parameters:
#    path     file path
#    config   rotating configuration (a Log::Agent::Rotate object)
#
# Other attributes:
#    fd       currently opened file descriptor
#    handle   symbol used for Perl handle
#    warned   records calls made to hardwired warn() to only do them once
#    written  total amount written since opening
#    size     logfile size
#    opened   time when opening occurred
#    dev      device holding logfile
#    ino      inode number of logfile
#    lockmgr  lockfile manager
#    rotating within the rotate() routine
#
sub make {
    my $self = bless {}, shift;
    my ($path, $config) = @_;
    $self->{'path'} = $path;
    $self->{'config'} = $config;
    $self->{'fd'} = undef;
    $self->{'handle'} = gensym;
    $self->{'warned'} = {};
    $self->{'rotating'} = 0; 
    $self->{'lockmgr'} = LockFile::Simple->make(
        -autoclean => 1,
        -delay     => 1,        # until sleep(.25) is supported
        -efunc     => undef,
        -hold      => 60,
        -max       => 5,
        -nfs       => !$config->single_host,
        -stale     => 1,
        -warn      => 0,
        -wfunc     => undef
    );
    return $self;
}

#
# Attribute access
#

sub path     { $_[0]->{'path'} }
sub config   { $_[0]->{'config'} }
sub fd       { $_[0]->{'fd'} }
sub handle   { $_[0]->{'handle'} }
sub warned   { $_[0]->{'warned'} }
sub written  { $_[0]->{'written'} }
sub opened   { $_[0]->{'opened'} }
sub size     { $_[0]->{'size'} }
sub dev      { $_[0]->{'dev'} }
sub ino      { $_[0]->{'ino'} }
sub lockmgr  { $_[0]->{'lockmgr'} }
sub rotating { $_[0]->{'rotating'} }

#
# ->print
#
# Print to file.
# This is where all the monitoring is performed:
#
# . If the file was renamed underneath us, re-open it.
#   This costs a stat() system call each time a log is to be emitted
#   and can be avoided by setting config->is_alone.
#
sub print {
    my $self = shift;
    my $str = join('', @_);

    my $fd = $self->fd;
    my $cf = $self->config;

    #
    # If the file was renamed underneath us, re-open it.
    # This costs a stat() system call each time a log is to be emitted
    # and can be avoided by setting config->is_alone when appropriate.
    #

    if (defined $fd && !$cf->is_alone) {
        my $st = stat($self->path);
        if (!$st || $st->dev != $self->dev || $st->ino != $self->ino) {
            $self->close;
            undef $fd;  # Will be re-opened below
        }
    }

    #
    # Open file if not already done.
    #

    unless (defined $fd) {
        $fd = $self->open;
        return unless defined $fd;
    }

    #
    # Write to logfile
    #

    return unless syswrite($fd, $str, length $str);

    #
    # If the overall logfile size is monitored, update it.
    # Unless we're alone, we have to fstat() the file descriptor.
    #

    if ($cf->max_size) {
        if ($cf->is_alone) {
            $self->{'size'} += length $str;
        } else {
            my $st = stat($fd);
            if ($st) {
                $self->{'size'} = $st->size;    # Paranoid test
            } else {
                $self->{'size'} += length $str;
            }
        }
        if ($self->size > $cf->max_size) {
            $self->rotate;
            return;
        }
    }

    #
    # If the amount of bytes written exceeds the threshold,
    # rotate the files.
    #

    if ($cf->max_write) {
        $self->{'written'} += length $str;
        if ($self->written > $cf->max_write) {
            $self->rotate;
            return;
        }
    }

    #
    # If the opening time is exceeded, rotate the files.
    #

    if ($cf->max_time) {
        if (time - $self->opened > $cf->max_time) {
            $self->rotate;
            return;
        }
    }

    # Did not rotate anything
    return;
}

#
# ->open
#
# Open current logfile.
# Returns opened handle, or nothing if error.
#
sub open {
    my $self = shift;
    my $fd = $self->handle;
    my $path = $self->path;
    my $mode = O_CREAT|O_APPEND|O_WRONLY;
    my $perm = ($self->config)->file_perm;
    warn "opening $path\n" if $DEBUG;

    unless (sysopen($fd, $path, $mode, $perm)) {
        #
        # Can't log errors via Log::Agent since we might recurse down here.
        # Therefore, use warn(), but only once, and clear condition when
        # opening is successful.
        #

        warn "$0: can't open logfile \"$path\": $!\n"
                unless $self->warned->{$path}++;
        return;
    }

    my $st = stat($fd);                         # An fstat(), really
    $self->warned->{$path} = 0;                 # Clear warning condition
    $self->{'fd'} = $fd;                        # Records: file opened
    $self->{'written'} = 0;                     # Amount written
    $self->{'opened'} = time;                   # Opening time
    $self->{'size'} = $st ? $st->size : 0;      # Current size
    $self->{'dev'} = $st->dev;
    $self->{'ino'} = $st->ino;

    return $fd;
}

#
# ->close
#
# Close current logfile.
#
sub close {
    my $self = shift;
    my $fd = $self->fd;
    return unless defined $fd;  # Already closed
    warn "closing logfile\n" if $DEBUG;
    close($fd);
    $self->{'fd'} = undef;      # Mark as closed
}

#
# ->rotate
#
# Perform logfile rotation, as configured, and log any returned error
# to the error channel.
#
sub rotate {
    my $self = shift;
    return if $self->rotating;  # no recusion if error & limits too small
    $self->{'rotating'} = 1;

    my @errors = $self->do_rotate;
    unless (@errors) {
        $self->{'rotating'} = 0;
        return;
    }

    #
    # Errors are logged using logerr().  There's no danger we could
    # recurse down here since we're protected by the `rotating' flag.
    #

    my $error = @errors == 1 ? "error" : sprintf("%d errors", scalar @errors);
    logerr "the following $error occurred while rotating logfiles:";
    foreach my $err (@errors) {
        logerr $err;
        warn "ERROR: $err\n" if $DEBUG;
    }

    $self->{'rotating'} = 0;
}

#
# ->do_rotate
#
# Perform logfile rotation, as configured.
# Returns nothing if OK, an array of error messages otherwise.
#
sub do_rotate {
    my $self = shift;
    my $path = $self->path;
    my $cf = $self->config;
    my $lock = $self->lockmgr->lock($path);

    #
    # Emission of errors has to be delayed, since we're in the middle of
    # logfile rotation, which could be the error channel.
    #

    my @errors = ();

    push(@errors, "proceeded with rotation of $path without lock")
            unless defined $lock;

    #
    # We're unix-centric in the following code fragment, but I don't know
    # how to do the same thing on non-unix operating systems.  Sorry.
    #

    my ($dir, $file) = ($path =~ m|^(.*)/(.*)|);
    ($dir, $file) = (".", $path) unless $dir;

    local *DIR;
    unless (opendir(DIR, $dir)) {
        my $error = "can't open directory \"$dir\" to rotate $path: $!";
        $lock->release if defined $lock;
        return ($error);
    }
    my @files = readdir DIR;
    closedir DIR;

    #
    # Identify the logfiles already present.
    #
    # We use the common convention of renaming un-compressed logfiles
    # as "path.0", "path.1", etc... the .0 being the more recent file,
    # and use "path.0.gz", "path.1.gz", etc... for compressed logfiles.
    #

    my @logfiles = ();  # Logfiles to rotate
    my @unlink = ();    # Logfiles to unlink
    my $lookfor = "$file.";
    my $unlink_at = $cf->backlog - 1;

    warn "unlink_at=$unlink_at\n" if $DEBUG;

    foreach my $f (@files) {
        next unless substr($f, 0, length $lookfor) eq $lookfor;
        my ($idx) = ($f =~ /\.(\d+)(?:\.gz)?$/);
        warn "f=$f, idx=$idx\n" if $DEBUG;
        next unless defined $idx;
        $f = $1 if $f =~ /^(.*)$/; # untaint
        if ($idx >= $unlink_at) {
            push(@unlink, $f);
        } else {
            $logfiles[$idx] = $f;
        }
    }

    if ($DEBUG) {
        warn "unlink=@unlink\n";
        warn "logfiles=@logfiles\n";
    }

    #
    # Delete old files, if any.
    #

    foreach my $f (@unlink) {
        unlink("$dir/$f") or push(@errors, "can't unlink $dir/$f: $!");
    }

    #
    # File rotation section...
    #
    # If backlog=5 and unzipped=2, then, when things have stabilized,
    # we have the following logfiles:
    #
    #   path.4.gz        was unlinked above
    #   path.3.gz        renamed as path.4.gz
    #   path.2.gz        renamed as path.3.gz
    #   path.1           compressed as path.2.gz
    #   path.0           renamed as path.1
    #   path             current logfile, closed and renamed path.0
    #
    # The code below is prepared to deal with missing files, or policy
    # changes. Compressed file are not uncompressed though.
    #

    my $last = $cf->backlog - 2;   # Oldest logfile already deleted
    my $gz_limit = $cf->unzipped;  # Files up to that index are .gz

    warn "last=$last, gz_limit=$gz_limit\n" if $DEBUG;

    #
    # Handle renaming of compressed files
    #

    for (my $i = $last; $i >= $gz_limit; $i--) {
        next unless defined $logfiles[$i]; # Not that much backlog yet?
        my $old = "$dir/$logfiles[$i]";
        my $new = "$path." . ($i+1) . ".gz";
        warn "compressing old=$old, new=$new\n" if $DEBUG;
        if ($old =~ /\.gz$/) {
            rename($old, $new) or
                    push(@errors, "can't rename $old to $new: $!");
        } else {
            # Compression policy changed?
            my $err = $self->mv_gzip($old, $new);
            push(@errors, $err) if defined $err;
        }
    }

    #
    # Handle compression and renaming of the oldest uncompressed file
    #

    if ($gz_limit > 0 && defined $logfiles[$gz_limit-1]) {
        my $old = "$dir/$logfiles[$gz_limit-1]";
        my $new = "$path.$gz_limit.gz";
        warn "rename and compress old=$old, new=$new\n" if $DEBUG;
        if ($old !~ /\.gz$/) {
            my $err = $self->mv_gzip($old, $new);
            push(@errors, $err) if defined $err;
        } else {
            # Compression policy changed?
            rename($old, $new) or
            push(@errors, "can't rename $old to $new: $!");
        }
    }

    #
    # Handle renaming of uncompressed files
    #

    for (my $i = $gz_limit - 2; $i >= 0; $i--) {
        next unless defined $logfiles[$i]; # Not that much backlog yet?
        my $old = "$dir/$logfiles[$i]";
        my $new = "$path." . ($i+1);
        warn "rename old=$old, new=$new\n" if $DEBUG;
        $new .= ".gz" if $old =~ /\.gz$/;  # Compression policy changed?
        rename($old, $new) or
                push(@errors, "can't rename $old to $new: $!");
    }

    #
    # Mark rotation, in case they "tail -f" on it.
    #

    my $fd = $self->fd;
    syswrite($fd, "*** LOGFILE ROTATED ON " . scalar(localtime) . "\n");

    #
    # Finally, close current logfile and rename it.
    #

    $self->close;
    if ($gz_limit) {
        rename($path, "$path.0") or
                push(@errors, "can't rename $path to $path.0: $!");
    } else {
        my $err = $self->mv_gzip($path, "$path.0.gz");
        push(@errors, $err) if defined $err;
    }

    #
    # Unlock logfile and propagate errors to be logged in new current file.
    #

    $lock->release if defined $lock;
    return @errors if @errors;
    return;
}

#
# ->mv_gzip
#
# Compress old file into new file and unlink old file, propagating mtime.
# Returns error string, nothing if OK.
#
sub mv_gzip {
    my $self = shift;
    my ($old, $new) = @_;

    local *FILE;
    my $st = stat($old);
    unless (defined $st && CORE::open(FILE, $old)) {
        return "can't open $old to compress into $new: $!";
    }
    my $gz = gzopen($new, "wb9");
    unless (defined $gz) {
        CORE::close FILE;
        return "can't write into $new: $gzerrno";
    }

    local $_;
    my $error;
    while (<FILE>) {
        unless ($gz->gzwrite($_)) {
            $error = "error while compressing $old in $new: $gzerrno";
            last;
        }
    }
    CORE::close FILE;
    $gz->gzclose();

    utime $st->atime, $st->mtime, $new; # don't care if it fails
    unlink $old or do { $error = "can't unlink $old: $!" };

    return $error if defined $error;
    return;
}

1; # for require

__END__

=head1 NAME

Log::Agent::File::Rotate - a rotating logfile set

=head1 SYNOPSIS

 #
 # This class is not user-visible.
 #
 # It is documented only for programmers wishing to inherit
 # from it to further extend its behaviour.
 #

 require Log::Agent::Driver::File;
 require Log::Agent::Rotate;
 require Log::Agent::File::Rotate;

 my $config = Log::Agent::Rotate->make(...);
 my $driver = Log::Agent::Driver::File->make(...);
 my $fh = Log::Agent::File::Rotate->make("file", $config, $driver);

=head1 DESCRIPTION

This class represents a rotating logfile and is used drivers wishing
to rotate their logfiles periodically.  From the outside, it exports
a single C<print> routine, just like C<Log::Agent::File::Native>.

Internally, it uses the parameters given by a C<Log::Agent::Rotate> object
to transparently close the current logfile and cycle the older logs.

Before rotating the current logfile, the string:

    *** LOGFILE ROTATED ON <local date>

is emitted, so that people monitoring the file via "tail -f" know about
it and are not surprised by the sudden stop of messages.

Its exported interface is:

=over 4

=item make I<file>, I<config>

This is the creation routine.  The I<config> object is an instance of
C<Log::Agent::Rotate>.

=item print I<args>

Prints I<args> to the file.  After having printed the data, monitor the file
against the thresholds defined in the configuration, and possibly rotate
the logfiles according to the parameters held in the same configuration
object.

When the C<is_alone> flag is not set in the configuration, the logfile is
checked everytime a C<print> is issued to see if its inode changed.  Indeed,
when several instances of the same program using rotating logfiles are
running, each of them may decide to cycle the logs at some point in time, and
therefore our opened handle could point to an already renamed or unlinked file.

=back

=head1 AUTHORS

Originally written by Raphael Manfredi E<lt>Raphael_Manfredi@pobox.comE<gt>,
currently maintained by Mark Rogaski E<lt>mrogaski@pobox.comE<gt>.

=head1 SEE ALSO

Log::Agent::Rotate(3), Log::Agent::Driver::File(3).

=cut