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
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.
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.
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
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>
30 #include "utils/common/common.h"
31 #include "utils/ignorelist/ignorelist.h"
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>
41 #define ERR_BUF_SIZE 1024
46 struct c_ipmi_sensor_list_s;
47 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
49 struct c_ipmi_instance_s {
51 ignorelist_t *ignorelist;
52 ignorelist_t *sel_ignorelist;
55 bool notify_notpresent;
64 unsigned int authtype;
67 ipmi_con_t *connection;
68 pthread_mutex_t sensor_list_lock;
69 c_ipmi_sensor_list_t *sensor_list;
75 struct c_ipmi_instance_s *next;
77 typedef struct c_ipmi_instance_s c_ipmi_instance_t;
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;
90 struct c_ipmi_db_type_map_s {
91 enum ipmi_unit_type_e type;
92 const char *type_name;
94 typedef struct c_ipmi_db_type_map_s c_ipmi_db_type_map_t;
97 * Module global variables
99 static os_handler_t *os_handler;
100 static c_ipmi_instance_t *instances;
103 * Misc private functions
105 static void c_ipmi_error(c_ipmi_instance_t *st, const char *func, int status) {
106 char errbuf[ERR_BUF_SIZE] = {0};
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));
113 if (errbuf[0] == 0) {
114 ssnprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
116 errbuf[sizeof(errbuf) - 1] = '\0';
118 ERROR("ipmi plugin: %s failed for `%s`: %s", func, st->name, errbuf);
119 } /* void c_ipmi_error */
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];
125 vsnprintf(msg, sizeof(msg), format, ap);
129 INFO("ipmi plugin: %s", msg);
131 case IPMI_LOG_WARNING:
132 NOTICE("ipmi plugin: %s", msg);
134 case IPMI_LOG_SEVERE:
135 WARNING("ipmi plugin: %s", msg);
138 ERROR("ipmi plugin: %s", msg);
140 case IPMI_LOG_ERR_INFO:
141 ERROR("ipmi plugin: %s", msg);
144 case IPMI_LOG_DEBUG_START:
146 DEBUG("ipmi plugin: %s", msg);
148 case IPMI_LOG_DEBUG_CONT:
149 case IPMI_LOG_DEBUG_END:
153 case IPMI_LOG_DEBUG_START:
155 case IPMI_LOG_DEBUG_CONT:
156 case IPMI_LOG_DEBUG_END:
160 } /* void c_ipmi_log */
162 static notification_t c_ipmi_notification_init(c_ipmi_instance_t const *st,
164 notification_t n = {severity, cdtime(), "", "", "ipmi", "", "", "", NULL};
166 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
168 } /* notification_t c_ipmi_notification_init */
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);
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,
181 value_list_t vl = VALUE_LIST_INIT;
183 c_ipmi_sensor_list_t *list_item = user_data;
184 c_ipmi_instance_t *st = list_item->instance;
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;
194 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` "
196 list_item->sensor_name, st->name);
198 if (st->notify_notpresent) {
199 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
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);
207 plugin_dispatch_notification(&n);
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);
220 char errbuf[ERR_BUF_SIZE] = {0};
221 ipmi_get_error_string(err, errbuf, sizeof(errbuf) - 1);
223 if (IPMI_IS_IPMI_ERR(err))
224 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
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: "
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: "
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: "
238 list_item->sensor_name, st->name, errbuf, IPMI_GET_SOL_ERR(err));
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);
245 } else if (list_item->sensor_not_present == 1) {
246 list_item->sensor_not_present = 0;
248 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` present.",
249 list_item->sensor_name, st->name);
251 if (st->notify_notpresent) {
252 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
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);
260 plugin_dispatch_notification(&n);
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"
271 sensor_list_remove(st, sensor);
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);
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);
289 vl.values = &(value_t){.gauge = value};
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));
299 plugin_dispatch_values(&vl);
300 } /* void sensor_read_handler */
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;
309 if ((buffer == NULL) || (buf_len == 0))
312 ipmi_sensor_get_name(sensor, temp, sizeof(temp));
313 temp[sizeof(temp) - 1] = '\0';
315 if (entity_id_string != NULL && strlen(temp))
316 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", temp,
318 else if (entity_id_string != NULL)
319 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
321 sstrncpy(sensor_name, temp, sizeof(sensor_name));
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'. */
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". */
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,
345 /* else: don't touch sensor_name. */
348 sstrncpy(buffer, sensor_name, buf_len);
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"}};
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))
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;
367 } /* const char* sensor_unit_to_type */
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;
374 char buffer[DATA_MAX_NAME_LEN] = {0};
375 char *sensor_name_ptr = buffer;
379 sensor_id = ipmi_sensor_convert_to_id(sensor);
380 sensor_get_name(sensor, buffer, sizeof(buffer));
382 DEBUG("ipmi plugin: sensor_list_add: Found sensor `%s` of `%s`,"
384 " Event reading type: %#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));
392 /* Both `ignorelist' and `sensor_name_ptr' may be NULL. */
393 if (ignorelist_match(st->ignorelist, sensor_name_ptr) != 0)
396 /* FIXME: Use rate unit or base unit to scale the value */
398 sensor_type = ipmi_sensor_get_sensor_type(sensor);
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
406 * ipmi_sensor_id_get_reading() supports only 'Threshold' sensors.
407 * See lib/sensor.c:4842, stand_ipmi_sensor_get_reading() for details.
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));
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));
427 switch (sensor_type) {
428 case IPMI_SENSOR_TYPE_TEMPERATURE:
429 type = "temperature";
432 case IPMI_SENSOR_TYPE_VOLTAGE:
436 case IPMI_SENSOR_TYPE_CURRENT:
440 case IPMI_SENSOR_TYPE_FAN:
444 case IPMI_SENSOR_TYPE_MEMORY:
449 /* try to get collectd DB type based on sensor base unit type */
450 if ((type = sensor_unit_to_type(sensor)) != NULL)
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));
463 } /* switch (sensor_type) */
465 pthread_mutex_lock(&st->sensor_list_lock);
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)
472 list_prev = list_item;
473 } /* for (list_item) */
475 if (list_item != NULL) {
476 pthread_mutex_unlock(&st->sensor_list_lock);
480 list_item = calloc(1, sizeof(*list_item));
481 if (list_item == NULL) {
482 pthread_mutex_unlock(&st->sensor_list_lock);
486 list_item->instance = st;
487 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
489 if (list_prev != NULL)
490 list_prev->next = list_item;
492 st->sensor_list = list_item;
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);
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));
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));
510 pthread_mutex_unlock(&st->sensor_list_lock);
512 if (st->notify_add && (st->init_in_progress == 0)) {
513 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
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);
521 plugin_dispatch_notification(&n);
525 } /* int sensor_list_add */
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;
532 sensor_id = ipmi_sensor_convert_to_id(sensor);
534 pthread_mutex_lock(&st->sensor_list_lock);
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)
541 list_prev = list_item;
542 } /* for (list_item) */
544 if (list_item == NULL) {
545 pthread_mutex_unlock(&st->sensor_list_lock);
549 if (list_prev == NULL)
550 st->sensor_list = list_item->next;
552 list_prev->next = list_item->next;
555 list_item->next = NULL;
557 pthread_mutex_unlock(&st->sensor_list_lock);
559 if (st->notify_remove && st->active) {
560 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
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);
568 plugin_dispatch_notification(&n);
573 } /* int sensor_list_remove */
575 static int sensor_list_read_all(c_ipmi_instance_t *st) {
576 pthread_mutex_lock(&st->sensor_list_lock);
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);
583 /* Reading already initiated */
588 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
589 /* user data = */ (void *)list_item);
590 } /* for (list_item) */
592 pthread_mutex_unlock(&st->sensor_list_lock);
595 } /* int sensor_list_read_all */
597 static int sensor_list_remove_all(c_ipmi_instance_t *st) {
598 c_ipmi_sensor_list_t *list_item;
600 pthread_mutex_lock(&st->sensor_list_lock);
602 list_item = st->sensor_list;
603 st->sensor_list = NULL;
605 pthread_mutex_unlock(&st->sensor_list_lock);
607 while (list_item != NULL) {
608 c_ipmi_sensor_list_t *list_next = list_item->next;
612 list_item = list_next;
613 } /* while (list_item) */
616 } /* int sensor_list_remove_all */
618 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
620 case IPMI_LOWER_NON_CRITICAL:
621 case IPMI_UPPER_NON_CRITICAL:
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;
631 } /* switch (severity) */
632 } /* int sensor_convert_threshold_severity */
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);
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);
648 plugin_notification_meta_add_signed_int(n, "event_type",
649 ipmi_event_get_type(event));
650 } /* void add_event_sensor_meta_data */
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) {
658 c_ipmi_instance_t *st = cb_data;
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. */
666 return IPMI_EVENT_NOT_HANDLED;
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,
682 ssnprintf(n.message, sizeof(n.message),
683 "sensor %s received event: %s, value not provided",
684 n.type_instance, event_state);
686 DEBUG("Threshold event received for sensor %s", n.type_instance);
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));
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));
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);
708 } /* switch (value_present) */
710 add_event_common_data(&n, sensor, dir, event);
712 plugin_dispatch_notification(&n);
713 plugin_notification_meta_free(n.meta);
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;
721 return IPMI_EVENT_NOT_HANDLED;
722 } /* int sensor_threshold_event_handler */
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) {
729 c_ipmi_instance_t *st = cb_data;
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. */
737 return IPMI_EVENT_NOT_HANDLED;
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);
748 DEBUG("Discrete event received for sensor %s", n.type_instance);
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));
753 plugin_notification_meta_add_signed_int(&n, "offset", offset);
756 plugin_notification_meta_add_signed_int(&n, "severity", severity);
758 if (prev_severity != -1)
759 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
761 add_event_common_data(&n, sensor, dir, event);
763 plugin_dispatch_notification(&n);
764 plugin_notification_meta_free(n.meta);
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;
772 return IPMI_EVENT_NOT_HANDLED;
773 } /* int sensor_discrete_event_handler */
775 static int sel_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
776 char sensor_name[DATA_MAX_NAME_LEN] = {0};
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)
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);
795 ERROR("Unable to add sensor %s event handler, status: %d", sensor_name,
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);
806 ipmi_sensor_remove_discrete_event_handler(
807 sensor, sensor_discrete_event_handler, st);
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;
818 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
819 /* Will check for duplicate entries.. */
820 sensor_list_add(st, sensor);
822 sel_list_add(st, sensor);
823 } else if (op == IPMI_DELETED) {
824 sensor_list_remove(st, sensor);
826 sel_list_remove(st, sensor);
828 } /* void entity_sensor_update_handler */
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) {
838 c_ipmi_instance_t *st = user_data;
840 if (op == IPMI_ADDED) {
841 status = ipmi_entity_add_sensor_update_handler(
842 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
844 c_ipmi_error(st, "ipmi_entity_add_sensor_update_handler", status);
846 } else if (op == IPMI_DELETED) {
847 status = ipmi_entity_remove_sensor_update_handler(
848 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
850 c_ipmi_error(st, "ipmi_entity_remove_sensor_update_handler", status);
853 } /* void domain_entity_update_handler */
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;
862 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
865 /* It's not a standard IPMI event. */
868 /* force domain to reread SELs */
869 ipmi_domain_reread_sels(domain, NULL, NULL);
872 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
873 unsigned int conn_num,
874 unsigned int port_num,
878 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
879 "conn_num = %u, port_num = %u, still_connected = %i, "
881 (void *)domain, err, conn_num, port_num, still_connected, user_data);
883 c_ipmi_instance_t *st = user_data;
886 c_ipmi_error(st, "domain_connection_change_handler", err);
888 if (!still_connected) {
890 if (st->notify_conn && st->connected && st->init_in_progress == 0) {
891 notification_t n = c_ipmi_notification_init(st, NOTIF_FAILURE);
893 sstrncpy(n.message, "IPMI connection lost", sizeof(n.plugin));
895 plugin_dispatch_notification(&n);
898 st->connected = false;
902 if (st->notify_conn && !st->connected && st->init_in_progress == 0) {
903 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
905 sstrncpy(n.message, "IPMI connection restored", sizeof(n.plugin));
907 plugin_dispatch_notification(&n);
910 st->connected = true;
912 int status = ipmi_domain_add_entity_update_handler(
913 domain, domain_entity_update_handler, /* user data = */ st);
915 c_ipmi_error(st, "ipmi_domain_add_entity_update_handler", status);
918 status = st->connection->add_event_handler(st->connection, smi_event_handler,
922 c_ipmi_error(st, "Failed to register smi event handler", status);
923 } /* void domain_connection_change_handler */
925 static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
926 ipmi_domain_id_t domain_id;
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);
936 c_ipmi_error(st, "ipmi_ip_setup_con", status);
940 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
941 /* user data = */ NULL, &st->connection);
943 c_ipmi_error(st, "ipmi_smi_setup_con", status);
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}},
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.
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);
966 c_ipmi_error(st, "ipmi_open_domain", status);
971 } /* int c_ipmi_thread_init */
973 static void *c_ipmi_thread_main(void *user_data) {
974 c_ipmi_instance_t *st = user_data;
976 int status = c_ipmi_thread_init(st);
978 ERROR("ipmi plugin: c_ipmi_thread_init failed.");
984 struct timeval tv = {1, 0};
985 os_handler->perform_one_op(os_handler, &tv);
988 } /* void *c_ipmi_thread_main */
990 static c_ipmi_instance_t *c_ipmi_init_instance() {
991 c_ipmi_instance_t *st;
993 st = calloc(1, sizeof(*st));
995 ERROR("ipmi plugin: calloc failed.");
999 st->name = strdup("main");
1000 if (st->name == NULL) {
1002 ERROR("ipmi plugin: strdup() failed.");
1006 st->ignorelist = ignorelist_create(/* invert = */ 1);
1007 if (st->ignorelist == NULL) {
1010 ERROR("ipmi plugin: ignorelist_create() failed.");
1014 st->sel_ignorelist = ignorelist_create(/* invert = */ 1);
1015 if (st->sel_ignorelist == NULL) {
1016 ignorelist_free(st->ignorelist);
1019 ERROR("ipmi plugin: SEL ignorelist_create() failed.");
1023 st->sensor_list = NULL;
1024 pthread_mutex_init(&st->sensor_list_lock, /* attr = */ NULL);
1027 st->connaddr = NULL;
1028 st->username = NULL;
1029 st->password = NULL;
1030 st->authtype = IPMI_AUTHTYPE_DEFAULT;
1035 } /* c_ipmi_instance_t *c_ipmi_init_instance */
1037 static void c_ipmi_free_instance(c_ipmi_instance_t *st) {
1041 assert(st->next == NULL);
1045 sfree(st->connaddr);
1046 sfree(st->username);
1047 sfree(st->password);
1049 ignorelist_free(st->sel_ignorelist);
1050 ignorelist_free(st->ignorelist);
1051 pthread_mutex_destroy(&st->sensor_list_lock);
1053 } /* void c_ipmi_free_instance */
1055 static void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
1056 if (instances == NULL) {
1057 instances = instance;
1061 c_ipmi_instance_t *last = instances;
1063 while (last->next != NULL)
1066 last->next = instance;
1067 } /* void c_ipmi_add_instance */
1069 static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
1071 c_ipmi_instance_t *st = c_ipmi_init_instance();
1075 if (strcasecmp(ci->key, "Instance") == 0)
1076 status = cf_util_get_string(ci, &st->name);
1079 c_ipmi_free_instance(st);
1083 for (int i = 0; i < ci->children_num; i++) {
1084 oconfig_item_t *child = ci->children + i;
1086 if (strcasecmp("Sensor", child->key) == 0) {
1088 status = cf_util_get_string(child, &value);
1091 ignorelist_add(st->ignorelist, value);
1093 } else if (strcasecmp("IgnoreSelected", child->key) == 0) {
1095 status = cf_util_get_boolean(child, &t);
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) {
1109 status = cf_util_get_string(child, &value);
1112 ignorelist_add(st->sel_ignorelist, value);
1114 } else if (strcasecmp("SELIgnoreSelected", child->key) == 0) {
1116 status = cf_util_get_boolean(child, &t);
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) {
1134 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
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;
1143 WARNING("ipmi plugin: The value \"%s\" is not valid for the "
1144 "\"AuthType\" option.",
1147 WARNING("ipmi plugin: Option `%s' not allowed here.", child->key);
1156 c_ipmi_free_instance(st);
1160 c_ipmi_add_instance(st);
1163 } /* int c_ipmi_config_add_instance */
1165 static int c_ipmi_config(oconfig_item_t *ci) {
1166 bool have_instance_block = 0;
1168 for (int i = 0; i < ci->children_num; i++) {
1169 oconfig_item_t *child = ci->children + i;
1171 if (strcasecmp("Instance", child->key) == 0) {
1172 int status = c_ipmi_config_add_instance(child);
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.
1181 WARNING("ipmi plugin: Legacy configuration found! Please update your "
1183 return c_ipmi_config_add_instance(ci);
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?",
1192 } /* for (ci->children) */
1195 } /* int c_ipmi_config */
1197 static int c_ipmi_read(user_data_t *user_data) {
1198 c_ipmi_instance_t *st = user_data->data;
1200 if (st->active == false) {
1201 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
1205 if (st->connected == false)
1208 sensor_list_read_all(st);
1210 if (st->init_in_progress > 0)
1211 st->init_in_progress--;
1213 st->init_in_progress = 0;
1216 } /* int c_ipmi_read */
1218 static int c_ipmi_init(void) {
1219 c_ipmi_instance_t *st;
1220 char callback_name[3 * DATA_MAX_NAME_LEN];
1222 if (os_handler != NULL) {
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.");
1232 os_handler->set_log_handler(os_handler, c_ipmi_log);
1234 if (ipmi_init(os_handler) != 0) {
1235 ERROR("ipmi plugin: ipmi_init() failed.");
1236 os_handler->free_os_handler(os_handler);
1240 if (instances == NULL) {
1241 /* No instances were configured, let's start a default instance. */
1242 st = c_ipmi_init_instance();
1246 c_ipmi_add_instance(st);
1249 /* Don't send `ADD' notifications during startup (~ 1 minute) */
1250 int cycles = 1 + (int)(TIME_T_TO_CDTIME_T(60) / plugin_get_interval());
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);
1263 int status = plugin_register_complex_read(
1264 /* group = */ "ipmi",
1265 /* name = */ callback_name,
1266 /* callback = */ c_ipmi_read,
1268 /* user_data = */ &ud);
1275 st->init_in_progress = cycles;
1278 status = plugin_thread_create(&st->thread_id, /* attr = */ NULL,
1280 /* user data = */ (void *)st, "ipmi");
1284 st->thread_id = (pthread_t){0};
1286 plugin_unregister_read(callback_name);
1288 ERROR("ipmi plugin: pthread_create failed for `%s`.", callback_name);
1295 } /* int c_ipmi_init */
1297 static int c_ipmi_shutdown(void) {
1298 c_ipmi_instance_t *st = instances;
1301 while (st != NULL) {
1302 c_ipmi_instance_t *next = st->next;
1307 if (!pthread_equal(st->thread_id, (pthread_t){0})) {
1308 pthread_join(st->thread_id, NULL);
1309 st->thread_id = (pthread_t){0};
1312 sensor_list_remove_all(st);
1313 c_ipmi_free_instance(st);
1318 os_handler->free_os_handler(os_handler);
1322 } /* int c_ipmi_shutdown */
1324 void module_register(void) {
1325 plugin_register_complex_config("ipmi", c_ipmi_config);
1326 plugin_register_init("ipmi", c_ipmi_init);
1327 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
1328 } /* void module_register */