You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Jim Jagielski <ji...@jaguNET.com> on 1995/12/15 21:26:22 UTC

New httpd_monitor

I'd like to submit a patched version of httpd_monitor for inclusion
in the ./support area.

This version is a bit more generic, as well as slightly more streamlined.
It also supports the patch for scoreboard (i.e. the "Starting" status)
as well as fixing some, at least what looked to me like, bugs.

This is the full source, since it's small enough that a patch isn't
needed:

------------------>8 cut here START OF FILE---------------------
#!/usr/local/bin/perl

# ====================================================================
# Copyright (c) 1995 The Apache Group.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer. 
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. All advertising materials mentioning features or use of this
#    software must display the following acknowledgment:
#    "This product includes software developed by the Apache Group
#    for use in the Apache HTTP server project (http://www.apache.org/)."
#
# 4. The names "Apache Server" and "Apache Group" must not be used to
#    endorse or promote products derived from this software without
#    prior written permission.
#
# 5. Redistributions of any form whatsoever must retain the following
#    acknowledgment:
#    "This product includes software developed by the Apache Group
#    for use in the Apache HTTP server project (http://www.apache.org/)."
#
# THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
# IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
# ====================================================================
#
# This software consists of voluntary contributions made by many
# individuals on behalf of the Apache Group and was originally based
# on public domain software written at the National Center for
# Supercomputing Applications, University of Illinois, Urbana-Champaign.
# For more information on the Apache Group and the Apache HTTP server
# project, please see <http://www.apache.org/>.


# simple script to monitor the child Apache processes
#   Usage:
#      httpd_monitor N     
#                Will give you an update ever N seconds
#                If you choose 0, it might chew up lots of CPU time.
#
# Output explanation..
#
#  s = sleeping but "ready to go" child
#  R = active child
#  _ = dead child (no longer needed)
#  t = just starting
#
# Version History:
#  v1.0
#    Original release
#
#  v1.1
#    Modifications and "fixes":
#     o According to scoreboard.h, struct for short_score is:
#        { pid_t; char; }.
#       Now generally, pid_t is an int but some systems may have it
#       as a short. Also, we don't know whether we need to worry about
#       alignment before and after the read of the char. I'm sure we can
#       get Perl to figure this all out for us, but we'll take the lazy
#       approach and just make changable options. By default, we assume
#       we read 4 bytes (for the PID), skip 0 bytes, read 1 byte (for the
#       status) and then skip 1 byte for alignment.
#
#     o Added Status=3 condition (scoreboard patch) and folded it into
#       an easy-to-use list.
#
#     o Changed seek() from EOF (2) to SOF (0).
#

$PID_FILE = "/usr/local/httpd/logs/httpd.pid";
$SCOREB =   "/tmp/htstatus";
$MAX_PROC = 40;    # we never have (or are interested in) more than 40 children
@kidstat = ("_", "s", "R", "t");

#
# Here are those alignment parameters mentioned above:
$pid_size = 4;
$before_stat = 0;
$after_stat = 1;

#########################################################################
select STDOUT; $| = 1;

$delay = @ARGV[0];

if ($delay =~ /\-h/i) {
  print STDERR "usage:\n$0 [i]\n  i = interval in seconds (defaults to 1)\n";
  exit(0);
}

$delay = 1 unless $delay =~ /^[0-9]+$/;

open (P, "$PID_FILE") || die "Unable to open $PID_FILE";
$PID = <P>;
$ext = sprintf("a%05d", $PID);
close(P);
$SCOREB = "$SCOREB.$ext";

open (SB, $SCOREB) || die "Unable to open scoreboard file $SCOREB";

$last_len = 0;
while (1) {

  if ( ($last_mod = (stat("$SCOREB"))[9]) != $before) {
    open (SB, $SCOREB) || die "Unable to open scoreboard file $SCOREB";
    seek(SB, 0 , 0);
  
    $len = 0; $pad =""; $running = 0; $dead = 0;  $total=0;
    for ( $child=1; $child<=$MAX_PROC; $child++) {

       read(SB, $p, $pid_size);		# get to PID, which is 4 bytes (?)
       $p = unpack("I", $p);		# and get the int it refers to

       read(SB, $junk, $before_stat);	# alignment ??

       read(SB, $status, 1);		# next byte = status
       $status = unpack("C", $status);
     
       read(SB, $junk, $after_stat);	# skip to next entry
          
       $c = $kidstat[$status];
       
       if ($p != 0 && $p != $PID) {
         $total++;
         if ($c eq "R") {
            ++$running;
         }
         $printed .= "$pad$c";
         
         $pad = ""; 
         $dead = 0;
       } 
    }
    $printed .= " ($running/$total)";
    $len = length($printed);  
    
    if ($last_len > $len) {
       print "\010"x$last_len ;
       print " "x$last_len ;
       print "\010"x$last_len ;
    } else {
       print "\010"x$len ;
    }
    print $printed; $printed = "";

    $before = $last_mod;
    close(SB);
    $last_len = $len;
   }
   sleep($delay) if $delay >0;
}
------------------>8 cut here  END  OF FILE---------------------
-- 
Jim Jagielski  << jim@jaguNET.com >>   |           "Wind the frog!"
  **  jaguNET Access Services  **      |       - Woody (from Toy Story)
++       Email: info@jaguNET.com      +++        Voice:  410-931-3157       ++
++       http://www.jaguNET.com/      +++         Data: 410-931-7060        ++