home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / httpd_mo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-21  |  8.5 KB  |  303 lines

  1. /*
  2.  * ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  
  52.  
  53.  * simple script to monitor the child Apache processes
  54.  *   Usage:
  55.  *      httpd_monitor -p pid_file -s sleep_time
  56.  *                Will give you an update ever sleep_time seconds
  57.  *                 using pid_file as the location of the PID file.
  58.  *                If you choose 0, it might chew up lots of CPU time.
  59.  *
  60.  * Output explanation..
  61.  *
  62.  *  s = sleeping but "ready to go" child
  63.  *  R = active child
  64.  *  _ = dead child (no longer needed)
  65.  *  t = just starting
  66.  *
  67.  *
  68.  *  Jim Jagielski <jim@jaguNET.com>
  69.  *   v1.0 Notes:
  70.  *    This code is much more ugly and complicated than it
  71.  *    needs to be.
  72.  *
  73.  *   v1.1:
  74.  *    Minor fixes
  75.  */
  76.  
  77. #include <stdio.h>
  78. #include <string.h>
  79. #include <sys/types.h>
  80. #include <sys/stat.h>
  81. #include "scoreboard.h"
  82. #include "httpd.h"
  83.  
  84. #define PIDFILE_OPT        "PidFile"
  85. #define    SCORE_OPT        "ScoreBoardFile"
  86. #define DEFAULT_SLEEPTIME    2
  87. #define ASIZE            1024
  88. #define MAX_PROC        40
  89.  
  90. int
  91. main(argc, argv)
  92. int argc;
  93. char **argv;
  94. {
  95.     short_score scoreboard_image;
  96.     FILE *afile;
  97.     char conf_name[ASIZE];
  98.     char pid_name[ASIZE];
  99.     char score_name[ASIZE];
  100.     char tbuf[ASIZE];
  101.     char *ptmp;
  102.     static char kid_stat[] = { '_', 's', 'R', 't' };
  103.     char achar;
  104.     long thepid;
  105.     int score_fd;
  106.     int sleep_time = DEFAULT_SLEEPTIME;
  107.     int last_len = 0;
  108.     int kiddies;
  109.     int running, dead, total, loop;
  110.     short got_config = 0;
  111.     struct stat statbuf;
  112.     time_t last_time = 0;
  113.     extern char *optarg;
  114.     extern int optind, opterr;
  115.     void lookfor();
  116.  
  117.     int usage();
  118.  
  119.     /*
  120.      * Handle the options. Using getopt() is most probably overkill,
  121.      * but let's think about the future!
  122.      */
  123.     strcpy(conf_name, HTTPD_ROOT);
  124.     while((achar = getopt(argc,argv,"s:d:f:")) != -1) {
  125.     switch(achar) {
  126.       case 'd':
  127.         strcpy(conf_name, optarg);
  128.         break;
  129.       case 'f':
  130.         strcpy(conf_name, optarg);
  131.         got_config = 1;
  132.         break;
  133.       case 's':
  134.         sleep_time = atoi(optarg);
  135.         break;
  136.       case '?':
  137.         usage(argv[0]);
  138.     }
  139.     }
  140.  
  141.     /*
  142.      * Now build the name of the httpd.conf file
  143.      */
  144.      if (!got_config) {
  145.      strcat(conf_name, "/");
  146.      strcat(conf_name, SERVER_CONFIG_FILE);
  147.     }
  148.  
  149.     /*
  150.      * Make sure we have the right file... Barf if not
  151.      */
  152.     if (!(afile = fopen(conf_name, "r"))) {
  153.     perror("httpd_monitor");
  154.     fprintf(stderr, "Can't open config file: %s\n", conf_name);
  155.     exit(1);
  156.     }
  157.     /*
  158.      * now scan thru the ConfigFile to look for the items that
  159.      * interest us
  160.      */
  161.     lookfor(pid_name, score_name, afile);
  162.     fclose(afile);
  163.  
  164.     /*
  165.      * now open the PidFile and then the ScoreBoardFile
  166.      */
  167.     if (!(afile = fopen(pid_name, "r"))) {
  168.     perror("httpd_monitor");
  169.     fprintf(stderr, "Can't open PIDfile: %s\n", pid_name);
  170.     exit(1);
  171.     }
  172.     fscanf(afile, "%ld", &thepid);
  173.     fclose(afile);
  174.  
  175.     /*
  176.      * Enough taters, time for the MEAT!
  177.      */
  178.     for(;;sleep(sleep_time)) {
  179.     if (stat(score_name, &statbuf)) {
  180.         perror("httpd_monitor");
  181.         fprintf(stderr, "Can't stat scoreboard file: %s\n", score_name);
  182.         exit(1);
  183.     }
  184.     if (last_time == statbuf.st_mtime)
  185.         continue;    /* tricky ;) */
  186.     last_time = statbuf.st_mtime;    /* for next time */
  187.     if ((score_fd = open(score_name, 0)) == -1 ) {
  188.         perror("httpd_monitor");
  189.         fprintf(stderr, "Can't open scoreboard file: %s\n", score_name);
  190.         exit(1);
  191.     }
  192.     /*
  193.      * all that for _this_
  194.      */
  195.     running = dead = total = 0;
  196.     ptmp = tbuf;
  197.     *ptmp = '\0';
  198.     for(kiddies=0;kiddies<MAX_PROC; kiddies++) {
  199.         read(score_fd, (char *)&scoreboard_image, sizeof(short_score));
  200.         achar = kid_stat[(int)scoreboard_image.status];
  201.         if (scoreboard_image.pid != 0 && scoreboard_image.pid != thepid) {
  202.         total++;
  203.         if (achar == 'R')
  204.             running++;
  205.         *ptmp = achar;
  206.         *++ptmp = '\0';
  207.         }
  208.     }
  209.     close(score_fd);
  210.     sprintf(ptmp, " (%d/%d)", running, total);
  211.     for(loop=1;loop<=last_len;loop++)
  212.         putchar('\010');
  213.     if (last_len > strlen(tbuf)) {
  214.         for(loop=1;loop<=last_len;loop++)
  215.         putchar(' ');
  216.         for(loop=1;loop<=last_len;loop++)
  217.         putchar('\010');
  218.     }
  219.     printf("%s", tbuf);
  220.     fflush(stdout);
  221.     last_len = strlen(tbuf);
  222.     }    /* for */
  223. }
  224.  
  225. int
  226. usage(arg)
  227. char *arg;
  228. {
  229.     printf("httpd_monitor: Usage\n");
  230.     printf("  httpd_monitor [ -d config-dir] [ -s sleep-time ]\n");
  231.     printf("    Defaults: config-dir = %s\n", HTTPD_ROOT);
  232.     printf("              sleep-time = %d seconds\n", DEFAULT_SLEEPTIME);
  233.     exit(0);
  234. }
  235.  
  236. /*
  237.  * This function uses some hard-wired knowledge about the
  238.  * Apache httpd.conf file setup (basically names of the 3
  239.  * parameters we are interested in)
  240.  *
  241.  * We basically scan thru the file and grab the 3 values we
  242.  * need. This could be done better...
  243.  */
  244. void
  245. lookfor(pidname, scorename, thefile)
  246. char *pidname, *scorename;
  247. FILE *thefile;
  248. {
  249.     char line[ASIZE], param[ASIZE], value[ASIZE];
  250.     char sroot[ASIZE], pidfile[ASIZE], scorefile[ASIZE];
  251.  
  252.     *sroot = *pidfile  = *scorefile = '\0';
  253.     while (!(feof(thefile))) {
  254.     fgets(line, ASIZE-1, thefile);
  255.     *value = '\0';    /* protect braindead sscanf() */
  256.     sscanf(line, "%s %s", param, value);
  257.     if (strcmp(param, "PidFile")==0 && *value)
  258.         strcpy(pidfile, value);
  259.     if (strcmp(param, "ScoreBoardFile")==0 && *value)
  260.         strcpy(scorefile, value);
  261.     if (strcmp(param, "ServerRoot")==0 && *value)
  262.         strcpy(sroot, value);
  263.     }
  264.  
  265.     /*
  266.      * We've reached EOF... we should have encountered the
  267.      * ServerRoot line... if not, we bail out
  268.      */
  269.     if (!*sroot) {
  270.     perror("httpd_monitor");
  271.     fprintf(stderr, "Can't find ServerRoot!\n");
  272.     exit(1);
  273.     }
  274.  
  275.     /*
  276.      * Not finding PidFile or ScoreBoardFile is OK, since
  277.      * we have defaults for them
  278.      */
  279.     if (!*pidfile)
  280.     strcpy(pidfile, DEFAULT_PIDLOG);
  281.     if (!*scorefile)
  282.     strcpy(scorefile, DEFAULT_SCOREBOARD);
  283.  
  284.     /*
  285.      * Relative or absolute? Handle both
  286.      */
  287.     if (*pidfile == '/')
  288.     strcpy(pidname, pidfile);
  289.     else {
  290.     strcpy(pidname, sroot);
  291.     strcat(pidname, "/");
  292.     strcat(pidname, pidfile);
  293.     }
  294.     if (*scorefile == '/')
  295.     strcpy(scorename, scorefile);
  296.     else {
  297.     strcpy(scorename, sroot);
  298.     strcat(scorename, "/");
  299.     strcat(scorename, scorefile);
  300.     }
  301. }
  302.  
  303.