1 /* (c) 2009 Jerome Loyet */
2
3 #include "php.h"
4 #include "zend_long.h"
5 #include "SAPI.h"
6 #include <stdio.h>
7
8 #include "fpm_config.h"
9 #include "fpm_scoreboard.h"
10 #include "fpm_status.h"
11 #include "fpm_clock.h"
12 #include "zlog.h"
13 #include "fpm_atomic.h"
14 #include "fpm_conf.h"
15 #include "fpm_php.h"
16 #include "ext/standard/html.h"
17 #include "ext/json/php_json.h"
18
19 static char *fpm_status_uri = NULL;
20 static char *fpm_status_ping_uri = NULL;
21 static char *fpm_status_ping_response = NULL;
22
23
fpm_status_init_child(struct fpm_worker_pool_s * wp)24 int fpm_status_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
25 {
26 if (!wp || !wp->config) {
27 zlog(ZLOG_ERROR, "unable to init fpm_status because conf structure is NULL");
28 return -1;
29 }
30
31 if (wp->config->pm_status_path) {
32 fpm_status_uri = strdup(wp->config->pm_status_path);
33 }
34
35 if (wp->config->ping_path) {
36 if (!wp->config->ping_response) {
37 zlog(ZLOG_ERROR, "[pool %s] ping is set (%s) but ping.response is not set.", wp->config->name, wp->config->ping_path);
38 return -1;
39 }
40 fpm_status_ping_uri = strdup(wp->config->ping_path);
41 fpm_status_ping_response = strdup(wp->config->ping_response);
42 }
43
44 return 0;
45 }
46 /* }}} */
47
fpm_status_export_to_zval(zval * status)48 int fpm_status_export_to_zval(zval *status)
49 {
50 struct fpm_scoreboard_s scoreboard, *scoreboard_p;
51 zval fpm_proc_stats, fpm_proc_stat;
52 time_t now_epoch;
53 struct timeval duration, now;
54 double cpu;
55 int i;
56
57 scoreboard_p = fpm_scoreboard_acquire(NULL, 1);
58 if (!scoreboard_p) {
59 zlog(ZLOG_NOTICE, "[pool (unknown)] status: scoreboard already in use.");
60 return -1;
61 }
62
63 /* copy the scoreboard not to bother other processes */
64 scoreboard = *scoreboard_p;
65 struct fpm_scoreboard_proc_s procs[scoreboard.nprocs];
66
67 struct fpm_scoreboard_proc_s *proc_p;
68 for(i=0; i<scoreboard.nprocs; i++) {
69 proc_p = fpm_scoreboard_proc_acquire(scoreboard_p, i, 1);
70 if (!proc_p){
71 procs[i].used=-1;
72 continue;
73 }
74 procs[i] = *proc_p;
75 fpm_scoreboard_proc_release(proc_p);
76 }
77 fpm_scoreboard_release(scoreboard_p);
78
79 now_epoch = time(NULL);
80 fpm_clock_get(&now);
81
82 array_init(status);
83 add_assoc_string(status, "pool", scoreboard.pool);
84 add_assoc_string(status, "process-manager", PM2STR(scoreboard.pm));
85 add_assoc_long(status, "start-time", scoreboard.start_epoch);
86 add_assoc_long(status, "start-since", now_epoch - scoreboard.start_epoch);
87 add_assoc_long(status, "accepted-conn", scoreboard.requests);
88 add_assoc_long(status, "listen-queue", scoreboard.lq);
89 add_assoc_long(status, "max-listen-queue", scoreboard.lq_max);
90 add_assoc_long(status, "listen-queue-len", scoreboard.lq_len);
91 add_assoc_long(status, "idle-processes", scoreboard.idle);
92 add_assoc_long(status, "active-processes", scoreboard.active);
93 add_assoc_long(status, "total-processes", scoreboard.idle + scoreboard.active);
94 add_assoc_long(status, "max-active-processes", scoreboard.active_max);
95 add_assoc_long(status, "max-children-reached", scoreboard.max_children_reached);
96 add_assoc_long(status, "slow-requests", scoreboard.slow_rq);
97
98 array_init(&fpm_proc_stats);
99 for(i=0; i<scoreboard.nprocs; i++) {
100 if (!procs[i].used) {
101 continue;
102 }
103 proc_p = &procs[i];
104 /* prevent NaN */
105 if (procs[i].cpu_duration.tv_sec == 0 && procs[i].cpu_duration.tv_usec == 0) {
106 cpu = 0.;
107 } else {
108 cpu = (procs[i].last_request_cpu.tms_utime + procs[i].last_request_cpu.tms_stime + procs[i].last_request_cpu.tms_cutime + procs[i].last_request_cpu.tms_cstime) / fpm_scoreboard_get_tick() / (procs[i].cpu_duration.tv_sec + procs[i].cpu_duration.tv_usec / 1000000.) * 100.;
109 }
110
111 array_init(&fpm_proc_stat);
112 add_assoc_long(&fpm_proc_stat, "pid", procs[i].pid);
113 add_assoc_string(&fpm_proc_stat, "state", fpm_request_get_stage_name(procs[i].request_stage));
114 add_assoc_long(&fpm_proc_stat, "start-time", procs[i].start_epoch);
115 add_assoc_long(&fpm_proc_stat, "start-since", now_epoch - procs[i].start_epoch);
116 add_assoc_long(&fpm_proc_stat, "requests", procs[i].requests);
117 if (procs[i].request_stage == FPM_REQUEST_ACCEPTING) {
118 duration = procs[i].duration;
119 } else {
120 timersub(&now, &procs[i].accepted, &duration);
121 }
122 add_assoc_long(&fpm_proc_stat, "request-duration", duration.tv_sec * 1000000UL + duration.tv_usec);
123 add_assoc_string(&fpm_proc_stat, "request-method", procs[i].request_method[0] != '\0' ? procs[i].request_method : "-");
124 add_assoc_string(&fpm_proc_stat, "request-uri", procs[i].request_uri);
125 add_assoc_string(&fpm_proc_stat, "query-string", procs[i].query_string);
126 add_assoc_long(&fpm_proc_stat, "request-length", procs[i].content_length);
127 add_assoc_string(&fpm_proc_stat, "user", procs[i].auth_user[0] != '\0' ? procs[i].auth_user : "-");
128 add_assoc_string(&fpm_proc_stat, "script", procs[i].script_filename[0] != '\0' ? procs[i].script_filename : "-");
129 add_assoc_double(&fpm_proc_stat, "last-request-cpu", procs[i].request_stage == FPM_REQUEST_ACCEPTING ? cpu : 0.);
130 add_assoc_long(&fpm_proc_stat, "last-request-memory", procs[i].request_stage == FPM_REQUEST_ACCEPTING ? procs[i].memory : 0);
131 add_next_index_zval(&fpm_proc_stats, &fpm_proc_stat);
132 }
133 add_assoc_zval(status, "procs", &fpm_proc_stats);
134 return 0;
135 }
136 /* }}} */
137
fpm_status_handle_request(void)138 int fpm_status_handle_request(void) /* {{{ */
139 {
140 struct fpm_scoreboard_s *scoreboard_p;
141 struct fpm_scoreboard_proc_s *proc;
142 char *buffer, *time_format, time_buffer[64];
143 time_t now_epoch;
144 int full, has_start_time;
145 bool encode_html, encode_json;
146 char *short_syntax, *short_post;
147 char *full_pre, *full_syntax, *full_post, *full_separator;
148 zend_string *_GET_str;
149
150 if (!SG(request_info).request_uri) {
151 return 0;
152 }
153
154 /* PING */
155 if (fpm_status_ping_uri && fpm_status_ping_response && !strcmp(fpm_status_ping_uri, SG(request_info).request_uri)) {
156 fpm_request_executing();
157 sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1);
158 sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1);
159 sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1);
160 SG(sapi_headers).http_response_code = 200;
161
162 /* handle HEAD */
163 if (SG(request_info).headers_only) {
164 return 1;
165 }
166
167 PUTS(fpm_status_ping_response);
168 return 1;
169 }
170
171 /* STATUS */
172 if (fpm_status_uri && !strcmp(fpm_status_uri, SG(request_info).request_uri)) {
173 fpm_request_executing();
174
175 /* full status ? */
176 _GET_str = zend_string_init("_GET", sizeof("_GET")-1, 0);
177 full = (fpm_php_get_string_from_table(_GET_str, "full") != NULL);
178 short_syntax = short_post = NULL;
179 full_separator = full_pre = full_syntax = full_post = NULL;
180 encode_html = false;
181 encode_json = false;
182 has_start_time = 1;
183
184 scoreboard_p = fpm_scoreboard_get();
185 if (scoreboard_p) {
186 scoreboard_p = fpm_scoreboard_copy(scoreboard_p->shared ? scoreboard_p->shared : scoreboard_p, full);
187 }
188 if (!scoreboard_p) {
189 zlog(ZLOG_ERROR, "status: unable to find or access status shared memory");
190 SG(sapi_headers).http_response_code = 500;
191 sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1);
192 sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1);
193 sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1);
194 PUTS("Internal error. Please review log file for errors.");
195 return 1;
196 }
197
198 if (scoreboard_p->idle < 0 || scoreboard_p->active < 0) {
199 fpm_scoreboard_free_copy(scoreboard_p);
200 zlog(ZLOG_ERROR, "[pool %s] invalid status values", scoreboard_p->pool);
201 SG(sapi_headers).http_response_code = 500;
202 sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1);
203 sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1);
204 sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1);
205 PUTS("Internal error. Please review log file for errors.");
206 return 1;
207 }
208
209 /* send common headers */
210 sapi_add_header_ex(ZEND_STRL("Expires: Thu, 01 Jan 1970 00:00:00 GMT"), 1, 1);
211 sapi_add_header_ex(ZEND_STRL("Cache-Control: no-cache, no-store, must-revalidate, max-age=0"), 1, 1);
212 SG(sapi_headers).http_response_code = 200;
213
214 /* handle HEAD */
215 if (SG(request_info).headers_only) {
216 fpm_scoreboard_free_copy(scoreboard_p);
217 return 1;
218 }
219
220 /* HTML */
221 if (fpm_php_get_string_from_table(_GET_str, "html")) {
222 sapi_add_header_ex(ZEND_STRL("Content-Type: text/html"), 1, 1);
223 time_format = "%d/%b/%Y:%H:%M:%S %z";
224 encode_html = true;
225
226 short_syntax =
227 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
228 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
229 "<head><title>PHP-FPM Status Page</title></head>\n"
230 "<body>\n"
231 "<table>\n"
232 "<tr><th>pool</th><td>%s</td></tr>\n"
233 "<tr><th>process manager</th><td>%s</td></tr>\n"
234 "<tr><th>start time</th><td>%s</td></tr>\n"
235 "<tr><th>start since</th><td>%lu</td></tr>\n"
236 "<tr><th>accepted conn</th><td>%lu</td></tr>\n"
237 "<tr><th>listen queue</th><td>%d</td></tr>\n"
238 "<tr><th>max listen queue</th><td>%d</td></tr>\n"
239 "<tr><th>listen queue len</th><td>%u</td></tr>\n"
240 "<tr><th>idle processes</th><td>%d</td></tr>\n"
241 "<tr><th>active processes</th><td>%d</td></tr>\n"
242 "<tr><th>total processes</th><td>%d</td></tr>\n"
243 "<tr><th>max active processes</th><td>%d</td></tr>\n"
244 "<tr><th>max children reached</th><td>%u</td></tr>\n"
245 "<tr><th>slow requests</th><td>%lu</td></tr>\n"
246 "</table>\n";
247
248 if (!full) {
249 short_post = "</body></html>";
250 } else {
251 full_pre =
252 "<table border=\"1\">\n"
253 "<tr>"
254 "<th>pid</th>"
255 "<th>state</th>"
256 "<th>start time</th>"
257 "<th>start since</th>"
258 "<th>requests</th>"
259 "<th>request duration</th>"
260 "<th>request method</th>"
261 "<th>request uri</th>"
262 "<th>content length</th>"
263 "<th>user</th>"
264 "<th>script</th>"
265 "<th>last request cpu</th>"
266 "<th>last request memory</th>"
267 "</tr>\n";
268
269 full_syntax =
270 "<tr>"
271 "<td>%d</td>"
272 "<td>%s</td>"
273 "<td>%s</td>"
274 "<td>%lu</td>"
275 "<td>%lu</td>"
276 "<td>%lu</td>"
277 "<td>%s</td>"
278 "<td>%s%s%s</td>"
279 "<td>%zu</td>"
280 "<td>%s</td>"
281 "<td>%s</td>"
282 "<td>%.2f</td>"
283 "<td>%zu</td>"
284 "</tr>\n";
285
286 full_post = "</table></body></html>";
287 }
288
289 /* XML */
290 } else if (fpm_php_get_string_from_table(_GET_str, "xml")) {
291 sapi_add_header_ex(ZEND_STRL("Content-Type: text/xml"), 1, 1);
292 time_format = "%s";
293 encode_html = true;
294
295 short_syntax =
296 "<?xml version=\"1.0\" ?>\n"
297 "<status>\n"
298 "<pool>%s</pool>\n"
299 "<process-manager>%s</process-manager>\n"
300 "<start-time>%s</start-time>\n"
301 "<start-since>%lu</start-since>\n"
302 "<accepted-conn>%lu</accepted-conn>\n"
303 "<listen-queue>%d</listen-queue>\n"
304 "<max-listen-queue>%d</max-listen-queue>\n"
305 "<listen-queue-len>%u</listen-queue-len>\n"
306 "<idle-processes>%d</idle-processes>\n"
307 "<active-processes>%d</active-processes>\n"
308 "<total-processes>%d</total-processes>\n"
309 "<max-active-processes>%d</max-active-processes>\n"
310 "<max-children-reached>%u</max-children-reached>\n"
311 "<slow-requests>%lu</slow-requests>\n";
312
313 if (!full) {
314 short_post = "</status>";
315 } else {
316 full_pre = "<processes>\n";
317 full_syntax =
318 "<process>"
319 "<pid>%d</pid>"
320 "<state>%s</state>"
321 "<start-time>%s</start-time>"
322 "<start-since>%lu</start-since>"
323 "<requests>%lu</requests>"
324 "<request-duration>%lu</request-duration>"
325 "<request-method>%s</request-method>"
326 "<request-uri>%s%s%s</request-uri>"
327 "<content-length>%zu</content-length>"
328 "<user>%s</user>"
329 "<script>%s</script>"
330 "<last-request-cpu>%.2f</last-request-cpu>"
331 "<last-request-memory>%zu</last-request-memory>"
332 "</process>\n"
333 ;
334 full_post = "</processes>\n</status>";
335 }
336
337 /* JSON */
338 } else if (fpm_php_get_string_from_table(_GET_str, "json")) {
339 sapi_add_header_ex(ZEND_STRL("Content-Type: application/json"), 1, 1);
340 time_format = "%s";
341
342 encode_json = true;
343
344 short_syntax =
345 "{"
346 "\"pool\":\"%s\","
347 "\"process manager\":\"%s\","
348 "\"start time\":%s,"
349 "\"start since\":%lu,"
350 "\"accepted conn\":%lu,"
351 "\"listen queue\":%d,"
352 "\"max listen queue\":%d,"
353 "\"listen queue len\":%u,"
354 "\"idle processes\":%d,"
355 "\"active processes\":%d,"
356 "\"total processes\":%d,"
357 "\"max active processes\":%d,"
358 "\"max children reached\":%u,"
359 "\"slow requests\":%lu";
360
361 if (!full) {
362 short_post = "}";
363 } else {
364 full_separator = ",";
365 full_pre = ", \"processes\":[";
366
367 full_syntax = "{"
368 "\"pid\":%d,"
369 "\"state\":\"%s\","
370 "\"start time\":%s,"
371 "\"start since\":%lu,"
372 "\"requests\":%lu,"
373 "\"request duration\":%lu,"
374 "\"request method\":\"%s\","
375 "\"request uri\":\"%s%s%s\","
376 "\"content length\":%zu,"
377 "\"user\":\"%s\","
378 "\"script\":\"%s\","
379 "\"last request cpu\":%.2f,"
380 "\"last request memory\":%zu"
381 "}";
382
383 full_post = "]}";
384 }
385
386 /* OpenMetrics */
387 } else if (fpm_php_get_string_from_table(_GET_str, "openmetrics")) {
388 sapi_add_header_ex(ZEND_STRL("Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8"), 1, 1);
389 time_format = "%s";
390
391 short_syntax =
392 "# HELP phpfpm_up Could pool %s using a %s PM on PHP-FPM be reached?\n"
393 "# TYPE phpfpm_up gauge\n"
394 "phpfpm_up 1\n"
395 "# HELP phpfpm_start_since The number of seconds since FPM has started.\n"
396 "# TYPE phpfpm_start_since counter\n"
397 "phpfpm_start_since %lu\n"
398 "# HELP phpfpm_accepted_connections The number of requests accepted by the pool.\n"
399 "# TYPE phpfpm_accepted_connections counter\n"
400 "phpfpm_accepted_connections %lu\n"
401 "# HELP phpfpm_listen_queue The number of requests in the queue of pending connections.\n"
402 "# TYPE phpfpm_listen_queue gauge\n"
403 "phpfpm_listen_queue %d\n"
404 "# HELP phpfpm_max_listen_queue The maximum number of requests in the queue of pending connections since FPM has started.\n"
405 "# TYPE phpfpm_max_listen_queue counter\n"
406 "phpfpm_max_listen_queue %d\n"
407 "# TYPE phpfpm_listen_queue_length gauge\n"
408 "# HELP phpfpm_listen_queue_length The size of the socket queue of pending connections.\n"
409 "phpfpm_listen_queue_length %u\n"
410 "# HELP phpfpm_idle_processes The number of idle processes.\n"
411 "# TYPE phpfpm_idle_processes gauge\n"
412 "phpfpm_idle_processes %d\n"
413 "# HELP phpfpm_active_processes The number of active processes.\n"
414 "# TYPE phpfpm_active_processes gauge\n"
415 "phpfpm_active_processes %d\n"
416 "# HELP phpfpm_total_processes The number of idle + active processes.\n"
417 "# TYPE phpfpm_total_processes gauge\n"
418 "phpfpm_total_processes %d\n"
419 "# HELP phpfpm_max_active_processes The maximum number of active processes since FPM has started.\n"
420 "# TYPE phpfpm_max_active_processes counter\n"
421 "phpfpm_max_active_processes %d\n"
422 "# HELP phpfpm_max_children_reached The number of times, the process limit has been reached, when pm tries to start more children (works only for pm 'dynamic' and 'ondemand').\n"
423 "# TYPE phpfpm_max_children_reached counter\n"
424 "phpfpm_max_children_reached %u\n"
425 "# HELP phpfpm_slow_requests The number of requests that exceeded your 'request_slowlog_timeout' value.\n"
426 "# TYPE phpfpm_slow_requests counter\n"
427 "phpfpm_slow_requests %lu\n"
428 "# EOF\n";
429
430 has_start_time = 0;
431 if (!full) {
432 short_post = "";
433 } else {
434 full_separator = "";
435 full_pre = "";
436 full_syntax = "";
437 full_post = "";
438 }
439
440 /* TEXT */
441 } else {
442 sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1);
443 time_format = "%d/%b/%Y:%H:%M:%S %z";
444
445 short_syntax =
446 "pool: %s\n"
447 "process manager: %s\n"
448 "start time: %s\n"
449 "start since: %lu\n"
450 "accepted conn: %lu\n"
451 "listen queue: %d\n"
452 "max listen queue: %d\n"
453 "listen queue len: %u\n"
454 "idle processes: %d\n"
455 "active processes: %d\n"
456 "total processes: %d\n"
457 "max active processes: %d\n"
458 "max children reached: %u\n"
459 "slow requests: %lu\n";
460
461 if (full) {
462 full_syntax =
463 "\n"
464 "************************\n"
465 "pid: %d\n"
466 "state: %s\n"
467 "start time: %s\n"
468 "start since: %lu\n"
469 "requests: %lu\n"
470 "request duration: %lu\n"
471 "request method: %s\n"
472 "request URI: %s%s%s\n"
473 "content length: %zu\n"
474 "user: %s\n"
475 "script: %s\n"
476 "last request cpu: %.2f\n"
477 "last request memory: %zu\n";
478 }
479 }
480
481 now_epoch = time(NULL);
482 if (has_start_time) {
483 strftime(time_buffer, sizeof(time_buffer) - 1, time_format, localtime(&scoreboard_p->start_epoch));
484 spprintf(&buffer, 0, short_syntax,
485 scoreboard_p->pool,
486 PM2STR(scoreboard_p->pm),
487 time_buffer,
488 (unsigned long) (now_epoch - scoreboard_p->start_epoch),
489 scoreboard_p->requests,
490 scoreboard_p->lq,
491 scoreboard_p->lq_max,
492 scoreboard_p->lq_len,
493 scoreboard_p->idle,
494 scoreboard_p->active,
495 scoreboard_p->idle + scoreboard_p->active,
496 scoreboard_p->active_max,
497 scoreboard_p->max_children_reached,
498 scoreboard_p->slow_rq);
499 } else {
500 spprintf(&buffer, 0, short_syntax,
501 scoreboard_p->pool,
502 PM2STR(scoreboard_p->pm),
503 (unsigned long) (now_epoch - scoreboard_p->start_epoch),
504 scoreboard_p->requests,
505 scoreboard_p->lq,
506 scoreboard_p->lq_max,
507 scoreboard_p->lq_len,
508 scoreboard_p->idle,
509 scoreboard_p->active,
510 scoreboard_p->idle + scoreboard_p->active,
511 scoreboard_p->active_max,
512 scoreboard_p->max_children_reached,
513 scoreboard_p->slow_rq);
514 }
515
516 PUTS(buffer);
517 efree(buffer);
518 zend_string_release_ex(_GET_str, 0);
519
520 if (short_post) {
521 PUTS(short_post);
522 }
523
524 /* no need to test the var 'full' */
525 if (full_syntax) {
526 unsigned int i;
527 int first;
528 zend_string *tmp_query_string;
529 char *query_string;
530 struct timeval duration, now;
531 float cpu;
532
533 fpm_clock_get(&now);
534
535 if (full_pre) {
536 PUTS(full_pre);
537 }
538
539 first = 1;
540 for (i=0; i<scoreboard_p->nprocs; i++) {
541 if (!scoreboard_p->procs[i].used) {
542 continue;
543 }
544 proc = &scoreboard_p->procs[i];
545
546 if (first) {
547 first = 0;
548 } else {
549 if (full_separator) {
550 PUTS(full_separator);
551 }
552 }
553
554 query_string = NULL;
555 tmp_query_string = NULL;
556 if (proc->query_string[0] != '\0') {
557 if (encode_html) {
558 tmp_query_string = php_escape_html_entities_ex(
559 (const unsigned char *) proc->query_string,
560 strlen(proc->query_string), 1, ENT_HTML_IGNORE_ERRORS & ENT_COMPAT,
561 NULL, /* double_encode */ 1, /* quiet */ 0);
562 } else if (encode_json) {
563 tmp_query_string = php_json_encode_string(proc->query_string,
564 strlen(proc->query_string), PHP_JSON_INVALID_UTF8_IGNORE);
565 } else {
566 query_string = proc->query_string;
567 }
568 if (tmp_query_string) {
569 query_string = ZSTR_VAL(tmp_query_string);
570 /* remove quotes around the string */
571 if (encode_json && ZSTR_LEN(tmp_query_string) >= 2) {
572 query_string[ZSTR_LEN(tmp_query_string) - 1] = '\0';
573 ++query_string;
574 }
575 }
576 }
577
578 /* prevent NaN */
579 if (proc->cpu_duration.tv_sec == 0 && proc->cpu_duration.tv_usec == 0) {
580 cpu = 0.;
581 } else {
582 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.;
583 }
584
585 if (proc->request_stage == FPM_REQUEST_ACCEPTING) {
586 duration = proc->duration;
587 } else {
588 timersub(&now, &proc->accepted, &duration);
589 }
590 strftime(time_buffer, sizeof(time_buffer) - 1, time_format, localtime(&proc->start_epoch));
591 spprintf(&buffer, 0, full_syntax,
592 (int) proc->pid,
593 fpm_request_get_stage_name(proc->request_stage),
594 time_buffer,
595 (unsigned long) (now_epoch - proc->start_epoch),
596 proc->requests,
597 duration.tv_sec * 1000000UL + duration.tv_usec,
598 proc->request_method[0] != '\0' ? proc->request_method : "-",
599 proc->request_uri[0] != '\0' ? proc->request_uri : "-",
600 query_string ? "?" : "",
601 query_string ? query_string : "",
602 proc->content_length,
603 proc->auth_user[0] != '\0' ? proc->auth_user : "-",
604 proc->script_filename[0] != '\0' ? proc->script_filename : "-",
605 proc->request_stage == FPM_REQUEST_ACCEPTING ? cpu : 0.,
606 proc->request_stage == FPM_REQUEST_ACCEPTING ? proc->memory : 0);
607 PUTS(buffer);
608 efree(buffer);
609
610 if (tmp_query_string) {
611 zend_string_free(tmp_query_string);
612 }
613 }
614
615 if (full_post) {
616 PUTS(full_post);
617 }
618 }
619
620 fpm_scoreboard_free_copy(scoreboard_p);
621 return 1;
622 }
623
624 return 0;
625 }
626 /* }}} */
627