From: Michael Tremer Date: Tue, 11 Nov 2025 17:35:53 +0000 (+0000) Subject: sources: Add a shortpath for running a command X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=58c8e0c1546e644a435f464dce8e50e157521503;p=telemetry.git sources: Add a shortpath for running a command Signed-off-by: Michael Tremer --- diff --git a/src/daemon/command.h b/src/daemon/command.h index 3dccd4e..30730a4 100644 --- a/src/daemon/command.h +++ b/src/daemon/command.h @@ -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); diff --git a/src/daemon/source.c b/src/daemon/source.c index 649d2bc..b9fc620 100644 --- a/src/daemon/source.c +++ b/src/daemon/source.c @@ -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)) diff --git a/src/daemon/source.h b/src/daemon/source.h index 17d0ef0..bc0423b 100644 --- a/src/daemon/source.h +++ b/src/daemon/source.h @@ -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, diff --git a/src/daemon/sources/hostapd.c b/src/daemon/sources/hostapd.c index c03fbf1..aebf055 100644 --- a/src/daemon/sources/hostapd.c +++ b/src/daemon/sources/hostapd.c @@ -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 = { diff --git a/src/daemon/sources/legacy-gateway-latency4.c b/src/daemon/sources/legacy-gateway-latency4.c index 39547a4..8ee4c7a 100644 --- a/src/daemon/sources/legacy-gateway-latency4.c +++ b/src/daemon/sources/legacy-gateway-latency4.c @@ -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) { diff --git a/src/daemon/sources/suricata.c b/src/daemon/sources/suricata.c index e317566..9ac8673 100644 --- a/src/daemon/sources/suricata.c +++ b/src/daemon/sources/suricata.c @@ -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 = { diff --git a/src/daemon/sources/unbound.c b/src/daemon/sources/unbound.c index 98d5964..d9bb61f 100644 --- a/src/daemon/sources/unbound.c +++ b/src/daemon/sources/unbound.c @@ -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 = {