-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.c
406 lines (359 loc) · 9.35 KB
/
init.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License") 1.1!
* You may not use this file except in compliance with the License.
*
* See https://spdx.org/licenses/CDDL-1.1.html for the specific
* language governing permissions and limitations under the License.
*
* Copyright 2021 Jens Elkner (jel+ipmimex-src@cs.ovgu.de)
*/
#include <unistd.h>
#include <stdlib.h>
#include <prom.h>
#include "common.h"
#include "init.h"
#include "ipmi_if.h"
#include "ipmi_sdr.h"
#include "ipmi_sdr_convert.h"
#include "prom_ipmi.h"
static uint8_t started = 0;
#define WAIT4REPO_SLOT 10 // seconds
#define MAX_WAIT4REPO 300 // seconds
static char *versionProm = NULL; // version string emitted via /metrics
static char *versionHR = NULL; // version string emitted to stdout/stderr
static char *bmcVersion = NULL;
static bool bmc_version_done = false;
static int
cmp_sensor(const void *p1, const void *p2) {
const sensor_t *a = *(sensor_t * const *)p1;
const sensor_t *b = *(sensor_t * const *)p2;
int d = a->category - b->category;
if (d !=0)
return d;
d = strcmp(a->prom.unit, b->prom.unit);
if (d != 0)
return d;
return strcmp(a->prom.name, b->prom.name);
}
static sensor_t *
sort_sensors(sensor_t *list, size_t sz) {
size_t n;
int len;
char buf[64]; // metric name buffer - should be more than sufficient
if (list == NULL)
return NULL;
sensor_t *e, **sa = malloc(sizeof(sensor_t *) * sz);
if (sa == NULL) {
perror("sort sensors: ");
return NULL;
}
e = list;
n = 0;
while (e != NULL) {
sa[n] = e;
strcpy(buf, e->name);
len = strlen(buf);
if ((e->category == 1) && ((strcmp(buf + len - 5, " Temp") == 0)
|| (strcmp(buf + len - 5, "_TEMP") == 0)))
{
buf[len-5] = '\0';
}
// just enough to sort prom output like
e->prom.name = strdup(buf);
e->prom.unit = strdup(unit2prom(&(e->unit)));
e = e->next;
n++;
}
if (sz != n) {
PROM_FATAL("Software bug: sz != c (%ld != %ld)", sz, n);
free(sa);
return NULL;
}
qsort(sa, sz, sizeof(sensor_t *), cmp_sensor);
for (n = sz - 1; n > 0 ; n--) {
sa[n - 1]->next = sa[n];
}
sa[sz - 1]->next = NULL;
e = sa[0];
free(sa);
return e;
}
#define MMATCH(_x) (cfg->_x && (regexec(cfg->_x, buf, 0,NULL,0) == 0))
#define SMATCH(_x) (cfg->_x && (regexec(cfg->_x, e->prom.name, 0,NULL,0) == 0))
static sensor_t *
drop_unneeded(sensor_t *head, scan_cfg_t *cfg, uint32_t *sensors) {
if (head == NULL)
return NULL;
sensor_t *e = head, *first = NULL, *last = NULL, *tmp;
char buf[128]; // 8+1+32+1+20+5+20+20+16+1 = 124
char tbuf[4096]; // 6*(124 + 27 + 317) = 2808
int len, ulen;
uint8_t cc;
while (e != NULL) {
len = sprintf(buf, IPMIMEXM_IPMI_N "_%s_%s",
category2prom(e->category), e->prom.unit);
if ((MMATCH(exc_metrics) || SMATCH(exc_sensors))
&& !(MMATCH(inc_metrics) || SMATCH(inc_sensors)))
{
PROM_INFO("Dropping sensor '%s' (0x%02x): excluded via -x or -X.",
e->prom.name, e->sensor_num);
tmp = e->next;
if (last != NULL)
last->next = tmp;
e->next = NULL;
free_sensor(e);
e = tmp;
(*sensors)--;
continue;
}
if (first == NULL)
first = e;
last = e;
ulen = len - strlen(e->prom.unit);
sprintf(buf + len, "{sensor=\"%s\"}", e->prom.name);
e->prom.mname_reading = strdup(buf);
if (!cfg->no_state) {
sprintf(buf + ulen, "state{sensor=\"%s\"}", e->prom.name);
e->prom.mname_state = strdup(buf);
}
sdr_thresholds_t *t = cfg->no_thresholds
? NULL
: get_thresholds(e->sensor_num, &cc);
if (t != NULL && cc == 0) {
sprintf(buf + ulen, "threshold_%s{sensor=\"%s\",bounds=",
e->prom.unit, e->prom.name);
len = 0;
#define TADD(_b, _s) if (t->readable._b ## _ ## _s) { \
len += (SDR_UNIT_FMT_IS_DISCRETE(e->unit.analog_fmt)) \
? sprintf(tbuf + len, "%s\"" #_b "\",state=\"" #_s "\"} %d\n", buf, \
t->_b ## _ ## _s) \
: sprintf(tbuf + len, "%s\"" #_b "\",state=\"" #_s "\"} %g\n", buf, \
sdr_convert_value(t->_b ## _ ## _s, e->unit.analog_fmt, e->factors)); \
}
TADD(lower, nr);
TADD(lower, cr);
TADD(lower, nc);
TADD(upper, nc);
TADD(upper, cr);
TADD(upper, nr);
if (len > 0) {
e->prom.mname_threshold = strdup(tbuf);
e->it_thresholds =
thresholds2ipmitool_str(t, e->unit.analog_fmt, e->factors);
}
}
e = e->next;
}
return first;
}
static uint8_t
get_current_bmc_info(void) {
int max_tries;
uint8_t cc;
char buf[256];
max_tries = MAX_WAIT4REPO/WAIT4REPO_SLOT;
while (max_tries > 0) {
ipmi_bmc_info_t *bmc = get_bmc_info(&cc);
if (SDR_REPO_TMP_NA(cc)) {
PROM_INFO("BMC temporarily not available. Sleeping %d seconds ...",
WAIT4REPO_SLOT);
sleep(WAIT4REPO_SLOT);
max_tries--;
continue;
}
if (bmc == NULL) {
PROM_WARN("\n Could not obtain BMC info!!!\n"
" If it is not IPMI v1.0, v1.5 or v2.0 compatible, shown results\n"
" (if any) might be total non-sense.\n", "");
return 1;
} else if (!bmc->supports_sensor) {
PROM_ERROR("BMC does not support SDR sensor device commands.", "");
return 2;
}
sprintf(buf, IPMIMEXM_VERS_N "{name=\"bmc\",value=\"%d.%d\"} 1\n",
bmc->fw_rev_major, bmc->fw_rev_minor);
if (bmcVersion != NULL)
free(bmcVersion);
bmcVersion = strdup(buf);
break;
}
return max_tries == 0 ? 3 : 0;
}
sensor_t *
get_sensor_list(scan_cfg_t *cfg, uint32_t *sensors) {
int max_tries;
uint8_t cc;
if (cfg->no_ipmi)
return NULL;
sensor_t *slist = NULL, *tlist;
max_tries = MAX_WAIT4REPO/WAIT4REPO_SLOT;
while (max_tries > 0) {
*sensors = 0;
slist = scan_sdr_repo(sensors, cfg->ignore_disabled_flag,
cfg->drop_no_read, &cc);
if (SDR_REPO_TMP_NA(cc)) {
free_sensor(slist);
slist = NULL;
PROM_INFO("BMC temporarily not available. Sleeping %d seconds ...",
WAIT4REPO_SLOT);
sleep(WAIT4REPO_SLOT);
max_tries--;
continue;
}
if (cc == 0) {
PROM_INFO("%d potential sensors found.", *sensors);
break;
} else {
PROM_FATAL("Scanning the SDR repository failed. No sensors.", "");
goto error;
}
}
if (max_tries == 0) {
*sensors = 0;
return NULL;
}
tlist = sort_sensors(slist, *sensors);
if (tlist != NULL) {
slist = tlist;
tlist = drop_unneeded(slist, cfg, sensors);
if (tlist != NULL)
return tlist;
PROM_WARN("No sensors to monitor.", "");
}
error:
free_sensor(slist);
slist = NULL;
*sensors = 0;
return NULL;
}
/**
* This function expects a sensor list sorted by category, prom.unit and
* prom.name and a prom.mname_reading to be populated for all list members.
* If not run through drop_unneeded() or any field is NULL expect coredumps.
*/
static void
gen_help(sensor_t *list) {
if (list == NULL)
return;
sensor_t *s = list, *last = NULL;
char buf[256];
size_t len;
const char *u;
char *t;
while (s != NULL) {
if (last == NULL
|| strncmp(last->prom.mname_reading,s->prom.mname_reading,len) != 0)
{
u = (s->it_unit) ? s->it_unit : sdr_unit2str(&(s->unit));
t = strchr(s->prom.mname_reading, '{');
*t = '\0';
sprintf(buf, "\n# HELP %s IPMI %s sensor in %s\n# TYPE %s %s\n",
s->prom.mname_reading, sdr_category2str(s->category), u,
s->prom.mname_reading, IPMIMEXM_IPMI_T);
*t = '{';
s->prom.note = strdup(buf);
len = t - s->prom.mname_reading + 1;
last = s;
}
s = s->next;
}
}
sensor_t *
start(scan_cfg_t *cfg, bool compact, uint32_t *sensors) {
uint8_t cc;
*sensors = 0;
if (started)
return NULL;
if (cfg->no_ipmi && cfg->no_dcmi)
return NULL;
PROM_INFO("Checking BMC (%s) ...",
cfg->bmc == NULL ? "default path" : cfg->bmc);
if (ipmi_if_open(cfg->bmc) != 0)
return NULL;
cc = get_current_bmc_info();
if (cc == 2) {
cfg->no_ipmi = true;
} else if (cc == 3) {
ipmi_if_close();
return NULL;
}
sensor_t *slist = get_sensor_list(cfg, sensors);
if (*sensors == 0)
cfg->no_ipmi = true;
else if (!compact)
gen_help(slist);
if (!cfg->no_dcmi) {
get_power(&cc);
if (cc == SDR_CC_INVALID_CMD)
cfg->no_dcmi = true;
}
if (cfg->no_ipmi && cfg->no_dcmi) {
ipmi_if_close();
return NULL;
}
//show_ipmitool_sensors(slist, NULL, true);
if (!cfg->no_dcmi)
(*sensors)++;
PROM_INFO("IPMI stack initialized. All sensors to monitor: %d", *sensors);
started = 1;
return slist;
}
void
stop(sensor_t *list) {
ipmi_if_close();
free_sensor(list);
list = NULL;
free(versionHR);
versionHR = NULL;
free(versionProm);
versionProm = NULL;
free(bmcVersion);
bmcVersion = NULL;
bmc_version_done = false;
PROM_DEBUG("IPMI stack has been properly shutdown", "");
started = 0;
}
char *
getVersions(psb_t *sbp, bool compact) {
psb_t *sbi = NULL, *sb = NULL;
if (versionProm != NULL) {
if (!bmc_version_done && bmcVersion != NULL) {
size_t n = strlen(versionProm), m = strlen(bmcVersion);
char *t, *s = malloc(sizeof(char) * (n + m) + 1);
if (s != NULL) {
strcpy(s, versionProm);
strcpy(s + n, bmcVersion);
t = versionProm;
versionProm = s;
free(t);
bmc_version_done = true;
}
}
goto end;
}
sbi = psb_new();
sb = psb_new();
if (sbi == NULL || sb == NULL) {
psb_destroy(sbi);
psb_destroy(sb);
return NULL;
}
psb_add_str(sbi, "ipmimex " IPMIMEX_VERSION "\n(C) 2021 "
IPMIMEX_AUTHOR "\n");
versionHR = psb_dump(sbi);
psb_destroy(sbi);
if (!compact)
addPromInfo(IPMIMEXM_VERS);
psb_add_str(sb, IPMIMEXM_VERS_N "{name=\"server\",value=\"" IPMIMEX_VERSION
"\"} 1\n");
versionProm = psb_dump(sb);
psb_destroy(sb);
end:
if (sbp == NULL) {
fprintf(stdout, "%s", versionHR);
} else {
psb_add_str(sbp, versionProm);
}
return versionHR;
}