xref: /PHP-5.3/sapi/fpm/fpm/fpm_status.c (revision 151b16cf)
1 
2 	/* $Id$ */
3 	/* (c) 2009 Jerome Loyet */
4 
5 #include "php.h"
6 #include "SAPI.h"
7 #include <stdio.h>
8 
9 #include "fpm_config.h"
10 #include "fpm_scoreboard.h"
11 #include "fpm_status.h"
12 #include "fpm_clock.h"
13 #include "fpm_scoreboard.h"
14 #include "zlog.h"
15 #include "fpm_atomic.h"
16 #include "fpm_conf.h"
17 #include "fpm_php.h"
18 #include <ext/standard/html.h>
19 
20 static char *fpm_status_uri = NULL;
21 static char *fpm_status_ping_uri = NULL;
22 static char *fpm_status_ping_response = NULL;
23 
24 
fpm_status_init_child(struct fpm_worker_pool_s * wp)25 int fpm_status_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
26 {
27 	if (!wp || !wp->config) {
28 		zlog(ZLOG_ERROR, "unable to init fpm_status because conf structure is NULL");
29 		return -1;
30 	}
31 
32 	if (wp->config->pm_status_path) {
33 		fpm_status_uri = strdup(wp->config->pm_status_path);
34 	}
35 
36 	if (wp->config->ping_path) {
37 		if (!wp->config->ping_response) {
38 			zlog(ZLOG_ERROR, "[pool %s] ping is set (%s) but ping.response is not set.", wp->config->name, wp->config->ping_path);
39 			return -1;
40 		}
41 		fpm_status_ping_uri = strdup(wp->config->ping_path);
42 		fpm_status_ping_response = strdup(wp->config->ping_response);
43 	}
44 
45 	return 0;
46 }
47 /* }}} */
48 
fpm_status_handle_request(TSRMLS_D)49 int fpm_status_handle_request(TSRMLS_D) /* {{{ */
50 {
51 	struct fpm_scoreboard_s scoreboard, *scoreboard_p;
52 	struct fpm_scoreboard_proc_s proc;
53 	char *buffer, *time_format, time_buffer[64];
54 	time_t now_epoch;
55 	int full, encode;
56 	char *short_syntax, *short_post;
57 	char *full_pre, *full_syntax, *full_post, *full_separator;
58 
59 	if (!SG(request_info).request_uri) {
60 		return 0;
61 	}
62 
63 	/* PING */
64 	if (fpm_status_ping_uri && fpm_status_ping_response && !strcmp(fpm_status_ping_uri, SG(request_info).request_uri)) {
65 		fpm_request_executing();
66 		sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
67 		sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1 TSRMLS_CC);
68 		sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1 TSRMLS_CC);
69 		SG(sapi_headers).http_response_code = 200;
70 
71 		/* handle HEAD */
72 		if (SG(request_info).headers_only) {
73 			return 1;
74 		}
75 
76 		PUTS(fpm_status_ping_response);
77 		return 1;
78 	}
79 
80 	/* STATUS */
81 	if (fpm_status_uri && !strcmp(fpm_status_uri, SG(request_info).request_uri)) {
82 		fpm_request_executing();
83 
84 		scoreboard_p = fpm_scoreboard_get();
85 		if (!scoreboard_p) {
86 			zlog(ZLOG_ERROR, "status: unable to find or access status shared memory");
87 			SG(sapi_headers).http_response_code = 500;
88 			sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
89 			sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1 TSRMLS_CC);
90 			sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1 TSRMLS_CC);
91 			PUTS("Internal error. Please review log file for errors.");
92 			return 1;
93 		}
94 
95 		if (!fpm_spinlock(&scoreboard_p->lock, 1)) {
96 			zlog(ZLOG_NOTICE, "[pool %s] status: scoreboard already in used.", scoreboard_p->pool);
97 			SG(sapi_headers).http_response_code = 503;
98 			sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
99 			sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1 TSRMLS_CC);
100 			sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1 TSRMLS_CC);
101 			PUTS("Server busy. Please try again later.");
102 			return 1;
103 		}
104 		/* copy the scoreboard not to bother other processes */
105 		scoreboard = *scoreboard_p;
106 		fpm_unlock(scoreboard_p->lock);
107 
108 		if (scoreboard.idle < 0 || scoreboard.active < 0) {
109 			zlog(ZLOG_ERROR, "[pool %s] invalid status values", scoreboard.pool);
110 			SG(sapi_headers).http_response_code = 500;
111 			sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
112 			sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1 TSRMLS_CC);
113 			sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1 TSRMLS_CC);
114 			PUTS("Internal error. Please review log file for errors.");
115 			return 1;
116 		}
117 
118 		/* send common headers */
119 		sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1 TSRMLS_CC);
120 		sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1 TSRMLS_CC);
121 		SG(sapi_headers).http_response_code = 200;
122 
123 		/* handle HEAD */
124 		if (SG(request_info).headers_only) {
125 			return 1;
126 		}
127 
128 		/* full status ? */
129 		full = (fpm_php_get_string_from_table("_GET", "full" TSRMLS_CC) != NULL);
130 		short_syntax = short_post = NULL;
131 		full_separator = full_pre = full_syntax = full_post = NULL;
132 		encode = 0;
133 
134 		/* HTML */
135 		if (fpm_php_get_string_from_table("_GET", "html" TSRMLS_CC)) {
136 			sapi_add_header_ex(ZEND_STRL("Content-Type: text/html"), 1, 1 TSRMLS_CC);
137 			time_format = "%d/%b/%Y:%H:%M:%S %z";
138 			encode = 1;
139 
140 			short_syntax =
141 				"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
142 				"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
143 				"<head><title>PHP-FPM Status Page</title></head>\n"
144 				"<body>\n"
145 				"<table>\n"
146 					"<tr><th>pool</th><td>%s</td></tr>\n"
147 					"<tr><th>process manager</th><td>%s</td></tr>\n"
148 					"<tr><th>start time</th><td>%s</td></tr>\n"
149 					"<tr><th>start since</th><td>%lu</td></tr>\n"
150 					"<tr><th>accepted conn</th><td>%lu</td></tr>\n"
151 #ifdef HAVE_FPM_LQ
152 					"<tr><th>listen queue</th><td>%u</td></tr>\n"
153 					"<tr><th>max listen queue</th><td>%u</td></tr>\n"
154 					"<tr><th>listen queue len</th><td>%d</td></tr>\n"
155 #endif
156 					"<tr><th>idle processes</th><td>%d</td></tr>\n"
157 					"<tr><th>active processes</th><td>%d</td></tr>\n"
158 					"<tr><th>total processes</th><td>%d</td></tr>\n"
159 					"<tr><th>max active processes</th><td>%d</td></tr>\n"
160 					"<tr><th>max children reached</th><td>%u</td></tr>\n"
161 				"</table>\n";
162 
163 			if (!full) {
164 				short_post = "</body></html>";
165 			} else {
166 				full_pre =
167 					"<table border=\"1\">\n"
168 					"<tr>"
169 						"<th>pid</th>"
170 						"<th>state</th>"
171 						"<th>start time</th>"
172 						"<th>start since</th>"
173 						"<th>requests</th>"
174 						"<th>request duration</th>"
175 						"<th>request method</th>"
176 						"<th>request uri</th>"
177 						"<th>content length</th>"
178 						"<th>user</th>"
179 						"<th>script</th>"
180 #ifdef HAVE_FPM_LQ
181 						"<th>last request cpu</th>"
182 #endif
183 						"<th>last request memory</th>"
184 					"</tr>\n";
185 
186 				full_syntax =
187 					"<tr>"
188 						"<td>%d</td>"
189 						"<td>%s</td>"
190 						"<td>%s</td>"
191 						"<td>%lu</td>"
192 						"<td>%lu</td>"
193 						"<td>%lu</td>"
194 						"<td>%s</td>"
195 						"<td>%s%s%s</td>"
196 						"<td>%zu</td>"
197 						"<td>%s</td>"
198 						"<td>%s</td>"
199 #ifdef HAVE_FPM_LQ
200 						"<td>%.2f</td>"
201 #endif
202 						"<td>%zu</td>"
203 					"</tr>\n";
204 
205 				full_post = "</table></body></html>";
206 			}
207 
208 		/* XML */
209 		} else if (fpm_php_get_string_from_table("_GET", "xml" TSRMLS_CC)) {
210 			sapi_add_header_ex(ZEND_STRL("Content-Type: text/xml"), 1, 1 TSRMLS_CC);
211 			time_format = "%s";
212 			encode = 1;
213 
214 			short_syntax =
215 				"<?xml version=\"1.0\" ?>\n"
216 				"<status>\n"
217 				"<pool>%s</pool>\n"
218 				"<process-manager>%s</process-manager>\n"
219 				"<start-time>%s</start-time>\n"
220 				"<start-since>%lu</start-since>\n"
221 				"<accepted-conn>%lu</accepted-conn>\n"
222 #ifdef HAVE_FPM_LQ
223 				"<listen-queue>%u</listen-queue>\n"
224 				"<max-listen-queue>%u</max-listen-queue>\n"
225 				"<listen-queue-len>%d</listen-queue-len>\n"
226 #endif
227 				"<idle-processes>%d</idle-processes>\n"
228 				"<active-processes>%d</active-processes>\n"
229 				"<total-processes>%d</total-processes>\n"
230 				"<max-active-processes>%d</max-active-processes>\n"
231 				"<max-children-reached>%u</max-children-reached>\n";
232 
233 				if (!full) {
234 					short_post = "</status>";
235 				} else {
236 					full_pre = "<processes>\n";
237 					full_syntax =
238 						"<process>"
239 							"<pid>%d</pid>"
240 							"<state>%s</state>"
241 							"<start-time>%s</start-time>"
242 							"<start-since>%lu</start-since>"
243 							"<requests>%lu</requests>"
244 							"<request-duration>%lu</request-duration>"
245 							"<request-method>%s</request-method>"
246 							"<request-uri>%s%s%s</request-uri>"
247 							"<content-length>%zu</content-length>"
248 							"<user>%s</user>"
249 							"<script>%s</script>"
250 #ifdef HAVE_FPM_LQ
251 							"<last-request-cpu>%.2f</last-request-cpu>"
252 #endif
253 							"<last-request-memory>%zu</last-request-memory>"
254 						"</process>\n"
255 					;
256 					full_post = "</processes>\n</status>";
257 				}
258 
259 			/* JSON */
260 		} else if (fpm_php_get_string_from_table("_GET", "json" TSRMLS_CC)) {
261 			sapi_add_header_ex(ZEND_STRL("Content-Type: application/json"), 1, 1 TSRMLS_CC);
262 			time_format = "%s";
263 
264 			short_syntax =
265 				"{"
266 				"\"pool\":\"%s\","
267 				"\"process manager\":\"%s\","
268 				"\"start time\":%s,"
269 				"\"start since\":%lu,"
270 				"\"accepted conn\":%lu,"
271 #ifdef HAVE_FPM_LQ
272 				"\"listen queue\":%u,"
273 				"\"max listen queue\":%u,"
274 				"\"listen queue len\":%d,"
275 #endif
276 				"\"idle processes\":%d,"
277 				"\"active processes\":%d,"
278 				"\"total processes\":%d,"
279 				"\"max active processes\":%d,"
280 				"\"max children reached\":%u";
281 
282 			if (!full) {
283 				short_post = "}";
284 			} else {
285 				full_separator = ",";
286 				full_pre = ", \"processes\":[";
287 
288 				full_syntax = "{"
289 					"\"pid\":%d,"
290 					"\"state\":\"%s\","
291 					"\"start time\":%s,"
292 					"\"start since\":%lu,"
293 					"\"requests\":%lu,"
294 					"\"request duration\":%lu,"
295 					"\"request method\":\"%s\","
296 					"\"request uri\":\"%s%s%s\","
297 					"\"content length\":%zu,"
298 					"\"user\":\"%s\","
299 					"\"script\":\"%s\","
300 #ifdef HAVE_FPM_LQ
301 					"\"last request cpu\":%.2f,"
302 #endif
303 					"\"last request memory\":%zu"
304 					"}";
305 
306 				full_post = "]}";
307 			}
308 
309 		/* TEXT */
310 		} else {
311 			sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
312 			time_format = "%d/%b/%Y:%H:%M:%S %z";
313 
314 			short_syntax =
315 				"pool:                 %s\n"
316 				"process manager:      %s\n"
317 				"start time:           %s\n"
318 				"start since:          %lu\n"
319 				"accepted conn:        %lu\n"
320 #ifdef HAVE_FPM_LQ
321 				"listen queue:         %u\n"
322 				"max listen queue:     %u\n"
323 				"listen queue len:     %d\n"
324 #endif
325 				"idle processes:       %d\n"
326 				"active processes:     %d\n"
327 				"total processes:      %d\n"
328 				"max active processes: %d\n"
329 				"max children reached: %u\n";
330 
331 				if (full) {
332 					full_syntax =
333 						"\n"
334 						"************************\n"
335 						"pid:                  %d\n"
336 						"state:                %s\n"
337 						"start time:           %s\n"
338 						"start since:          %lu\n"
339 						"requests:             %lu\n"
340 						"request duration:     %lu\n"
341 						"request method:       %s\n"
342 						"request URI:          %s%s%s\n"
343 						"content length:       %zu\n"
344 						"user:                 %s\n"
345 						"script:               %s\n"
346 #ifdef HAVE_FPM_LQ
347 						"last request cpu:     %.2f\n"
348 #endif
349 						"last request memory:  %zu\n";
350 				}
351 		}
352 
353 		strftime(time_buffer, sizeof(time_buffer) - 1, time_format, localtime(&scoreboard.start_epoch));
354 		now_epoch = time(NULL);
355 		spprintf(&buffer, 0, short_syntax,
356 				scoreboard.pool,
357 				PM2STR(scoreboard.pm),
358 				time_buffer,
359 				now_epoch - scoreboard.start_epoch,
360 				scoreboard.requests,
361 #ifdef HAVE_FPM_LQ
362 				scoreboard.lq,
363 				scoreboard.lq_max,
364 				scoreboard.lq_len,
365 #endif
366 				scoreboard.idle,
367 				scoreboard.active,
368 				scoreboard.idle + scoreboard.active,
369 				scoreboard.active_max,
370 				scoreboard.max_children_reached);
371 
372 		PUTS(buffer);
373 		efree(buffer);
374 
375 		if (short_post) {
376 			PUTS(short_post);
377 		}
378 
379 		/* no need to test the var 'full' */
380 		if (full_syntax) {
381 			int i, len, first;
382 			char *query_string;
383 			struct timeval duration, now;
384 #ifdef HAVE_FPM_LQ
385 			float cpu;
386 #endif
387 
388 			fpm_clock_get(&now);
389 
390 			if (full_pre) {
391 				PUTS(full_pre);
392 			}
393 
394 			first = 1;
395 			for (i=0; i<scoreboard_p->nprocs; i++) {
396 				if (!scoreboard_p->procs[i] || !scoreboard_p->procs[i]->used) {
397 					continue;
398 				}
399 				proc = *scoreboard_p->procs[i];
400 
401 				if (first) {
402 					first = 0;
403 				} else {
404 					if (full_separator) {
405 						PUTS(full_separator);
406 					}
407 				}
408 
409 				query_string = NULL;
410 				len = 0;
411 				if (proc.query_string[0] != '\0') {
412 					if (!encode) {
413 						query_string = proc.query_string;
414 					} else {
415 						query_string = php_escape_html_entities_ex((unsigned char *)proc.query_string, strlen(proc.query_string), &len, 1, ENT_HTML_IGNORE_ERRORS & ENT_COMPAT, NULL, 1 TSRMLS_CC);
416 					}
417 				}
418 
419 #ifdef HAVE_FPM_LQ
420 				/* prevent NaN */
421 				if (proc.cpu_duration.tv_sec == 0 && proc.cpu_duration.tv_usec == 0) {
422 					cpu = 0.;
423 				} else {
424 					cpu = (proc.last_request_cpu.tms_utime + proc.last_request_cpu.tms_stime + proc.last_request_cpu.tms_cutime + proc.last_request_cpu.tms_cstime) / fpm_scoreboard_get_tick() / (proc.cpu_duration.tv_sec + proc.cpu_duration.tv_usec / 1000000.) * 100.;
425 				}
426 #endif
427 
428 				if (proc.request_stage == FPM_REQUEST_ACCEPTING) {
429 					duration = proc.duration;
430 				} else {
431 					timersub(&now, &proc.accepted, &duration);
432 				}
433 				strftime(time_buffer, sizeof(time_buffer) - 1, time_format, localtime(&proc.start_epoch));
434 				spprintf(&buffer, 0, full_syntax,
435 					proc.pid,
436 					fpm_request_get_stage_name(proc.request_stage),
437 					time_buffer,
438 					now_epoch - proc.start_epoch,
439 					proc.requests,
440 					duration.tv_sec * 1000000UL + duration.tv_usec,
441 					proc.request_method[0] != '\0' ? proc.request_method : "-",
442 					proc.request_uri[0] != '\0' ? proc.request_uri : "-",
443 					query_string ? "?" : "",
444 					query_string ? query_string : "",
445 					proc.content_length,
446 					proc.auth_user[0] != '\0' ? proc.auth_user : "-",
447 					proc.script_filename[0] != '\0' ? proc.script_filename : "-",
448 #ifdef HAVE_FPM_LQ
449 					proc.request_stage == FPM_REQUEST_ACCEPTING ? cpu : 0.,
450 #endif
451 					proc.request_stage == FPM_REQUEST_ACCEPTING ? proc.memory : 0);
452 				PUTS(buffer);
453 				efree(buffer);
454 
455 				if (len > 0 && query_string) {
456 					efree(query_string);
457 				}
458 			}
459 
460 			if (full_post) {
461 				PUTS(full_post);
462 			}
463 		}
464 
465 		return 1;
466 	}
467 
468 	return 0;
469 }
470 /* }}} */
471 
472