#!/usr/bin/perl # ------------------------------------------------------------------------------ # "THE BEER-WARE LICENSE" (Revision 42): # wrote this file. As long as you retain this notice you # can do whatever you want with this stuff. If we meet some day, and you think # this stuff is worth it, you can buy me a beer in return. Anders Giversen # ------------------------------------------------------------------------------ use strict; use File::Copy; my $DIRCONFIG = '/etc/awstats'; my $Debug = 0; my $wwwroot = '/var/www'; my $folder = 'web/awstats'; my $indexfile = ''; my $AwstatsProg = '/pack/awstats/awstats_buildstaticpages.pl'; my $Awstats = '/pack/awstats/awstats.pl'; use vars qw($dataDir $outputdir); sub debug { if ($Debug >= 1) { my $debugstring = $_[0]; print localtime(time) . " - DEBUG $Debug - $debugstring\n"; } } sub error { print STDERR "Error: $_[0]\n"; exit 1; } sub parse_conf { my $CONF = $_[0]; my $configFile = $_[1]; while (<$CONF>) { chomp $_; s/\r//; # Empty lines if ($_ =~ /^\s*$/) { next; } # Includes if ($_ =~ /^Include \"([^\"]+)\"/ || $_ =~ /^#include \"([^\"]+)\"/) { my $includeFile = $1; if ($includeFile !~ /^[\\\/]/) { if ($configFile =~ /^(.*[\\\/])[^\\\/]*$/) { $includeFile = "$1$includeFile"; } } debug("Include $includeFile"); open(CONFIG, $includeFile) || error("Unable to open included configuration file $includeFile - $!"); &parse_conf(*CONFIG, $includeFile); close(CONFIG); } # Output directory if ($_ =~ /^#output \"([^\"]+)\"/) { $outputdir = $1; } # Comments if ($_ =~ /^\s*#/) { next; } $_ =~ s/\s#.*$//; my ($param, $value) = split(/=/, $_, 2); $param =~ s/^\s+//; $param =~ s/\s+$//; if (! $param) { next; } if (! defined $value) { next; } if ($value) { $value =~ s/^\s+//; $value =~ s/\s+$//; $value =~ s/^\"//; $value =~ s/\"$//; } if ($param =~ /^DirData/) { $dataDir = $value; } } } if (! @ARGV) { print "$0: Use $0 domain or 'all' for all hosted domains\n"; exit 1; } my @filesindir; if ($ARGV[0] eq 'all') { debug("Scan directory $DIRCONFIG"); # Scan directory $DIRCONFIG opendir(DIR, $DIRCONFIG) || error("Can't scan directory $DIRCONFIG"); @filesindir = grep { /^awstats\.(.+)\.conf$/ } sort readdir(DIR); closedir(DIR); debug("List of files found: " . join(", ", @filesindir)); } else { @filesindir = (); for (0..@ARGV-1) { push @filesindir, "awstats." . $ARGV[$_] . ".conf"; } } # Build file list my @files=(); foreach my $file (@filesindir) { if ($file =~ /^awstats\.(.+)\.conf$/) { my $conf=$1; $conf =~ s/\.$//; if ($conf eq 'model') { next; } } push @files, $file; } debug("List of files qualified: " . join(", ", @files)); if (@files) { # Check $Awstats and $AwstatsProg my $found = 0; if (-s $Awstats && -s $AwstatsProg) { $found = 1; } if (! $found) { error("awstats.pl and/or awstats_buildstaticpages.pl wasn't found"); } foreach (@files) { if ($_ =~ /^awstats\.(.+)\.conf$/) { my $domain = $1||"default"; $domain =~ s/\.$//; my $conf = "$DIRCONFIG/$_"; $outputdir = ""; $dataDir = ""; debug("Parsing configuration file $conf"); open(CONF, $conf) || error("Unable to open configuration file $conf - $!"); &parse_conf(*CONF, $conf); close(CONF); if ($dataDir eq "") { error("No DirData found"); } debug("DirData: $dataDir"); if ($outputdir eq "") { $outputdir = "$wwwroot/$domain/$folder"; } if ($outputdir !~ /[\\\/]$/) { $outputdir .= "/"; } debug("output: $outputdir"); my $command = "$Awstats -config=$domain -configdir=$DIRCONFIG -update"; debug("Running '$command' to update stats for $domain"); my $output = `$command 2>&1`; if ($Debug >= 2) { print $output; } my %periods = (); opendir(DIR, $dataDir) || error("Can't scan directory $dataDir - $!"); my @dataFiles = grep { /^awstats(\d+)\.$domain\.txt$/ } sort readdir(DIR); closedir(DIR); foreach my $dataFile (@dataFiles) { if ($dataFile =~ /^awstats(\d{2})(\d{4})\.$domain\.txt$/) { if (! $periods{"$2-$1"}) { $periods{"$2-$1"} = 1; } if (! $periods{"$2-all"}) { $periods{"$2-all"} = 1; } } } debug("Periods: " . join(", ", sort keys %periods)); # Create directory if it doesn't exist if (! -d $outputdir) { my $res = mkdir($outputdir, 0755); if (! $res) { error("Can't create directory $outputdir"); } } # indexfile if ($indexfile ne '') { my $tmp = $indexfile; $tmp =~ s/^.*[\\\/]([^\\\/]*)$/$1/; $tmp = "$outputdir$tmp"; if (! -s $tmp) { copy($indexfile, $tmp); } } # Generate stats foreach my $period (sort keys %periods) { my ($year, $month) = split(/-/, $period, 2); my $command = "$AwstatsProg -awstatsprog=$Awstats -config=$domain -configdir=$DIRCONFIG"; $command .= " -dir=$outputdir -month=$month -year=$year"; debug("Running '$command' to update stats for $domain"); my $output = `$command 2>&1`; if ($Debug >= 2) { print $output; } } } } } else { print "No AWStats config file found in $DIRCONFIG\n"; } 0;