Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Apache log splitter/compressor

by lhoward (Vicar)
on Jun 12, 2000 at 18:06 UTC ( #17704=sourcecode: print w/replies, xml ) Need Help??
Category: Web Stuff
Author/Contact Info Les Howard
Description: This program is designed to read from an apache logfile pipe. It automatically compresses the data and splits it into files each containg one day's worth of data. The directory to write the log files to should be set in the environmental variable LOG_DIR. Log files are named access_log_YYYY_MM_DD.N.gz. Here is how I have apache configured to call this program.

CustomLog |/path/to/ combined

#!/usr/bin/perl -w

use strict;

use Time::ParseDate;
use Time::CTime;
use Compress::Zlib;

my $out_dir=$ENV{LOG_DIR};

my $cur_str='';
my $outlog;

my $line;

$SIG{TERM}= sub {
  #handle apache closing by closing the .gz file properly;
  if(defined $outlog){
    undef $outlog;

  while (defined($line=<STDIN>)){
    chomp $line;
      #lines must contain an extractable date
      my ($dd,$mm,$yy)=($1,$2,$3);
      my $str="$dd $mm $yy";
      if($str ne $cur_str){
    # new day, rotate to next file
        my $dt=parsedate($str);
        my $ts=strftime("%Y_%m_%d",localtime($dt));
        # close current log (If necessary)
        $outlog->gzclose() if defined $outlog;
    #find a unique name for the new log    
        my $ct=1;
        my $fn="$out_dir$ts.0.gz";
        while(-f $fn){
      # unrecognized line.... there should probably be some error chec
+king here

  if(defined $outlog){
Replies are listed 'Best First'.
RE: Apache log splitter/compressor
by jjhorner (Hermit) on Jun 12, 2000 at 19:32 UTC

    lhoward, some AM below requested this in mod_perl format.

    Do you mind if I rewrite for use in mod_perl? Or are you going to do it?

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
      Feel free to rewrite it in mod_perl format. I currently have no plans to rewrite it in mod_perl myself. I originally wrote this outside of mod_perl because I did not do mod_perl at the time of its original development.

      One feature I would love to add to this program is real-time reverse-DNS lookups of browsing hosts. The tricky part about that is you have to do it in a way that doesn't block the webserver (so it isn't slowed down).

        I sometimes have issues determining when something should be programmed or scripted. If I need my nightly logs dns checked, should I write the program to do it automagically, or should I just set a cron job to do logresolve every night.

        I guess I'm not the only person who has ever had this issue.

        J. J. Horner
        Linux, Perl, Apache, Stronghold, Unix
RE: Apache log splitter/compressor
by Anonymous Monk on Jun 12, 2000 at 19:29 UTC
    All the standalone programs are great, and this one is no exception. However, it would be nice to rewrite these as an Apache module, preferably a C version for the lusers out there that haven't installed mod_perl (what are they thinking?!). Anybody know if there is a decent package either in C or Perl?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://17704]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2020-09-29 23:53 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (154 votes). Check out past polls.