1 /**
2 * collectd - src/ipmi.c
3 * Copyright (C) 2008-2009 Florian octo Forster
4 * Copyright (C) 2008 Peter Holik
5 * Copyright (C) 2009 Bruno Prémont
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at collectd.org>
22 * Peter Holik <peter at holik.at>
23 * Bruno Prémont <bonbons at linux-vserver.org>
24 * Pavel Rochnyak <pavel2000 ngs.ru>
25 **/
26
27 #include "collectd.h"
28
29 #include "plugin.h"
30 #include "utils/common/common.h"
31 #include "utils/ignorelist/ignorelist.h"
32
33 #include <OpenIPMI/ipmi_auth.h>
34 #include <OpenIPMI/ipmi_conn.h>
35 #include <OpenIPMI/ipmi_err.h>
36 #include <OpenIPMI/ipmi_lan.h>
37 #include <OpenIPMI/ipmi_posix.h>
38 #include <OpenIPMI/ipmi_smi.h>
39 #include <OpenIPMI/ipmiif.h>
40
41 #define ERR_BUF_SIZE 1024
42
43 /*
44 * Private data types
45 */
46 struct c_ipmi_sensor_list_s;
47 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
48
49 struct c_ipmi_instance_s {
50 char *name;
51 ignorelist_t *ignorelist;
52 ignorelist_t *sel_ignorelist;
53 bool notify_add;
54 bool notify_remove;
55 bool notify_notpresent;
56 bool notify_conn;
57 bool sel_enabled;
58 bool sel_clear_event;
59
60 char *host;
61 char *connaddr;
62 char *username;
63 char *password;
64 unsigned int authtype;
65
66 bool connected;
67 ipmi_con_t *connection;
68 pthread_mutex_t sensor_list_lock;
69 c_ipmi_sensor_list_t *sensor_list;
70
71 bool active;
72 pthread_t thread_id;
73 int init_in_progress;
74
75 struct c_ipmi_instance_s *next;
76 };
77 typedef struct c_ipmi_instance_s c_ipmi_instance_t;
78
79 struct c_ipmi_sensor_list_s {
80 ipmi_sensor_id_t sensor_id;
81 char sensor_name[DATA_MAX_NAME_LEN];
82 char sensor_type[DATA_MAX_NAME_LEN];
83 char type_instance[DATA_MAX_NAME_LEN];
84 int sensor_not_present;
85 c_ipmi_sensor_list_t *next;
86 c_ipmi_instance_t *instance;
87 unsigned int use;
88 };
89
90 struct c_ipmi_db_type_map_s {
91 enum ipmi_unit_type_e type;
92 const char *type_name;
93 };
94 typedef struct c_ipmi_db_type_map_s c_ipmi_db_type_map_t;
95
96 /*
97 * Module global variables
98 */
99 static os_handler_t *os_handler;
100 static c_ipmi_instance_t *instances;
101
102 /*
103 * Misc private functions
104 */
c_ipmi_error(c_ipmi_instance_t * st,const char * func,int status)105 static void c_ipmi_error(c_ipmi_instance_t *st, const char *func, int status) {
106 char errbuf[ERR_BUF_SIZE] = {0};
107
108 if (IPMI_IS_OS_ERR(status) || IPMI_IS_RMCPP_ERR(status) ||
109 IPMI_IS_IPMI_ERR(status)) {
110 ipmi_get_error_string(status, errbuf, sizeof(errbuf));
111 }
112
113 if (errbuf[0] == 0) {
114 ssnprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
115 }
116 errbuf[sizeof(errbuf) - 1] = '\0';
117
118 ERROR("ipmi plugin: %s failed for `%s`: %s", func, st->name, errbuf);
119 } /* void c_ipmi_error */
120
c_ipmi_log(os_handler_t * handler,const char * format,enum ipmi_log_type_e log_type,va_list ap)121 static void c_ipmi_log(os_handler_t *handler, const char *format,
122 enum ipmi_log_type_e log_type, va_list ap) {
123 char msg[ERR_BUF_SIZE];
124
125 vsnprintf(msg, sizeof(msg), format, ap);
126
127 switch (log_type) {
128 case IPMI_LOG_INFO:
129 INFO("ipmi plugin: %s", msg);
130 break;
131 case IPMI_LOG_WARNING:
132 NOTICE("ipmi plugin: %s", msg);
133 break;
134 case IPMI_LOG_SEVERE:
135 WARNING("ipmi plugin: %s", msg);
136 break;
137 case IPMI_LOG_FATAL:
138 ERROR("ipmi plugin: %s", msg);
139 break;
140 case IPMI_LOG_ERR_INFO:
141 ERROR("ipmi plugin: %s", msg);
142 break;
143 #if COLLECT_DEBUG
144 case IPMI_LOG_DEBUG_START:
145 case IPMI_LOG_DEBUG:
146 DEBUG("ipmi plugin: %s", msg);
147 break;
148 case IPMI_LOG_DEBUG_CONT:
149 case IPMI_LOG_DEBUG_END:
150 DEBUG("%s", msg);
151 break;
152 #else
153 case IPMI_LOG_DEBUG_START:
154 case IPMI_LOG_DEBUG:
155 case IPMI_LOG_DEBUG_CONT:
156 case IPMI_LOG_DEBUG_END:
157 break;
158 #endif
159 }
160 } /* void c_ipmi_log */
161
c_ipmi_notification_init(c_ipmi_instance_t const * st,int severity)162 static notification_t c_ipmi_notification_init(c_ipmi_instance_t const *st,
163 int severity) {
164 notification_t n = {severity, cdtime(), "", "", "ipmi", "", "", "", NULL};
165
166 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
167 return n;
168 } /* notification_t c_ipmi_notification_init */
169
170 /*
171 * Sensor handlers
172 */
173 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
174 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor);
175
sensor_read_handler(ipmi_sensor_t * sensor,int err,enum ipmi_value_present_e value_present,unsigned int raw_value,double value,ipmi_states_t * states,void * user_data)176 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
177 enum ipmi_value_present_e value_present,
178 unsigned int __attribute__((unused)) raw_value,
179 double value, ipmi_states_t *states,
180 void *user_data) {
181 value_list_t vl = VALUE_LIST_INIT;
182
183 c_ipmi_sensor_list_t *list_item = user_data;
184 c_ipmi_instance_t *st = list_item->instance;
185
186 list_item->use--;
187
188 if (err != 0) {
189 if (IPMI_IS_IPMI_ERR(err) &&
190 IPMI_GET_IPMI_ERR(err) == IPMI_NOT_PRESENT_CC) {
191 if (list_item->sensor_not_present == 0) {
192 list_item->sensor_not_present = 1;
193
194 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` "
195 "not present.",
196 list_item->sensor_name, st->name);
197
198 if (st->notify_notpresent) {
199 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
200
201 sstrncpy(n.type_instance, list_item->type_instance,
202 sizeof(n.type_instance));
203 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
204 ssnprintf(n.message, sizeof(n.message), "sensor %s not present",
205 list_item->sensor_name);
206
207 plugin_dispatch_notification(&n);
208 }
209 }
210 } else if (IPMI_IS_IPMI_ERR(err) &&
211 IPMI_GET_IPMI_ERR(err) ==
212 IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
213 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` not ready.",
214 list_item->sensor_name, st->name);
215 } else if (IPMI_IS_IPMI_ERR(err) &&
216 IPMI_GET_IPMI_ERR(err) == IPMI_TIMEOUT_CC) {
217 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` timed out.",
218 list_item->sensor_name, st->name);
219 } else {
220 char errbuf[ERR_BUF_SIZE] = {0};
221 ipmi_get_error_string(err, errbuf, sizeof(errbuf) - 1);
222
223 if (IPMI_IS_IPMI_ERR(err))
224 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
225 "%s.",
226 list_item->sensor_name, st->name, errbuf);
227 else if (IPMI_IS_OS_ERR(err))
228 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
229 "%s (%#x).",
230 list_item->sensor_name, st->name, errbuf, IPMI_GET_OS_ERR(err));
231 else if (IPMI_IS_RMCPP_ERR(err))
232 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
233 "%s.",
234 list_item->sensor_name, st->name, errbuf);
235 else if (IPMI_IS_SOL_ERR(err))
236 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
237 "%s (%#x).",
238 list_item->sensor_name, st->name, errbuf, IPMI_GET_SOL_ERR(err));
239 else
240 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed "
241 "with error %#x. of class %#x",
242 list_item->sensor_name, st->name, err & 0xff, err & 0xffffff00);
243 }
244 return;
245 } else if (list_item->sensor_not_present == 1) {
246 list_item->sensor_not_present = 0;
247
248 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` present.",
249 list_item->sensor_name, st->name);
250
251 if (st->notify_notpresent) {
252 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
253
254 sstrncpy(n.type_instance, list_item->type_instance,
255 sizeof(n.type_instance));
256 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
257 ssnprintf(n.message, sizeof(n.message), "sensor %s present",
258 list_item->sensor_name);
259
260 plugin_dispatch_notification(&n);
261 }
262 }
263
264 if (value_present != IPMI_BOTH_VALUES_PRESENT) {
265 INFO("ipmi plugin: sensor_read_handler: Removing sensor `%s` of `%s`, "
266 "because it provides %s. If you need this sensor, "
267 "please file a bug report.",
268 list_item->sensor_name, st->name,
269 (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
270 : "no value");
271 sensor_list_remove(st, sensor);
272 return;
273 }
274
275 if (!ipmi_is_sensor_scanning_enabled(states)) {
276 DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
277 "it is in 'scanning disabled' state.",
278 list_item->sensor_name, st->name);
279 return;
280 }
281
282 if (ipmi_is_initial_update_in_progress(states)) {
283 DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
284 "it is in 'initial update in progress' state.",
285 list_item->sensor_name, st->name);
286 return;
287 }
288
289 vl.values = &(value_t){.gauge = value};
290 vl.values_len = 1;
291
292 if (st->host != NULL)
293 sstrncpy(vl.host, st->host, sizeof(vl.host));
294 sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
295 sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
296 sstrncpy(vl.type_instance, list_item->type_instance,
297 sizeof(vl.type_instance));
298
299 plugin_dispatch_values(&vl);
300 } /* void sensor_read_handler */
301
sensor_get_name(ipmi_sensor_t * sensor,char * buffer,int buf_len)302 static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) {
303 char temp[DATA_MAX_NAME_LEN] = {0};
304 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
305 const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
306 char sensor_name[DATA_MAX_NAME_LEN] = "";
307 char *sensor_name_ptr;
308
309 if ((buffer == NULL) || (buf_len == 0))
310 return;
311
312 ipmi_sensor_get_name(sensor, temp, sizeof(temp));
313 temp[sizeof(temp) - 1] = '\0';
314
315 if (entity_id_string != NULL && strlen(temp))
316 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", temp,
317 entity_id_string);
318 else if (entity_id_string != NULL)
319 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
320 else
321 sstrncpy(sensor_name, temp, sizeof(sensor_name));
322
323 if (strlen(temp)) {
324 sstrncpy(temp, sensor_name, sizeof(temp));
325 sensor_name_ptr = strstr(temp, ").");
326 if (sensor_name_ptr != NULL) {
327 /* If name is something like "foo (123).bar",
328 * change that to "bar (123)".
329 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
330 * `temp' array, which holds a copy of the current `sensor_name'. */
331 char *sensor_id_ptr;
332
333 /* `sensor_name_ptr' points to ").bar". */
334 sensor_name_ptr[1] = 0;
335 /* `temp' holds "foo (123)\0bar\0". */
336 sensor_name_ptr += 2;
337 /* `sensor_name_ptr' now points to "bar". */
338
339 sensor_id_ptr = strstr(temp, "(");
340 if (sensor_id_ptr != NULL) {
341 /* `sensor_id_ptr' now points to "(123)". */
342 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
343 sensor_id_ptr);
344 }
345 /* else: don't touch sensor_name. */
346 }
347 }
348 sstrncpy(buffer, sensor_name, buf_len);
349 }
350
sensor_unit_to_type(ipmi_sensor_t * sensor)351 static const char *sensor_unit_to_type(ipmi_sensor_t *sensor) {
352 static const c_ipmi_db_type_map_t ipmi_db_type_map[] = {
353 {IPMI_UNIT_TYPE_WATTS, "power"}, {IPMI_UNIT_TYPE_CFM, "flow"}};
354
355 /* check the modifier and rate of the sensor value */
356 if ((ipmi_sensor_get_modifier_unit_use(sensor) != IPMI_MODIFIER_UNIT_NONE) ||
357 (ipmi_sensor_get_rate_unit(sensor) != IPMI_RATE_UNIT_NONE))
358 return NULL;
359
360 /* find the db type by using sensor base unit type */
361 enum ipmi_unit_type_e ipmi_type = ipmi_sensor_get_base_unit(sensor);
362 for (size_t i = 0; i < STATIC_ARRAY_SIZE(ipmi_db_type_map); i++)
363 if (ipmi_db_type_map[i].type == ipmi_type)
364 return ipmi_db_type_map[i].type_name;
365
366 return NULL;
367 } /* const char* sensor_unit_to_type */
368
sensor_list_add(c_ipmi_instance_t * st,ipmi_sensor_t * sensor)369 static int sensor_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
370 ipmi_sensor_id_t sensor_id;
371 c_ipmi_sensor_list_t *list_item;
372 c_ipmi_sensor_list_t *list_prev;
373
374 char buffer[DATA_MAX_NAME_LEN] = {0};
375 char *sensor_name_ptr = buffer;
376 int sensor_type;
377 const char *type;
378
379 sensor_id = ipmi_sensor_convert_to_id(sensor);
380 sensor_get_name(sensor, buffer, sizeof(buffer));
381
382 DEBUG("ipmi plugin: sensor_list_add: Found sensor `%s` of `%s`,"
383 " Type: %#x"
384 " Event reading type: %#x"
385 " Direction: %#x"
386 " Event support: %#x",
387 sensor_name_ptr, st->name, ipmi_sensor_get_sensor_type(sensor),
388 ipmi_sensor_get_event_reading_type(sensor),
389 ipmi_sensor_get_sensor_direction(sensor),
390 ipmi_sensor_get_event_support(sensor));
391
392 /* Both `ignorelist' and `sensor_name_ptr' may be NULL. */
393 if (ignorelist_match(st->ignorelist, sensor_name_ptr) != 0)
394 return 0;
395
396 /* FIXME: Use rate unit or base unit to scale the value */
397
398 sensor_type = ipmi_sensor_get_sensor_type(sensor);
399
400 /*
401 * ipmitool/lib/ipmi_sdr.c sdr_sensor_has_analog_reading() has a notice
402 * about 'Threshold sensors' and 'analog readings'. Discrete sensor may
403 * have analog data, but discrete sensors support is not implemented
404 * in Collectd yet.
405 *
406 * ipmi_sensor_id_get_reading() supports only 'Threshold' sensors.
407 * See lib/sensor.c:4842, stand_ipmi_sensor_get_reading() for details.
408 */
409 if (!ipmi_sensor_get_is_readable(sensor)) {
410 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
411 "because it isn't readable! Its type: (%#x, %s). ",
412 sensor_name_ptr, st->name, sensor_type,
413 ipmi_sensor_get_sensor_type_string(sensor));
414 return -1;
415 }
416
417 if (ipmi_sensor_get_event_reading_type(sensor) !=
418 IPMI_EVENT_READING_TYPE_THRESHOLD) {
419 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
420 "because it is discrete (%#x)! Its type: (%#x, %s). ",
421 sensor_name_ptr, st->name, sensor_type,
422 ipmi_sensor_get_event_reading_type(sensor),
423 ipmi_sensor_get_sensor_type_string(sensor));
424 return -1;
425 }
426
427 switch (sensor_type) {
428 case IPMI_SENSOR_TYPE_TEMPERATURE:
429 type = "temperature";
430 break;
431
432 case IPMI_SENSOR_TYPE_VOLTAGE:
433 type = "voltage";
434 break;
435
436 case IPMI_SENSOR_TYPE_CURRENT:
437 type = "current";
438 break;
439
440 case IPMI_SENSOR_TYPE_FAN:
441 type = "fanspeed";
442 break;
443
444 case IPMI_SENSOR_TYPE_MEMORY:
445 type = "memory";
446 break;
447
448 default: {
449 /* try to get collectd DB type based on sensor base unit type */
450 if ((type = sensor_unit_to_type(sensor)) != NULL)
451 break;
452
453 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
454 "because I don't know how to handle its units (%#x, %#x, %#x). "
455 "Sensor type: (%#x, %s). If you need this sensor, please file "
456 "a bug report at http://collectd.org/.",
457 sensor_name_ptr, st->name, ipmi_sensor_get_base_unit(sensor),
458 ipmi_sensor_get_modifier_unit(sensor),
459 ipmi_sensor_get_rate_unit(sensor), sensor_type,
460 ipmi_sensor_get_sensor_type_string(sensor));
461 return -1;
462 }
463 } /* switch (sensor_type) */
464
465 pthread_mutex_lock(&st->sensor_list_lock);
466
467 list_prev = NULL;
468 for (list_item = st->sensor_list; list_item != NULL;
469 list_item = list_item->next) {
470 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
471 break;
472 list_prev = list_item;
473 } /* for (list_item) */
474
475 if (list_item != NULL) {
476 pthread_mutex_unlock(&st->sensor_list_lock);
477 return 0;
478 }
479
480 list_item = calloc(1, sizeof(*list_item));
481 if (list_item == NULL) {
482 pthread_mutex_unlock(&st->sensor_list_lock);
483 return -1;
484 }
485
486 list_item->instance = st;
487 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
488
489 if (list_prev != NULL)
490 list_prev->next = list_item;
491 else
492 st->sensor_list = list_item;
493
494 /* if sensor provides the percentage value, use "percent" collectd type
495 and add the `percent` to the type instance of the reported value */
496 if (ipmi_sensor_get_percentage(sensor)) {
497 ssnprintf(list_item->type_instance, sizeof(list_item->type_instance),
498 "percent-%s", sensor_name_ptr);
499 type = "percent";
500 } else {
501 /* use type instance as a name of the sensor */
502 sstrncpy(list_item->type_instance, sensor_name_ptr,
503 sizeof(list_item->type_instance));
504 }
505
506 sstrncpy(list_item->sensor_name, sensor_name_ptr,
507 sizeof(list_item->sensor_name));
508 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
509
510 pthread_mutex_unlock(&st->sensor_list_lock);
511
512 if (st->notify_add && (st->init_in_progress == 0)) {
513 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
514
515 sstrncpy(n.type_instance, list_item->type_instance,
516 sizeof(n.type_instance));
517 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
518 ssnprintf(n.message, sizeof(n.message), "sensor %s added",
519 list_item->sensor_name);
520
521 plugin_dispatch_notification(&n);
522 }
523
524 return 0;
525 } /* int sensor_list_add */
526
sensor_list_remove(c_ipmi_instance_t * st,ipmi_sensor_t * sensor)527 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
528 ipmi_sensor_id_t sensor_id;
529 c_ipmi_sensor_list_t *list_item;
530 c_ipmi_sensor_list_t *list_prev;
531
532 sensor_id = ipmi_sensor_convert_to_id(sensor);
533
534 pthread_mutex_lock(&st->sensor_list_lock);
535
536 list_prev = NULL;
537 for (list_item = st->sensor_list; list_item != NULL;
538 list_item = list_item->next) {
539 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
540 break;
541 list_prev = list_item;
542 } /* for (list_item) */
543
544 if (list_item == NULL) {
545 pthread_mutex_unlock(&st->sensor_list_lock);
546 return -1;
547 }
548
549 if (list_prev == NULL)
550 st->sensor_list = list_item->next;
551 else
552 list_prev->next = list_item->next;
553
554 list_prev = NULL;
555 list_item->next = NULL;
556
557 pthread_mutex_unlock(&st->sensor_list_lock);
558
559 if (st->notify_remove && st->active) {
560 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
561
562 sstrncpy(n.type_instance, list_item->type_instance,
563 sizeof(n.type_instance));
564 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
565 ssnprintf(n.message, sizeof(n.message), "sensor %s removed",
566 list_item->sensor_name);
567
568 plugin_dispatch_notification(&n);
569 }
570
571 free(list_item);
572 return 0;
573 } /* int sensor_list_remove */
574
sensor_list_read_all(c_ipmi_instance_t * st)575 static int sensor_list_read_all(c_ipmi_instance_t *st) {
576 pthread_mutex_lock(&st->sensor_list_lock);
577
578 for (c_ipmi_sensor_list_t *list_item = st->sensor_list; list_item != NULL;
579 list_item = list_item->next) {
580 DEBUG("ipmi plugin: try read sensor `%s` of `%s`, use: %d",
581 list_item->sensor_name, st->name, list_item->use);
582
583 /* Reading already initiated */
584 if (list_item->use)
585 continue;
586
587 list_item->use++;
588 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
589 /* user data = */ (void *)list_item);
590 } /* for (list_item) */
591
592 pthread_mutex_unlock(&st->sensor_list_lock);
593
594 return 0;
595 } /* int sensor_list_read_all */
596
sensor_list_remove_all(c_ipmi_instance_t * st)597 static int sensor_list_remove_all(c_ipmi_instance_t *st) {
598 c_ipmi_sensor_list_t *list_item;
599
600 pthread_mutex_lock(&st->sensor_list_lock);
601
602 list_item = st->sensor_list;
603 st->sensor_list = NULL;
604
605 pthread_mutex_unlock(&st->sensor_list_lock);
606
607 while (list_item != NULL) {
608 c_ipmi_sensor_list_t *list_next = list_item->next;
609
610 free(list_item);
611
612 list_item = list_next;
613 } /* while (list_item) */
614
615 return 0;
616 } /* int sensor_list_remove_all */
617
sensor_convert_threshold_severity(enum ipmi_thresh_e severity)618 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
619 switch (severity) {
620 case IPMI_LOWER_NON_CRITICAL:
621 case IPMI_UPPER_NON_CRITICAL:
622 return NOTIF_OKAY;
623 case IPMI_LOWER_CRITICAL:
624 case IPMI_UPPER_CRITICAL:
625 return NOTIF_WARNING;
626 case IPMI_LOWER_NON_RECOVERABLE:
627 case IPMI_UPPER_NON_RECOVERABLE:
628 return NOTIF_FAILURE;
629 default:
630 return NOTIF_OKAY;
631 } /* switch (severity) */
632 } /* int sensor_convert_threshold_severity */
633
add_event_common_data(notification_t * n,ipmi_sensor_t * sensor,enum ipmi_event_dir_e dir,ipmi_event_t * event)634 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
635 enum ipmi_event_dir_e dir,
636 ipmi_event_t *event) {
637 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
638
639 plugin_notification_meta_add_string(n, "entity_name",
640 ipmi_entity_get_entity_id_string(ent));
641 plugin_notification_meta_add_signed_int(n, "entity_id",
642 ipmi_entity_get_entity_id(ent));
643 plugin_notification_meta_add_signed_int(n, "entity_instance",
644 ipmi_entity_get_entity_instance(ent));
645 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
646
647 if (event)
648 plugin_notification_meta_add_signed_int(n, "event_type",
649 ipmi_event_get_type(event));
650 } /* void add_event_sensor_meta_data */
651
sensor_threshold_event_handler(ipmi_sensor_t * sensor,enum ipmi_event_dir_e dir,enum ipmi_thresh_e threshold,enum ipmi_event_value_dir_e high_low,enum ipmi_value_present_e value_present,unsigned int raw_value,double value,void * cb_data,ipmi_event_t * event)652 static int sensor_threshold_event_handler(
653 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
654 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
655 enum ipmi_value_present_e value_present, unsigned int raw_value,
656 double value, void *cb_data, ipmi_event_t *event) {
657
658 c_ipmi_instance_t *st = cb_data;
659
660 /* From the IPMI specification Chapter 2: Events.
661 * If a callback handles the event, then all future callbacks called due to
662 * the event will receive a NULL for the event. So be ready to handle a NULL
663 * event in all your event handlers. A NULL may also be passed to an event
664 * handler if the callback was not due to an event. */
665 if (event == NULL)
666 return IPMI_EVENT_NOT_HANDLED;
667
668 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
669 /* offset is a table index and it's represented as enum of strings that are
670 organized in the way - high and low for each threshold severity level */
671 unsigned int offset = (2 * threshold) + high_low;
672 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
673 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
674 const char *event_state =
675 ipmi_get_reading_name(event_type, sensor_type, offset);
676 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
677 if (value_present != IPMI_NO_VALUES_PRESENT)
678 ssnprintf(n.message, sizeof(n.message),
679 "sensor %s received event: %s, value is %f", n.type_instance,
680 event_state, value);
681 else
682 ssnprintf(n.message, sizeof(n.message),
683 "sensor %s received event: %s, value not provided",
684 n.type_instance, event_state);
685
686 DEBUG("Threshold event received for sensor %s", n.type_instance);
687
688 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
689 n.severity = sensor_convert_threshold_severity(threshold);
690 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
691
692 plugin_notification_meta_add_string(&n, "severity",
693 ipmi_get_threshold_string(threshold));
694 plugin_notification_meta_add_string(&n, "direction",
695 ipmi_get_value_dir_string(high_low));
696
697 switch (value_present) {
698 case IPMI_BOTH_VALUES_PRESENT:
699 plugin_notification_meta_add_double(&n, "val", value);
700 /* both values present, so fall-through to add raw value too */
701 case IPMI_RAW_VALUE_PRESENT: {
702 char buf[DATA_MAX_NAME_LEN] = {0};
703 ssnprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
704 plugin_notification_meta_add_string(&n, "raw", buf);
705 } break;
706 default:
707 break;
708 } /* switch (value_present) */
709
710 add_event_common_data(&n, sensor, dir, event);
711
712 plugin_dispatch_notification(&n);
713 plugin_notification_meta_free(n.meta);
714
715 /* Delete handled ipmi event from the list */
716 if (st->sel_clear_event) {
717 ipmi_event_delete(event, NULL, NULL);
718 return IPMI_EVENT_HANDLED;
719 }
720
721 return IPMI_EVENT_NOT_HANDLED;
722 } /* int sensor_threshold_event_handler */
723
sensor_discrete_event_handler(ipmi_sensor_t * sensor,enum ipmi_event_dir_e dir,int offset,int severity,int prev_severity,void * cb_data,ipmi_event_t * event)724 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
725 enum ipmi_event_dir_e dir, int offset,
726 int severity, int prev_severity,
727 void *cb_data, ipmi_event_t *event) {
728
729 c_ipmi_instance_t *st = cb_data;
730
731 /* From the IPMI specification Chapter 2: Events.
732 * If a callback handles the event, then all future callbacks called due to
733 * the event will receive a NULL for the event. So be ready to handle a NULL
734 * event in all your event handlers. A NULL may also be passed to an event
735 * handler if the callback was not due to an event. */
736 if (event == NULL)
737 return IPMI_EVENT_NOT_HANDLED;
738
739 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
740 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
741 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
742 const char *event_state =
743 ipmi_get_reading_name(event_type, sensor_type, offset);
744 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
745 ssnprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
746 n.type_instance, event_state);
747
748 DEBUG("Discrete event received for sensor %s", n.type_instance);
749
750 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
751 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
752
753 plugin_notification_meta_add_signed_int(&n, "offset", offset);
754
755 if (severity != -1)
756 plugin_notification_meta_add_signed_int(&n, "severity", severity);
757
758 if (prev_severity != -1)
759 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
760
761 add_event_common_data(&n, sensor, dir, event);
762
763 plugin_dispatch_notification(&n);
764 plugin_notification_meta_free(n.meta);
765
766 /* Delete handled ipmi event from the list */
767 if (st->sel_clear_event) {
768 ipmi_event_delete(event, NULL, NULL);
769 return IPMI_EVENT_HANDLED;
770 }
771
772 return IPMI_EVENT_NOT_HANDLED;
773 } /* int sensor_discrete_event_handler */
774
sel_list_add(c_ipmi_instance_t * st,ipmi_sensor_t * sensor)775 static int sel_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
776 char sensor_name[DATA_MAX_NAME_LEN] = {0};
777 int status = 0;
778
779 /* Check if sensor on sel_ignorelist */
780 sensor_get_name(sensor, sensor_name, sizeof(sensor_name));
781 if (ignorelist_match(st->sel_ignorelist, sensor_name) != 0)
782 return 0;
783
784 /* register threshold event if threshold sensor support events */
785 if (ipmi_sensor_get_event_reading_type(sensor) ==
786 IPMI_EVENT_READING_TYPE_THRESHOLD)
787 status = ipmi_sensor_add_threshold_event_handler(
788 sensor, sensor_threshold_event_handler, st);
789 /* register discrete handler if discrete/specific sensor support events */
790 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
791 status = ipmi_sensor_add_discrete_event_handler(
792 sensor, sensor_discrete_event_handler, st);
793
794 if (status)
795 ERROR("Unable to add sensor %s event handler, status: %d", sensor_name,
796 status);
797 return status;
798 }
799
sel_list_remove(c_ipmi_instance_t * st,ipmi_sensor_t * sensor)800 static void sel_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
801 if (ipmi_sensor_get_event_reading_type(sensor) ==
802 IPMI_EVENT_READING_TYPE_THRESHOLD)
803 ipmi_sensor_remove_threshold_event_handler(
804 sensor, sensor_threshold_event_handler, st);
805 else
806 ipmi_sensor_remove_discrete_event_handler(
807 sensor, sensor_discrete_event_handler, st);
808 }
809 /*
810 * Entity handlers
811 */
812 static void
entity_sensor_update_handler(enum ipmi_update_e op,ipmi_entity_t * entity,ipmi_sensor_t * sensor,void * user_data)813 entity_sensor_update_handler(enum ipmi_update_e op,
814 ipmi_entity_t __attribute__((unused)) * entity,
815 ipmi_sensor_t *sensor, void *user_data) {
816 c_ipmi_instance_t *st = user_data;
817
818 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
819 /* Will check for duplicate entries.. */
820 sensor_list_add(st, sensor);
821 if (st->sel_enabled)
822 sel_list_add(st, sensor);
823 } else if (op == IPMI_DELETED) {
824 sensor_list_remove(st, sensor);
825 if (st->sel_enabled)
826 sel_list_remove(st, sensor);
827 }
828 } /* void entity_sensor_update_handler */
829
830 /*
831 * Domain handlers
832 */
833 static void
domain_entity_update_handler(enum ipmi_update_e op,ipmi_domain_t * domain,ipmi_entity_t * entity,void * user_data)834 domain_entity_update_handler(enum ipmi_update_e op,
835 ipmi_domain_t __attribute__((unused)) * domain,
836 ipmi_entity_t *entity, void *user_data) {
837 int status;
838 c_ipmi_instance_t *st = user_data;
839
840 if (op == IPMI_ADDED) {
841 status = ipmi_entity_add_sensor_update_handler(
842 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
843 if (status != 0) {
844 c_ipmi_error(st, "ipmi_entity_add_sensor_update_handler", status);
845 }
846 } else if (op == IPMI_DELETED) {
847 status = ipmi_entity_remove_sensor_update_handler(
848 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
849 if (status != 0) {
850 c_ipmi_error(st, "ipmi_entity_remove_sensor_update_handler", status);
851 }
852 }
853 } /* void domain_entity_update_handler */
854
smi_event_handler(ipmi_con_t * ipmi,const ipmi_addr_t * addr,unsigned int addr_len,ipmi_event_t * event,void * cb_data)855 static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi,
856 const ipmi_addr_t __attribute__((unused)) * addr,
857 unsigned int __attribute__((unused)) addr_len,
858 ipmi_event_t *event, void *cb_data) {
859 unsigned int type = ipmi_event_get_type(event);
860 ipmi_domain_t *domain = cb_data;
861
862 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
863
864 if (type != 0x02)
865 /* It's not a standard IPMI event. */
866 return;
867
868 /* force domain to reread SELs */
869 ipmi_domain_reread_sels(domain, NULL, NULL);
870 }
871
domain_connection_change_handler(ipmi_domain_t * domain,int err,unsigned int conn_num,unsigned int port_num,int still_connected,void * user_data)872 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
873 unsigned int conn_num,
874 unsigned int port_num,
875 int still_connected,
876 void *user_data) {
877
878 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
879 "conn_num = %u, port_num = %u, still_connected = %i, "
880 "user_data = %p);",
881 (void *)domain, err, conn_num, port_num, still_connected, user_data);
882
883 c_ipmi_instance_t *st = user_data;
884
885 if (err != 0)
886 c_ipmi_error(st, "domain_connection_change_handler", err);
887
888 if (!still_connected) {
889
890 if (st->notify_conn && st->connected && st->init_in_progress == 0) {
891 notification_t n = c_ipmi_notification_init(st, NOTIF_FAILURE);
892
893 sstrncpy(n.message, "IPMI connection lost", sizeof(n.plugin));
894
895 plugin_dispatch_notification(&n);
896 }
897
898 st->connected = false;
899 return;
900 }
901
902 if (st->notify_conn && !st->connected && st->init_in_progress == 0) {
903 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
904
905 sstrncpy(n.message, "IPMI connection restored", sizeof(n.plugin));
906
907 plugin_dispatch_notification(&n);
908 }
909
910 st->connected = true;
911
912 int status = ipmi_domain_add_entity_update_handler(
913 domain, domain_entity_update_handler, /* user data = */ st);
914 if (status != 0) {
915 c_ipmi_error(st, "ipmi_domain_add_entity_update_handler", status);
916 }
917
918 status = st->connection->add_event_handler(st->connection, smi_event_handler,
919 (void *)domain);
920
921 if (status != 0)
922 c_ipmi_error(st, "Failed to register smi event handler", status);
923 } /* void domain_connection_change_handler */
924
c_ipmi_thread_init(c_ipmi_instance_t * st)925 static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
926 ipmi_domain_id_t domain_id;
927 int status;
928
929 if (st->connaddr != NULL) {
930 status = ipmi_ip_setup_con(
931 &st->connaddr, &(char *){IPMI_LAN_STD_PORT_STR}, 1, st->authtype,
932 (unsigned int)IPMI_PRIVILEGE_USER, st->username, strlen(st->username),
933 st->password, strlen(st->password), os_handler,
934 /* user data = */ NULL, &st->connection);
935 if (status != 0) {
936 c_ipmi_error(st, "ipmi_ip_setup_con", status);
937 return -1;
938 }
939 } else {
940 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
941 /* user data = */ NULL, &st->connection);
942 if (status != 0) {
943 c_ipmi_error(st, "ipmi_smi_setup_con", status);
944 return -1;
945 }
946 }
947
948 ipmi_open_option_t opts[] = {
949 {.option = IPMI_OPEN_OPTION_ALL, {.ival = 1}},
950 #ifdef IPMI_OPEN_OPTION_USE_CACHE
951 /* OpenIPMI-2.0.17 and later: Disable SDR cache in local file */
952 {.option = IPMI_OPEN_OPTION_USE_CACHE, {.ival = 0}},
953 #endif
954 };
955
956 /*
957 * NOTE: Domain names must be unique. There is static `domains_list` common
958 * to all threads inside lib/domain.c and some ops are done by name.
959 */
960 status = ipmi_open_domain(
961 st->name, &st->connection, /* num_con = */ 1,
962 domain_connection_change_handler, /* user data = */ (void *)st,
963 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, opts,
964 STATIC_ARRAY_SIZE(opts), &domain_id);
965 if (status != 0) {
966 c_ipmi_error(st, "ipmi_open_domain", status);
967 return -1;
968 }
969
970 return 0;
971 } /* int c_ipmi_thread_init */
972
c_ipmi_thread_main(void * user_data)973 static void *c_ipmi_thread_main(void *user_data) {
974 c_ipmi_instance_t *st = user_data;
975
976 int status = c_ipmi_thread_init(st);
977 if (status != 0) {
978 ERROR("ipmi plugin: c_ipmi_thread_init failed.");
979 st->active = false;
980 return (void *)-1;
981 }
982
983 while (st->active) {
984 struct timeval tv = {1, 0};
985 os_handler->perform_one_op(os_handler, &tv);
986 }
987 return (void *)0;
988 } /* void *c_ipmi_thread_main */
989
c_ipmi_init_instance()990 static c_ipmi_instance_t *c_ipmi_init_instance() {
991 c_ipmi_instance_t *st;
992
993 st = calloc(1, sizeof(*st));
994 if (st == NULL) {
995 ERROR("ipmi plugin: calloc failed.");
996 return NULL;
997 }
998
999 st->name = strdup("main");
1000 if (st->name == NULL) {
1001 sfree(st);
1002 ERROR("ipmi plugin: strdup() failed.");
1003 return NULL;
1004 }
1005
1006 st->ignorelist = ignorelist_create(/* invert = */ 1);
1007 if (st->ignorelist == NULL) {
1008 sfree(st->name);
1009 sfree(st);
1010 ERROR("ipmi plugin: ignorelist_create() failed.");
1011 return NULL;
1012 }
1013
1014 st->sel_ignorelist = ignorelist_create(/* invert = */ 1);
1015 if (st->sel_ignorelist == NULL) {
1016 ignorelist_free(st->ignorelist);
1017 sfree(st->name);
1018 sfree(st);
1019 ERROR("ipmi plugin: SEL ignorelist_create() failed.");
1020 return NULL;
1021 }
1022
1023 st->sensor_list = NULL;
1024 pthread_mutex_init(&st->sensor_list_lock, /* attr = */ NULL);
1025
1026 st->host = NULL;
1027 st->connaddr = NULL;
1028 st->username = NULL;
1029 st->password = NULL;
1030 st->authtype = IPMI_AUTHTYPE_DEFAULT;
1031
1032 st->next = NULL;
1033
1034 return st;
1035 } /* c_ipmi_instance_t *c_ipmi_init_instance */
1036
c_ipmi_free_instance(c_ipmi_instance_t * st)1037 static void c_ipmi_free_instance(c_ipmi_instance_t *st) {
1038 if (st == NULL)
1039 return;
1040
1041 assert(st->next == NULL);
1042
1043 sfree(st->name);
1044 sfree(st->host);
1045 sfree(st->connaddr);
1046 sfree(st->username);
1047 sfree(st->password);
1048
1049 ignorelist_free(st->sel_ignorelist);
1050 ignorelist_free(st->ignorelist);
1051 pthread_mutex_destroy(&st->sensor_list_lock);
1052 sfree(st);
1053 } /* void c_ipmi_free_instance */
1054
c_ipmi_add_instance(c_ipmi_instance_t * instance)1055 static void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
1056 if (instances == NULL) {
1057 instances = instance;
1058 return;
1059 }
1060
1061 c_ipmi_instance_t *last = instances;
1062
1063 while (last->next != NULL)
1064 last = last->next;
1065
1066 last->next = instance;
1067 } /* void c_ipmi_add_instance */
1068
c_ipmi_config_add_instance(oconfig_item_t * ci)1069 static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
1070 int status = 0;
1071 c_ipmi_instance_t *st = c_ipmi_init_instance();
1072 if (st == NULL)
1073 return ENOMEM;
1074
1075 if (strcasecmp(ci->key, "Instance") == 0)
1076 status = cf_util_get_string(ci, &st->name);
1077
1078 if (status != 0) {
1079 c_ipmi_free_instance(st);
1080 return status;
1081 }
1082
1083 for (int i = 0; i < ci->children_num; i++) {
1084 oconfig_item_t *child = ci->children + i;
1085
1086 if (strcasecmp("Sensor", child->key) == 0) {
1087 char *value = NULL;
1088 status = cf_util_get_string(child, &value);
1089 if (status != 0)
1090 break;
1091 ignorelist_add(st->ignorelist, value);
1092 sfree(value);
1093 } else if (strcasecmp("IgnoreSelected", child->key) == 0) {
1094 bool t;
1095 status = cf_util_get_boolean(child, &t);
1096 if (status != 0)
1097 break;
1098 ignorelist_set_invert(st->ignorelist, /* invert = */ !t);
1099 } else if (strcasecmp("NotifyIPMIConnectionState", child->key) == 0) {
1100 status = cf_util_get_boolean(child, &st->notify_conn);
1101 } else if (strcasecmp("NotifySensorAdd", child->key) == 0) {
1102 status = cf_util_get_boolean(child, &st->notify_add);
1103 } else if (strcasecmp("NotifySensorRemove", child->key) == 0) {
1104 status = cf_util_get_boolean(child, &st->notify_remove);
1105 } else if (strcasecmp("NotifySensorNotPresent", child->key) == 0) {
1106 status = cf_util_get_boolean(child, &st->notify_notpresent);
1107 } else if (strcasecmp("SELSensor", child->key) == 0) {
1108 char *value = NULL;
1109 status = cf_util_get_string(child, &value);
1110 if (status != 0)
1111 break;
1112 ignorelist_add(st->sel_ignorelist, value);
1113 sfree(value);
1114 } else if (strcasecmp("SELIgnoreSelected", child->key) == 0) {
1115 bool t;
1116 status = cf_util_get_boolean(child, &t);
1117 if (status != 0)
1118 break;
1119 ignorelist_set_invert(st->sel_ignorelist, /* invert = */ !t);
1120 } else if (strcasecmp("SELEnabled", child->key) == 0) {
1121 status = cf_util_get_boolean(child, &st->sel_enabled);
1122 } else if (strcasecmp("SELClearEvent", child->key) == 0) {
1123 status = cf_util_get_boolean(child, &st->sel_clear_event);
1124 } else if (strcasecmp("Host", child->key) == 0)
1125 status = cf_util_get_string(child, &st->host);
1126 else if (strcasecmp("Address", child->key) == 0)
1127 status = cf_util_get_string(child, &st->connaddr);
1128 else if (strcasecmp("Username", child->key) == 0)
1129 status = cf_util_get_string(child, &st->username);
1130 else if (strcasecmp("Password", child->key) == 0)
1131 status = cf_util_get_string(child, &st->password);
1132 else if (strcasecmp("AuthType", child->key) == 0) {
1133 char tmp[8];
1134 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
1135 if (status != 0)
1136 break;
1137
1138 if (strcasecmp("MD5", tmp) == 0)
1139 st->authtype = IPMI_AUTHTYPE_MD5;
1140 else if (strcasecmp("rmcp+", tmp) == 0)
1141 st->authtype = IPMI_AUTHTYPE_RMCP_PLUS;
1142 else
1143 WARNING("ipmi plugin: The value \"%s\" is not valid for the "
1144 "\"AuthType\" option.",
1145 tmp);
1146 } else {
1147 WARNING("ipmi plugin: Option `%s' not allowed here.", child->key);
1148 status = -1;
1149 }
1150
1151 if (status != 0)
1152 break;
1153 }
1154
1155 if (status != 0) {
1156 c_ipmi_free_instance(st);
1157 return status;
1158 }
1159
1160 c_ipmi_add_instance(st);
1161
1162 return 0;
1163 } /* int c_ipmi_config_add_instance */
1164
c_ipmi_config(oconfig_item_t * ci)1165 static int c_ipmi_config(oconfig_item_t *ci) {
1166 bool have_instance_block = 0;
1167
1168 for (int i = 0; i < ci->children_num; i++) {
1169 oconfig_item_t *child = ci->children + i;
1170
1171 if (strcasecmp("Instance", child->key) == 0) {
1172 int status = c_ipmi_config_add_instance(child);
1173 if (status != 0)
1174 return status;
1175
1176 have_instance_block = 1;
1177 } else if (!have_instance_block) {
1178 /* Non-instance option: Assume legacy configuration (without <Instance />
1179 * blocks) and call c_ipmi_config_add_instance with the <Plugin /> block.
1180 */
1181 WARNING("ipmi plugin: Legacy configuration found! Please update your "
1182 "config file.");
1183 return c_ipmi_config_add_instance(ci);
1184 } else {
1185 WARNING("ipmi plugin: The configuration option "
1186 "\"%s\" is not allowed here. Did you "
1187 "forget to add an <Instance /> block "
1188 "around the configuration?",
1189 child->key);
1190 return -1;
1191 }
1192 } /* for (ci->children) */
1193
1194 return 0;
1195 } /* int c_ipmi_config */
1196
c_ipmi_read(user_data_t * user_data)1197 static int c_ipmi_read(user_data_t *user_data) {
1198 c_ipmi_instance_t *st = user_data->data;
1199
1200 if (st->active == false) {
1201 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
1202 return -1;
1203 }
1204
1205 if (st->connected == false)
1206 return 0;
1207
1208 sensor_list_read_all(st);
1209
1210 if (st->init_in_progress > 0)
1211 st->init_in_progress--;
1212 else
1213 st->init_in_progress = 0;
1214
1215 return 0;
1216 } /* int c_ipmi_read */
1217
c_ipmi_init(void)1218 static int c_ipmi_init(void) {
1219 c_ipmi_instance_t *st;
1220 char callback_name[3 * DATA_MAX_NAME_LEN];
1221
1222 if (os_handler != NULL) {
1223 return 0;
1224 }
1225
1226 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
1227 if (os_handler == NULL) {
1228 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
1229 return -1;
1230 }
1231
1232 os_handler->set_log_handler(os_handler, c_ipmi_log);
1233
1234 if (ipmi_init(os_handler) != 0) {
1235 ERROR("ipmi plugin: ipmi_init() failed.");
1236 os_handler->free_os_handler(os_handler);
1237 return -1;
1238 };
1239
1240 if (instances == NULL) {
1241 /* No instances were configured, let's start a default instance. */
1242 st = c_ipmi_init_instance();
1243 if (st == NULL)
1244 return ENOMEM;
1245
1246 c_ipmi_add_instance(st);
1247 }
1248
1249 /* Don't send `ADD' notifications during startup (~ 1 minute) */
1250 int cycles = 1 + (int)(TIME_T_TO_CDTIME_T(60) / plugin_get_interval());
1251
1252 st = instances;
1253 while (NULL != st) {
1254 /* The `st->name` is used as "domain name" for ipmi_open_domain().
1255 * That value should be unique, so we do plugin_register_complex_read()
1256 * at first as it checks the uniqueness. */
1257 ssnprintf(callback_name, sizeof(callback_name), "ipmi/%s", st->name);
1258
1259 user_data_t ud = {
1260 .data = st,
1261 };
1262
1263 int status = plugin_register_complex_read(
1264 /* group = */ "ipmi",
1265 /* name = */ callback_name,
1266 /* callback = */ c_ipmi_read,
1267 /* interval = */ 0,
1268 /* user_data = */ &ud);
1269
1270 if (status != 0) {
1271 st = st->next;
1272 continue;
1273 }
1274
1275 st->init_in_progress = cycles;
1276 st->active = true;
1277
1278 status = plugin_thread_create(&st->thread_id, c_ipmi_thread_main,
1279 /* user data = */ (void *)st, "ipmi");
1280
1281 if (status != 0) {
1282 st->active = false;
1283 st->thread_id = (pthread_t){0};
1284
1285 plugin_unregister_read(callback_name);
1286
1287 ERROR("ipmi plugin: pthread_create failed for `%s`.", callback_name);
1288 }
1289
1290 st = st->next;
1291 }
1292
1293 return 0;
1294 } /* int c_ipmi_init */
1295
c_ipmi_shutdown(void)1296 static int c_ipmi_shutdown(void) {
1297 c_ipmi_instance_t *st = instances;
1298 instances = NULL;
1299
1300 while (st != NULL) {
1301 c_ipmi_instance_t *next = st->next;
1302
1303 st->next = NULL;
1304 st->active = false;
1305
1306 if (!pthread_equal(st->thread_id, (pthread_t){0})) {
1307 pthread_join(st->thread_id, NULL);
1308 st->thread_id = (pthread_t){0};
1309 }
1310
1311 sensor_list_remove_all(st);
1312 c_ipmi_free_instance(st);
1313
1314 st = next;
1315 }
1316
1317 os_handler->free_os_handler(os_handler);
1318 os_handler = NULL;
1319
1320 return 0;
1321 } /* int c_ipmi_shutdown */
1322
module_register(void)1323 void module_register(void) {
1324 plugin_register_complex_config("ipmi", c_ipmi_config);
1325 plugin_register_init("ipmi", c_ipmi_init);
1326 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
1327 } /* void module_register */
1328