#!/usr/bin/perl -w use strict; use Getopt::Long; use Time::Local; use Time::localtime; my $file="xaa"; my %increment = (); my $interval = 900; #An interval of 15 minutes my %MonthToNumber = (); @MonthToNumber{qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = map { sprintf "%02d", $_; } (0..11); count_recs(); sub count_recs { my ($Day, $Month, $Year, $Hour, $Minute, $Second); my ($datetime, $get_post); my $dmytime; my $dateproc; my @dates = (); my @dates_mod = (); open (INFILE, "<$file") || die "Cannot read from $file"; while () { ($datetime,$get_post) = (split / /) [3,6]; $datetime =~ s/\[//; ($Day,$Month,$Year,$Hour,$Minute,$Second)= $datetime =~m#^(\d\d)/(\w\w\w)/(\d\d\d\d):(\d\d):(\d\d):(\d\d)#; next if ($get_post =~ /\.js$/ || $get_post =~ /\.gif$/ || $get_post =~ /\.css$/ || ($Minute%15) != 00); push (@dates, $datetime); } #outer while # Calculate the time in epoch seconds in the subroutine convert_epoch. foreach $dmytime (@dates){ my $calc_seconds= &convert_epoch($dmytime); push (@dates_mod, $calc_seconds); } foreach $dateproc (@dates_mod) { $increment{$dateproc}++; } # Print the headings in the output file open (OUTFILE, ">>access_log.out") || die "Cannot write to $file"; print OUTFILE " Date Range of Time Visual Pages per second(VPPS)\n"; print OUTFILE " Avg Max Min\n"; print OUTFILE "------------ ----------------- ------------\n"; &calculate_time(); print OUTFILE "\n\n\n"; $interval = 3600; &calculate_time(); close(OUTFILE); close(INFILE); } sub convert_epoch { my $localdate = shift; my ($epoch_Day,$epoch_Month,$epoch_Year,$epoch_Hour,$epoch_Minute,$epoch_Second)= $localdate =~m#^(\d\d)/(\w\w\w)/(\d\d\d\d):(\d\d):(\d\d):(\d\d)#; my $epoch_seconds = timelocal($epoch_Second, $epoch_Minute, $epoch_Hour, $epoch_Day, $MonthToNumber{$epoch_Month}, $epoch_Year-1900); return $epoch_seconds; } sub calculate_time { my $first = 0; my $start_time; my $end_time; my $time; my ($max, $min, $avg, $count); my $partial=0; my $col_ind; my $processed; my $tm; foreach $processed ( sort keys %increment ) { if ($first == 0 ) { $start_time = $processed; $end_time = $start_time + $interval; $time = $start_time; $min = $increment{$processed}; $max = 0; $avg = 0; $first++; } if ($processed < $end_time) { if ($increment{$processed} < $min) { $min = $increment{$processed}; } if ($increment{$processed} > $max) { $max = $increment{$processed}; } $avg+= $increment{$processed}; $count++; $partial = 1; }else { $avg=$avg/$count; my $tm = localtime($end_time); my $start = localtime($start_time); if ($interval == 900) { $col_ind = 'P'; }else { $col_ind = 'H'; } printf OUTFILE "%01s %02d/%02d/%04d %02d:%02d:00-%02d:%02d:00 %03.2f %03d %03d\n", $col_ind, ($tm->mon)+1,$tm->mday, $tm->year+1900,$start->hour, $start->min, $tm->hour,$tm->min, $avg, $max, $min; $min = $increment{$processed}; $max = $increment{$processed}; $avg = $increment{$processed}; $count = 1; $start_time = $processed; $time = $start_time; $end_time = $start_time + $interval; $partial = 0; } } if ( $partial == 1 ) { $avg = $avg/$count; $tm = localtime($end_time); my $start = localtime($start_time); printf OUTFILE "%01s %02d/%02d/%04d %02d:%02d:00-%02d:%02d:00 %03.2f %03d %03d\n", $col_ind, ($tm->mon)+1,$tm->mday, $tm->year+1900,$start->hour, $start->min, $tm->hour,$tm->min, $avg, $max, $min; } }