]> git.ipfire.org Git - telemetry.git/commitdiff
daemon: Create a modules registry
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Sep 2025 16:50:25 +0000 (16:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Sep 2025 16:50:25 +0000 (16:50 +0000)
Just like we have for graphs now

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c
src/daemon/modules.c
src/daemon/modules.h

index 436db95a7cf4cba16b554cfe3768547fd71600e5..c3eabe42b5ecf81ab3f1edd79271c38a941b5ba6 100644 (file)
@@ -56,6 +56,9 @@ struct collecty_daemon {
        // Queue
        collecty_queue* queue;
 
+       // Modules
+       collecty_modules* modules;
+
        // Graphs
        collecty_graphs* graphs;
 };
@@ -67,7 +70,7 @@ static int collecty_daemon_init(sd_event_source* source, void* data) {
        DEBUG(self->ctx, "Initializing daemon...\n");
 
        // Initialize all modules
-       r = collecty_modules_init(self->ctx, self);
+       r = collecty_modules_create(&self->modules, self->ctx, self);
        if (r < 0)
                return r;
 
@@ -90,6 +93,12 @@ static int collecty_daemon_exit(sd_event_source* source, void* data) {
                self->graphs = NULL;
        }
 
+       // Free all modules
+       if (self->modules) {
+               collecty_modules_unref(self->modules);
+               self->modules = NULL;
+       }
+
        return 0;
 }
 
@@ -179,6 +188,8 @@ static void collecty_daemon_free(collecty_daemon* self) {
                sd_event_source_unref(self->events.init);
        if (self->events.exit)
                sd_event_source_unref(self->events.exit);
+       if (self->modules)
+               collecty_modules_unref(self->modules);
        if (self->graphs)
                collecty_graphs_unref(self->graphs);
        if (self->queue)
index c9e2698f89e454d91c14335010e61afd0a3146f0..dfd4c911866868338316fcbf56a023744ddcc99f 100644 (file)
 #include "modules/loadavg.h"
 
 // Register all modules
-static const collecty_module_methods* modules[] = {
+static const collecty_module_methods* module_impls[] = {
        &conntrack_module,
        &contextswitches_module,
        &loadavg_module,
        NULL,
 };
 
-int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon) {
+struct collecty_modules {
+       collecty_ctx* ctx;
+       int nrefs;
+
+       // Daemon
+       collecty_daemon* daemon;
+
+       // Modules
+       collecty_module** modules;
+       unsigned int num_modules;
+};
+
+
+static int collecty_modules_init(collecty_modules* self) {
+       collecty_module** modules = NULL;
        collecty_module* module = NULL;
        int r;
 
-       DEBUG(ctx, "Initializing all modules...\n");
-
        // Initialize all modules
-       for (const collecty_module_methods** m = modules; *m; m++) {
-               r = collecty_module_create(&module, ctx, daemon, *m);
+       for (const collecty_module_methods** impl = module_impls; *impl; impl++) {
+               r = collecty_module_create(&module, self->ctx, self->daemon, *impl);
                if (r < 0)
                        return r;
 
+               // Make space to store the module
+               modules = reallocarray(self->modules, self->num_modules + 1, sizeof(*self->modules));
+               if (!modules) {
+                       r = -errno;
+                       goto ERROR;
+               }
+
+               // Store a reference to the module in the array
+               modules[self->num_modules++] = collecty_module_ref(module);
+
+               // Replace the array
+               self->modules = modules;
+
+               // Unref the module
+               collecty_module_unref(module);
+               module = NULL;
+       }
+
+       return 0;
+
+ERROR:
+       if (module)
                collecty_module_unref(module);
+
+       return r;
+}
+
+static void collecty_modules_free(collecty_modules* self) {
+       if (self->modules) {
+               for (unsigned int i = 0; i < self->num_modules; i++)
+                       collecty_module_unref(self->modules[i]);
+               free(self->modules);
        }
+       if (self->daemon)
+               collecty_daemon_unref(self->daemon);
+       if (self->ctx)
+               collecty_ctx_unref(self->ctx);
+       free(self);
+}
+
+int collecty_modules_create(collecty_modules** modules,
+               collecty_ctx* ctx, collecty_daemon* daemon) {
+       collecty_modules* self = NULL;
+       int r;
+
+       // Allocate some memory
+       self = calloc(1, sizeof(*self));
+       if (!self)
+               return -errno;
+
+       // Initialize the reference counter
+       self->nrefs = 1;
+
+       // Store a reference to the context
+       self->ctx = collecty_ctx_ref(ctx);
+
+       // Store a reference to the daemon
+       self->daemon = collecty_daemon_ref(daemon);
 
+       // Setup all modules
+       r = collecty_modules_init(self);
+       if (r < 0)
+               goto ERROR;
+
+       // Return the pointer
+       *modules = self;
        return 0;
+
+ERROR:
+       if (self)
+               collecty_modules_unref(self);
+
+       return r;
+}
+
+collecty_modules* collecty_modules_ref(collecty_modules* self) {
+       ++self->nrefs;
+       return self;
+}
+
+collecty_modules* collecty_modules_unref(collecty_modules* self) {
+       if (--self->nrefs > 0)
+               return self;
+
+       collecty_modules_free(self);
+       return NULL;
 }
index 0dfc45078c7df7d1d3996ea821585bc019b17381..f244750573cdb116807c45b554d758ccca4ac2b0 100644 (file)
 #ifndef COLLECTY_MODULES_H
 #define COLLECTY_MODULES_H
 
+typedef struct collecty_modules collecty_modules;
+
 #include "ctx.h"
 #include "daemon.h"
 
-int collecty_modules_init(collecty_ctx* ctx, collecty_daemon* daemon);
+int collecty_modules_create(collecty_modules** modules,
+       collecty_ctx* ctx, collecty_daemon* daemon);
+
+collecty_modules* collecty_modules_ref(collecty_modules* self);
+collecty_modules* collecty_modules_unref(collecty_modules* self);
 
 #endif /* COLLECTY_MODULES_H */