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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#define _POSIX_C_SOURCE 200809L
#include <dirent.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "hwmon.h"
// size of input buffer for paths and lines
#define BUF_SIZE 256
// size of input buffer for labels
#define LABEL_SIZE 32
static void hwmon_collect(scrape_req *req, void *ctx);
const struct collector hwmon_collector = {
.name = "hwmon",
.collect = hwmon_collect,
};
static double hwmon_conv_millis(const char *text) {
char *endptr;
double value = strtod(text, &endptr);
if (*endptr == '\n' || *endptr == '\0')
return value / 1000;
return NAN;
}
static double hwmon_conv_id(const char *text) {
char *endptr;
double value = strtod(text, &endptr);
if (*endptr == '\n' || *endptr == '\0')
return value;
return NAN;
}
static double hwmon_conv_flag(const char *text) {
if ((text[0] == '0' || text[0] == '1') && (text[1] == '\n' || text[1] == '\0'))
return text[0] - '0';
return NAN;
}
struct metric_type {
const char *suffix;
const char *metric;
double (*conv)(const char *text);
};
struct metric_data {
const char *prefix;
const struct metric_type *types;
};
static const struct metric_data metrics[] = {
{
.prefix = "in",
.types = (const struct metric_type[]){
{
.suffix = "_input",
.metric = "node_hwmon_in_volts",
.conv = hwmon_conv_millis,
},
{
.suffix = "_min",
.metric = "node_hwmon_in_min_volts",
.conv = hwmon_conv_millis,
},
{
.suffix = "_max",
.metric = "node_hwmon_in_max_volts",
.conv = hwmon_conv_millis,
},
{
.suffix = "_alarm",
.metric = "node_hwmon_in_alarm",
.conv = hwmon_conv_flag,
},
{ .suffix = 0 },
},
},
{
.prefix = "fan",
.types = (const struct metric_type[]){
{
.suffix = "_input",
.metric = "node_hwmon_fan_rpm",
.conv = hwmon_conv_id,
},
{
.suffix = "_min",
.metric = "node_hwmon_fan_min_rpm",
.conv = hwmon_conv_id,
},
{
.suffix = "_alarm",
.metric = "node_hwmon_fan_alarm",
.conv = hwmon_conv_flag,
},
{ .suffix = 0 },
},
},
{
.prefix = "temp",
.types = (const struct metric_type[]){
{
.suffix = "_input",
.metric = "node_hwmon_temp_celsius",
.conv = hwmon_conv_millis,
},
{ .suffix = 0 },
},
},
{ .prefix = 0 },
};
static void hwmon_name(const char *path, char *dst, size_t dst_len) {
char buf[BUF_SIZE];
FILE *f;
size_t len;
// if the path is a symlink to "../../devices/X/Y/...", use X/Y as the name,
// except if it is "virtual/hwmon"
len = readlink(path, buf, sizeof buf);
if (len > 14 && memcmp(buf, "../../devices/", 14) == 0) {
char *start = buf + 14;
char *end = strchr(start, '/');
if (end)
end = strchr(end + 1, '/');
if (end)
*end = '\0';
if (strcmp(start, "virtual/hwmon") != 0) {
snprintf(dst, dst_len, "%s", start);
return;
}
}
// try to use the 'name' file
snprintf(buf, sizeof buf, "%s/name", path);
f = fopen(buf, "r");
if (f) {
bool found = false;
if (fgets(buf, sizeof buf, f)) {
len = strlen(buf);
if (len > 0 && buf[len-1] == '\n') {
len--;
buf[len] = '\0';
}
if (len > 0) {
snprintf(dst, dst_len, "hwmon/%s", buf);
found = true;
}
}
fclose(f);
if (found)
return;
}
// give up, just call this unknown
// TODO: add an index
snprintf(dst, dst_len, "unknown");
}
static void hwmon_collect(scrape_req *req, void *ctx) {
// buffers
char chip_label[LABEL_SIZE];
char sensor_label[LABEL_SIZE];
const char *labels[][2] = {
{ "chip", chip_label },
{ "sensor", sensor_label },
{ 0, 0 },
};
char path[BUF_SIZE];
char buf[BUF_SIZE];
FILE *f;
DIR *root;
struct dirent *dent;
// iterate over all hwmon instances in /sys/class/hwmon
root = opendir("/sys/class/hwmon");
if (!root)
return;
while ((dent = readdir(root))) {
if (strncmp(dent->d_name, "hwmon", 5) != 0)
continue;
snprintf(path, sizeof path, "/sys/class/hwmon/%s", dent->d_name);
hwmon_name(path, chip_label, sizeof chip_label);
DIR *dir = opendir(path);
if (!dir)
continue;
while ((dent = readdir(dir))) {
for (const struct metric_data *metric = metrics; metric->prefix; metric++) {
if (strncmp(dent->d_name, metric->prefix, strlen(metric->prefix)) != 0)
continue;
char *suffix = strchr(dent->d_name, '_');
if (!suffix)
continue;
snprintf(sensor_label, sizeof sensor_label, "%.*s", (int)(suffix - dent->d_name), dent->d_name);
for (const struct metric_type *type = metric->types; type->suffix; type++) {
if (strcmp(suffix, type->suffix) != 0)
continue;
snprintf(buf, sizeof buf, "%s/%s", path, dent->d_name);
f = fopen(buf, "r");
if (!f)
continue;
if (fgets(buf, sizeof buf, f)) {
double value = type->conv(buf);
if (!isnan(value))
scrape_write(req, type->metric, labels, value);
}
fclose(f);
}
}
}
closedir(dir);
}
closedir(root);
}
|