]> git.ipfire.org Git - collecty.git/commitdiff
sources: Add source for disk usage
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Oct 2025 13:26:55 +0000 (13:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Oct 2025 13:26:55 +0000 (13:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/sources.c
src/daemon/sources/df.c [new file with mode: 0644]
src/daemon/sources/df.h [new file with mode: 0644]

index a264ed97b179e738172998700b7d0a5d5c4031f6..cde8540a98f610818473296c510deeb67ae17a2b 100644 (file)
@@ -130,6 +130,8 @@ dist_collectyd_SOURCES = \
        src/daemon/sources/conntrack.h \
        src/daemon/sources/contextswitches.c \
        src/daemon/sources/contextswitches.h \
+       src/daemon/sources/df.c \
+       src/daemon/sources/df.h \
        src/daemon/sources/loadavg.c \
        src/daemon/sources/loadavg.h \
        src/daemon/sources/pressure-cpu.c \
index 55e4f011f8fbd82ad49172ef2eca26b3f1ef4d3b..caef0e1e626b0a018e73e519b2334472ba327e1e 100644 (file)
@@ -30,6 +30,7 @@
 // Load all sources
 #include "sources/conntrack.h"
 #include "sources/contextswitches.h"
+#include "sources/df.h"
 #include "sources/loadavg.h"
 #include "sources/pressure-cpu.h"
 #include "sources/pressure-io.h"
@@ -40,6 +41,7 @@
 static const collecty_source_impl* source_impls[] = {
        &conntrack_source,
        &contextswitches_source,
+       &df_source,
        &loadavg_source,
        &pressure_cpu_source,
        &pressure_io_source,
diff --git a/src/daemon/sources/df.c b/src/daemon/sources/df.c
new file mode 100644 (file)
index 0000000..ce4040f
--- /dev/null
@@ -0,0 +1,101 @@
+/*#############################################################################
+#                                                                             #
+# 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 <mntent.h>
+#include <sys/statvfs.h>
+
+#include "../ctx.h"
+#include "../source.h"
+#include "df.h"
+
+static int df_collect(collecty_ctx* ctx, collecty_source* source) {
+       struct mntent* mountpoint = NULL;
+       struct statvfs stat = {};
+       FILE* f = NULL;
+       int r = 0;
+
+       // Open /proc/self/mounts
+       f = setmntent("/proc/self/mounts", "r");
+       if (!f) {
+               ERROR(ctx, "Failed to open /proc/self/mounts: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Walk through all mountpoints
+       for (;;) {
+               // Fetch the next mountpoint
+               mountpoint = getmntent(f);
+               if (!mountpoint)
+                       break;
+
+               // Ignore everything that is a virtually mounted device
+               if (*mountpoint->mnt_fsname != '/')
+                       continue;
+
+               // Call statvfs() on the mountpoint
+               r = statvfs(mountpoint->mnt_dir, &stat);
+               if (r < 0) {
+                       ERROR(ctx, "statvfs() on %s failed: %m\n", mountpoint->mnt_dir);
+                       r = -errno;
+                       goto ERROR;
+               }
+
+               // Determine the block size
+               size_t block_size = (stat.f_frsize) ? stat.f_frsize : stat.f_bsize;
+
+               // Submit stats
+               r = collecty_source_submit(source, mountpoint->mnt_dir, "%lu:%lu:%lu:%lu",
+                       // used
+                       (stat.f_blocks - stat.f_bfree) * block_size,
+                       // free
+                       stat.f_bfree * block_size,
+                       // inodes used
+                       stat.f_files - stat.f_ffree,
+                       // inodes free
+                       stat.f_ffree
+               );
+               if (r < 0)
+                       return r;
+       }
+
+ERROR:
+       if (f)
+               endmntent(f);
+
+       return r;
+}
+
+const collecty_source_impl df_source = {
+       .name    = "df",
+
+       // RRD Data Sources
+       .rrd_dss = {
+               { "bytes_used",  "GAUGE", 0, -1, },
+               { "bytes_free",  "GAUGE", 0, -1, },
+               { "inodes_used", "GAUGE", 0, -1, },
+               { "inodes_free", "GAUGE", 0, -1, },
+               { NULL },
+       },
+
+       // Methods
+       .collect = df_collect,
+};
diff --git a/src/daemon/sources/df.h b/src/daemon/sources/df.h
new file mode 100644 (file)
index 0000000..a68c968
--- /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_SOURCE_DF_H
+#define COLLECTY_SOURCE_DF_H
+
+#include "../source.h"
+
+extern const collecty_source_impl df_source;
+
+#endif /* COLLECTY_SOURCE_DF_H */