]> git.ipfire.org Git - telemetry.git/commitdiff
daemon: Allow to select sources on the CLI
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 20 Oct 2025 10:15:29 +0000 (10:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 20 Oct 2025 10:15:29 +0000 (10:15 +0000)
This is only useful for testing to basically disable all other sources
for better log readability.

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

index 4e0ba951774ff23edd0cdf7c567d4db2157b751c..48178ec8cc8f1926303c79e0920a10fdf8fb059f 100644 (file)
@@ -38,6 +38,9 @@ struct td_daemon {
        td_ctx* ctx;
        int nrefs;
 
+       // Enabled sources
+       char** enabled_sources;
+
        // Event Loop
        sd_event* loop;
 
@@ -71,7 +74,7 @@ static int td_daemon_init(sd_event_source* source, void* data) {
        DEBUG(self->ctx, "Initializing daemon...\n");
 
        // Initialize all sources
-       r = td_sources_create(&self->sources, self->ctx, self);
+       r = td_sources_create(&self->sources, self->ctx, self, self->enabled_sources);
        if (r < 0)
                return r;
 
@@ -219,7 +222,7 @@ static void td_daemon_free(td_daemon* self) {
        free(self);
 }
 
-int td_daemon_create(td_daemon** daemon, td_ctx* ctx) {
+int td_daemon_create(td_daemon** daemon, td_ctx* ctx, char** enabled_sources) {
        td_daemon* self = NULL;
        int r;
 
@@ -234,6 +237,9 @@ int td_daemon_create(td_daemon** daemon, td_ctx* ctx) {
        // Store a reference to the context
        self->ctx = td_ctx_ref(ctx);
 
+       // Store the enabled sources
+       self->enabled_sources = enabled_sources;
+
        // Setup the event loop
        r = td_daemon_setup_loop(self);
        if (r < 0)
index 503c58041154922d66dfbcee146df3109622f97d..d02bb24fac978ac845e2a4dc90cdf0bb08688c7d 100644 (file)
@@ -31,7 +31,7 @@ typedef struct td_daemon td_daemon;
 #include "source.h"
 #include "sources.h"
 
-int td_daemon_create(td_daemon** daemon, td_ctx* ctx);
+int td_daemon_create(td_daemon** daemon, td_ctx* ctx, char** enabled_sources);
 
 td_daemon* td_daemon_ref(td_daemon* daemon);
 td_daemon* td_daemon_unref(td_daemon* daemon);
index fce7fc65ec4a83735fe167214474fe621021e96d..cdd3a83282f316a8f2ad4893650dcc00b8a14cce 100644 (file)
@@ -27,6 +27,7 @@
 #include "ctx.h"
 #include "daemon.h"
 #include "logging.h"
+#include "string.h"
 
 const char* argp_program_version = PACKAGE_VERSION;
 
@@ -34,13 +35,18 @@ static const char* doc = "The IPFire Telemetry Daemon";
 
 enum {
        OPT_DEBUG  = 1,
+       OPT_SOURCE = 2,
 };
 
 static struct argp_option options[] = {
        { "debug", OPT_DEBUG, NULL, 0, "Run in debug mode", 0 },
+       { "source", OPT_SOURCE, "SOURCE", 0, "Only enable this source (for testing)", 0 },
        { NULL },
 };
 
+// Collect all sources that should be enabled
+static char** sources = NULL;
+
 static error_t parse(int key, char* arg, struct argp_state* state) {
        td_ctx* ctx = state->input;
 
@@ -53,6 +59,9 @@ static error_t parse(int key, char* arg, struct argp_state* state) {
                        td_ctx_set_log_level(ctx, LOG_DEBUG);
                        break;
 
+               case OPT_SOURCE:
+                       return td_strings_append(&sources, arg);
+
                default:
                        return ARGP_ERR_UNKNOWN;
        }
@@ -82,7 +91,7 @@ int main(int argc, char* argv[]) {
                goto ERROR;
 
        // Create a daemon
-       r = td_daemon_create(&daemon, ctx);
+       r = td_daemon_create(&daemon, ctx, sources);
        if (r < 0) {
                ERROR(ctx, "Failed to initialize the daemon: %s\n", strerror(-r));
                goto ERROR;
index c3e08c492d81e0d0ffdab69e7382ac67805731a3..d237836e924bb5e04b6f870fc7219adb841c182f 100644 (file)
@@ -89,13 +89,28 @@ struct td_sources {
 };
 
 
-static int td_sources_init(td_sources* self) {
+static int td_sources_init(td_sources* self, char** enabled_sources) {
        td_source** sources = NULL;
        td_source* source = NULL;
        int r;
 
+       // Dump enabled sources
+       if (enabled_sources) {
+               DEBUG(self->ctx, "Will only enable the following sources:\n");
+
+               for (char** enabled_source = enabled_sources; *enabled_source; enabled_source++)
+                       DEBUG(self->ctx, "  %s\n", *enabled_source);
+       }
+
        // Initialize all sources
        for (const td_source_impl** impl = source_impls; *impl; impl++) {
+               const char* name = (*impl)->name;
+
+               // Skip source if not enabled
+               if (enabled_sources && !td_strings_contain(enabled_sources, name))
+                       continue;
+
+               // Create the source object
                r = td_source_create(&source, self->ctx, self->daemon, *impl);
                if (r < 0)
                        return r;
@@ -141,7 +156,7 @@ static void td_sources_free(td_sources* self) {
 }
 
 int td_sources_create(td_sources** sources,
-               td_ctx* ctx, td_daemon* daemon) {
+               td_ctx* ctx, td_daemon* daemon, char** enabled_sources) {
        td_sources* self = NULL;
        int r;
 
@@ -160,7 +175,7 @@ int td_sources_create(td_sources** sources,
        self->daemon = td_daemon_ref(daemon);
 
        // Setup all sources
-       r = td_sources_init(self);
+       r = td_sources_init(self, enabled_sources);
        if (r < 0)
                goto ERROR;
 
index 98c0c527c48dfe2d8b2e51ec741931dbdd4d71f1..34cd91067714d3dbb05c90c2e5ddc275933a0489 100644 (file)
@@ -28,7 +28,7 @@ typedef struct td_sources td_sources;
 #include "source.h"
 
 int td_sources_create(td_sources** sources,
-       td_ctx* ctx, td_daemon* daemon);
+       td_ctx* ctx, td_daemon* daemon, char** enabled_sources);
 
 td_sources* td_sources_ref(td_sources* self);
 td_sources* td_sources_unref(td_sources* self);
index e0faac31d4d2adece23794938345c4921444ac6d..3caf5de8b5391bf697e6631e446726c923025892 100644 (file)
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "util.h"
@@ -163,4 +164,99 @@ static inline void td_string_strip(char* s) {
        td_string_rstrip(s);
 }
 
+static inline void td_strings_free(char** array) {
+       for (char** s = array; *s; s++)
+               free(*s);
+
+       free(array);
+}
+
+static inline size_t td_strings_length(char** array) {
+       size_t length = 0;
+
+       // If we have received NULL, we will return zero
+       if (!array)
+               return 0;
+
+       // Count all elements
+       for (char** s = array; *s; s++)
+               length++;
+
+       return length;
+}
+
+static inline int td_strings_append(char*** array, const char* s) {
+       if (!array)
+               return -EINVAL;
+
+       // Copy the string
+       char* p = strdup(s);
+       if (!p)
+               goto ERROR;
+
+       // Fetch the length
+       size_t length = td_strings_length(*array);
+
+       // Resize the array
+       *array = reallocarray(*array, length + 2, sizeof(**array));
+       if (!*array)
+               goto ERROR;
+
+       // Store the string
+       (*array)[length++] = p;
+
+       // Terminate the array
+       (*array)[length] = NULL;
+
+       // Return the total length of the array
+       return 0;
+
+ERROR:
+       if (p)
+               free(p);
+
+       return -errno;
+}
+
+/*
+       This function dumps the string array to the console. This is useful for debugging only.
+*/
+static inline int td_strings_dump(char** array) {
+       int r;
+
+       // Check inputs
+       if (!array)
+               return -EINVAL;
+
+       // Determine the length of the array
+       size_t length = td_strings_length(array);
+
+       // If the array is empty, we return a message
+       if (!length) {
+               printf("Empty string array\n");
+               return 0;
+       }
+
+       // Dump the array
+       for (unsigned int i = 0; i < length; i++) {
+               r = printf("array[%u] : %s\n", i, array[i]);
+               if (r < 0)
+                       return r;
+       }
+
+       return 0;
+}
+
+static inline int td_strings_contain(char** array, const char* string) {
+       int r;
+
+       for (char** s = array; *s; s++) {
+               r = td_string_equals(*s, string);
+               if (r)
+                       return r;
+       }
+
+       return 0;
+}
+
 #endif /* TELEMETRY_STRING_H */