about summary refs log tree commit diff
path: root/uname.c
blob: 4fd4eac0cb41293ab5eb0c2ee9ea81a667f28bfe (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
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>

#include "collector.h"
#include "util.h"

static void *uname_init(int argc, char *argv[]);
static void uname_collect(scrape_req *req, void *ctx);

const struct collector uname_collector = {
  .name = "uname",
  .collect = uname_collect,
  .init = uname_init,
};

enum {
  label_machine,
  label_nodename,
  label_release,
  label_sysname,
  label_version,
  max_label,
};

struct uname_context {
  const char *labels[max_label + 1][2];
};

static void *uname_init(int argc, char *argv[]) {
  struct utsname name;
  if (uname(&name) == -1) {
    perror("uname");
    return 0;
  }

  struct uname_context *ctx = must_malloc(sizeof *ctx);
  ctx->labels[label_machine][0] = "machine";
  ctx->labels[label_machine][1] = must_strdup(name.machine);
  ctx->labels[label_nodename][0] = "nodename";
  ctx->labels[label_nodename][1] = must_strdup(name.nodename);
  ctx->labels[label_release][0] = "release";
  ctx->labels[label_release][1] = must_strdup(name.release);
  ctx->labels[label_sysname][0] = "sysname";
  ctx->labels[label_sysname][1] = must_strdup(name.sysname);
  ctx->labels[label_version][0] = "version";
  ctx->labels[label_version][1] = must_strdup(name.version);
  ctx->labels[max_label][0] = ctx->labels[max_label][1] = 0;
  return ctx;
}

static void uname_collect(scrape_req *req, void *ctx_ptr) {
  struct uname_context *ctx = ctx_ptr;
  scrape_write(req, "node_uname_info", ctx->labels, 1.0);
}