diff options
author | Leah Neukirchen <leah@vuxu.org> | 2024-06-09 20:09:41 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2024-06-09 20:09:41 +0200 |
commit | 6a39520fb104ddc0bedf19e7de872615590ba709 (patch) | |
tree | 1cebdcd12e7f36a69b56f14611c8bf8b907e0ec5 /schedstat.c | |
parent | ae3717a44deb305d5dca9151b2f7cb02a73cd6c1 (diff) | |
download | nano-exporter-6a39520fb104ddc0bedf19e7de872615590ba709.tar.gz nano-exporter-6a39520fb104ddc0bedf19e7de872615590ba709.tar.xz nano-exporter-6a39520fb104ddc0bedf19e7de872615590ba709.zip |
add schedstat collector
Diffstat (limited to 'schedstat.c')
-rw-r--r-- | schedstat.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/schedstat.c b/schedstat.c new file mode 100644 index 0000000..985ce5c --- /dev/null +++ b/schedstat.c @@ -0,0 +1,45 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#include "scrape.h" +#include "util.h" + +// size of input buffer for paths and lines +#define BUF_SIZE 256 + +static void schedstat_collect(scrape_req *req, void *ctx); + +const struct collector schedstat_collector = { + .name = "schedstat", + .collect = schedstat_collect, +}; + +static void schedstat_collect(scrape_req *req, void *ctx) { + (void) ctx; + + FILE *f = fopen("/proc/schedstat", "r"); + if (!f) + return; + + char cpu[8]; + long long running, waiting, timeslices; + + char line[BUF_SIZE]; + while(fgets_line(line, sizeof line, f)) { + if (sscanf(line, "cpu%[0-9] %*d %*d %*d %*d %*d %*d %lld %lld %lld\n", + cpu, &running, &waiting, ×lices) == 4) { + struct label labels[] = { + { .key = "cpu", .value = cpu }, + LABEL_END, + }; + scrape_write(req, "node_schedstat_running_seconds_total", labels, running / 1e9); + scrape_write(req, "node_schedstat_waiting_seconds_total", labels, waiting / 1e9); + scrape_write(req, "node_schedstat_timeslices_total", labels, (double)timeslices); + } + } + + fclose(f); +} |