Stellarcore.net

Stellarcore.net

This script is used to generate Webalizer logfiles for multiple Vhosts. The logic for this one is I keep all my vhosts under /www and I only generate logs for vhosts that are in the form of domain.com. So the script just gets a list of valid directories and then reads their log files. I run it once per day but it can be run more often, one client wanted it run every hour.

#!/usr/bin/perl

########################################
# Webalizer nightly cron script
# This script will rotate the log files weekly
#
# Author: Mike Tremaine 
# Written: Feb 18th 2004
#########################################

use strict;
use File::Copy;

my $LOG_DIR = "/var/apache2/logs/";
my $LOG_OUTPUT = "/www/stellarcore.net/securedoc/logs"
my $ARCHIVE_DIR = "/var/apache2/logs/Archives";
my $WWW_DIR = "/www";
my $WEBALIZER = "/usr/local/bin/webalizer";

#############################
#Make pretty timestamp
############################
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon"};
if ($mday < 10) {$mday = "0$mday"};
if ($hour < 10) {$hour = "0$hour"};
my $timestamp = "$mon" . "-$mday" . "-$year";

#############################
#Open WWW_DIR and get list of sites
#############################
opendir(DIR, "$WWW_DIR") || die "Can not open DIR  $!\n";
my @sites = (grep(/\.\w+$/, readdir(DIR)));
closedir(DIR) || die "Can not close DIR  $!\n";

##############################
#MAIN LOOP for each hostname
##############################
foreach my $hostname (@sites) {

###############################
# Run WEBALIZER if stats dir and logfile exist
###############################
  if ( (-e "$LOG_OUTPUT/$hostname") && (-e "$LOG_DIR/$hostname-access_logs") ) {
     system ("$WEBALIZER -u *.gif -u *.jpeg -u *.jpg -C 10 -R 10 -S 10 -N 10
	-D /tmp/webalizer.cache -o $LOG_OUTPUT/$hostname 
	-p -n $hostname $LOG_DIR/$hostname-access_logs");
  } #End if sanity check

###############################
#Check date if sunday at 1am lets roll the logs
###############################
  if ( ( $wday == 0 ) && (-e "$LOG_DIR/$hostname-access_logs") && (-e "$ARCHIVE_DIR") ) {
	#Copy LogFile to archive dir and timestamp it
    copy ("$LOG_DIR/$hostname-access_logs", "$ARCHIVE_DIR/$hostname-$timestamp") 
	|| die "Can't copy $hostname logs $!\n";
    #Then throw a destructive open at the log
    open (NEWFILE, ">$LOG_DIR/$hostname-access_logs")
	 || die "Can't open $hostname logfile to destroy $!\n";
    close (NEWFILE);
  }

} #End MAIN LOOP for $hostname

exit;