Get info on inactive LXCs

This commit is contained in:
Dan Snyder 2025-01-18 13:14:08 -05:00
parent afc17db869
commit 692f9dbd32
3 changed files with 37 additions and 2 deletions

View File

@ -1,5 +1,6 @@
#include "lxcstat.h" #include "lxcstat.h"
#include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -42,6 +43,32 @@ int readLxcConfig(lxcinfo *lxc, int lxcid) {
return 0; 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 *getLXCInfo(lxcinfo **lxcs, int lxcid) {
lxcinfo *lxc = *lxcs; lxcinfo *lxc = *lxcs;
while (lxc != NULL) { while (lxc != NULL) {
@ -51,6 +78,7 @@ lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid) {
lxc = calloc(1, sizeof(lxcinfo)); lxc = calloc(1, sizeof(lxcinfo));
lxc->lxcid = lxcid; lxc->lxcid = lxcid;
lxc->running = 0;
if (readLxcConfig(lxc, lxcid)) { if (readLxcConfig(lxc, lxcid)) {
// failed to read LXC // failed to read LXC

View File

@ -5,6 +5,7 @@ typedef struct _lxc_info {
struct _lxc_info *next; struct _lxc_info *next;
struct _lxc_info *head; struct _lxc_info *head;
struct _lxc_info *tail; // only valid for head lxcinfo struct _lxc_info *tail; // only valid for head lxcinfo
int running;
int lxcid; int lxcid;
int cpucount; int cpucount;
int memlimit; int memlimit;
@ -13,5 +14,6 @@ typedef struct _lxc_info {
} lxcinfo; } lxcinfo;
lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid); lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid);
void getInactiveLXCs(lxcinfo **lxcs);
#endif #endif

View File

@ -318,8 +318,12 @@ char *readProcesses(char *procdir) {
if (memcmp(cur->cpuset, lxcTag, strlen(lxcTag)) == 0) { if (memcmp(cur->cpuset, lxcTag, strlen(lxcTag)) == 0) {
// Resides in LXC -- read file and tag // Resides in LXC -- read file and tag
sscanf(cur->cpuset, "/lxc/%d/ns", &cur->lxc); 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); printf("Failed to read LXC config <%d>\n", cur->lxc);
} else {
lxc->running = 1;
}
} else { } else {
cur->lxc = -1; cur->lxc = -1;
} }
@ -336,7 +340,7 @@ nextProcess:
linkFamily(head); linkFamily(head);
aggregateStats(head); aggregateStats(head);
// printFamilyTree(head); getInactiveLXCs(&lxcs);
int clocks = sysconf(_SC_CLK_TCK); int clocks = sysconf(_SC_CLK_TCK);
char *output = malloc(8 * 1024 * 1024); 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_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_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_swap_limit_bytes{%s} %llu\n", buffer, lxc->swaplimit);
ptr += sprintf(ptr, "lxc_is_running{%s} %d\n", buffer, lxc->running);
// free and proceed // free and proceed
lxcinfo *prev = lxc; lxcinfo *prev = lxc;
lxc = lxc->next; lxc = lxc->next;