about summary refs log tree commit diff
path: root/uname.c
blob: b5ac849587bd4676eb0ab951d415733368e7af46 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#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 {
  struct label labels[max_label + 1];
};

static void uname_set_labels(struct uname_context *ctx, struct utsname *name) {
  ctx->labels[label_machine].key = "machine";
  ctx->labels[label_machine].value = must_strdup(name->machine);
  ctx->labels[label_nodename].key = "nodename";
  ctx->labels[label_nodename].value = must_strdup(name->nodename);
  ctx->labels[label_release].key = "release";
  ctx->labels[label_release].value = must_strdup(name->release);
  ctx->labels[label_sysname].key = "sysname";
  ctx->labels[label_sysname].value = must_strdup(name->sysname);
  ctx->labels[label_version].key = "version";
  ctx->labels[label_version].value = must_strdup(name->version);
  ctx->labels[max_label].key = ctx->labels[max_label].value = 0;
}

static void *uname_init(int argc, char *argv[]) {
  (void) argc; (void) argv;

  struct utsname name;
  if (uname(&name) == -1) {
    perror("uname");
    return 0;
  }

  struct uname_context *ctx = must_malloc(sizeof *ctx);
  uname_set_labels(ctx, &name);
  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);
}

// exposed only for testing

void uname_test_override_data(void *ctx, struct utsname *name) {
  uname_set_labels((struct uname_context *) ctx, name);
}