From 692f9dbd32d1d0d541cff0674d1ab3dd689f362c Mon Sep 17 00:00:00 2001 From: Dan Snyder Date: Sat, 18 Jan 2025 13:14:08 -0500 Subject: [PATCH] Get info on inactive LXCs --- src/lxcstat.c | 28 ++++++++++++++++++++++++++++ src/lxcstat.h | 2 ++ src/process.c | 9 +++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/lxcstat.c b/src/lxcstat.c index db93472..6aec139 100644 --- a/src/lxcstat.c +++ b/src/lxcstat.c @@ -1,5 +1,6 @@ #include "lxcstat.h" +#include #include #include #include @@ -42,6 +43,32 @@ int readLxcConfig(lxcinfo *lxc, int lxcid) { return 0; } +void getInactiveLXCs(lxcinfo **lxcs) { + struct dirent *pDirent; + DIR *pDir; + + // Ensure we can open directory. + pDir = opendir("/etc/pve/lxc"); + if (pDir == NULL) { + printf("Cannot open directory '/etc/pve/lxc'\n"); + return; + } + + // Process each entry. + while ((pDirent = readdir(pDir)) != NULL) { + char *fname = pDirent->d_name; + char extension[20]; + int lxcid; + + int argc = sscanf(fname, "%d.%s", &lxcid, (char *) extension); + // ensure it's an LXC config + if (argc != 2 || strcmp((char *) extension, "conf") != 0) continue; + getLXCInfo(lxcs, lxcid); // don't care about result + } + + closedir(pDir); +} + lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid) { lxcinfo *lxc = *lxcs; while (lxc != NULL) { @@ -51,6 +78,7 @@ lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid) { lxc = calloc(1, sizeof(lxcinfo)); lxc->lxcid = lxcid; + lxc->running = 0; if (readLxcConfig(lxc, lxcid)) { // failed to read LXC diff --git a/src/lxcstat.h b/src/lxcstat.h index 5032b1d..d42c1be 100644 --- a/src/lxcstat.h +++ b/src/lxcstat.h @@ -5,6 +5,7 @@ typedef struct _lxc_info { struct _lxc_info *next; struct _lxc_info *head; struct _lxc_info *tail; // only valid for head lxcinfo + int running; int lxcid; int cpucount; int memlimit; @@ -13,5 +14,6 @@ typedef struct _lxc_info { } lxcinfo; lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid); +void getInactiveLXCs(lxcinfo **lxcs); #endif \ No newline at end of file diff --git a/src/process.c b/src/process.c index 0a7179c..642ef9a 100644 --- a/src/process.c +++ b/src/process.c @@ -318,8 +318,12 @@ char *readProcesses(char *procdir) { if (memcmp(cur->cpuset, lxcTag, strlen(lxcTag)) == 0) { // Resides in LXC -- read file and tag sscanf(cur->cpuset, "/lxc/%d/ns", &cur->lxc); - if (getLXCInfo(&lxcs, cur->lxc) == NULL) + lxcinfo *lxc = getLXCInfo(&lxcs, cur->lxc); + if (lxc == NULL) { printf("Failed to read LXC config <%d>\n", cur->lxc); + } else { + lxc->running = 1; + } } else { cur->lxc = -1; } @@ -336,7 +340,7 @@ nextProcess: linkFamily(head); aggregateStats(head); - // printFamilyTree(head); + getInactiveLXCs(&lxcs); int clocks = sysconf(_SC_CLK_TCK); char *output = malloc(8 * 1024 * 1024); @@ -378,6 +382,7 @@ nextProcess: ptr += sprintf(ptr, "lxc_cpu_core_count{%s} %u\n", buffer, lxc->cpucount); ptr += sprintf(ptr, "lxc_memory_limit_bytes{%s} %llu\n", buffer, lxc->memlimit); ptr += sprintf(ptr, "lxc_swap_limit_bytes{%s} %llu\n", buffer, lxc->swaplimit); + ptr += sprintf(ptr, "lxc_is_running{%s} %d\n", buffer, lxc->running); // free and proceed lxcinfo *prev = lxc; lxc = lxc->next;