From: Michael Tremer Date: Sat, 27 Sep 2025 15:23:10 +0000 (+0000) Subject: daemon: Store any submitted values X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e091772fbfdd7f74aae36a9f71f7ff9a8c448efc;p=collecty.git daemon: Store any submitted values Signed-off-by: Michael Tremer --- diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 993efcc..d0a760d 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -21,14 +21,33 @@ #include #include #include +#include +#include #include #include #include "ctx.h" #include "daemon.h" +#include "module.h" #include "modules.h" +struct collecty_queue_object { + STAILQ_ENTRY(collecty_queue_object) nodes; + + // Module + collecty_module* module; + + // Object + char* object; + + // Timestamp + struct timeval t; + + // Value + char* value; +}; + struct collecty_daemon { collecty_ctx* ctx; int nrefs; @@ -42,6 +61,9 @@ struct collecty_daemon { sd_event_source* sigint; sd_event_source* modules_init; } events; + + // Write Queue + STAILQ_HEAD(queue, collecty_queue_object) queue; }; static int collecty_daemon_modules_init(sd_event_source* source, void* data) { @@ -102,6 +124,22 @@ static int collecty_daemon_setup_loop(collecty_daemon* self) { return 0; } +static void collecty_daemon_free_queue_object(struct collecty_queue_object* o) { + if (o->module) + collecty_module_unref(o->module); + if (o->object) + free(o->object); + if (o->value) + free(o->value); +} + +static int collecty_daemon_setup_queue(collecty_daemon* self) { + // Initialize the queue + STAILQ_INIT(&self->queue); + + return 0; +} + static void collecty_daemon_free(collecty_daemon* self) { if (self->events.modules_init) sd_event_source_unref(self->events.modules_init); @@ -135,6 +173,11 @@ int collecty_daemon_create(collecty_daemon** daemon, collecty_ctx* ctx) { if (r < 0) goto ERROR; + // Setup the write queue + r = collecty_daemon_setup_queue(self); + if (r < 0) + goto ERROR; + // Return the pointer *daemon = self; return 0; @@ -186,3 +229,62 @@ ERROR: return 1; } + +/* + Submits a new reading into the queue +*/ +int collecty_daemon_submit(collecty_daemon* self, + collecty_module* module, const char* object, const char* value) { + struct collecty_queue_object* o = NULL; + int r; + + // Check inputs + if (!value) + return -EINVAL; + + // Allocate some memory + o = calloc(1, sizeof(*self)); + if (!o) + return -errno; + + // Reference the module + o->module = collecty_module_ref(module); + + // Fetch the current timestamp + r = gettimeofday(&o->t, NULL); + if (r < 0) { + r = -errno; + goto ERROR; + } + + // Store the object + if (o->object) { + o->object = strdup(object); + if (!o->object) { + r = -errno; + goto ERROR; + } + } + + // Store the value + o->value = strdup(value); + if (!o->value) { + r = -errno; + goto ERROR; + } + + // Append the object to the queue + STAILQ_INSERT_TAIL(&self->queue, o, nodes); + + // Log action + DEBUG(self->ctx, "%s(%s) submitted: %s\n", + collecty_module_name(module), (o->object) ? o->object : "", o->value); + + return 0; + +ERROR: + if (o) + collecty_daemon_free_queue_object(o); + + return r; +} diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index a2f98a0..8c88645 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -26,6 +26,7 @@ typedef struct collecty_daemon collecty_daemon; #include "ctx.h" +#include "module.h" int collecty_daemon_create(collecty_daemon** daemon, collecty_ctx* ctx); @@ -36,4 +37,7 @@ sd_event* collecty_daemon_loop(collecty_daemon* self); int collecty_daemon_run(collecty_daemon* self); +int collecty_daemon_submit(collecty_daemon* self, + collecty_module* module, const char* object, const char* value); + #endif /* COLLECTY_DAEMON_H */ diff --git a/src/daemon/module.c b/src/daemon/module.c index e970f5b..739fb8a 100644 --- a/src/daemon/module.c +++ b/src/daemon/module.c @@ -238,10 +238,8 @@ int collecty_module_submit(collecty_module* self, if (r < 0) return -errno; - DEBUG(self->ctx, "%s(%s) submitted: %s\n", - collecty_module_name(self), (object) ? object : "", value); - - // TODO + // Submit the data to the daemon + r = collecty_daemon_submit(self->daemon, self, object, value); if (value) free(value); diff --git a/src/daemon/module.h b/src/daemon/module.h index e8621fb..db0180a 100644 --- a/src/daemon/module.h +++ b/src/daemon/module.h @@ -21,11 +21,11 @@ #ifndef COLLECTY_MODULE_H #define COLLECTY_MODULE_H +typedef struct collecty_module collecty_module; + #include "ctx.h" #include "daemon.h" -typedef struct collecty_module collecty_module; - typedef struct collecty_module_methods { const char* name;