summaryrefslogtreecommitdiffstats
path: root/src/rest.c
diff options
context:
space:
mode:
authorJavier Sánchez Parra <jsanchez@soleta.eu>2022-03-11 14:33:37 +0100
committerJavier Sánchez Parra <jsanchez@soleta.eu>2022-03-14 14:46:13 +0100
commit3b4aa721a46135ca1f441e79066248ad3cb79466 (patch)
treed2843288b727eac4ef6a8a72e63f51d86f967b0e /src/rest.c
parent281d661fc9c7f1ee9777ff17c2de88cd8ec959a4 (diff)
#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 */ } }
Diffstat (limited to 'src/rest.c')
-rw-r--r--src/rest.c62
1 files changed, 62 insertions, 0 deletions
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 <unistd.h>
#include <sys/wait.h>
#include <sys/statvfs.h>
+#include <sys/sysinfo.h>
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, &params);
+ } 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);