]> git.ipfire.org Git - collecty.git/commitdiff
graphs: Add uptime graph master
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Oct 2025 15:16:39 +0000 (15:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Oct 2025 15:16:39 +0000 (15:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/colors.h
src/daemon/graphs.c
src/daemon/graphs/graph.h
src/daemon/graphs/uptime.c [new file with mode: 0644]
src/daemon/graphs/uptime.h [new file with mode: 0644]
src/daemon/util.h

index 309135e26cdbaaa0ec1a7fad812bceda744db5c6..127645f83db6a43bc6be7aba18226af263c4f576 100644 (file)
@@ -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 \
index 083011e397d3bc94acc993e6d4073c57b952846a..d4290b71ed7c52d10e36fb975b6597268506964b 100644 (file)
@@ -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
 
index 4e59d034bbf319ba1de265694891709e2b98afc3..3248bfe28a0d118c7eab99c97cb0c3df076fc2be 100644 (file)
@@ -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,
 };
 
index 2e6c6f7815d26c5f57a48814320335adb4b30964..30b06b771e209cc7d6ba7a585e82d8601f190d11 100644 (file)
 #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); \
                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 (file)
index 0000000..1b9e78d
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <limits.h>
+
+#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 (file)
index 0000000..a90fc3e
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#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 */
index 86865e14c390ef7d943929b53015d4cfa254a60e..143e8b2105652440d4c6bb38c23f6df18eef1610 100644 (file)
 #include <stdint.h>
 #include <stdlib.h>
 
+// 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)