]> git.ipfire.org Git - collecty.git/commitdiff
sources: knot-resolver: Connect to socket instead of kresctl
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Jul 2026 11:13:19 +0000 (11:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Jul 2026 11:13:19 +0000 (11:13 +0000)
This has significantly less overhead than calling the rather slow Python
command line tool.

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

index 2d67892f2bc5cb0dc45e9bd0ce15110f0ce3d4df..e0e758d1c23115bf878e7647dc53426deefa54fd 100644 (file)
@@ -23,8 +23,8 @@
 #include <errno.h>
 #include <limits.h>
 
-#include "../command.h"
 #include "../ctx.h"
+#include "../request.h"
 #include "../source.h"
 #include "../string.h"
 #include "knot-resolver.h"
@@ -159,14 +159,14 @@ static int knot_resolver_parse_metrics(td_ctx* ctx,
        return 0;
 }
 
-static int knot_resolver_on_success(td_ctx* ctx, td_file* stdout, void* data) {
+static int knot_resolver_on_success(td_ctx* ctx, td_file* output, void* data) {
        sd_json_variant* json = NULL;
        td_metrics* metrics = NULL;
        td_source* source = data;
        int r;
 
        // Parse the output as JSON
-       r = td_file_parse_json(stdout, &json, 0);
+       r = td_file_parse_json(output, &json, 0);
        if (r < 0)
                goto ERROR;
 
@@ -193,12 +193,35 @@ ERROR:
 }
 
 static int knot_resolver_heartbeat(td_ctx* ctx, td_source* source) {
-       // Run kresctl to fetch metrics
-       const char* argv[] = {
-               "kresctl", "metrics", NULL,
-       };
+       td_request* request = NULL;
+       int r;
+
+       // Create a new request
+       r = td_source_create_request(source, &request);
+       if (r < 0)
+               goto ERROR;
+
+       // Set the URL
+       r = td_request_set_url(request, "http://localhost/metrics/json");
+       if (r < 0)
+               goto ERROR;
 
-       return td_source_run_command(source, NULL, argv, knot_resolver_on_success, source);
+       // Set the socket
+       r = td_request_set_socket(request, "/var/run/knot-resolver/kres-api.sock");
+       if (r < 0)
+               goto ERROR;
+
+       // Configure the success callback
+       td_request_on_success(request, knot_resolver_on_success, source);
+
+       // Launch the request
+       r = td_request_launch(request);
+
+ERROR:
+       if (request)
+               td_request_unref(request);
+
+       return r;
 }
 
 const td_source_impl knot_resolver_source = {