From: Michael Tremer Date: Sat, 4 Oct 2025 13:26:55 +0000 (+0000) Subject: sources: Add source for disk usage X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4321be8c609f6e4eb8bdfa6dd93ac4bf157361a7;p=telemetry.git sources: Add source for disk usage Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index a264ed9..cde8540 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/daemon/sources.c b/src/daemon/sources.c index 55e4f01..caef0e1 100644 --- a/src/daemon/sources.c +++ b/src/daemon/sources.c @@ -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 index 0000000..ce4040f --- /dev/null +++ b/src/daemon/sources/df.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#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 index 0000000..a68c968 --- /dev/null +++ b/src/daemon/sources/df.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_SOURCE_DF_H +#define COLLECTY_SOURCE_DF_H + +#include "../source.h" + +extern const collecty_source_impl df_source; + +#endif /* COLLECTY_SOURCE_DF_H */