From 3b4aa721a46135ca1f441e79066248ad3cb79466 Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Fri, 11 Mar 2022 14:33:37 +0100 Subject: #915 Add GET /stats REST request This request returns certain statistics on memory and swap usage, as well as the uptime. The below structure gives the sizes of the memory and swap fields in bytes. Request: GET /stats NO BODY Response: 200 OK { "time": { "now": 1647262765, /* Seconds since 1970 */ "boot": 2151909 /* Seconds since boot */ }, "memory": { "size": 4104679424, /* Total usable main memory size */ "free": 322174976 /* Available memory size */ }, "swap": { "size": 2147479552, /* Total swap space size */ "free": 2122563584 /* Swap space still available */ } } --- src/rest.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/rest.c b/src/rest.c index 37dcc99..d4c6dd7 100644 --- a/src/rest.c +++ b/src/rest.c @@ -30,6 +30,7 @@ #include #include #include +#include struct ev_loop *og_loop; @@ -5416,6 +5417,60 @@ static int og_cmd_oglive_set(json_t *element, struct og_msg_params *params) return 0; } +static int og_cmd_get_server_stats(char *buffer_reply) +{ + json_t *root, *time_obj, *memory, *swap; + struct og_buffer og_buffer = { + .data = buffer_reply + }; + struct sysinfo stats; + + sysinfo(&stats); + + root = json_object(); + if (!root) + return -1; + time_obj = json_object(); + if (!time_obj) { + json_decref(root); + return -1; + } + memory = json_object(); + if (!memory) { + json_decref(root); + json_decref(time_obj); + return -1; + } + swap = json_object(); + if (!swap) { + json_decref(root); + json_decref(time_obj); + json_decref(memory); + return -1; + } + + json_object_set_new(time_obj, "now", json_integer(time(NULL))); + json_object_set_new(time_obj, "boot", json_integer(stats.uptime)); + json_object_set_new(root, "time", time_obj); + + json_object_set_new(memory, "size", json_integer(stats.totalram)); + json_object_set_new(memory, "free", json_integer(stats.freeram)); + json_object_set_new(root, "memory", memory); + + json_object_set_new(swap, "size", json_integer(stats.totalswap)); + json_object_set_new(swap, "free", json_integer(stats.freeswap)); + json_object_set_new(root, "swap", swap); + + if (json_dump_callback(root, og_json_dump_clients, &og_buffer, 0)) { + json_decref(root); + return -1; + } + + json_decref(root); + + return 0; +} + static int og_client_method_not_found(struct og_client *cli) { /* To meet RFC 7231, this function MUST generate an Allow header field @@ -6049,6 +6104,13 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } err = og_cmd_post_task_add(root, ¶ms); + } else if (!strncmp(cmd, "stats", strlen("stats"))) { + if (method != OG_METHOD_GET) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + err = og_cmd_get_server_stats(buf_reply); } else { syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd); err = og_client_not_found(cli); -- cgit v1.2.3-18-g5258