#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,
// 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);
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))
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,
}
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 = {
));
}
-
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) {
}
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 = {
}
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 = {