]> git.ipfire.org Git - collecty.git/commitdiff
daemon: Store any submitted values
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 15:23:10 +0000 (15:23 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 15:23:10 +0000 (15:23 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c
src/daemon/daemon.h
src/daemon/module.c
src/daemon/module.h

index 993efcc382828bb54b331691366a996338373bdb..d0a760d8bf137497053e0164210ded198b2148f0 100644 (file)
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/queue.h>
+#include <sys/time.h>
 
 #include <systemd/sd-daemon.h>
 #include <systemd/sd-event.h>
 
 #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;
+}
index a2f98a0673bf97587ad6bc1dfd75424160095ba9..8c886458fcc1dfabc6dcc0c240bf8047371fafdc 100644 (file)
@@ -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 */
index e970f5b1ec5d2b5f76522695d344448e2ed330fd..739fb8a1bbdd953638073581d0c80db8d10e244b 100644 (file)
@@ -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);
index e8621fbfdd9e08171a461efa116df53723a766da..db0180a7df8992d1460d67a38d8ef6d11cec9f65 100644 (file)
 #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;