#! /usr/bin/perl -w # # Short Summary : # Rollover and compress Apache log files, following Includes within the # httpd.conf (and all other configuration files) # # Detailed Description : # This script will rotate and gzip Apache log files automatically. It reads # httpd.conf, descends all Included configuration files, and makes a note of # each unique Log file. It then renames each to a file with a datestamp, # restarts Apache, then gzip compresses the datestamped log files. # It also chgrps the compressed logs to whatever group defined by "gid" # variable and sets up permissions for that group to be able to read and/or # write to them. This makes it simple for other (non-root) processes to come # along later and post-process the compressed logs # # Run this periodically (period decided by traffic) via cron to collect a # regular repository of compressed logs and keep live log files down to a # manageable size. # # Linux Server Hacks, Rob Flickenger, O'Reilly Publications # Hack # 92, pp. 193 # # First written : S Gowtham, Fri Nov 11 19:20:56 EST 2005 # Last modified : S Gowtham, Fri Nov 11 19:20:56 EST 2005 # S Gowtham, Sat Nov 12 00:37:36 EST 2005, # with bug-fix from David Torrey (tj@mtu.edu) # use strict; $|++; # Some of the following might need modification, based on the # particulars of distribution my $server_root = "/etc/httpd"; my $conf = "$server_root/conf/httpd.conf"; my $gid = "apache"; my (%logs, %included, @files, @gzip); my $date = `date +%Y%m%d`; chomp $date; push @files, $conf; for $conf (@files) { open(CONF, "<$conf") || die "Cannot open config file $conf: $!\n"; while () { chomp; next if /^(\s+)?#/; if (/(Transfer|Custom|Error)Log\s+(\S+)(\s+)/i) { $logs{$2}++; } elsif (/^(ResourceConfig|Include)\s+(\S+)/i) { if(!$included{$2}) { # Bug-Fix : David Torrey (tj@mtu.edu) # push @files, "$2"; push @files, "$server_root/$2"; $included{$2}++; } } } close CONF; } for my $logfile (sort keys %logs) { $logfile = "$server_root/$logfile" unless ($logfile =~ m|^/|); rename($logfile, "$logfile.$date"); push(@gzip, "$logfile.$date"); } system("/etc/init.d/httpd restart"); for my $logfile (@gzip) { system("gzip $logfile"); system("chgrp $gid $logfile.gz"); system("chmod 664 $logfile.gz"); }