From fa92394f80ddcfd1f1ce95da2d5213a0a972e8d4 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 5 Jun 2026 14:30:01 +0000 Subject: [PATCH] graphs: Render an empty graph if there are no sources 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 --- src/daemon/graph.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/daemon/graph.c b/src/daemon/graph.c index 19cfff9..1f8720e 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -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) -- 2.47.3