Separate LXC info fields

This commit is contained in:
Dan Snyder 2025-01-14 12:29:15 -05:00 committed by dan
parent 7901ad145d
commit b7e1868f24
7 changed files with 412 additions and 318 deletions

View File

@ -1,5 +1,6 @@
{
"files.associations": {
"stdlib.h": "c"
"stdlib.h": "c",
"process.h": "c"
}
}

75
src/lxcstat.c Normal file
View File

@ -0,0 +1,75 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "lxcstat.h"
#include "parsing.h"
int parseLxcConfigFile(lxcinfo *lxc, char *filedata) {
lxc->cpucount = 0;
lxc->memlimit = 0;
char *temp = filedata;
lxc->cpucount = findAndParseField(&temp, "cores");
temp = filedata;
lxc->memlimit = findAndParseField(&temp, "memory") * 1024 * 1024;
char *ptr = strstr(filedata, "hostname:");
ptr += strlen("hostname:");
while (*ptr == ' ') ptr++;
strtok(ptr, "\n\t ");
strcpy(lxc->hostname, ptr);
}
int readLxcConfig(lxcinfo *lxc, int lxcid) {
char *fname = malloc(1024);
char *buffer = malloc(4096);
sprintf(fname, "/etc/pve/lxc/%d.conf", lxcid);
FILE *file = fopen(fname, "rb");
if (file == NULL) {
printf("Failed to open <%s>: %d\n", fname, errno);
return -1;
}
fread(buffer, 1, 4096, file);
fclose(file);
parseLxcConfigFile(lxc, buffer);
free(buffer);
free(fname);
return 0;
}
lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid) {
lxcinfo *lxc = *lxcs;
while (lxc != NULL) {
if (lxc->lxcid == lxcid) return lxc;
lxc = lxc->next;
}
lxc = malloc(sizeof(lxcinfo));
lxc->lxcid = lxcid;
if (readLxcConfig(lxc, lxcid)) {
// failed to read LXC
free(lxc);
return NULL;
}
if (*lxcs == NULL) {
// head was NULL - first LXC
*lxcs = lxc;
lxc->head = lxc;
lxc->tail = lxc;
lxc->next = NULL;
} else {
// other LXCs exist - chain to tail
(*lxcs)->tail->next = lxc;
(*lxcs)->tail = lxc;
lxc->head = *lxcs;
lxc->next = NULL;
}
return lxc;
}

16
src/lxcstat.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef LXCSTAT_H
#define LXCSTAT_H
typedef struct _lxc_info {
struct _lxc_info *next;
struct _lxc_info *head;
struct _lxc_info *tail; // only valid for head lxcinfo
int lxcid;
int cpucount;
int memlimit;
char hostname[128];
} lxcinfo;
lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid);
#endif

44
src/parsing.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef PARSING_H
#define PARSING_H
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
static inline uint64_t fast_str2ull(char **str) {
uint64_t result = 0;
int maxlen = 20; // length of maximum value of 18446744073709551615
while (maxlen-- && **str >= '0' && **str <= '9') {
result *= 10;
result += **str - '0';
(*str)++;
}
return result;
}
static inline int64_t fast_str2ll(char **str) {
bool neg = false;
if (**str == '-') {
neg = true;
(*str)++;
}
uint64_t res = fast_str2ull(str);
int64_t result = (int64_t)res;
return neg ? -result : result;
}
static inline uint64_t findAndParseField(char **filedata, char *fld) {
char *field = strstr(*filedata, fld);
if (!field) return -1;
while (*field < '0' || *field > '9') field++;
*filedata = field;
uint64_t val = fast_str2ull(filedata);
return val;
}
#endif

View File

