1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/* ====================================================================
* Copyright (c) 1995-1997 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 [ -d serverdir | -f conffile ] [ -s sleep_time ]
* -d/-f options specify server dir or config files, as per
* httpd.
* -s specifies how long to pause between screen updates
* If you choose 0, it might chew up lots of CPU time.
*
* Output explanation..
*
* s = sleeping but "ready to go" child (this is '_' in mod_status)
* R = active child - writing to client
* W = active child - reading from client
* K = active child - waiting for additional request on kept-alive connection
* D = active child - doing DNS lookup
* L = active child - logging
* _ = dead child (no longer needed) (this is '.' in mod_status)
* t = just starting (this is 'S' in mod_status)
*
*
* Jim Jagielski <jim@jaguNET.com>
* v1.0 Notes:
* This code is much more ugly and complicated than it
* needs to be.
*
* v1.1:
* Minor fixes
*
* v1.2:
* Handles Apache 1.1.* scoreboard format (W/K/D/L states) -- PCS 09Jul96
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../src/httpd.h"
#include "../src/scoreboard.h"
#define DEFAULT_SLEEPTIME 2
#define ASIZE 1024
#define MAX_PROC HARD_SERVER_LIMIT
int
main(argc, argv)
int argc;
char **argv;
{
short_score scoreboard_image;
FILE *afile;
char conf_name[ASIZE];
char pid_name[ASIZE];
char score_name[ASIZE];
char tbuf[ASIZE];
char *ptmp;
static char kid_stat[] = { '_', 's', 'R', 't', 'W', 'K', 'L', 'D' };
int achar;
long thepid;
int score_fd;
int sleep_time = DEFAULT_SLEEPTIME;
int last_len = 0;
int kiddies;
int running, dead, total, loop;
short got_config = 0;
struct stat statbuf;
time_t last_time = 0;
extern char *optarg;
extern int optind, opterr;
void lookfor();
int usage();
/*
* Handle the options. Using getopt() is most probably overkill,
* but let's think about the future!
*/
strcpy(conf_name, HTTPD_ROOT);
while((achar = getopt(argc,argv,"s:d:f:")) != -1) {
switch(achar) {
case 'd':
strcpy(conf_name, optarg);
break;
case 'f':
strcpy(conf_name, optarg);
got_config = 1;
break;
case 's':
sleep_time = atoi(optarg);
break;
case '?':
usage(argv[0]);
}
}
/*
* Now build the name of the httpd.conf file
*/
if (!got_config) {
strcat(conf_name, "/");
strcat(conf_name, SERVER_CONFIG_FILE);
}
/*
* Make sure we have the right file... Barf if not
*/
if (!(afile = fopen(conf_name, "r"))) {
perror("httpd_monitor");
fprintf(stderr, "Can't open config file: %s\n", conf_name);
exit(1);
}
/*
* now scan thru the ConfigFile to look for the items that
* interest us
*/
lookfor(pid_name, score_name, afile);
fclose(afile);
/*
* now open the PidFile and then the ScoreBoardFile
*/
if (!(afile = fopen(pid_name, "r"))) {
perror("httpd_monitor");
fprintf(stderr, "Can't open PIDfile: %s\n", pid_name);
exit(1);
}
fscanf(afile, "%ld", &thepid);
fclose(afile);
/*
* Enough taters, time for the MEAT!
*/
for(;;sleep(sleep_time)) {
if (stat(score_name, &statbuf)) {
perror("httpd_monitor");
fprintf(stderr, "Can't stat scoreboard file: %s\n", score_name);
exit(1);
}
if (last_time == statbuf.st_mtime)
continue; /* tricky ;) */
last_time = statbuf.st_mtime; /* for next time */
if ((score_fd = open(score_name, 0)) == -1 ) {
perror("httpd_monitor");
fprintf(stderr, "Can't open scoreboard file: %s\n", score_name);
exit(1);
}
/*
* all that for _this_
*/
running = dead = total = 0;
ptmp = tbuf;
*ptmp = '\0';
for(kiddies=0;kiddies<MAX_PROC; kiddies++) {
read(score_fd, (char *)&scoreboard_image, sizeof(short_score));
achar = kid_stat[(int)scoreboard_image.status];
if (scoreboard_image.pid != 0 && scoreboard_image.pid != thepid) {
total++;
if (scoreboard_image.status != SERVER_DEAD &&
scoreboard_image.status != SERVER_READY)
running++;
*ptmp = achar;
*++ptmp = '\0';
}
}
close(score_fd);
sprintf(ptmp, " (%d/%d)", running, total);
for(loop=1;loop<=last_len;loop++)
putchar('\010');
if (last_len > strlen(tbuf)) {
for(loop=1;loop<=last_len;loop++)
putchar(' ');
for(loop=1;loop<=last_len;loop++)
putchar('\010');
}
printf("%s", tbuf);
fflush(stdout);
last_len = strlen(tbuf);
} /* for */
}
int
usage(arg)
char *arg;
{
printf("httpd_monitor: Usage\n");
printf(" httpd_monitor [ -d config-dir] [ -s sleep-time ]\n");
printf(" Defaults: config-dir = %s\n", HTTPD_ROOT);
printf(" sleep-time = %d seconds\n", DEFAULT_SLEEPTIME);
exit(0);
}
/*
* This function uses some hard-wired knowledge about the
* Apache httpd.conf file setup (basically names of the 3
* parameters we are interested in)
*
* We basically scan thru the file and grab the 3 values we
* need. This could be done better...
*/
void
lookfor(pidname, scorename, thefile)
char *pidname, *scorename;
FILE *thefile;
{
char line[ASIZE], param[ASIZE], value[ASIZE];
char sroot[ASIZE], pidfile[ASIZE], scorefile[ASIZE];
*sroot = *pidfile = *scorefile = '\0';
while (!(feof(thefile))) {
fgets(line, ASIZE-1, thefile);
*value = '\0'; /* protect braindead sscanf() */
sscanf(line, "%1024s %1024s", param, value);
if (strcmp(param, "PidFile")==0 && *value)
strcpy(pidfile, value);
if (strcmp(param, "ScoreBoardFile")==0 && *value)
strcpy(scorefile, value);
if (strcmp(param, "ServerRoot")==0 && *value)
strcpy(sroot, value);
}
/*
* We've reached EOF... we should have encountered the
* ServerRoot line... if not, we bail out
*/
if (!*sroot) {
fprintf(stderr, "Can't find ServerRoot!\n");
exit(1);
}
/*
* Not finding PidFile or ScoreBoardFile is OK, since
* we have defaults for them
*/
if (!*pidfile)
strcpy(pidfile, DEFAULT_PIDLOG);
if (!*scorefile)
strcpy(scorefile, DEFAULT_SCOREBOARD);
/*
* Relative or absolute? Handle both
*/
if (*pidfile == '/')
strcpy(pidname, pidfile);
else {
strcpy(pidname, sroot);
strcat(pidname, "/");
strcat(pidname, pidfile);
}
if (*scorefile == '/')
strcpy(scorename, scorefile);
else {
strcpy(scorename, sroot);
strcat(scorename, "/");
strcat(scorename, scorefile);
}
}
|