awstats - The Static Way

Posted by Pat Tue, 24 Jan 2006 23:27:00 GMT

awstats seemed pretty nice, but I didn’t really want to run it via CGI. I’m sure there’s nothing wrong with it…but one less server side process is one less point of attack, particularly if I didn’t write the process.

awstats provides a way of building static HTML pages instead of using a cgi script to view the stats. I wrote up a quick script that will generate stats for all my sites. Make it run whenever you want via cron, and you’re all set. Here’s the code:

awstats_build_stats.rb
#!/usr/local/bin/ruby

# Define the location of awstats, and the dir to output the stats pages to
perl = '/usr/bin/perl'
awstats = '/usr/local/www/awstats'
statscmd = "#{perl} #{awstats}/tools/awstats_buildstaticpages.pl"
statsdir = '/home/pergesu/www/stats'

# Get all the config files
configs = Dir["#{awstats}/cgi-bin/awstats.*.{com,net,org}.conf"]
siteregexp = /awstats\.(\S+)\.conf$/

# Loop through all the configs
configs.each do |conf|
  res = siteregexp.match(conf)
  site = res[1]
  # Create the dir if it doesn't exist
  Dir.mkdir("#{statsdir}/#{site}") unless File.exists?("#{statsdir}/#{site}") and File.directory?("#{statsdir}/#{site}")

  # Now run the command to generate the stats
  `#{statscmd} -config=#{site} -update -awstatsprog=#{awstats}/cgi-bin/awstats.pl -dir=#{statsdir}/#{site}`
end

I throw it all in a single stats dir because I have a couple sites that all have different docroots…they’re all in the same main /home/www/pergesu folder, but some have rails project folders which throws the naming off. So I just spit the output to a central stats dir, then I manually symlinked a stats folder in each site’s docroot to its appropriate awstats folder. I also symlinked index.html to the generated main awstats page (awstats.site.com.html) so I can just go to http://www.mysite.com/stats/ and view the stats.

One limitation here is that it doesn’t let me navigate between months or years to view past stats. There may be an option in the awstats_buildstaticpages.pl script, I’m not sure. For now, this works fine, and I don’t have to run a CGI process to view my stats.

Tags ,  | 1 comment

Comments

  1. Ben Reubenstien said 29 minutes later:

    Very nice tutorial. This might be a good solution for including daily statistics in my app.

(leave url/email »)

   Preview comment