about summary refs log tree commit diff
path: root/zfs.c
blob: 283c7a8d42d7fea8e21fb991bba40930b72a8967 (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
#define _GNU_SOURCE
#include <ctype.h>
#include <fcntl.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "scrape.h"
#include "util.h"

// size of input buffer for paths and lines
#define BUF_SIZE 256

static void zfs_collect(scrape_req *req, void *ctx);

const struct collector zfs_collector = {
  .name = "zfs",
  .collect = zfs_collect,
};

static void zfs_collect_state(scrape_req *req, char *path) {
  char buf[BUF_SIZE];
  char pool[BUF_SIZE] = { 0 };
  memcpy(pool, path + 20 /* "/proc/spl/kstat/zfs/" */, strlen(path) - 20 - 6 /* "/state" */);

  if (read_file_at(AT_FDCWD, path, buf, sizeof buf) < 0)
    return;

  for (char *s = buf; *s; s++)
    *s = tolower(*s);

  struct label zfs_label[] = {
    { .key = "device", .value = pool },
    { .key = "state", .value = buf },
    LABEL_END,
  };

  scrape_write(req, "node_zfs_zpool_state", zfs_label, 1.0);
}

static void zfs_collect(scrape_req *req, void *ctx) {
  (void) ctx;

  // scan /proc/spl/kstat/zfs/*/state for metrics

  glob_t globbuf = { 0 };
  if (glob("/proc/spl/kstat/zfs/*/state", GLOB_NOSORT | GLOB_ONLYDIR, 0, &globbuf) != 0)
    return;

  for (size_t i = 0; i < globbuf.gl_pathc; i++)
    zfs_collect_state(req, globbuf.gl_pathv[i]);

  globfree(&globbuf);
}