about summary refs log tree commit diff
path: root/vmstat.c
blob: 3e21b0fd26f4bcfed22c7d3ef1e6987e158f6fc4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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);
}