about summary refs log tree commit diff
path: root/diskstats.c
blob: 1cabd95d850a45a1e2c702be13bb9fa0b3c14a3f (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * 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.
 */

#define _POSIX_C_SOURCE 200809L

#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

// assumed constant size of disk sector in /proc/diskstats
#define SECTOR_SIZE 512

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

const struct collector diskstats_collector = {
  .name = "diskstats",
  .collect = diskstats_collect,
  .init = diskstats_init,
  .has_args = true,
};

struct diskstats_context {
  struct slist *include;
  struct slist *exclude;
  bool filter_unused;
};

static void *diskstats_init(int argc, char *argv[]) {
  struct diskstats_context *ctx = must_malloc(sizeof *ctx);

  ctx->include = 0;
  ctx->exclude = 0;
  ctx->filter_unused = true;

  for (int arg = 0; arg < argc; arg++) {
    if (strncmp(argv[arg], "include=", 8) == 0) {
      ctx->include = slist_split(&argv[arg][8], ",");
    } else if (strncmp(argv[arg], "exclude=", 8) == 0) {
      ctx->exclude = slist_split(&argv[arg][8], ",");
    } else if (strcmp(argv[arg], "keep-unused") == 0) {
      ctx->filter_unused = false;
    } else {
      fprintf(stderr, "unknown argument for diskstats collector: %s", argv[arg]);
      return 0;
    }
  }

  return ctx;
}

static const struct {
  const char *metric;
  double factor;
} columns[] = {
  { .metric = "node_disk_reads_completed_total", .factor = 1.0 },
  { .metric = "node_disk_reads_merged_total", .factor = 1.0 },
  { .metric = "node_disk_read_bytes_total", .factor = SECTOR_SIZE },
  { .metric = "node_disk_read_time_seconds_total", .factor = 0.001 },
  { .metric = "node_disk_writes_completed_total", .factor = 1.0 },
  { .metric = "node_disk_writes_merged_total", .factor = 1.0 },
  { .metric = "node_disk_written_bytes_total", .factor = SECTOR_SIZE },
  { .metric = "node_disk_write_time_seconds_total", .factor = 0.001 },
  { .metric = "node_disk_io_now", .factor = 1.0 },
  { .metric = "node_disk_io_time_seconds_total", .factor = 0.001 },
  { .metric = "node_disk_io_time_weighted_seconds_total", .factor = 0.001 },
  { .metric = "node_disk_discards_completed_total", .factor = 1.0 },
  { .metric = "node_disk_discards_merged_total", .factor = 1.0 },
  { .metric = "node_disk_discarded_sectors_total", .factor = 1.0 },
  { .metric = "node_disk_discard_time_seconds_total", .factor = 0.001 },
};
#define NCOLUMNS (sizeof columns / sizeof *columns)

static void diskstats_collect(scrape_req *req, void *ctx_ptr) {
  struct diskstats_context *ctx = ctx_ptr;

  // buffers

  struct label labels[] = {
    { .key = "device", .value = 0 },  // value filled by code
    LABEL_END,
  };

  char buf[BUF_SIZE];

  FILE *f;

  // emit metrics for all known columns of /proc/diskstats for included devices

  f = fopen(PATH("/proc/diskstats"), "r");
  if (!f)
    return;

  while (fgets_line(buf, sizeof buf, f)) {
    char *p;

    // skip first two columns (device node numbers)

    strtok_r(buf, " ", &p);
    strtok_r(0, " ", &p);

    // extract device name

    char *dev = strtok_r(0, " ", &p);
    if (!dev || *dev == '\0')
      continue;
    labels[0].value = dev;

    // filter

    if (ctx->include) {
      if (!slist_matches(ctx->include, dev))
        continue;
    } else if (ctx->exclude) {
      if (slist_matches(ctx->exclude, dev))
        continue;
    }
    if (ctx->filter_unused) {
      char *tail = dev + strlen(dev) + 1;
      if (tail < buf + sizeof buf && strspn(tail, " 0\n") == strlen(tail))
        continue;  // only spaces, zeros and newlines: unused device
    }

    // emit metrics while known columns last

    for (size_t c = 0; c < NCOLUMNS; c++) {
      char *v = strtok_r(0, " \n", &p);
      if (!v || *v == '\0')
        break;

      char *end;
      double d = strtod(v, &end);
      if (*end != '\0')
        continue;

      scrape_write(req, columns[c].metric, labels, d * columns[c].factor);
    }
  }

  fclose(f);
}