]> git.ipfire.org Git - collecty.git/commitdiff
graphs: Render an empty graph if there are no sources master
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Jun 2026 14:30:01 +0000 (14:30 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Jun 2026 14:30:01 +0000 (14:30 +0000)
Graphs like temperature graphs don't have any data sources whatsoever
for example in a virtual machine. Because rrdtool would normally fail,
we will instead create an empty graph that even has the correct labels
and shows a note at the bottom.

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

index 19cfff9d56579d05d495866347ef6292155844c1..1f8720eb5b4b550ff44b49306e71cfe7d1612794 100644 (file)
@@ -36,6 +36,9 @@
 #include "string.h"
 #include "time.h"
 
+// Include graph macros
+#include "graphs/graph.h"
+
 // Maximum length of the title/vlabel
 #define TITLE_MAX              128
 #define VLABEL_MAX             64
@@ -283,6 +286,28 @@ static void td_graph_reset_locale(td_ctx* ctx, char* locale) {
                ERROR(ctx, "Failed to reset the locale to '%s': %m\n", locale);
 }
 
+static int td_graph_empty(td_graph* self, td_args* args) {
+       int r;
+
+       // Draw a line so that something will get rendered
+       r = td_args_push(args, "HRULE:0" COLOR_AXIS);
+       if (r < 0)
+               return r;
+
+       // Add an empty line
+       PRINT_EMPTY_LINE(args);
+
+       // Add a message that explains that no data is available
+       r = td_args_push(args, "COMMENT:%s\\c", _("No Data Available"));
+       if (r < 0)
+               return r;
+
+       // Add another empty line
+       PRINT_EMPTY_LINE(args);
+
+       return 0;
+}
+
 int td_graph_render(td_graph* self, const char* object,
                const td_graph_render_options* options, char** buffer, size_t* length) {
        td_args* args = NULL;
@@ -463,6 +488,16 @@ int td_graph_render(td_graph* self, const char* object,
        if (r < 0)
                goto ERROR;
 
+       // We cannot render the graph if there are no sources
+       if (!td_args_has_sources(args)) {
+               DEBUG(self->ctx, "Graph has no sources\n");
+
+               // Show nothing
+               r = td_graph_empty(self, args);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        // Dump the command
        r = td_args_dump(args);
        if (r < 0)