From 820b798a46975e15b025b56d0e12d126665f1002 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 5 Oct 2025 15:16:39 +0000 Subject: [PATCH] graphs: Add uptime graph Signed-off-by: Michael Tremer --- Makefile.am | 2 ++ src/daemon/colors.h | 4 +++ src/daemon/graphs.c | 2 ++ src/daemon/graphs/graph.h | 12 +++++++ src/daemon/graphs/uptime.c | 67 ++++++++++++++++++++++++++++++++++++++ src/daemon/graphs/uptime.h | 28 ++++++++++++++++ src/daemon/util.h | 4 +++ 7 files changed, 119 insertions(+) create mode 100644 src/daemon/graphs/uptime.c create mode 100644 src/daemon/graphs/uptime.h diff --git a/Makefile.am b/Makefile.am index 309135e..127645f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -114,6 +114,8 @@ dist_collectyd_SOURCES = \ src/daemon/graphs/loadavg.h \ src/daemon/graphs/processor.c \ src/daemon/graphs/processor.h \ + src/daemon/graphs/uptime.c \ + src/daemon/graphs/uptime.h \ src/daemon/i18n.h \ src/daemon/logging.c \ src/daemon/logging.h \ diff --git a/src/daemon/colors.h b/src/daemon/colors.h index 083011e..d4290b7 100644 --- a/src/daemon/colors.h +++ b/src/daemon/colors.h @@ -23,6 +23,7 @@ #define BLACK "#000000" #define WHITE "#ffffff" +#define GREY "#9e9e9e" #define RED "#f44336" #define LIGHT_RED "#cc0033" #define YELLOW "#ffeb3b" @@ -39,6 +40,9 @@ Define some colours with a special meaning... */ +// Default - If something does not need to have a special meaning +#define DEFAULT GREY + // Limit - When there is a ceiling to resource usage #define LIMIT RED diff --git a/src/daemon/graphs.c b/src/daemon/graphs.c index 4e59d03..3248bfe 100644 --- a/src/daemon/graphs.c +++ b/src/daemon/graphs.c @@ -32,6 +32,7 @@ #include "graphs/contextswitches.h" #include "graphs/loadavg.h" #include "graphs/processor.h" +#include "graphs/uptime.h" // Register all graphs static const collecty_graph_impl* graph_impls[] = { @@ -39,6 +40,7 @@ static const collecty_graph_impl* graph_impls[] = { &contextswitches_graph, &loadavg_graph, &processor_graph, + &uptime_graph, NULL, }; diff --git a/src/daemon/graphs/graph.h b/src/daemon/graphs/graph.h index 2e6c6f7..30b06b7 100644 --- a/src/daemon/graphs/graph.h +++ b/src/daemon/graphs/graph.h @@ -107,6 +107,12 @@ #define PRINT_FLOAT(args, field, ...) PRINT(args, field, FLOAT __VA_ARGS__) #define PRINT_LARGE_FLOAT(args, field, ...) PRINT(args, field, LARGE_FLOAT __VA_ARGS__) +#define PRINT_HEADER1(args, header1) \ + do { \ + PRINT_EMPTY_LABEL(args); \ + PRINT_HEADER(args, header1, EOL); \ + } while (0) + #define PRINT_HEADER4(args, header1, header2, header3, header4) \ do { \ PRINT_EMPTY_LABEL(args); \ @@ -143,6 +149,12 @@ VALUE_ALL(args, sum); \ } while (0) +#define COMPUTE_DIVIDE(args, fraction, dividend, divisor) \ + do { \ + SCRIPT(args, "CDEF:" fraction "=" dividend "," TOSTRING(divisor) ",/"); \ + VALUE_ALL(args, fraction); \ + } while (0) + #define COMPUTE_PERCENTAGE(args, field, total) \ do { \ SCRIPT(args, "CDEF:" FIELD_PERCENT(field) "=100," field ",*," total ",/"); \ diff --git a/src/daemon/graphs/uptime.c b/src/daemon/graphs/uptime.c new file mode 100644 index 0000000..1b9e78d --- /dev/null +++ b/src/daemon/graphs/uptime.c @@ -0,0 +1,67 @@ +/*############################################################################# +# # +# collecty - A system statistics collection daemon for IPFire # +# Copyright (C) 2025 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include + +#include "graph.h" +#include "uptime.h" + +static int uptime_title(collecty_ctx* ctx, collecty_graph* graph, + const char* object, char** title) { + return collecty_format_title(title, "%s", _("Uptime")); +} + +static int uptime_vlabel(collecty_ctx* ctx, collecty_graph* graph, + const char* object, char** title) { + return collecty_format_title(title, "%s", _("Days")); +} + +static int uptime_render(collecty_ctx* ctx, + collecty_graph* graph, collecty_args* args, const char* object) { + int r; + + // This requires the uptime source + r = collecty_graph_require_source(graph, args, "uptime", object); + if (r < 0) + return r; + + // Convert the uptime into days + COMPUTE_DIVIDE(args, "uptime_days", "uptime", 86400); + + // Header + PRINT_HEADER1(args, _("Current")); + + // Draw the uptime + DRAW_AREA_WITH_LABEL(args, "uptime_days", DEFAULT, _("Uptime")); + PRINT_INTEGER(args, "uptime_days_cur", EOL); + + return 0; +} + +const collecty_graph_impl uptime_graph = { + .name = "Uptime", + .render = uptime_render, + .title = uptime_title, + .vlabel = uptime_vlabel, + + // Limits + .lower_limit = 0, + .upper_limit = LONG_MAX, +}; diff --git a/src/daemon/graphs/uptime.h b/src/daemon/graphs/uptime.h new file mode 100644 index 0000000..a90fc3e --- /dev/null +++ b/src/daemon/graphs/uptime.h @@ -0,0 +1,28 @@ +/*############################################################################# +# # +# collecty - A system statistics collection daemon for IPFire # +# Copyright (C) 2025 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef COLLECTY_GRAPH_UPTIME_H +#define COLLECTY_GRAPH_UPTIME_H + +#include "../graph.h" + +extern const collecty_graph_impl uptime_graph; + +#endif /* COLLECTY_GRAPH_UPTIME_H */ diff --git a/src/daemon/util.h b/src/daemon/util.h index 86865e1..143e8b2 100644 --- a/src/daemon/util.h +++ b/src/daemon/util.h @@ -24,6 +24,10 @@ #include #include +// Preprocessor macro to make something a string +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) + // Macros to help the compiler with branch prediction #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) -- 2.47.3