i have an apache webserver with a number of virtual hosts. The logfiles for each virtual host are rotated by cronolog daily. This is a problem when i want to see the webhits in a cacti graph because there are many files and the filenames are different from day to day.
The following script takes a logfile-pattern (the same as defined in cronolog) and returns the number of hits in all logfiles that match this pattern.
Code: Select all
#!/usr/bin/perl -w
#
# webhits_rot.pl - count apache webhits on cronolog-rotated logfiles
# Version 0.2 - by Achim Schrepfer <a.schrepfer@oncom.de>
#
# Changelog:
# 24.04.03 (as): - removed the counterfile as it is not needed for use with rrdtool/cacti
#
# Note:
# The only disadvantage of this script is the fact, that the counter jumps to zero at midnight or
# whenever the logfiles are rotated. Rrdtool has an option to set a maximum value for the counter so
# it can correct the error when the counter jumps to zero (i.e. the delta is negative). The problem is, that
# there is *no* maximum value for the webhits counter.
use strict;
# check commandline-arguments
if (!$ARGV[0]) { exit 1; }
my $logfile_pattern = $ARGV[0];
# preparings
my ($day, $month, $year) = (localtime)[3,4,5];
$month ++;
$year += 1900;
$day = sprintf '%02d', $day; # 2-digit zero-leading day (%d)
$month = sprintf '%02d', $month; # 2-digit zero-leading month (%m)
$year = sprintf '%04d', $year; # 4-digit year (%Y)
# replace placeholders with time-informations
$logfile_pattern =~ s/%d/$day/go;
$logfile_pattern =~ s/%m/$month/go;
$logfile_pattern =~ s/%Y/$year/go;
# count webhits for logfiles that match the expanded $logfile_pattern
my $todays_hits = `cat $logfile_pattern | wc -l`;
$todays_hits =~ s/^.*?(\d+).*$/$1/g;
chomp($todays_hits);
# printout todays_hits
print $todays_hits;
In cacti i call the script like this to gather the data:
Code: Select all
./webhits_rot.pl /var/log/apache/cronolog/%Y/%m/%Y%m%d-*-access_log
moojoo[/code]