From 4d8ea7a26982ff8793b1f1777944c624094d08c5 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 3 Oct 2025 12:46:08 +0000 Subject: [PATCH] graphs: Add a conntrack graph Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/daemon/colors.h | 7 ++ src/daemon/graphs.c | 2 + src/daemon/graphs/conntrack.c | 134 ++++++++++++++++++++++++++++++++++ src/daemon/graphs/conntrack.h | 28 +++++++ src/daemon/graphs/graph.h | 21 +++++- 6 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 src/daemon/graphs/conntrack.c create mode 100644 src/daemon/graphs/conntrack.h diff --git a/Makefile.am b/Makefile.am index ef2e564..956e758 100644 --- a/Makefile.am +++ b/Makefile.am @@ -105,6 +105,8 @@ dist_collectyd_SOURCES = \ src/daemon/graph-bus.h \ src/daemon/graphs.c \ src/daemon/graphs.h \ + src/daemon/graphs/conntrack.c \ + src/daemon/graphs/conntrack.h \ src/daemon/graphs/graph.h \ src/daemon/graphs/loadavg.c \ src/daemon/graphs/loadavg.h \ diff --git a/src/daemon/colors.h b/src/daemon/colors.h index 363d9f2..8994377 100644 --- a/src/daemon/colors.h +++ b/src/daemon/colors.h @@ -33,6 +33,13 @@ #define BLUE "#2196f3" #define LIGHT_BLUE "#03a9f4" +/* + Define some colours with a special meaning... +*/ + +// Limit - When there is a ceiling to resource usage +#define LIMIT RED + // Macro to make colours transparent #define COLOR_WITH_ALPHA(base, alpha) base alpha diff --git a/src/daemon/graphs.c b/src/daemon/graphs.c index 801d72f..02bb745 100644 --- a/src/daemon/graphs.c +++ b/src/daemon/graphs.c @@ -27,10 +27,12 @@ #include "graphs.h" // Load all graphs +#include "graphs/conntrack.h" #include "graphs/loadavg.h" // Register all graphs static const collecty_graph_impl* graph_impls[] = { + &conntrack_graph, &loadavg_graph, NULL, }; diff --git a/src/daemon/graphs/conntrack.c b/src/daemon/graphs/conntrack.c new file mode 100644 index 0000000..3eaf1ba --- /dev/null +++ b/src/daemon/graphs/conntrack.c @@ -0,0 +1,134 @@ +/*############################################################################# +# # +# 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 "conntrack.h" + +// Set some colors +#define COLOR_LOAD15 RED +#define COLOR_LOAD5 ORANGE +#define COLOR_LOAD1 YELLOW + +static int conntrack_title(collecty_ctx* ctx, + collecty_graph* graph, const char* object, char** title) { + return collecty_format_title(title, "%s", _("Connection Tracking Table")); +} + +static int conntrack_vlabel(collecty_ctx* ctx, + collecty_graph* graph, const char* object, char** title) { + return collecty_format_title(title, "%s", _("Entries")); +} + +static int conntrack_render(collecty_ctx* ctx, + collecty_graph* graph, collecty_args* args, const char* object) { + int r; + + // This requires the conntrack source + r = collecty_graph_require_source(graph, args, "conntrack", object); + if (r < 0) + return r; + + // Draw the maximum line + r = collecty_args_push(args, + DRAW_WITH_LABEL("LINE1", "max", LIMIT, DASHES SKIPSCALE), _("Limit")); + if (r < 0) + return r; + + // Show the current limit + r = collecty_args_push(args, PRINT_LARGE_INTEGER("max_cur")); + if (r < 0) + return r; + + // Add an empty column + r = collecty_args_push(args, PRINT_NOTHING); + if (r < 0) + return r; + + // Add an empty column + r = collecty_args_push(args, PRINT_NOTHING); + if (r < 0) + return r; + + // Add an empty column + r = collecty_args_push(args, PRINT_NOTHING EOL); + if (r < 0) + return r; + + // Draw the main area + r = collecty_args_push(args, DRAW_WITH_LABEL("AREA", "count", GREEN), _("Entries")); + if (r < 0) + return r; + + r = collecty_args_push(args, PRINT_LARGE_INTEGER("count_cur")); + if (r < 0) + return r; + + r = collecty_args_push(args, PRINT_LARGE_INTEGER("count_avg")); + if (r < 0) + return r; + + r = collecty_args_push(args, PRINT_LARGE_INTEGER("count_min")); + if (r < 0) + return r; + + r = collecty_args_push(args, PRINT_LARGE_INTEGER("count_max") EOL); + if (r < 0) + return r; + + // Add an empty line + r = collecty_args_push(args, EMPTY_COLUMN); + if (r < 0) + return r; + + // Add the headline for the "Current" column + r = collecty_args_push(args, HEADER, _("Current")); + if (r < 0) + return r; + + // Add the headline for the "Average" column + r = collecty_args_push(args, HEADER, _("Average")); + if (r < 0) + return r; + + // Add the headline for the "Minimum" column + r = collecty_args_push(args, HEADER, _("Minimum")); + if (r < 0) + return r; + + // Add the headline for the "Maximum" column + r = collecty_args_push(args, HEADER EOL, _("Maximum")); + if (r < 0) + return r; + + return 0; +} + +const collecty_graph_impl conntrack_graph = { + .name = "Conntrack", + .render = conntrack_render, + .title = conntrack_title, + .vlabel = conntrack_vlabel, + + // Limits + .lower_limit = 0, + .upper_limit = LONG_MAX, +}; diff --git a/src/daemon/graphs/conntrack.h b/src/daemon/graphs/conntrack.h new file mode 100644 index 0000000..45e0619 --- /dev/null +++ b/src/daemon/graphs/conntrack.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_CONNTRACK_H +#define COLLECTY_GRAPH_CONNTRACK_H + +#include "../graph.h" + +extern const collecty_graph_impl conntrack_graph; + +#endif /* COLLECTY_GRAPH_CONNTRACK_H */ diff --git a/src/daemon/graphs/graph.h b/src/daemon/graphs/graph.h index ad299aa..dabbfa5 100644 --- a/src/daemon/graphs/graph.h +++ b/src/daemon/graphs/graph.h @@ -44,16 +44,31 @@ // A column is exactly 16 characters wide #define COLUMN "%16s" +#define INTEGER "%%16.0lf" +#define LARGE_INTEGER "%%14.0lf %%s" #define FLOAT "%%14.2lf" +#define LARGE_FLOAT "%%12.2lf %%s" // Macro to terminate a line #define EOL "\\j" // Draw a LINE, AREA, etc. -#define DRAW(what, field, color) what ":" field color -#define DRAW_WITH_LABEL(what, field, color) DRAW(what, field, color) ":" LABEL +#define DRAW(what, field, color, ...) \ + what ":" field color __VA_ARGS__ + +#define DRAW_WITH_LABEL(what, field, color, ...) \ + DRAW(what, field, color) ":" LABEL __VA_ARGS__ + +// Modifiers for lines +#define DASHES ":dashes" +#define SKIPSCALE ":skipscale" // Add something to the legend of the graph -#define PRINT_FLOAT(field) "GPRINT:" field ":" FLOAT +#define PRINT(field, ...) "GPRINT:" field ":" __VA_ARGS__ +#define PRINT_NOTHING "COMMENT: " +#define PRINT_INTEGER(field) PRINT(field, INTEGER) +#define PRINT_LARGE_INTEGER(field) PRINT(field, LARGE_INTEGER) +#define PRINT_FLOAT(field) PRINT(field, FLOAT) +#define PRINT_LARGE_FLOAT(field) PRINT(field, LARGE_FLOAT) #endif /* COLLECTY_GRAPHS_GRAPH_H */ -- 2.47.3