The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Concatenate Logs - Perl Question ganga.dharan Shell Programming and Scripting 4 05-13-2009 08:41 AM
Perl Scripting for monitoring logs solitare123 UNIX for Dummies Questions & Answers 1 06-25-2008 06:49 AM
Perl script to rotate logs theninja Shell Programming and Scripting 18 03-21-2008 03:06 PM
ppp errror message in logs frankkahle UNIX for Advanced & Expert Users 1 05-02-2007 08:38 AM
Message Logs Error ByasB UNIX for Dummies Questions & Answers 1 04-07-2005 01:40 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-24-2009
w1r3d w1r3d is offline
Registered User
  
 

Join Date: Jul 2009
Location: Dallas, TX
Posts: 3
Rotating logs in Perl without message loss

(I'm aware log rotation is a common subject, but I tried searching and couldn't find an answer)

For some time now, I've been using the Logfile::Rotate module to rotate logs in a log-monitoring script. So far, I haven't experienced any problems, and it works great because I can use it in Linux and Windows (mainly using Red Hat, and XP/2003). Well, it was brought to my attention by a co-worker that it is possible to lose log data using the "copy and truncate" method that the module uses. He mentioned that traditionally, in Linux, you do "move then send HUP signal to process". It seems like that is the preferred method (from what I see by searching online), but that would make it difficult for the script to port accross multiple OS's like it does now.

I guess my question is - what are your thoughts on this? I guess I thought this module worked perfectly, but this is the point he brings:

Code:
    ## copy current to next incremental
    $next = "${currn}.1";
    copy ($curr, $next);        

    ## preserve permissions and status
    if ( $self->{'Persist'} eq 'yes' ){
        my @stat = stat $curr;
        chmod( $stat[2], $next ) or carp "error: chmod failed: ($next)";
        utime( $stat[8], $stat[9], $next ) or carp "error: failed: ($next)";
        chown( $stat[4], $stat[5], $next ) or carp "error: chown failed: ($next)";
    }

    # now truncate the file
    if( $self->{'Flock'} eq 'yes' )
    {
        truncate $curr,0 or croak "error: could not truncate $curr: $!"; }
    else{
        local(*IN);
        open(IN, "+>$self->{'File'}") 
            or croak "error: could not truncate $curr: $!";
    }
The module does a "flock" on the file, which is an advisory lock on Linux. If the application that is writing to the current log file doesn't flock, then the flock on the rotation module is a no-op.

He mentioned that if the rotation module is task switched by the OS between the 'copy' on the third line and the 'truncate' or 'open' lines, log messages will be lost.

I am not very familiar with the way Linux works, and what I find online are mostly examples (code) of log rotation, and not actual explanations. Any insight on this subject will be greatly appreciated.

Thanks in advance!

edit: did some more testing, and was able to confirm (very minor) log loss with Linux's chatty auditd. I now have it so auditd rotates its own log files, and my script only monitors it. I'll have to come up with log file rotation schemes for all the different files I'm monitoring. Ugh. :-\

Last edited by w1r3d; 07-24-2009 at 07:56 AM..
  #2 (permalink)  
Old 07-25-2009
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,114
That perl module does seem to be badly written. And most OS's already have a log rotation technique. Linux comes with a logrotate command. link: logrotate - Linux Command - Unix Command I wonder why you don't just use that. In the XP world it seems that the typical solution is to let software that keeps logs rotate its own logs. IIS takes this approach. It is not as flexible as logrotate, but I would investigate that before implementing a new solution.
Reply

Bookmarks

Tags
logfile rotation, perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0