Amass

Using Perl and Logrotate to update stats and logs

March 26th, 2008 Luke

I recently moved girasquid.com, and all of my other sites, to my own Linux server - which I built myself.

It’s been a learning experience.

One of the things that I’ve been learning about is awstats, which is my stats utility of choice. In order to keep my log files from becoming too large, I’ve also turned on logrotate, so that it runs at 3:30 AM every day.

The only problem with this is that I’m running using virtualhosts - which means that I either need to create awstats configuration files for each specific virtualhost, and I need to create logrotate rules for each virtualhost’s log files.

They’re all in the same location, though. So I thought to myself: “hey! Why not get Perl to handle updating awstats for me? Then I can just write one logrotate rule for all of my virtualhosts!”. And so I did. This is what the logrotate rule looks like:

/home/*/logs/*.log { notifempty daily rotate 7 compress sharedscripts prerotate /usr/share/utils/updatestats.pl endscript postrotate /etc/init.d/apache2 reload endscript }

Which will nicely rotate both the access.log and error.log files for me(I haven’t quite worked out how to do per-virtualhost FTP logging just yet). The code inside /usr/share/utils/updatestats.pl is:

#!/usr/bin/perl -w use strict; opendir(SITES,'/etc/apache2/sites-enabled'); while(defined(my $site = readdir(SITES))) { next if $site =~ /^\.\.?$/; system("/usr/lib/cgi-bin/awstats.pl -update -config=$site"); } closedir(SITES);

As you can see, I basically loop through all of the currently enabled virtualhosts, and pass their names to awstats so that it will update them. This does require that your virtualhosts be named the same thing in either case(for example, the Apache virtualhost for this site is girasquid.com, and the awstats virtualhost is also girasquid.com), but as long as that’s true then it all works beautifully.