Skip to content

Commit 22ce389

Browse files
author
Jérôme Loyet
committed
Add html and json syntax to status page
1 parent 1e0a172 commit 22ce389

File tree

4 files changed

+93
-16
lines changed

4 files changed

+93
-16
lines changed

sapi/fpm/fpm/fpm_main.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ consult the installation file that came with this distribution, or visit \n\
17651765
SG(server_context) = (void *) &request;
17661766
init_request_info(TSRMLS_C);
17671767
CG(interactive) = 0;
1768-
char *status_buffer;
1768+
char *status_buffer, *status_content_type;
17691769

17701770
fpm_request_info();
17711771

@@ -1778,14 +1778,24 @@ consult the installation file that came with this distribution, or visit \n\
17781778
return FAILURE;
17791779
}
17801780

1781-
if (!strcasecmp(SG(request_info).request_method, "GET") && fpm_status_handle_status(SG(request_info).request_uri, &status_buffer)) {
1782-
sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
1781+
if (!strcasecmp(SG(request_info).request_method, "GET") && fpm_status_handle_status(SG(request_info).request_uri, SG(request_info).query_string, &status_buffer, &status_content_type)) {
17831782
if (status_buffer) {
17841783
int i;
1784+
1785+
if (status_content_type) {
1786+
sapi_add_header_ex(status_content_type, strlen(status_content_type), 1, 1 TSRMLS_CC);
1787+
} else {
1788+
sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
1789+
}
1790+
17851791
SG(sapi_headers).http_response_code = 200;
17861792
PUTS(status_buffer);
17871793
efree(status_buffer);
1794+
if (status_content_type) {
1795+
efree(status_content_type);
1796+
}
17881797
} else {
1798+
sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1 TSRMLS_CC);
17891799
SG(sapi_headers).http_response_code = 500;
17901800
PUTS("Unable to retrieve status\n");
17911801
}

sapi/fpm/fpm/fpm_status.c

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,73 @@ int fpm_status_get(int *idle, int *active, int *total, int *pm) /* {{{ */
144144
}
145145
/* }}} */
146146

147+
static void fpm_status_handle_status_txt(struct fpm_status_s *status, char **output, char **content_type) /* {{{ */
148+
{
149+
if (!status || !output || !content_type) {
150+
return;
151+
}
152+
153+
spprintf(output, 0,
154+
"accepted conn: %lu\n"
155+
"pool: %s\n"
156+
"process manager: %s\n"
157+
"idle processes: %d\n"
158+
"active processes: %d\n"
159+
"total processes: %d\n",
160+
status->accepted_conn, fpm_status_pool, status->pm == PM_STYLE_STATIC ? "static" : "dynamic", status->idle, status->active, status->total);
161+
162+
spprintf(content_type, 0, "text/plain");
163+
}
164+
/* }}} */
165+
166+
static void fpm_status_handle_status_html(struct fpm_status_s *status, char **output, char **content_type) /* {{{ */
167+
{
168+
if (!status || !output || !content_type) {
169+
return;
170+
}
171+
172+
spprintf(output, 0,
173+
"<table>\n"
174+
"<tr><th>accepted conn</th><td>%lu</td></tr>\n"
175+
"<tr><th>pool</th><td>%s</td></tr>\n"
176+
"<tr><th>process manager</th><td>%s</td></tr>\n"
177+
"<tr><th>idle processes</th><td>%d</td></tr>\n"
178+
"<tr><th>active processes</th><td>%d</td></tr>\n"
179+
"<tr><th>total processes</th><td>%d</td></tr>\n"
180+
"</table>",
181+
status->accepted_conn, fpm_status_pool, status->pm == PM_STYLE_STATIC ? "static" : "dynamic", status->idle, status->active, status->total);
182+
183+
spprintf(content_type, 0, "text/html");
184+
}
185+
/* }}} */
186+
187+
static void fpm_status_handle_status_json(struct fpm_status_s *status, char **output, char **content_type) /* {{{ */
188+
{
189+
if (!status || !output || !content_type) {
190+
return;
191+
}
192+
193+
spprintf(output, 0,
194+
"{"
195+
"\"accepted conn\":%lu,"
196+
"\"pool\":\"%s\","
197+
"\"process manager\":\"%s\","
198+
"\"idle processes\":%d,"
199+
"\"active processes\":%d,"
200+
"\"total processes\":%d"
201+
"}",
202+
status->accepted_conn, fpm_status_pool, status->pm == PM_STYLE_STATIC ? "static" : "dynamic", status->idle, status->active, status->total);
203+
204+
spprintf(content_type, 0, "application/jsonrequest");
205+
}
206+
/* }}} */
207+
147208
/* return 0 if it's not the request page
148209
* return 1 if ouput has been set)
149210
* *output unchanged: error (return 500)
150211
* *output changed: no error (return 200)
151212
*/
152-
int fpm_status_handle_status(char *uri, char **output) /* {{{ */
213+
int fpm_status_handle_status(char *uri, char *query_string, char **output, char **content_type) /* {{{ */
153214
{
154215
struct fpm_status_s status;
155216

@@ -162,7 +223,7 @@ int fpm_status_handle_status(char *uri, char **output) /* {{{ */
162223
return(0);
163224
}
164225

