From 1e609e8b5cbef2759307df869a0ec2837b9d7f1c Mon Sep 17 00:00:00 2001 From: Darold Gilles Date: Wed, 29 May 2013 01:03:58 +0200 Subject: [PATCH] Add --no-year-stat to disable year statistics, reports will start at month level. --- SquidAnalyzer.pm | 144 ++++++++++++++++++++++++++++++++++++++--------- squid-analyzer | 4 ++ 2 files changed, 120 insertions(+), 28 deletions(-) diff --git a/SquidAnalyzer.pm b/SquidAnalyzer.pm index 61d88b0..194d2f2 100644 --- a/SquidAnalyzer.pm +++ b/SquidAnalyzer.pm @@ -356,12 +356,14 @@ sub parseFile } # Compute year statistics - if (!$self->{QuietMode}) { - print STDERR "Compute and dump year statistics for $self->{first_year} to $self->{last_year}\n"; - } - for my $year ($self->{first_year} .. $self->{last_year}) { - if (-d "$self->{Output}/$year") { - $self->_save_data($year); + if (!$self->{no_year_stat}) { + if (!$self->{QuietMode}) { + print STDERR "Compute and dump year statistics for $self->{first_year} to $self->{last_year}\n"; + } + for my $year ($self->{first_year} .. $self->{last_year}) { + if (-d "$self->{Output}/$year") { + $self->_save_data($year); + } } } } @@ -446,6 +448,7 @@ sub _init $self->{Locale} = $options{Locale} || ''; $self->{WriteDelay} = $options{WriteDelay} || 3600; $self->{TopUrlUser} = $options{TopUrlUser} || 0; + $self->{no_year_stat} = 0; if ($self->{Lang}) { open(IN, "$self->{Lang}") or die "ERROR: can't open translation file $self->{Lang}, $!\n"; @@ -587,6 +590,14 @@ sub _init
  • $Translate{'Mime_link'}
  • +}; + + $self->{menu3} = qq{ + }; } @@ -1306,7 +1317,11 @@ sub buildHTML $self->gen_html_output($outdir, $y); } - $self->_gen_summary($outdir); + if (!$self->{no_year_stat}) { + $self->_gen_summary($outdir); + } else { + $self->_gen_year_summary($outdir); + } } sub gen_html_output @@ -1376,30 +1391,31 @@ sub _print_cache_stat } # Load code statistics - my $infile = new IO::File; - $infile->open("$outdir/stat_code.dat") || return; my %code_stat = (); my %detail_code_stat = (); - while (my $l = <$infile>) { - chomp($l); - my ($code, $data) = split(/\s/, $l); - $data =~ /hits_$type=([^;]+);bytes_$type=([^;]+)/; - my $hits = $1 || ''; - my $bytes = $2 || ''; - $hits =~ s/,$//; - $bytes =~ s/,$//; - my %hits_tmp = split(/[:,]/, $hits); - foreach my $tmp (sort {$a <=> $b} keys %hits_tmp) { - $detail_code_stat{$code}{$tmp}{request} = $hits_tmp{$tmp}; - $code_stat{$code}{request} += $hits_tmp{$tmp}; - } - my %bytes_tmp = split(/[:,]/, $bytes); - foreach my $tmp (sort {$a <=> $b} keys %bytes_tmp) { - $detail_code_stat{$code}{$tmp}{bytes} = $bytes_tmp{$tmp}; - $code_stat{$code}{bytes} += $bytes_tmp{$tmp}; + my $infile = new IO::File; + if ($infile->open("$outdir/stat_code.dat")) { + while (my $l = <$infile>) { + chomp($l); + my ($code, $data) = split(/\s/, $l); + $data =~ /hits_$type=([^;]+);bytes_$type=([^;]+)/; + my $hits = $1 || ''; + my $bytes = $2 || ''; + $hits =~ s/,$//; + $bytes =~ s/,$//; + my %hits_tmp = split(/[:,]/, $hits); + foreach my $tmp (sort {$a <=> $b} keys %hits_tmp) { + $detail_code_stat{$code}{$tmp}{request} = $hits_tmp{$tmp}; + $code_stat{$code}{request} += $hits_tmp{$tmp}; + } + my %bytes_tmp = split(/[:,]/, $bytes); + foreach my $tmp (sort {$a <=> $b} keys %bytes_tmp) { + $detail_code_stat{$code}{$tmp}{bytes} = $bytes_tmp{$tmp}; + $code_stat{$code}{bytes} += $bytes_tmp{$tmp}; + } } + $infile->close(); } - $infile->close(); my $total_request = $code_stat{HIT}{request} + $code_stat{MISS}{request}; my $total_bytes = $code_stat{HIT}{bytes} + $code_stat{MISS}{bytes}; @@ -1408,11 +1424,22 @@ sub _print_cache_stat $out->open(">$file") || die "ERROR: Unable to open $file. $!\n"; # Print the HTML header my $cal = $self->_get_calendar($stat_date, $type, $outdir); - $self->_print_header(\$out, $self->{menu}, $cal); + if ( $self->{no_year_stat} && ($type eq 'month') ) { + $self->_print_header(\$out, $self->{menu3}, $cal); + } else { + $self->_print_header(\$out, $self->{menu}, $cal); + } # Print title and calendar view print $out $self->_print_title($Translate{'Cache_title'}, $stat_date); + if ( $self->{no_year_stat} && ($type eq 'month') ) { + %code_stat = (); + $self->_print_footer(\$out); + $out->close(); + return; + } + my $total_cost = sprintf("%2.2f", int($total_bytes/1000000) * $self->{CostPrice}); my $comma_bytes = $self->format_bytes($total_bytes); my $hit_bytes = $self->format_bytes($code_stat{HIT}{bytes}); @@ -3471,6 +3498,67 @@ sub check_ip return 0; } +sub _gen_year_summary +{ + my ($self, $outdir) = @_; + + # Get all day subdirectory + opendir(DIR, "$outdir") or die "ERROR: Can't read directory $outdir, $!\n"; + my @dirs = grep { /^\d{4}$/ && -d "$outdir/$_" } readdir(DIR); + closedir DIR; + + my %code_stat = (); + my %total_request = (); + my %total_bytes = (); + foreach my $d (@dirs) { + $code_stat{$d} = 1; + } + my $file = $outdir . '/index.html'; + my $out = new IO::File; + $out->open(">$file") || die "ERROR: Unable to open $file. $!\n"; + # Print the HTML header + $self->_print_header(\$out); + my $colspn = 2; + $colspn = 3 if ($self->{CostPrice}); + print $out qq{ +

    $Translate{'Globals_Statistics'}

    +
    + + + + + + + +}; + foreach my $year (sort {$b <=> $a} keys %code_stat) { + print $out qq{ + + + +}; + } + print $out qq{ + +
    $Translate{'Years'}
    $Translate{'Stat_label'} $year *
    +
    (*) $Translate{'Click_year_stat'}
    + +

    $Translate{'Legend'}

    +
    +
    + $Translate{'Hit'}: $Translate{'Hit_help'}
    + $Translate{'Miss'}: $Translate{'Miss_help'}
    +}; + print $out qq{$Translate{'Cost'}: $Translate{'Cost_help'} $self->{CostPrice} $self->{Currency}
    } if ($self->{CostPrice}); + print $out qq{ +
    +}; + $self->_print_footer(\$out); + $out->close(); + +} + + 1; __END__ diff --git a/squid-analyzer b/squid-analyzer index 402e9e3..03d9c00 100644 --- a/squid-analyzer +++ b/squid-analyzer @@ -18,6 +18,7 @@ my $preserve = ''; my $debug = 0; my $version = 0; my $build_date = ''; +my $no_year_stat = 0; # get the command line parameters my $result = GetOptions ( @@ -29,6 +30,7 @@ my $result = GetOptions ( "r|rebuild!" => \$rebuild, "p|preserve=i" => \$preserve, "v|version!" => \$version, + "no-year-stat!" => \$no_year_stat, ); # Show version and exit @@ -59,6 +61,8 @@ if (!$configfile || $help) { # Instanciate SquidAnalyzer.pm perl module my $sa = new SquidAnalyzer($configfile, $logfile, $debug, $rebuild); +$sa->{no_year_stat} = $no_year_stat; + # Run parsing only if we have a log file or that we are not in rebuild mode $sa->parseFile() if (!$rebuild || $logfile);