]> git.ipfire.org Git - collecty.git/commitdiff
util: Add helper function to format numbers for RRD
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 28 Sep 2025 09:42:33 +0000 (09:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 28 Sep 2025 09:42:33 +0000 (09:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/util.c [new file with mode: 0644]
src/daemon/util.h [new file with mode: 0644]

index 1a799de319c2f2a8ab1fe7658b1f74709fc6af8d..7587c5664dc43e83210888a01085df073f0d5188 100644 (file)
@@ -105,7 +105,9 @@ dist_collectyd_SOURCES = \
        src/daemon/modules/loadavg.c \
        src/daemon/modules/loadavg.h \
        src/daemon/queue.c \
-       src/daemon/queue.h
+       src/daemon/queue.h \
+       src/daemon/util.c \
+       src/daemon/util.h
 
 collectyd_CPPFLAGS = \
        $(AM_CPPFLAGS)
diff --git a/src/daemon/util.c b/src/daemon/util.c
new file mode 100644 (file)
index 0000000..6231c47
--- /dev/null
@@ -0,0 +1,42 @@
+/*#############################################################################
+#                                                                             #
+# 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 <errno.h>
+#include <stdio.h>
+
+#include "util.h"
+
+int __collecty_format_number(char* buffer, size_t length, int number) {
+       int r;
+
+       // Format the number if positive or zero
+       if (number >= 0)
+               r = snprintf(buffer, length, "%d", number);
+
+       // Otherwise we set it to UNKNOWN
+       else
+               r = snprintf(buffer, length, "U");
+
+       // Abort on error
+       if (r < 0)
+               return -errno;
+
+       return 0;
+}
diff --git a/src/daemon/util.h b/src/daemon/util.h
new file mode 100644 (file)
index 0000000..04d8dfa
--- /dev/null
@@ -0,0 +1,29 @@
+/*#############################################################################
+#                                                                             #
+# 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_UTIL_H
+#define COLLECTY_UTIL_H
+
+#define collecty_format_number(buffer, number) \
+       __collecty_format_number(buffer, sizeof(buffer), number)
+
+int __collecty_format_number(char* buffer, size_t length, int number);
+
+#endif /* COLLECTY_UTIL_H */