165-
if (!output || !fpm_status_shm) {
226+
if (!output || !content_type || !fpm_status_shm) {
166227
return(1);
167228
}
168229

@@ -177,16 +238,15 @@ int fpm_status_handle_status(char *uri, char **output) /* {{{ */
177238
return(1);
178239
}
179240

180-
spprintf(output, 0,
181-
"accepted conn: %lu\n"
182-
"pool: %s\n"
183-
"process manager: %s\n"
184-
"idle processes: %d\n"
185-
"active processes: %d\n"
186-
"total processes: %d\n",
187-
status.accepted_conn, fpm_status_pool, status.pm == PM_STYLE_STATIC ? "static" : "dynamic", status.idle, status.active, status.total);
241+
if (query_string && strstr(query_string, "html")) {
242+
fpm_status_handle_status_html(&status, output, content_type);
243+
} else if (query_string && strstr(query_string, "json")) {
244+
fpm_status_handle_status_json(&status, output, content_type);
245+
} else {
246+
fpm_status_handle_status_txt(&status, output, content_type);
247+
}
188248

189-
if (!*output) {
249+
if (!*output || !content_type) {
190250
zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] unable to allocate status ouput buffer", fpm_status_pool);
191251
return(1);
192252
}

sapi/fpm/fpm/fpm_status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void fpm_status_update_accepted_conn(struct fpm_shm_s *shm, unsigned long int ac
2424
void fpm_status_increment_accepted_conn(struct fpm_shm_s *shm);
2525
void fpm_status_set_pm(struct fpm_shm_s *shm, int pm);
2626
int fpm_status_get(int *idle, int *active, int *total, int *pm);
27-
int fpm_status_handle_status(char *uri, char **output);
27+
int fpm_status_handle_status(char *uri, char *query_string, char **output, char **content_type);
2828
char* fpm_status_handle_ping(char *uri);
2929

3030
extern struct fpm_shm_s *fpm_status_shm;

sapi/fpm/php-fpm.conf.in

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383

8484
Sets the status URI to call to obtain php-fpm status page.
8585
If not set, no URI will be recognized as a status page.
86-
It show text/plain looking like:
86+
By default, it returns text/plain looking like:
8787
accepted conn: 12073
8888
pool: default
8989
process manager: static
@@ -98,6 +98,13 @@
9898
"total processes": idle + active
9999
The last three number are uptaded every second.
100100
The "accepted conn" is updated in real time
101+
*** Output ***
102+
By default it returns text/plain
103+
But passing as a query string html or json, it will returns
104+
the corresponding output syntax:
105+
http://www.foo.bar/status
106+
http://www.foo.bar/status?json
107+
http://www.foo.bar/status?html
101108
*** WARNING ***
102109
It has to start with a /. It could be named has you want.
103110
It's maybe not a good idea to use .php extension to be certain

0 commit comments

Comments
 (0)