diff options
author | Leah Neukirchen <leah@vuxu.org> | 2024-05-23 20:40:42 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2024-05-23 20:40:42 +0200 |
commit | de26ef5c51740e2ee6ba2e087a843400edcf8329 (patch) | |
tree | 55a3d919345b894746950c4cbbb922fab84b087c /vmstat.c | |
parent | 1b950a0077d3ed1bffda134e4d224dc3c6176819 (diff) | |
download | nano-exporter-de26ef5c51740e2ee6ba2e087a843400edcf8329.tar.gz nano-exporter-de26ef5c51740e2ee6ba2e087a843400edcf8329.tar.xz nano-exporter-de26ef5c51740e2ee6ba2e087a843400edcf8329.zip |
add vmstat collector
Diffstat (limited to 'vmstat.c')
-rw-r--r-- | vmstat.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/vmstat.c b/vmstat.c new file mode 100644 index 0000000..3e21b0f --- /dev/null +++ b/vmstat.c @@ -0,0 +1,64 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "scrape.h" +#include "util.h" + +// size of input buffer for paths and lines +#define BUF_SIZE 256 + +static void vmstat_collect(scrape_req *req, void *ctx); + +const struct collector vmstat_collector = { + .name = "vmstat", + .collect = vmstat_collect, +}; + +static const struct { + const char *name; + const char *key; + unsigned key_len; +} metrics[] = { + { .name = "node_vmstat_oom_kill", .key = "oom_kill ", .key_len = 9 }, + { .name = "node_vmstat_pgfault", .key = "pgfault ", .key_len = 8 }, + { .name = "node_vmstat_pgmajfault", .key = "pgmajfault ", .key_len = 11 }, + { .name = "node_vmstat_pgpgin", .key = "pgpgin ", .key_len = 7 }, + { .name = "node_vmstat_pgpgout", .key = "pgpgout ", .key_len = 8 }, + { .name = "node_vmstat_pswpin", .key = "pswpin ", .key_len = 7 }, + { .name = "node_vmstat_pswpout", .key = "pswpout ", .key_len = 8 }, +}; +#define NMETRICS (sizeof metrics / sizeof *metrics) + +static void vmstat_collect(scrape_req *req, void *ctx) { + (void) ctx; + + // buffers + + char buf[BUF_SIZE]; + + FILE *f; + + // scan /proc/stat for metrics + + f = fopen(PATH("/proc/vmstat"), "r"); + if (!f) + return; + + while (fgets_line(buf, sizeof buf, f)) { + for (size_t m = 0; m < NMETRICS; m++) { + if (strncmp(buf, metrics[m].key, metrics[m].key_len) != 0) + continue; + + char *end; + double d = strtod(buf + metrics[m].key_len, &end); + if (*end != '\0' && *end != ' ' && *end != '\n') + continue; + + scrape_write(req, metrics[m].name, 0, d); + break; + } + } + + fclose(f); +} |