]> git.ipfire.org Git - telemetry.git/commitdiff
sources: Add a shortpath for running a command
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 11 Nov 2025 17:35:53 +0000 (17:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 11 Nov 2025 17:35:53 +0000 (17:35 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/command.h
src/daemon/source.c
src/daemon/source.h
src/daemon/sources/hostapd.c
src/daemon/sources/legacy-gateway-latency4.c
src/daemon/sources/suricata.c
src/daemon/sources/unbound.c

index 3dccd4ed703369201d6c3188bd7ac1fbf3f3f2a3..30730a4ae22e8be3ea189c541bfbf23d5d8b4533 100644 (file)
@@ -25,6 +25,10 @@ typedef struct td_command td_command;
 
 #include "ctx.h"
 #include "file.h"
+
+typedef int (*td_command_success_callback)
+       (td_ctx* ctx, int rc, td_file* stdout, void* data);
+
 #include "daemon.h"
 
 int td_command_create(td_command** command,
@@ -36,9 +40,6 @@ td_command* td_command_unref(td_command* self);
 // Timeout
 void td_command_set_timeout(td_command* self, uint64_t timeout);
 
-typedef int (*td_command_success_callback)
-       (td_ctx* ctx, int rc, td_file* stdout, void* data);
-
 // Called if the command has exited successfully
 void td_command_on_success(td_command* self,
        td_command_success_callback callback, void* data);
index 649d2bc9c403d65142a87187496aef0ad8c1f733..b9fc6203bcb74a7315e981d1fe2cd87134aa8475 100644 (file)
@@ -523,6 +523,29 @@ int td_source_create_command(td_source* self, td_command** command) {
        return td_command_create(command, self->ctx, self->daemon);
 }
 
+int td_source_run_command(td_source* self, const char** argv,
+               td_command_success_callback callback, void* data) {
+       td_command* command = NULL;
+       int r;
+
+       // Create a new command
+       r = td_source_create_command(self, &command);
+       if (r < 0)
+               goto ERROR;
+
+       // Set the callback
+       td_command_on_success(command, callback, data);
+
+       // Execute the command
+       r = td_command_execute(command, argv);
+
+ERROR:
+       if (command)
+               td_command_unref(command);
+
+       return r;
+}
+
 #define td_source_path(source, object, path) \
        __td_source_path(source, object, path, sizeof(path))
 
index 17d0ef0b203f83f2f8685958df826394aa53d4b3..bc0423b225f85589b4012cb02685986fabc38220 100644 (file)
@@ -89,7 +89,10 @@ unsigned int td_source_num_data_sources(td_source* self);
 const td_rrd_ds* td_source_get_data_sources(td_source* self);
 
 int td_source_create_metrics(td_source* self, td_metrics** metrics, const char* object);
+
 int td_source_create_command(td_source* self, td_command** command);
+int td_source_run_command(td_source* self, const char** argv,
+       td_command_success_callback callback, void* data);
 
 int td_source_submit_metrics(td_source* self, td_metrics* metrics);
 int td_source_submit_values(td_source* self,
index c03fbf1f51abb7d56bbf45fad9da1bcc488e1be4..aebf055fe0ea9606834b0e35ef25b75bcd8b9d03 100644 (file)
@@ -210,28 +210,12 @@ static int hostapd_on_success(td_ctx* ctx,
 }
 
 static int hostapd_heartbeat(td_ctx* ctx, td_source* source) {
-       td_command* command = NULL;
-       int r;
-
        // Run hostapd_cli to fetch station information
-       const char* argv[] = { "hostapd_cli", "all_sta", NULL };
-
-       // Create a new command
-       r = td_source_create_command(source, &command);
-       if (r < 0)
-               goto ERROR;
-
-       // Register the success callback
-       td_command_on_success(command, hostapd_on_success, source);
-
-       // Execute the command
-       r = td_command_execute(command, argv);
-
-ERROR:
-       if (command)
-               td_command_unref(command);
+       const char* argv[] = {
+               "hostapd_cli", "all_sta", NULL,
+       };
 
-       return r;
+       return td_source_run_command(source, argv, hostapd_on_success, source);
 }
 
 const td_source_impl hostapd_source = {
index 39547a49fcbdd509003ce970d2c0a1060d93817d..8ee4c7a6c817b80bb51d7f0eb7a56b91094c3c07 100644 (file)
@@ -167,55 +167,20 @@ static int legacy_gateway_latency_on_success(td_ctx* ctx,
        ));
 }
 
-
 static int do_arping(td_ctx* ctx, td_source* source, const char* address) {
-       td_command* command = NULL;
-       int r;
-
-       // Run the ping command
-       const char* argv[] = { "arping", "-c3", address, NULL };
-
-       // Create a new command
-       r = td_source_create_command(source, &command);
-       if (r < 0)
-               goto ERROR;
-
-       // Register the success callback
-       td_command_on_success(command, legacy_gateway_latency_on_success, source);
-
-       // Execute the command
-       r = td_command_execute(command, argv);
-
-ERROR:
-       if (command)
-               td_command_unref(command);
+       const char* argv[] = {
+               "arping", "-c3", address, NULL,
+       };
 
-       return r;
+       return td_source_run_command(source, argv, legacy_gateway_latency_on_success, source);
 }
 
 static int do_ping(td_ctx* ctx, td_source* source, const char* address) {
-       td_command* command = NULL;
-       int r;
-
-       // Run the ping command
-       const char* argv[] = { "ping", "-c3", address, NULL };
-
-       // Create a new command
-       r = td_source_create_command(source, &command);
-       if (r < 0)
-               goto ERROR;
-
-       // Register the success callback
-       td_command_on_success(command, legacy_gateway_latency_on_success, source);
-
-       // Execute the command
-       r = td_command_execute(command, argv);
-
-ERROR:
-       if (command)
-               td_command_unref(command);
+       const char* argv[] = {
+               "ping", "-c3", address, NULL,
+       };
 
-       return r;
+       return td_source_run_command(source, argv, legacy_gateway_latency_on_success, source);
 }
 
 static int legacy_gateway_latency4_heartbeat(td_ctx* ctx, td_source* source) {
index e317566a5d0ad0fa36ef97542e145ee68314176b..9ac8673d0a23ee46169990ff7daa1208c62a13a0 100644 (file)
@@ -170,28 +170,12 @@ ERROR:
 }
 
 static int suricata_heartbeat(td_ctx* ctx, td_source* source) {
-       td_command* command = NULL;
-       int r;
-
        // Run suricatasc to fetch all counters
-       const char* argv[] = { "suricatasc", "--command=dump-counters", NULL };
-
-       // Create a new command
-       r = td_source_create_command(source, &command);
-       if (r < 0)
-               goto ERROR;
+       const char* argv[] = {
+               "suricatasc", "--command=dump-counters", NULL,
+       };
 
-       // Register the success callback
-       td_command_on_success(command, suricata_on_success, source);
-
-       // Execute the command
-       r = td_command_execute(command, argv);
-
-ERROR:
-       if (command)
-               td_command_unref(command);
-
-       return r;
+       return td_source_run_command(source, argv, suricata_on_success, source);
 }
 
 const td_source_impl suricata_source = {
index 98d59642455e15e1f6f4ed41f765c7526f0124c3..d9bb61fccc894ebdc210befc26cb1120217803ed 100644 (file)
@@ -66,28 +66,12 @@ static int unbound_on_success(td_ctx* ctx,
 }
 
 static int unbound_heartbeat(td_ctx* ctx, td_source* source) {
-       td_command* command = NULL;
-       int r;
-
        // Run unbound-control to fetch stats
-       const char* argv[] = { "unbound-control", "stats_noreset", NULL };
-
-       // Create a new command
-       r = td_source_create_command(source, &command);
-       if (r < 0)
-               goto ERROR;
-
-       // Register the success callback
-       td_command_on_success(command, unbound_on_success, source);
-
-       // Execute the command
-       r = td_command_execute(command, argv);
-
-ERROR:
-       if (command)
-               td_command_unref(command);
+       const char* argv[] = {
+               "unbound-control", "stats_noreset", NULL,
+       };
 
-       return r;
+       return td_source_run_command(source, argv, unbound_on_success, source);
 }
 
 const td_source_impl unbound_source = {