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

index 1cd98a00d3f6662d688e3537935e4ee8259c60f3..58bd9cf5348fec7c29cfa0c5e580d7e2c44f6f6e 100644 (file)
@@ -134,6 +134,8 @@ dist_collectyd_SOURCES = \
        src/daemon/sources/df.h \
        src/daemon/sources/loadavg.c \
        src/daemon/sources/loadavg.h \
+       src/daemon/sources/memory.c \
+       src/daemon/sources/memory.h \
        src/daemon/sources/pressure-cpu.c \
        src/daemon/sources/pressure-cpu.h \
        src/daemon/sources/pressure-io.c \
index e22f6efb6fc5125e27a2f876e2fa94ec27114dba..9fad49ad4c6ef50a1b21fd51f156e13ec738e9c3 100644 (file)
 
 #include "proc.h"
 
+int collecty_proc_read_meminfo(collecty_ctx* ctx, collecty_proc_meminfo* meminfo) {
+       unsigned long v;
+       FILE* f = NULL;
+       char k[64];
+       int r;
+
+       // Open /proc/meminfo
+       f = fopen("/proc/meminfo", "r");
+       if (!f)
+               return -errno;
+
+       // Read line by line
+       for (;;) {
+               r = fscanf(f, "%63s %lu kB\n", k, &v);
+               if (r < 0)
+                       break;
+
+               // Skip any lines that could not be parsed
+               if (r < 2)
+                       continue;
+
+               // Convert the value from kB into Bytes
+               v *= 1024;
+
+               // Store the values
+               if (strcmp("MemTotal:", k) == 0)
+                       meminfo->mem_total = v;
+               else if (strcmp("MemFree:", k) == 0)
+                       meminfo->mem_free = v;
+               else if (strcmp("MemAvailable:", k) == 0)
+                       meminfo->mem_available = v;
+               else if (strcmp("Cached:", k) == 0)
+                       meminfo->cached = v;
+               else if (strcmp("Buffers:", k) == 0)
+                       meminfo->buffers = v;
+               else if (strcmp("Active:", k) == 0)
+                       meminfo->active = v;
+               else if (strcmp("Inactive:", k) == 0)
+                       meminfo->inactive = v;
+               else if (strcmp("Active(anon):", k) == 0)
+                       meminfo->active_anon = v;
+               else if (strcmp("Inactive(anon):", k) == 0)
+                       meminfo->inactive_anon = v;
+               else if (strcmp("Active(file):", k) == 0)
+                       meminfo->active_file = v;
+               else if (strcmp("Inactive(file):", k) == 0)
+                       meminfo->inactive_file = v;
+               else if (strcmp("SwapTotal:", k) == 0)
+                       meminfo->swap_total = v;
+               else if (strcmp("SwapFree:", k) == 0)
+                       meminfo->swap_free = v;
+               else if (strcmp("Shmem:", k) == 0)
+                       meminfo->shmem = v;
+               else if (strcmp("Slab:", k) == 0)
+                       meminfo->slab = v;
+               else if (strcmp("SReclaimable:", k) == 0)
+                       meminfo->sreclaimable = v;
+               else if (strcmp("SUnreclaim:", k) == 0)
+                       meminfo->sunreclaim = v;
+       }
+
+       // Success
+       r = 0;
+
+       // Cleanup
+       if (f)
+               fclose(f);
+
+       return r;
+}
+
 static int collecty_proc_read_stat_line(collecty_ctx* ctx,
                const char* tag, char* line, unsigned long* fields, size_t max_fields) {
        char* p = NULL;
index e3f6aaecd8acf7b41e8d862abb24101136841058..2dfb62ce884a05c43f28442062814b221c50907a 100644 (file)
 #define COLLECTY_PROC_H
 
 #include <stddef.h>
+#include <stdint.h>
 
 #include "ctx.h"
 
+typedef struct collecty_proc_meminfo {
+       // Memory
+       uint64_t mem_total;
+       uint64_t mem_free;
+       uint64_t mem_available;
+       uint64_t cached;
+       uint64_t buffers;
+       uint64_t active;
+       uint64_t inactive;
+       uint64_t active_anon;
+       uint64_t inactive_anon;
+       uint64_t active_file;
+       uint64_t inactive_file;
+       uint64_t shmem;
+       uint64_t slab;
+       uint64_t sreclaimable;
+       uint64_t sunreclaim;
+
+       // Swap
+       uint64_t swap_total;
+       uint64_t swap_free;
+} collecty_proc_meminfo;
+
+int collecty_proc_read_meminfo(collecty_ctx* ctx, collecty_proc_meminfo* meminfo);
+
 int collecty_proc_read_stat(collecty_ctx* ctx,
        const char* tag, unsigned long* fields, size_t max_fields);
 
index 309bc687b3c57a8ba8c8cca768e3fc065105e988..5381b61ccf7e6c6916da05201735f09ba7ed11f8 100644 (file)
@@ -32,6 +32,7 @@
 #include "sources/contextswitches.h"
 #include "sources/df.h"
 #include "sources/loadavg.h"
+#include "sources/memory.h"
 #include "sources/pressure-cpu.h"
 #include "sources/pressure-io.h"
 #include "sources/pressure-memory.h"
@@ -46,6 +47,7 @@ static const collecty_source_impl* source_impls[] = {
        &contextswitches_source,
        &df_source,
        &loadavg_source,
+       &memory_source,
        &pressure_cpu_source,
        &pressure_io_source,
        &pressure_memory_source,
diff --git a/src/daemon/sources/memory.c b/src/daemon/sources/memory.c
new file mode 100644 (file)
index 0000000..dd06f1b
--- /dev/null
@@ -0,0 +1,75 @@
+/*#############################################################################
+#                                                                             #
+# 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 "../ctx.h"
+#include "../proc.h"
+#include "../source.h"
+#include "memory.h"
+
+static int memory_collect(collecty_ctx* ctx, collecty_source* source) {
+       collecty_proc_meminfo meminfo = {};
+       int r;
+
+       // Read all values from /proc/meminfo
+       r = collecty_proc_read_meminfo(ctx, &meminfo);
+       if (r < 0)
+               return r;
+
+       // Submit the values
+       return collecty_source_submit(source, NULL,
+               "%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu:%lu",
+               meminfo.mem_total, meminfo.mem_free, meminfo.mem_available,
+               meminfo.cached, meminfo.buffers, meminfo.active, meminfo.inactive,
+               meminfo.active_anon, meminfo.inactive_anon, meminfo.active_file,
+               meminfo.inactive_file, meminfo.shmem, meminfo.slab, meminfo.sreclaimable,
+               meminfo.sunreclaim, meminfo.swap_total, meminfo.swap_free);
+}
+
+const collecty_source_impl memory_source = {
+       .name    = "memory",
+
+       // RRD Data Sources
+       .rrd_dss = {
+               // Memory
+               { "mem_total",      "GAUGE", 0, -1, },
+               { "mem_free",       "GAUGE", 0, -1, },
+               { "mem_available",  "GAUGE", 0, -1, },
+               { "cached",         "GAUGE", 0, -1, },
+               { "buffers",        "GAUGE", 0, -1, },
+               { "active",         "GAUGE", 0, -1, },
+               { "inactive",       "GAUGE", 0, -1, },
+               { "active_anon",    "GAUGE", 0, -1, },
+               { "inactive_anon",  "GAUGE", 0, -1, },
+               { "active_file",    "GAUGE", 0, -1, },
+               { "inactive_file",  "GAUGE", 0, -1, },
+               { "shmem",          "GAUGE", 0, -1, },
+               { "slab",           "GAUGE", 0, -1, },
+               { "sreclaimable",   "GAUGE", 0, -1, },
+               { "sunreclaimable", "GAUGE", 0, -1, },
+
+               // Swap
+               { "swap_total",     "GAUGE", 0, -1, },
+               { "swap_free",      "GAUGE", 0, -1, },
+               { NULL },
+       },
+
+       // Methods
+       .collect = memory_collect,
+};
diff --git a/src/daemon/sources/memory.h b/src/daemon/sources/memory.h
new file mode 100644 (file)
index 0000000..f95aa5f
--- /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_MEMORY_H
+#define COLLECTY_SOURCE_MEMORY_H
+
+#include "../source.h"
+
+extern const collecty_source_impl memory_source;
+
+#endif /* COLLECTY_SOURCE_MEMORY_H */