]> git.nbdom.net Git - nb.git/commitdiff
http-log
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 Nov 2016 16:31:44 +0000 (17:31 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 Nov 2016 16:31:44 +0000 (17:31 +0100)
bin/http-log [new file with mode: 0755]

diff --git a/bin/http-log b/bin/http-log
new file mode 100755 (executable)
index 0000000..eafc4f0
--- /dev/null
@@ -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 (<LOG>) {
+
+       /" (\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;