From 572b8b82355ad2cafbe05dfc0a96a3bbeed44028 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 25 Nov 2016 17:31:44 +0100 Subject: [PATCH] http-log --- bin/http-log | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 bin/http-log diff --git a/bin/http-log b/bin/http-log new file mode 100755 index 00000000..eafc4f01 --- /dev/null +++ b/bin/http-log @@ -0,0 +1,89 @@ +#!/usr/bin/perl -w +################################################################################# +# +# NB 25.11.16 +# +################################################################################# +use strict; + +#-anF' +' +use strict; + +use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); +my %Opt; &GetOptions(\%Opt , + 'help|h|?!','man!','version|V!', + 'verbose|v+','debug+', + 'tail!' +) || exit -1; + +sub help { die "Usage: $0 -tail /log\n" } +&help() if $Opt{'help'} or $Opt{'man'}; + +sub version { die 'TODO' } +&version() if $Opt{'version'}; + +my $start = time(); +$|=1; + +@ARGV = ('/var/log/apache2/access.log') unless @ARGV; + +if ($Opt{tail}) { + + open (LOG,'tail -f '.join(' ',@ARGV).'|') or die $!; + +} else { + + open (LOG,'cat '.join(' ',@ARGV).'|') or die $!; + +} + +my $tot = 0; +my $ok = 0; +my %stat; + +while () { + + /" (\d+) \d+ "/ or next; + + $tot++; + + chomp($_); + + my $status = $1; + $ok++ if $status < 400; + $stat{$status}++; + + my $now = time(); + my $ellapse = $now - $start; + #my $procs = `pgrep -fl /apache|wc -l`; chomp($procs); + + next if !$Opt{tail} or $tot%10!=0; + + my $process = `pgrep -fl 'apache|http'|wc -l`; chomp($process); + + #print "Apache: $tot queries in ok=$ok\n"; + print "Http: ".join(" | ",( + #"status=$status", + "queries=$tot", + #"procs=$procs", + "secs=".($ellapse), + "queries/sec=".int($tot/($ellapse||1)), + "200_OK=".sprintf("%.2f",($ok/$tot)*100)."%", + "process=$process", + #." $ok/$tot" + #." - $_" + ))."\n"; + +} + +close LOG; + +#exit 0 if $Opt{tail}; + +print + #"200_OK=".sprintf("%.2f",($ok/$tot)*100)."%\n", +map { + "$_ : ".sprintf("%5s",sprintf("%.2f",($stat{$_}/$tot)*100))."% : ".$stat{$_}."\n" +} sort {$stat{$a} <=> $stat{$b}} keys(%stat); + +exit 0; -- 2.47.3