@ -8,17 +8,16 @@ typedef struct st_process {
struct st_process *next;
struct st_process *prev;
uint32_t flags;
char cpuset[128];
int tgid;
// attributes identifying thread
int lxc;
int pid;
int ppid;
int tgid;
int level;
char label[128];
char cpuset[128];
char lxcname[128];
int iskernel;
// statistics - each outputs 1 line per process
int iskernel;
uint64_t cpuTime;
uint64_t childCpuTime;
uint64_t nThreads;

View File

@ -1,12 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main3(int argc, char *argv[]) {
DIR *procdir = opendir("/proc");
struct dirent *pDirent;
while ((pDirent = readdir(procdir)) != NULL) {
printf("[%s]\n", pDirent->d_name);
}
}

View File

@ -1,48 +1,24 @@
#include <arpa/inet.h>
#include <dirent.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#include <dirent.h>
#include "process.h"
#include "lxcstat.h"
#include "parsing.h"
#ifndef PF_KTHREAD
#define PF_KTHREAD 0x00200000
#endif
static inline uint64_t fast_str2ull(char** str) {
uint64_t result = 0;
int maxlen = 20; // length of maximum value of 18446744073709551615
while (maxlen-- && **str >= '0' && **str <= '9') {
result *= 10;
result += **str - '0';
(*str)++;
}
return result;
}
static inline int64_t fast_str2ll(char** str) {
bool neg = false;
if (**str == '-') {
neg = true;
(*str)++;
}
uint64_t res = fast_str2ull(str);
int64_t result = (int64_t) res;
return neg ? -result : result;
}
#define skipRange(inclusive, exclusive) \
for (int i##inclusive=0; i##inclusive<exclusive-inclusive; i##inclusive++) \
for (int i##inclusive = 0; i##inclusive < exclusive - inclusive; \
i##inclusive++) \
location = strchr(location, ' ') + 1;
int parseStatFile(Process *proc, char *filedata) {
@ -103,20 +79,13 @@ int parseStatFile(Process *proc, char *filedata) {
return 0;
}
static inline uint64_t findAndParseField(char **filedata, char *fld) {
char *field = strstr(*filedata, fld);
if (!field) return -1;
while (*field < '0' || *field > '9') field++;
*filedata = field;
uint64_t val = fast_str2ull(filedata);
return val;
}
int parseStatusFile(Process *proc, char *filedata) {
proc->tgid = findAndParseField(&filedata, "Tgid");
proc->pid = findAndParseField(&filedata, "Pid");
if (proc->pid != proc->tgid) return -1; // ignore child threads, as their stats are the same as the parent
if (proc->pid != proc->tgid)
return -1; // ignore child threads, as their stats are the same as the
// parent
proc->resident = 1024 * findAndParseField(&filedata, "VmRSS");
proc->swap = 1024 * findAndParseField(&filedata, "VmSwap");
@ -153,6 +122,7 @@ char *readProcesses(char *procdir) {
cur->prev = NULL;
cur->next = NULL;
Process *head = cur;
lxcinfo *lxcs = NULL;
while ((pDirent = readdir(pDir)) != NULL) {
first = pDirent->d_name[0];
@ -194,19 +164,10 @@ 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);
sprintf(fname, "/etc/pve/lxc/%d.conf", cur->lxc);
file = fopen(fname, "rb");
if (file == NULL) goto nextProcess;
fread(buffer, 1, 4096, file);
fclose(file);
if (getLXCInfo(&lxcs, cur->lxc) == NULL)
printf("Failed to read LXC config <%d>\n", cur->lxc);
char *ptr = strstr(buffer, "hostname:");
ptr += strlen("hostname:");
while (*ptr == ' ') ptr++;
strtok(ptr, "\n\t ");
strcpy(cur->lxcname, ptr);
} else {
cur->lxcname[0] = 0;
cur->lxc = -1;
}
@ -228,24 +189,33 @@ nextProcess:
cur = head;
while (cur != NULL) {
// create process descriptor
sprintf(buffer, "pid=\"%d\",ppid=\"%d\",label=\"%s\",kernel=\"%d\",ctid=\"%d\",lxc=\"%s\"", cur->pid, cur->ppid, cur->label, cur->iskernel, cur->lxc, cur->lxcname);
ptr += sprintf(ptr, "proc_cpu_time{%s} %f\n", buffer, (double) cur->cpuTime / clocks);
ptr += sprintf(ptr, "proc_child_cpu_time{%s} %f\n", buffer, (double) cur->childCpuTime / clocks);
ptr += sprintf(ptr, "proc_num_threads{%s} %llu\n", buffer, cur->nThreads);
ptr += sprintf(ptr, "proc_resident_bytes{%s} %llu\n", buffer, cur->resident);
ptr += sprintf(ptr, "proc_swap_bytes{%s} %llu\n", buffer, cur->swap);
ptr += sprintf(ptr, "proc_fileio_bytes_written{%s} %llu\n", buffer, cur->written);
ptr += sprintf(ptr, "proc_fileio_bytes_read{%s} %llu\n", buffer, cur->read);
cur = cur->next;
}
cur = head;
while (cur != NULL) {
sprintf(buffer,
"pid=\"%d\",ppid=\"%d\",label=\"%s\",lxc=\"%d\",level=\"%d\"",
cur->pid, cur->ppid, cur->label, cur->lxc, cur->level);
ptr += sprintf(ptr, "process_cpu_time_seconds{%s} %f\n", buffer, (double) cur->cpuTime / clocks);
ptr += sprintf(ptr, "process_child_cpu_time_seconds{%s} %f\n", buffer,(double) cur->childCpuTime / clocks);
ptr += sprintf(ptr, "process_num_threads{%s} %llu\n", buffer, cur->nThreads);
ptr += sprintf(ptr, "process_resident_bytes{%s} %llu\n", buffer, cur->resident);
ptr += sprintf(ptr, "process_swap_bytes{%s} %llu\n", buffer, cur->swap);
ptr += sprintf(ptr, "process_fileio_bytes_written{%s} %llu\n", buffer, cur->written);
ptr += sprintf(ptr, "process_fileio_bytes_read{%s} %llu\n", buffer, cur->read);
ptr += sprintf(ptr, "process_is_kernel_process{%s} %d\n", buffer, cur->iskernel);
// free and proceed
Process *prev = cur;
cur = cur->next;
free(prev);
}
lxcinfo *lxc = lxcs;
while (lxc != NULL) {
sprintf(buffer, "lxc=\"%d\",lxcname=\"%s\"", lxc->lxcid, lxc->hostname);
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);
// free and proceed
lxcinfo *prev = lxc;
lxc = lxc->next;
free(prev);
}
closedir(pDir);
free(buffer);
@ -300,7 +270,6 @@ void handle_client(int client_socket) {
}
int main(int argc, char *argv[]) {
int port = 9101;
if (argc == 1) {
@ -329,7 +298,8 @@ int main(int argc, char *argv[]) {
server_addr.sin_port = htons(port);
// Bind the socket to the specified port
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
if (bind(server_socket, (struct sockaddr *)&server_addr,
sizeof(server_addr)) == -1) {
perror("Bind failed");
close(server_socket);
exit(EXIT_FAILURE);
@ -346,7 +316,8 @@ int main(int argc, char *argv[]) {
// Accept and handle incoming connections
while (1) {
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr,
&addr_len)) == -1) {
perror("Accept failed");
continue;
}