upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / generators / mod_status.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* Status Module.  Display lots of internal data about how Apache is
18  * performing and the state of all children processes.
19  *
20  * To enable this, add the following lines into any config file:
21  *
22  * <Location /server-status>
23  * SetHandler server-status
24  * </Location>
25  *
26  * You may want to protect this location by password or domain so no one
27  * else can look at it.  Then you can access the statistics with a URL like:
28  *
29  * http://your_server_name/server-status
30  *
31  * /server-status - Returns page using tables
32  * /server-status?notable - Returns page for browsers without table support
33  * /server-status?refresh - Returns page with 1 second refresh
34  * /server-status?refresh=6 - Returns page with refresh every 6 seconds
35  * /server-status?auto - Returns page with data for automatic parsing
36  *
37  * Mark Cox, mark@ukweb.com, November 1995
38  *
39  * 12.11.95 Initial version for www.telescope.org
40  * 13.3.96  Updated to remove rprintf's [Mark]
41  * 18.3.96  Added CPU usage, process information, and tidied [Ben Laurie]
42  * 18.3.96  Make extra Scoreboard variables #definable
43  * 25.3.96  Make short report have full precision [Ben Laurie suggested]
44  * 25.3.96  Show uptime better [Mark/Ben Laurie]
45  * 29.3.96  Better HTML and explanation [Mark/Rob Hartill suggested]
46  * 09.4.96  Added message for non-STATUS compiled version
47  * 18.4.96  Added per child and per slot counters [Jim Jagielski]
48  * 01.5.96  Table format, cleanup, even more spiffy data [Chuck Murcko/Jim J.]
49  * 18.5.96  Adapted to use new rprintf() routine, incidentally fixing a missing
50  *          piece in short reports [Ben Laurie]
51  * 21.5.96  Additional Status codes (DNS and LOGGING only enabled if
52  *          extended STATUS is enabled) [George Burgyan/Jim J.]
53  * 10.8.98  Allow for extended status info at runtime (no more STATUS)
54  *          [Jim J.]
55  */
56
57 #define CORE_PRIVATE
58 #include "httpd.h"
59 #include "http_config.h"
60 #include "http_core.h"
61 #include "http_protocol.h"
62 #include "http_main.h"
63 #include "ap_mpm.h"
64 #include "util_script.h"
65 #include <time.h>
66 #include "scoreboard.h"
67 #include "http_log.h"
68 #include "mod_status.h"
69 #if APR_HAVE_UNISTD_H
70 #include <unistd.h>
71 #endif
72 #define APR_WANT_STRFUNC
73 #include "apr_want.h"
74 #include "apr_strings.h"
75
76 #ifdef NEXT
77 #if (NX_CURRENT_COMPILER_RELEASE == 410)
78 #ifdef m68k
79 #define HZ 64
80 #else
81 #define HZ 100
82 #endif
83 #else
84 #include <machine/param.h>
85 #endif
86 #endif /* NEXT */
87
88 #define STATUS_MAXLINE 64
89
90 #define KBYTE 1024
91 #define MBYTE 1048576L
92 #define GBYTE 1073741824L
93
94 #ifndef DEFAULT_TIME_FORMAT
95 #define DEFAULT_TIME_FORMAT "%A, %d-%b-%Y %H:%M:%S %Z"
96 #endif
97
98 #define STATUS_MAGIC_TYPE "application/x-httpd-status"
99
100 module AP_MODULE_DECLARE_DATA status_module;
101
102 int server_limit, thread_limit;
103
104 #ifdef HAVE_TIMES
105 /* ugh... need to know if we're running with a pthread implementation
106  * such as linuxthreads that treats individual threads as distinct
107  * processes; that affects how we add up CPU time in a process
108  */
109 static pid_t child_pid;
110 #endif
111
112 /* Implement 'ap_run_status_hook'. */
113 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap, STATUS, int, status_hook,
114                                     (request_rec *r, int flags),
115                                     (r, flags),
116                                     OK, DECLINED)
117
118 /*
119  * command-related code. This is here to prevent use of ExtendedStatus
120  * without status_module included.
121  */
122 static const char *set_extended_status(cmd_parms *cmd, void *dummy, int arg)
123 {
124     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
125     if (err != NULL) {
126         return err;
127     }
128     ap_extended_status = arg;
129     return NULL;
130 }
131
132 static const command_rec status_module_cmds[] =
133 {
134     AP_INIT_FLAG("ExtendedStatus", set_extended_status, NULL, RSRC_CONF,
135       "\"On\" to enable extended status information, \"Off\" to disable"),
136     {NULL}
137 };
138
139 /* Format the number of bytes nicely */
140 static void format_byte_out(request_rec *r, apr_off_t bytes)
141 {
142     if (bytes < (5 * KBYTE))
143         ap_rprintf(r, "%d B", (int) bytes);
144     else if (bytes < (MBYTE / 2))
145         ap_rprintf(r, "%.1f kB", (float) bytes / KBYTE);
146     else if (bytes < (GBYTE / 2))
147         ap_rprintf(r, "%.1f MB", (float) bytes / MBYTE);
148     else
149         ap_rprintf(r, "%.1f GB", (float) bytes / GBYTE);
150 }
151
152 static void format_kbyte_out(request_rec *r, apr_off_t kbytes)
153 {
154     if (kbytes < KBYTE)
155         ap_rprintf(r, "%d kB", (int) kbytes);
156     else if (kbytes < MBYTE)
157         ap_rprintf(r, "%.1f MB", (float) kbytes / KBYTE);
158     else
159         ap_rprintf(r, "%.1f GB", (float) kbytes / MBYTE);
160 }
161
162 static void show_time(request_rec *r, apr_interval_time_t tsecs)
163 {
164     int days, hrs, mins, secs;
165
166     secs = (int)(tsecs % 60);
167     tsecs /= 60;
168     mins = (int)(tsecs % 60);
169     tsecs /= 60;
170     hrs = (int)(tsecs % 24);
171     days = (int)(tsecs / 24);
172
173     if (days)
174         ap_rprintf(r, " %d day%s", days, days == 1 ? "" : "s");
175
176     if (hrs)
177         ap_rprintf(r, " %d hour%s", hrs, hrs == 1 ? "" : "s");
178
179     if (mins)
180         ap_rprintf(r, " %d minute%s", mins, mins == 1 ? "" : "s");
181
182     if (secs)
183         ap_rprintf(r, " %d second%s", secs, secs == 1 ? "" : "s");
184 }
185
186 /* Main handler for x-httpd-status requests */
187
188 /* ID values for command table */
189
190 #define STAT_OPT_END     -1
191 #define STAT_OPT_REFRESH  0
192 #define STAT_OPT_NOTABLE  1
193 #define STAT_OPT_AUTO     2
194
195 struct stat_opt {
196     int id;
197     const char *form_data_str;
198     const char *hdr_out_str;
199 };
200
201 static const struct stat_opt status_options[] = /* see #defines above */
202 {
203     {STAT_OPT_REFRESH, "refresh", "Refresh"},
204     {STAT_OPT_NOTABLE, "notable", NULL},
205     {STAT_OPT_AUTO, "auto", NULL},
206     {STAT_OPT_END, NULL, NULL}
207 };
208
209 static char status_flags[SERVER_NUM_STATUS];
210
211 static int status_handler(request_rec *r)
212 {
213     const char *loc;
214     apr_time_t nowtime;
215     apr_interval_time_t up_time;
216     int j, i, res;
217     int ready;
218     int busy;
219     unsigned long count;
220     unsigned long lres, my_lres, conn_lres;
221     apr_off_t bytes, my_bytes, conn_bytes;
222     apr_off_t bcount, kbcount;
223     long req_time;
224 #ifdef HAVE_TIMES
225     float tick;
226     int times_per_thread = getpid() != child_pid;
227 #endif
228     int short_report;
229     int no_table_report;
230     worker_score *ws_record;
231     process_score *ps_record;
232     char *stat_buffer;
233     pid_t *pid_buffer;
234     clock_t tu, ts, tcu, tcs;
235
236     if (strcmp(r->handler, STATUS_MAGIC_TYPE) && 
237         strcmp(r->handler, "server-status")) {
238         return DECLINED;
239     }
240
241 #ifdef HAVE_TIMES
242 #ifdef _SC_CLK_TCK
243     tick = sysconf(_SC_CLK_TCK);
244 #else
245     tick = HZ;
246 #endif
247 #endif
248
249     ready = 0;
250     busy = 0;
251     count = 0;
252     bcount = 0;
253     kbcount = 0;
254     short_report = 0;
255     no_table_report = 0;
256
257     pid_buffer = apr_palloc(r->pool, server_limit * sizeof(pid_t));
258     stat_buffer = apr_palloc(r->pool, server_limit * thread_limit * sizeof(char));
259
260     nowtime = apr_time_now();
261     tu = ts = tcu = tcs = 0;
262
263     if (!ap_exists_scoreboard_image()) {
264         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
265                       "Server status unavailable in inetd mode");
266         return HTTP_INTERNAL_SERVER_ERROR;
267     }
268
269     r->allowed = (AP_METHOD_BIT << M_GET);
270     if (r->method_number != M_GET)
271         return DECLINED;
272
273     ap_set_content_type(r, "text/html; charset=ISO-8859-1");
274
275     /*
276      * Simple table-driven form data set parser that lets you alter the header
277      */
278
279     if (r->args) {
280         i = 0;
281         while (status_options[i].id != STAT_OPT_END) {
282             if ((loc = ap_strstr_c(r->args,
283                                    status_options[i].form_data_str)) != NULL) {
284                 switch (status_options[i].id) {
285                 case STAT_OPT_REFRESH: {
286                     apr_size_t len = strlen(status_options[i].form_data_str);
287                     long t = 0;
288
289                     if (*(loc + len ) == '=') {
290                         t = atol(loc + len + 1);
291                     }
292                     apr_table_set(r->headers_out,
293                                   status_options[i].hdr_out_str,
294                                   apr_ltoa(r->pool, t < 1 ? 10 : t));
295                     break;
296                 }
297                 case STAT_OPT_NOTABLE:
298                     no_table_report = 1;
299                     break;
300                 case STAT_OPT_AUTO:
301                     ap_set_content_type(r, "text/plain; charset=ISO-8859-1");
302                     short_report = 1;
303                     break;
304                 }
305             }
306
307             i++;
308         }
309     }
310
311     for (i = 0; i < server_limit; ++i) {
312 #ifdef HAVE_TIMES
313         clock_t proc_tu = 0, proc_ts = 0, proc_tcu = 0, proc_tcs = 0;
314         clock_t tmp_tu, tmp_ts, tmp_tcu, tmp_tcs;
315 #endif
316         
317         ps_record = ap_get_scoreboard_process(i);
318         for (j = 0; j < thread_limit; ++j) {
319             int indx = (i * thread_limit) + j;
320
321             ws_record = ap_get_scoreboard_worker(i, j);
322             res = ws_record->status;
323             stat_buffer[indx] = status_flags[res];
324
325             if (!ps_record->quiescing
326                 && ps_record->pid) {
327                 if (res == SERVER_READY
328                     && ps_record->generation == ap_my_generation)
329                     ready++;
330                 else if (res != SERVER_DEAD &&
331                          res != SERVER_STARTING &&
332                          res != SERVER_IDLE_KILL)
333                     busy++;
334             }
335
336             /* XXX what about the counters for quiescing/seg faulted
337              * processes?  should they be counted or not?  GLA
338              */
339             if (ap_extended_status) {
340                 lres = ws_record->access_count;
341                 bytes = ws_record->bytes_served;
342
343                 if (lres != 0 || (res != SERVER_READY && res != SERVER_DEAD)) {
344 #ifdef HAVE_TIMES
345                     tmp_tu = ws_record->times.tms_utime;
346                     tmp_ts = ws_record->times.tms_stime;
347                     tmp_tcu = ws_record->times.tms_cutime;
348                     tmp_tcs = ws_record->times.tms_cstime;
349
350                     if (times_per_thread) {
351                         proc_tu += tmp_tu;
352                         proc_ts += tmp_ts;
353                         proc_tcu += tmp_tcu;
354                         proc_tcs += proc_tcs;
355                     }
356                     else {
357                         if (tmp_tu > proc_tu ||
358                             tmp_ts > proc_ts ||
359                             tmp_tcu > proc_tcu ||
360                             tmp_tcs > proc_tcs) {
361                             proc_tu = tmp_tu;
362                             proc_ts = tmp_ts;
363                             proc_tcu = tmp_tcu;
364                             proc_tcs = proc_tcs;
365                         }
366                     }
367 #endif /* HAVE_TIMES */
368
369                     count += lres;
370                     bcount += bytes;
371
372                     if (bcount >= KBYTE) {
373                         kbcount += (bcount >> 10);
374                         bcount = bcount & 0x3ff;
375                     }
376                 }
377             }
378         }
379 #ifdef HAVE_TIMES
380         tu += proc_tu;
381         ts += proc_ts;
382         tcu += proc_tcu;
383         tcs += proc_tcs;
384 #endif
385         pid_buffer[i] = ps_record->pid;
386     }
387
388     /* up_time in seconds */
389     up_time = (apr_uint32_t) apr_time_sec(nowtime -
390                                ap_scoreboard_image->global->restart_time);
391
392     if (!short_report) {
393         ap_rputs(DOCTYPE_HTML_3_2
394                  "<html><head>\n<title>Apache Status</title>\n</head><body>\n",
395                  r);
396         ap_rputs("<h1>Apache Server Status for ", r);
397         ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
398         ap_rvputs(r, "<dl><dt>Server Version: ",
399                   ap_get_server_version(), "</dt>\n", NULL);
400         ap_rvputs(r, "<dt>Server Built: ",
401                   ap_get_server_built(), "\n</dt></dl><hr /><dl>\n", NULL);
402         ap_rvputs(r, "<dt>Current Time: ",
403                   ap_ht_time(r->pool, nowtime, DEFAULT_TIME_FORMAT, 0),
404                              "</dt>\n", NULL);
405         ap_rvputs(r, "<dt>Restart Time: ",
406                   ap_ht_time(r->pool,
407                              ap_scoreboard_image->global->restart_time,
408                              DEFAULT_TIME_FORMAT, 0),
409                   "</dt>\n", NULL);
410         ap_rprintf(r, "<dt>Parent Server Generation: %d</dt>\n",
411                    (int)ap_my_generation);
412         ap_rputs("<dt>Server uptime: ", r);
413         show_time(r, up_time);
414         ap_rputs("</dt>\n", r);
415     }
416
417     if (ap_extended_status) {
418         if (short_report) {
419             ap_rprintf(r, "Total Accesses: %lu\nTotal kBytes: %"
420                        APR_OFF_T_FMT "\n",
421                        count, kbcount);
422
423 #ifdef HAVE_TIMES
424             /* Allow for OS/2 not having CPU stats */
425             if (ts || tu || tcu || tcs)
426                 ap_rprintf(r, "CPULoad: %g\n",
427                            (tu + ts + tcu + tcs) / tick / up_time * 100.);
428 #endif
429
430             ap_rprintf(r, "Uptime: %ld\n", (long) (up_time));
431             if (up_time > 0)
432                 ap_rprintf(r, "ReqPerSec: %g\n",
433                            (float) count / (float) up_time);
434
435             if (up_time > 0)
436                 ap_rprintf(r, "BytesPerSec: %g\n",
437                            KBYTE * (float) kbcount / (float) up_time);
438
439             if (count > 0)
440                 ap_rprintf(r, "BytesPerReq: %g\n",
441                            KBYTE * (float) kbcount / (float) count);
442         }
443         else { /* !short_report */
444             ap_rprintf(r, "<dt>Total accesses: %lu - Total Traffic: ", count);
445             format_kbyte_out(r, kbcount);
446             ap_rputs("</dt>\n", r);
447
448 #ifdef HAVE_TIMES
449             /* Allow for OS/2 not having CPU stats */
450             ap_rprintf(r, "<dt>CPU Usage: u%g s%g cu%g cs%g",
451                        tu / tick, ts / tick, tcu / tick, tcs / tick);
452
453             if (ts || tu || tcu || tcs)
454                 ap_rprintf(r, " - %.3g%% CPU load</dt>\n",
455                            (tu + ts + tcu + tcs) / tick / up_time * 100.);
456 #endif
457
458             if (up_time > 0)
459                 ap_rprintf(r, "<dt>%.3g requests/sec - ",
460                            (float) count / (float) up_time);
461
462             if (up_time > 0) {
463                 format_byte_out(r, (unsigned long)(KBYTE * (float) kbcount
464                                                    / (float) up_time));
465                 ap_rputs("/second - ", r);
466             }
467
468             if (count > 0) {
469                 format_byte_out(r, (unsigned long)(KBYTE * (float) kbcount
470                                                    / (float) count));
471                 ap_rputs("/request", r);
472             }
473
474             ap_rputs("</dt>\n", r);
475         } /* short_report */
476     } /* ap_extended_status */
477
478     if (!short_report)
479         ap_rprintf(r, "<dt>%d requests currently being processed, "
480                       "%d idle workers</dt>\n", busy, ready);
481     else
482         ap_rprintf(r, "BusyWorkers: %d\nIdleWorkers: %d\n", busy, ready);
483
484     /* send the scoreboard 'table' out */
485     if (!short_report)
486         ap_rputs("</dl><pre>", r);
487     else
488         ap_rputs("Scoreboard: ", r);
489
490     for (i = 0; i < server_limit; ++i) {
491         for (j = 0; j < thread_limit; ++j) {
492             int indx = (i * thread_limit) + j;
493             ap_rputc(stat_buffer[indx], r);
494             if ((indx % STATUS_MAXLINE == (STATUS_MAXLINE - 1))
495                 && !short_report)
496                 ap_rputs("\n", r);
497         }
498     }
499
500     if (short_report)
501         ap_rputs("\n", r);
502     else {
503         ap_rputs("</pre>\n", r);
504         ap_rputs("<p>Scoreboard Key:<br />\n", r);
505         ap_rputs("\"<b><code>_</code></b>\" Waiting for Connection, \n", r);
506         ap_rputs("\"<b><code>S</code></b>\" Starting up, \n", r);
507         ap_rputs("\"<b><code>R</code></b>\" Reading Request,<br />\n", r);
508         ap_rputs("\"<b><code>W</code></b>\" Sending Reply, \n", r);
509         ap_rputs("\"<b><code>K</code></b>\" Keepalive (read), \n", r);
510         ap_rputs("\"<b><code>D</code></b>\" DNS Lookup,<br />\n", r);
511         ap_rputs("\"<b><code>C</code></b>\" Closing connection, \n", r);
512         ap_rputs("\"<b><code>L</code></b>\" Logging, \n", r);
513         ap_rputs("\"<b><code>G</code></b>\" Gracefully finishing,<br /> \n", r);
514         ap_rputs("\"<b><code>I</code></b>\" Idle cleanup of worker, \n", r);
515         ap_rputs("\"<b><code>.</code></b>\" Open slot with no current process</p>\n", r);
516         ap_rputs("<p />\n", r);
517         if (!ap_extended_status) {
518             int j;
519             int k = 0;
520             ap_rputs("PID Key: <br />\n", r);
521             ap_rputs("<pre>\n", r);
522             for (i = 0; i < server_limit; ++i) {
523                 for (j = 0; j < thread_limit; ++j) {
524                     int indx = (i * thread_limit) + j;
525
526                     if (stat_buffer[indx] != '.') {
527                         ap_rprintf(r, "   %" APR_PID_T_FMT
528                                    " in state: %c ", pid_buffer[i],
529                                    stat_buffer[indx]);
530
531                         if (++k >= 3) {
532                             ap_rputs("\n", r);
533                             k = 0;
534                         } else
535                             ap_rputs(",", r);
536                     }
537                 }
538             }
539
540             ap_rputs("\n", r);
541             ap_rputs("</pre>\n", r);
542         }
543     }
544
545     if (ap_extended_status && !short_report) {
546         if (no_table_report)
547             ap_rputs("<hr /><h2>Server Details</h2>\n\n", r);
548         else
549             ap_rputs("\n\n<table border=\"0\"><tr>"
550                      "<th>Srv</th><th>PID</th><th>Acc</th>"
551                      "<th>M</th>"
552 #ifdef HAVE_TIMES
553                      "<th>CPU\n</th>"
554 #endif
555                      "<th>SS</th><th>Req</th>"
556                      "<th>Conn</th><th>Child</th><th>Slot</th>"
557                      "<th>Client</th><th>VHost</th>"
558                      "<th>Request</th></tr>\n\n", r);
559
560         for (i = 0; i < server_limit; ++i) {
561             for (j = 0; j < thread_limit; ++j) {
562                 ws_record = ap_get_scoreboard_worker(i, j);
563
564                 if (ws_record->access_count == 0 &&
565                     (ws_record->status == SERVER_READY ||
566                      ws_record->status == SERVER_DEAD)) {
567                     continue;
568                 }
569
570                 ps_record = ap_get_scoreboard_process(i);
571
572                 if (ws_record->start_time == 0L)
573                     req_time = 0L;
574                 else
575                     req_time = (long)
576                         ((ws_record->stop_time - 
577                           ws_record->start_time) / 1000);
578                 if (req_time < 0L)
579                     req_time = 0L;
580
581                 lres = ws_record->access_count;
582                 my_lres = ws_record->my_access_count;
583                 conn_lres = ws_record->conn_count;
584                 bytes = ws_record->bytes_served;
585                 my_bytes = ws_record->my_bytes_served;
586                 conn_bytes = ws_record->conn_bytes;
587
588                 if (no_table_report) {
589                     if (ws_record->status == SERVER_DEAD)
590                         ap_rprintf(r,
591                                    "<b>Server %d-%d</b> (-): %d|%lu|%lu [",
592                                    i, (int)ps_record->generation,
593                                    (int)conn_lres, my_lres, lres);
594                     else
595                         ap_rprintf(r,
596                                    "<b>Server %d-%d</b> (%"
597                                    APR_PID_T_FMT "): %d|%lu|%lu [",
598                                    i, (int) ps_record->generation,
599                                    ps_record->pid,
600                                    (int)conn_lres, my_lres, lres);
601                     
602                     switch (ws_record->status) {
603                     case SERVER_READY:
604                         ap_rputs("Ready", r);
605                         break;
606                     case SERVER_STARTING:
607                         ap_rputs("Starting", r);
608                         break;
609                     case SERVER_BUSY_READ:
610                         ap_rputs("<b>Read</b>", r);
611                         break;
612                     case SERVER_BUSY_WRITE:
613                         ap_rputs("<b>Write</b>", r);
614                         break;
615                     case SERVER_BUSY_KEEPALIVE:
616                         ap_rputs("<b>Keepalive</b>", r);
617                         break;
618                     case SERVER_BUSY_LOG:
619                         ap_rputs("<b>Logging</b>", r);
620                         break;
621                     case SERVER_BUSY_DNS:
622                         ap_rputs("<b>DNS lookup</b>", r);
623                         break;
624                     case SERVER_CLOSING:
625                         ap_rputs("<b>Closing</b>", r);
626                         break;
627                     case SERVER_DEAD:
628                         ap_rputs("Dead", r);
629                         break;
630                     case SERVER_GRACEFUL:
631                         ap_rputs("Graceful", r);
632                         break;
633                     case SERVER_IDLE_KILL:
634                         ap_rputs("Dying", r);
635                         break;
636                     default:
637                         ap_rputs("?STATE?", r);
638                         break;
639                     }
640                     
641                     ap_rprintf(r, "] "
642 #ifdef HAVE_TIMES
643                                "u%g s%g cu%g cs%g"
644 #endif
645                                "\n %ld %ld (",
646 #ifdef HAVE_TIMES
647                                ws_record->times.tms_utime / tick,
648                                ws_record->times.tms_stime / tick,
649                                ws_record->times.tms_cutime / tick,
650                                ws_record->times.tms_cstime / tick,
651 #endif
652                                (long)apr_time_sec(nowtime -
653                                                   ws_record->last_used),
654                                (long) req_time);
655
656                     format_byte_out(r, conn_bytes);
657                     ap_rputs("|", r);
658                     format_byte_out(r, my_bytes);
659                     ap_rputs("|", r);
660                     format_byte_out(r, bytes);
661                     ap_rputs(")\n", r);
662                     ap_rprintf(r,
663                                " <i>%s {%s}</i> <b>[%s]</b><br />\n\n",
664                                ap_escape_html(r->pool,
665                                               ws_record->client),
666                                ap_escape_html(r->pool,
667                                               ap_escape_logitem(r->pool,
668                                                                 ws_record->request)),
669                                ap_escape_html(r->pool,
670                                               ws_record->vhost));
671                 }
672                 else { /* !no_table_report */
673                     if (ws_record->status == SERVER_DEAD)
674                         ap_rprintf(r,
675                                    "<tr><td><b>%d-%d</b></td><td>-</td><td>%d/%lu/%lu",
676                                    i, (int)ps_record->generation,
677                                    (int)conn_lres, my_lres, lres);
678                     else
679                         ap_rprintf(r,
680                                    "<tr><td><b>%d-%d</b></td><td>%"
681                                    APR_PID_T_FMT
682                                    "</td><td>%d/%lu/%lu",
683                                    i, (int)ps_record->generation,
684                                    ps_record->pid, (int)conn_lres,
685                                    my_lres, lres);
686                     
687                     switch (ws_record->status) {
688                     case SERVER_READY:
689                         ap_rputs("</td><td>_", r);
690                         break;
691                     case SERVER_STARTING:
692                         ap_rputs("</td><td><b>S</b>", r);
693                         break;
694                     case SERVER_BUSY_READ:
695                         ap_rputs("</td><td><b>R</b>", r);
696                         break;
697                     case SERVER_BUSY_WRITE:
698                         ap_rputs("</td><td><b>W</b>", r);
699                         break;
700                     case SERVER_BUSY_KEEPALIVE:
701                         ap_rputs("</td><td><b>K</b>", r);
702                         break;
703                     case SERVER_BUSY_LOG:
704                         ap_rputs("</td><td><b>L</b>", r);
705                         break;
706                     case SERVER_BUSY_DNS:
707                         ap_rputs("</td><td><b>D</b>", r);
708                         break;
709                     case SERVER_CLOSING:
710                         ap_rputs("</td><td><b>C</b>", r);
711                         break;
712                     case SERVER_DEAD:
713                         ap_rputs("</td><td>.", r);
714                         break;
715                     case SERVER_GRACEFUL:
716                         ap_rputs("</td><td>G", r);
717                         break;
718                     case SERVER_IDLE_KILL:
719                         ap_rputs("</td><td>I", r);
720                         break;
721                     default:
722                         ap_rputs("</td><td>?", r);
723                         break;
724                     }
725                     
726                     ap_rprintf(r,
727                                "\n</td>"
728 #ifdef HAVE_TIMES
729                                "<td>%.2f</td>"
730 #endif
731                                "<td>%ld</td><td>%ld",
732 #ifdef HAVE_TIMES
733                                (ws_record->times.tms_utime +
734                                 ws_record->times.tms_stime +
735                                 ws_record->times.tms_cutime +
736                                 ws_record->times.tms_cstime) / tick,
737 #endif
738                                (long)apr_time_sec(nowtime -
739                                                   ws_record->last_used),
740                                (long)req_time);
741                     
742                     ap_rprintf(r, "</td><td>%-1.1f</td><td>%-2.2f</td><td>%-2.2f\n",
743                                (float)conn_bytes / KBYTE, (float) my_bytes / MBYTE,
744                                (float)bytes / MBYTE);
745                     
746                     if (ws_record->status == SERVER_BUSY_READ)
747                         ap_rprintf(r,
748                                    "</td><td>?</td><td nowrap>?</td><td nowrap>..reading.. </td></tr>\n\n");
749                     else
750                         ap_rprintf(r,
751                                    "</td><td>%s</td><td nowrap>%s</td><td nowrap>%s</td></tr>\n\n",
752                                    ap_escape_html(r->pool,
753                                                   ws_record->client),
754                                    ap_escape_html(r->pool,
755                                                   ws_record->vhost),
756                                    ap_escape_html(r->pool,
757                                                   ap_escape_logitem(r->pool, 
758                                                                     ws_record->request)));
759                 } /* no_table_report */
760             } /* for (j...) */
761         } /* for (i...) */
762
763         if (!no_table_report) {
764             ap_rputs("</table>\n \
765 <hr /> \
766 <table>\n \
767 <tr><th>Srv</th><td>Child Server number - generation</td></tr>\n \
768 <tr><th>PID</th><td>OS process ID</td></tr>\n \
769 <tr><th>Acc</th><td>Number of accesses this connection / this child / this slot</td></tr>\n \
770 <tr><th>M</th><td>Mode of operation</td></tr>\n"
771
772 #ifdef HAVE_TIMES
773 "<tr><th>CPU</th><td>CPU usage, number of seconds</td></tr>\n"
774 #endif
775
776 "<tr><th>SS</th><td>Seconds since beginning of most recent request</td></tr>\n \
777 <tr><th>Req</th><td>Milliseconds required to process most recent request</td></tr>\n \
778 <tr><th>Conn</th><td>Kilobytes transferred this connection</td></tr>\n \
779 <tr><th>Child</th><td>Megabytes transferred this child</td></tr>\n \
780 <tr><th>Slot</th><td>Total megabytes transferred this slot</td></tr>\n \
781 </table>\n", r);
782         }
783     } /* if (ap_extended_status && !short_report) */
784     else {
785
786         if (!short_report) {
787             ap_rputs("<hr />To obtain a full report with current status "
788                      "information you need to use the "
789                      "<code>ExtendedStatus On</code> directive.\n", r);
790         }
791     }
792
793     {
794         /* Run extension hooks to insert extra content. */
795         int flags = 
796             (short_report ? AP_STATUS_SHORT : 0) | 
797             (no_table_report ? AP_STATUS_NOTABLE : 0) |
798             (ap_extended_status ? AP_STATUS_EXTENDED : 0);
799         
800         ap_run_status_hook(r, flags);
801     }
802
803     if (!short_report) {
804         ap_rputs(ap_psignature("<hr />\n",r), r);
805         ap_rputs("</body></html>\n", r);
806     }
807
808     return 0;
809 }
810
811
812 static int status_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp,
813                        server_rec *s)
814 {
815     status_flags[SERVER_DEAD] = '.';  /* We don't want to assume these are in */
816     status_flags[SERVER_READY] = '_'; /* any particular order in scoreboard.h */
817     status_flags[SERVER_STARTING] = 'S';
818     status_flags[SERVER_BUSY_READ] = 'R';
819     status_flags[SERVER_BUSY_WRITE] = 'W';
820     status_flags[SERVER_BUSY_KEEPALIVE] = 'K';
821     status_flags[SERVER_BUSY_LOG] = 'L';
822     status_flags[SERVER_BUSY_DNS] = 'D';
823     status_flags[SERVER_CLOSING] = 'C';
824     status_flags[SERVER_GRACEFUL] = 'G';
825     status_flags[SERVER_IDLE_KILL] = 'I';
826     ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
827     ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
828     return OK;
829 }
830
831 #ifdef HAVE_TIMES
832 static void status_child_init(apr_pool_t *p, server_rec *s)
833 {
834     child_pid = getpid();
835 }
836 #endif
837
838 static void register_hooks(apr_pool_t *p)
839 {
840     ap_hook_handler(status_handler, NULL, NULL, APR_HOOK_MIDDLE);
841     ap_hook_post_config(status_init, NULL, NULL, APR_HOOK_MIDDLE);
842 #ifdef HAVE_TIMES
843     ap_hook_child_init(status_child_init, NULL, NULL, APR_HOOK_MIDDLE);
844 #endif
845 }
846
847 module AP_MODULE_DECLARE_DATA status_module =
848 {
849     STANDARD20_MODULE_STUFF,
850     NULL,                       /* dir config creater */
851     NULL,                       /* dir merger --- default is to override */
852     NULL,                       /* server config */
853     NULL,                       /* merge server config */
854     status_module_cmds,         /* command table */
855     register_hooks              /* register_hooks */
856 };
857