logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



Server Status Logging

 
Post new topic   Reply to topic    Apache Forum Index -> Other Software



View previous topic :: View next topic  
Author Message
niroque



Joined: 07 Jul 2016
Posts: 1

PostPosted: Thu 07 Jul '16 21:09    Post subject: Server Status Logging Reply with quote

Hi all!

I came across Mark J Cox's Log Server Status script in my effort to find a way to get a log file friendly output of Apache's server-status, however it only output the server-status?auto information and I needed all the gritty details for debugging purposes (not just for kicks). So I made a script that grabs the information from that page either remotely or from a local apache instance. After making a cron job for it to run every 15 minutes I thought that someone else might find it useful. Of course if anyone wants to make it better, go nuts! Here it is:

Code:

#!/usr/bin/perl
#
#  This script is licensed under the GNU GPL.
#  For more information: https://www.gnu.org/licenses/gpl.html
#
#    This script takes the HTML output of Apache's server-status page, not
# server-status?auto,  and formats it into readable log format.  NOTE: This
# script assumes Apache's server-status is already enabled.  This script can
# work on remote servers as long as the script is running on a system with
# access to the server's server-status.
#
#    Log file naming format: By default it is named "activity.log", if you don't
# have logrotate enabled then switch it to the datestamp format (see below).
#
#    This script was partially adapted from Mark J Cox's log_server_status.
# Though there is very little remaining of the original script, I will still
# mention thanks and credit for getting me on the right track:
# (https://svn.apache.org/repos/asf/httpd/httpd/trunk/support/log_server_status.in)
#
#                                                      (Liz Pringi Jul-7-2016)
#
use strict;
use warnings;

use Scalar::Util qw(looks_like_number);

my $wherelog = "/var/log/server-status/";
my $server   = "localhost";        # Name of server, could be "www.foo.com"
my $port     = "80";               # Port on server
my $request = "/server-status";    # Request to send

my $datestring = localtime();

my @ltime = localtime(time);

my $day =
    $ltime[5] + 1900
  . sprintf( "%02d", $ltime[4] + 1 )
  . sprintf( "%02d", $ltime[3] );

# Chose your logfile naming format.  Suggest the second one if logrotate is not active.
open(OUT,">>$wherelog"."activity.log");
#open(OUT,">>$wherelog$day");

system("wget $server:$port$request -q -O /tmp/server-status");
open(INPUTFILE, "/tmp/server-status") || die "Error: $!\n";
my @lines = <INPUTFILE>;

print OUT "------------ [$datestring] -----------\n";

my ( $childnum, $lastchild, $inc, $pid, $accesses, $mode, $cpusecs, $ss, $reqms, $client, $vhost, $req );
$childnum=0;
$lastchild=100;
$pid="-";
for ($inc=35; $lines[$inc+3]; $inc++) {
  $childnum = $1 if $lines[$inc] =~ (/^<tr><td><b>(\S+)<\Sb><\Std><td>\S+<\Std><td>\S+<\Std><td>\S+$/);
  if($childnum ne $lastchild){
  $pid      = $1 if $lines[$inc] =~ (/^<tr><td><b>\S+-\d+<\Sb><\Std><td>(\S+)<\Std><td>\S+<\Std><td>\S+$/);
  $accesses = $1 if $lines[$inc] =~ (/^<tr><td><b>\S+-\d+<\Sb><\Std><td>\S+<\Std><td>(\S+)<\Std><td>\S+$/);
  $mode     = $1 if $lines[$inc] =~ (/^<tr><td><b>\S+-\d+<\Sb><\Std><td>\S+<\Std><td>\S+<\Std><td>(\S+)$/);
  $mode     = $1 if $lines[$inc] =~ (/^<tr><td><b>\S+-\d+<\Sb><\Std><td>\S+<\Std><td>\S+<\Std><td><b>(\S+)<\/b>$/);
  $cpusecs  = $1 if $lines[$inc+1] =~ (/^<\Std><td>(\S+)<\Std><td>\S+<\Std><td>\S+<\Std><td>\S+<\Std><td>\S+<\Std><td>\S+/);
  $ss       = $1 if $lines[$inc+1] =~ (/^<\Std><td>\S+<\Std><td>(\S+)<\Std><td>\S+<\Std><td>\S+<\Std><td>\S+<\Std><td>\S+/);
  $reqms    = $1 if $lines[$inc+1] =~ (/^<\Std><td>\S+<\Std><td>\S+<\Std><td>(\S+)<\Std><td>\S+<\Std><td>\S+<\Std><td>\S+/);
  $client   = $1 if $lines[$inc+2] =~ (/^<\S+td><td>(\S+)<\Std><td nowrap>\S+<\Std><td nowrap>\S+/);
  $vhost    = $1 if $lines[$inc+2] =~ (/^<\Std><td>\S+<\Std><td nowrap>(\S+)<\Std><td nowrap>\S+/);
  $req      = $1 if $lines[$inc+2] =~ (/<\/td><td>\S+<\/td><td nowrap>\S+<\/td><td nowrap>(.*)<\/td><\/tr>/);
  }
  if($childnum ne $lastchild && looks_like_number($pid)){
    print OUT "$childnum  [PID: $pid] [Accesses this conn/child/slot: $accesses] [Mode: $mode] [CPU Time: $cpusecs] [Seconds since: $ss] [MS required: $reqms] [Client: $client] [Vhost: $vhost] [Request: $req]\n";
    $client="-";
    $pid="-";
    $req="-";
    $lastchild = $childnum;
  }
}

print OUT "---------------------------------------------------\n";
close INPUTFILE;
close OUT;
Back to top


Post new topic   Reply to topic    Apache Forum Index -> Other Software
Page 1 of 1