From a3c6807ffd3f96a7adf22c13df7d38bda047055e Mon Sep 17 00:00:00 2001 From: Darold Gilles Date: Mon, 27 May 2013 22:27:18 +0200 Subject: [PATCH] Add -b | --build_date command line option to limit rebuilt to the given date, the format of its value can be yyyy-mm-dd, yyyy-mm or yyyy. This should be used to prevent rebuilding all html file from all data file. --- README | 31 +++++++++++++---------- SquidAnalyzer.pm | 57 +++++++++++++++++++++++++++++++------------ doc/SquidAnalyzer.pod | 31 +++++++++++++---------- doc/squidanalyzer.3 | 30 +++++++++++++---------- squid-analyzer | 22 ++++++++++++++--- 5 files changed, 113 insertions(+), 58 deletions(-) diff --git a/README b/README index 9bb4197..654d130 100644 --- a/README +++ b/README @@ -131,22 +131,27 @@ USAGE SquidAnalyzer can be run manually or by cron job using the squid-analyzer Perl script. Here are authorized usage: - squid-analyzer [ -c squidanalyzer.conf ] [-l logfile] + Usage: squid-analyzer [ -c squidanalyzer.conf ] [-l logfile] - -c | --configfile filename : path to the SquidAnalyzer configuration file. - By default: /etc/squidanalyzer.conf - -d | --debug : show debug informations. - -h | --help : show this message and exit. - -l | --logfile filename : path to the Squid logfile to parse. - By default: /var/log/squid/access.log - -p | --preserve number : used to set the statistic obsolescence in - number of month. Older stats will be removed. - -r | --rebuild : use this option to rebuild all html and graphs - output from all data files. - -v | version : show version and exit. + -c | --configfile filename : path to the SquidAnalyzer configuration file. + By default: /etc/squidanalyzer.conf + -b | --build_date date : set the day to be rebuilt, format: yyyy-mm-dd, + yyyy-mm or yyyy. Used with -r or --rebuild. + -d | --debug : show debug informations. + -h | --help : show this message and exit. + -l | --logfile filename : path to the Squid logfile to parse. + By default: /var/log/squid/access.log + -p | --preserve number : used to set the statistic obsolescence in + number of month. Older stats will be removed. + -r | --rebuild : use this option to rebuild all html and graphs + output from all data files. + -v | version : show version and exit. There is special options like --rebuild that force SquidAnalyzer to - rebuild all HTML reports, useful after an new feature or a bug fix. + rebuild all HTML reports, useful after an new feature or a bug fix. If + you want to limit the rebuild to a single day, a single month or year, + you can use the --build_date option by specifying the date part to + rebuild, format: yyyy-mm-dd, yyyy-mm or yyyy. The --preserve option should be used if you want to rotate your statistics and data. The value is the number of months to keep, older diff --git a/SquidAnalyzer.pm b/SquidAnalyzer.pm index 7d4b062..79f3263 100644 --- a/SquidAnalyzer.pm +++ b/SquidAnalyzer.pm @@ -16,7 +16,7 @@ use strict; # make things properly BEGIN { use Exporter(); use vars qw($VERSION $COPYRIGHT $AUTHOR @ISA @EXPORT $ZCAT_PROG $BZCAT_PROG $RM_PROG); - use POSIX; + use POSIX qw/ strftime /; use IO::File; # Set all internal variable @@ -127,19 +127,21 @@ my %Translate = ( 'Click_year_stat' => 'Click on year\'s statistics link for details', 'Mime_graph_hits_title' => 'Mime Type Hits Statistics on', 'Mime_graph_bytes_title' => 'Mime Type Bytes Statistiques on', + 'User' => 'User', + 'Count' => 'Count', ); sub new { - my ($class, $conf_file, $log_file, $debug) = @_; + my ($class, $conf_file, $log_file, $debug, $rebuild) = @_; # Construct the class my $self = {}; bless $self, $class; # Initialize all variables - $self->_init($conf_file, $log_file, $debug); + $self->_init($conf_file, $log_file, $debug, $rebuild); # Return the instance return($self); @@ -184,6 +186,7 @@ sub parseFile my $line_count = 0; my $line_processed_count = 0; my $line_stored_count = 0; + # Read and parse each line of the access log file while ($line = <$logfile>) { chomp($line); @@ -382,7 +385,7 @@ sub _clear_stats sub _init { - my ($self, $conf_file, $log_file, $debug) = @_; + my ($self, $conf_file, $log_file, $debug, $rebuild) = @_; # Prevent for a call without instance if (!ref($self)) { @@ -398,7 +401,7 @@ sub _init $conf_file = 'squidanalyzer.conf'; } } - my %options = &parse_config($conf_file, $log_file); + my %options = &parse_config($conf_file, $log_file, $rebuild); # Use squid log file given as command line parameter $options{LogFile} = $log_file if ($log_file); @@ -432,13 +435,13 @@ sub _init die "ERROR: 'Output' configuration option must be set.\n"; } if (! -d $self->{Output}) { - die "ERROR: 'Output' dorectory $self->{Output} doesn't exists.\n"; + die "ERROR: 'Output' directory $self->{Output} doesn't exists.\n"; } $self->{LogFile} = $options{LogFile} || '/var/log/squid/access.log'; if (!$self->{LogFile}) { die "ERROR: 'LogFile' configuration option must be set.\n"; } - if (! -e $self->{LogFile}) { + if (!$rebuild && ! -e $self->{LogFile}) { die "ERROR: 'LogFile' $self->{LogFile} doesn't exists.\n"; } $self->{OrderUser} = lc($options{OrderUser}) || 'bytes'; @@ -507,7 +510,7 @@ sub _init } # Get the last parsing date for incremental parsing - if (-e "$self->{Output}/SquidAnalyzer.current") { + if (!$rebuild && -e "$self->{Output}/SquidAnalyzer.current") { my $current = new IO::File; unless($current->open("$self->{Output}/SquidAnalyzer.current")) { print STDERR "ERROR: Can't read file $self->{Output}/SquidAnalyzer.current, $!\n" if (!$self->{QuietMode}); @@ -711,7 +714,7 @@ sub _save_stat my $path = join('/', $year, $month, $day); $path =~ s/[\/]+$//; - #### Load history + #### Load history if we are not rebuilding a particular day if ($type eq 'day') { foreach my $d ("01" .. "31") { $self->_read_stat($year, $month, $d, 'day'); @@ -1167,6 +1170,25 @@ sub _print_footer } +sub check_build_date +{ + my ($self, $year, $month, $day) = @_; + + return 0 if (!$self->{build_date}); + + my ($y, $m, $d) = split(/\-/, $self->{build_date}); + + return 1 if ($year ne $y); + if ($m) { + return 1 if ($month && ($month ne $m)); + if ($d) { + return 1 if ($day && ($day ne $d)); + } + } + + return 0; +} + sub buildHTML { my ($self, $outdir) = @_; @@ -1204,6 +1226,7 @@ sub buildHTML closedir DIR; foreach my $y (sort {$a <=> $b} @years) { next if (!$y); + next if ($self->check_build_date($y)); # Remove the full year repository if it is older that the last date to preserve if ($p_year && ($y < $p_year)) { print STDERR "Removing obsolete statistics for year $y\n" if (!$self->{QuietMode}); @@ -1216,6 +1239,7 @@ sub buildHTML closedir DIR; foreach my $m (sort {$a <=> $b} @months) { next if (!$m); + next if ($self->check_build_date($y, $m)); # Remove the full month repository if it is older that the last date to preserve if ($p_year && ("$y$m" < "$p_year$p_month")) { print STDERR "Removing obsolete statistics for month $y-$m\n" if (!$self->{QuietMode}); @@ -1227,6 +1251,7 @@ sub buildHTML my @days = grep { /^\d{2}$/ && -d "$outdir/$y/$m/$_"} readdir(DIR); closedir DIR; foreach my $d (sort {$a <=> $b} @days) { + next if ($self->check_build_date($y, $m, $d)); next if ("$y$m$d" < "$old_year$old_month$old_day"); print STDERR "Generating daily statistics for day $y-$m-$d\n" if (!$self->{QuietMode}); $self->gen_html_output($outdir, $y, $m, $d); @@ -2479,7 +2504,7 @@ sub _print_top_url_stat if (exists $url_stat{$u}{users}) { print $out qq{