diff --git a/core/emperor.c b/core/emperor.c index 1b02b541..8fcbcc0e 100644 --- a/core/emperor.c +++ b/core/emperor.c @@ -1129,6 +1129,13 @@ void emperor_send_stats(int fd) { return; } + if (uwsgi.stats_http) { + if (uwsgi_send_http_stats(client_fd)) { + close(client_fd); + return; + } + } + struct uwsgi_stats *us = uwsgi_stats_new(8192); if (uwsgi_stats_keyval_comma(us, "version", UWSGI_VERSION)) diff --git a/core/master_utils.c b/core/master_utils.c index da123c88..ed1109e7 100644 --- a/core/master_utils.c +++ b/core/master_utils.c @@ -778,6 +778,13 @@ void uwsgi_send_stats(int fd) { return; } + if (uwsgi.stats_http) { + if (uwsgi_send_http_stats(client_fd)) { + close(client_fd); + return; + } + } + struct uwsgi_stats *us = uwsgi_stats_new(8192); if (uwsgi_stats_keyval_comma(us, "version", UWSGI_VERSION)) diff --git a/core/utils.c b/core/utils.c index 5be77980..3b7f9355 100644 --- a/core/utils.c +++ b/core/utils.c @@ -4891,3 +4891,21 @@ int uwsgi_kvlist_parse(char *src, size_t len, char list_separator, char kv_separ free(buf); return 0; } + +int uwsgi_send_http_stats(int fd) { + struct uwsgi_buffer *ub = uwsgi_buffer_new(uwsgi.page_size); + if (!ub) return -1; + + if (uwsgi_buffer_append(ub, "HTTP/1.0 200 OK\r\n", 17)) goto error; + if (uwsgi_buffer_append(ub, "Connection: close\r\n", 19)) goto error; + if (uwsgi_buffer_append(ub, "Content-Type: application/json\r\n", 32)) goto error; + if (uwsgi_buffer_append(ub, "\r\n", 2)) goto error; + + if (uwsgi_buffer_send(ub, fd)) goto error; + uwsgi_buffer_destroy(ub); + return 0; + +error: + uwsgi_buffer_destroy(ub); + return -1; +} diff --git a/core/uwsgi.c b/core/uwsgi.c index e59d1453..3daea320 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -305,6 +305,7 @@ static struct uwsgi_option uwsgi_base_options[] = { #endif {"stats", required_argument, 0, "enable the stats server on the specified address", uwsgi_opt_set_str, &uwsgi.stats, UWSGI_OPT_MASTER}, {"stats-server", required_argument, 0, "enable the stats server on the specified address", uwsgi_opt_set_str, &uwsgi.stats, UWSGI_OPT_MASTER}, + {"stats-http", no_argument, 0, "prefix stats server json output with http headers", uwsgi_opt_true, &uwsgi.stats_http, UWSGI_OPT_MASTER}, #ifdef UWSGI_MULTICAST {"multicast", required_argument, 0, "subscribe to specified multicast group", uwsgi_opt_set_str, &uwsgi.multicast_group, UWSGI_OPT_MASTER}, {"multicast-ttl", required_argument, 0, "set multicast ttl", uwsgi_opt_set_int, &uwsgi.multicast_ttl, 0}, diff --git a/plugins/corerouter/corerouter.c b/plugins/corerouter/corerouter.c index 98fbc5b5..e67f00b1 100644 --- a/plugins/corerouter/corerouter.c +++ b/plugins/corerouter/corerouter.c @@ -933,6 +933,13 @@ void corerouter_send_stats(struct uwsgi_corerouter *ucr) { return; } + if (uwsgi.stats_http) { + if (uwsgi_send_http_stats(client_fd)) { + close(client_fd); + return; + } + } + struct uwsgi_stats *us = uwsgi_stats_new(8192); if (uwsgi_stats_keyval_comma(us, "version", UWSGI_VERSION)) goto end; diff --git a/uwsgi.h b/uwsgi.h index 4ee728a9..759378d9 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1832,6 +1832,7 @@ struct uwsgi_server { // the stats server char *stats; int stats_fd; + int stats_http; uint64_t queue_size; uint64_t queue_blocksize; @@ -3394,6 +3395,7 @@ char *uwsgi_matheval_str(char *); #endif int uwsgi_kvlist_parse(char *, size_t, char, char, ...); +int uwsgi_send_http_stats(int); void uwsgi_check_emperor(void); #ifdef UWSGI_AS_SHARED_LIBRARY