Skip to content

Commit 6fa696b

Browse files
committed
emit system free metrics with '/proc/meminfo'
1 parent abd6e77 commit 6fa696b

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/modules/keydb_modstatsd/modmain.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <sys/time.h>
1313
#include <regex>
1414
#include <sstream>
15+
#include <iostream>
16+
#include <fstream>
1517

1618
using namespace Statsd;
1719

@@ -529,6 +531,18 @@ void handle_client_list_response(struct RedisModuleCtx *ctx, const char *szReply
529531
g_stats->gauge("total_replica_client_output_buffer", totalReplicaClientOutputBuffer);
530532
}
531533

534+
void emit_system_free_memory() {
535+
std::ifstream meminfo("/proc/meminfo");
536+
std::string line;
537+
while (std::getline(meminfo, line)) {
538+
if (line.find("MemFree:") != std::string::npos) {
539+
unsigned long memFreeInKB;
540+
std::sscanf(line.c_str(), "MemFree: %lu kB", &memFreeInKB);
541+
g_stats->gauge("systemFreeMemory_MB", memFreeInKB / 1024);
542+
return;
543+
}
544+
}
545+
}
532546

533547
void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data) {
534548
static time_t lastTime = 0;
@@ -607,6 +621,8 @@ void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64
607621
g_stats->gauge("keys", keys);
608622
RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"keys\": %llu", keys);
609623
g_stats->timing("metrics_time_taken_us", ustime() - startTime);
624+
625+
emit_system_free_memory();
610626

611627
lastTime = curTime;
612628
}

0 commit comments

Comments
 (0)