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>
31 #include "utils_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;
54 _Bool notify_notpresent;
57 _Bool sel_clear_event;
63 unsigned int authtype;
66 ipmi_con_t *connection;
67 pthread_mutex_t sensor_list_lock;
68 c_ipmi_sensor_list_t *sensor_list;
74 struct c_ipmi_instance_s *next;
76 typedef struct c_ipmi_instance_s c_ipmi_instance_t;
78 struct c_ipmi_sensor_list_s {
79 ipmi_sensor_id_t sensor_id;
80 char sensor_name[DATA_MAX_NAME_LEN];
81 char sensor_type[DATA_MAX_NAME_LEN];
82 char type_instance[DATA_MAX_NAME_LEN];
83 int sensor_not_present;
84 c_ipmi_sensor_list_t *next;
85 c_ipmi_instance_t *instance;
89 struct c_ipmi_db_type_map_s {
90 enum ipmi_unit_type_e type;
91 const char *type_name;
93 typedef struct c_ipmi_db_type_map_s c_ipmi_db_type_map_t;
96 * Module global variables
98 static os_handler_t *os_handler = NULL;
99 static c_ipmi_instance_t *instances = NULL;
102 * Misc private functions
104 static void c_ipmi_error(c_ipmi_instance_t *st, const char *func, int status) {
105 char errbuf[ERR_BUF_SIZE] = {0};
107 if (IPMI_IS_OS_ERR(status) || IPMI_IS_RMCPP_ERR(status) ||
108 IPMI_IS_IPMI_ERR(status)) {
109 ipmi_get_error_string(status, errbuf, sizeof(errbuf));
112 if (errbuf[0] == 0) {
113 snprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
115 errbuf[sizeof(errbuf) - 1] = 0;
117 ERROR("ipmi plugin: %s failed for `%s`: %s", func, st->name, errbuf);
118 } /* void c_ipmi_error */
120 static void c_ipmi_log(os_handler_t *handler, const char *format,
121 enum ipmi_log_type_e log_type, va_list ap) {
122 char msg[ERR_BUF_SIZE];
124 vsnprintf(msg, sizeof(msg), format, ap);
128 INFO("ipmi plugin: %s", msg);
130 case IPMI_LOG_WARNING:
131 NOTICE("ipmi plugin: %s", msg);
133 case IPMI_LOG_SEVERE:
134 WARNING("ipmi plugin: %s", msg);
137 ERROR("ipmi plugin: %s", msg);
139 case IPMI_LOG_ERR_INFO:
140 ERROR("ipmi plugin: %s", msg);
143 case IPMI_LOG_DEBUG_START:
145 DEBUG("ipmi plugin: %s", msg);
147 case IPMI_LOG_DEBUG_CONT:
148 case IPMI_LOG_DEBUG_END:
152 case IPMI_LOG_DEBUG_START:
154 case IPMI_LOG_DEBUG_CONT:
155 case IPMI_LOG_DEBUG_END:
159 } /* void c_ipmi_log */
161 static notification_t c_ipmi_notification_init(c_ipmi_instance_t const *st,
163 notification_t n = {severity, cdtime(), "", "", "ipmi", "", "", "", NULL};
165 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
167 } /* notification_t c_ipmi_notification_init */
172 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
173 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor);
175 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
176 enum ipmi_value_present_e value_present,
177 unsigned int __attribute__((unused)) raw_value,
178 double value, ipmi_states_t *states,
180 value_list_t vl = VALUE_LIST_INIT;
182 c_ipmi_sensor_list_t *list_item = user_data;
183 c_ipmi_instance_t *st = list_item->instance;
188 if (IPMI_IS_IPMI_ERR(err) &&
189 IPMI_GET_IPMI_ERR(err) == IPMI_NOT_PRESENT_CC) {
190 if (list_item->sensor_not_present == 0) {
191 list_item->sensor_not_present = 1;
193 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` "
195 list_item->sensor_name, st->name);
197 if (st->notify_notpresent) {
198 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
200 sstrncpy(n.type_instance, list_item->type_instance,
201 sizeof(n.type_instance));
202 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
203 snprintf(n.message, sizeof(n.message), "sensor %s not present",
204 list_item->sensor_name);
206 plugin_dispatch_notification(&n);
209 } else if (IPMI_IS_IPMI_ERR(err) &&
210 IPMI_GET_IPMI_ERR(err) ==
211 IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
212 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` not ready.",
213 list_item->sensor_name, st->name);
214 } else if (IPMI_IS_IPMI_ERR(err) &&
215 IPMI_GET_IPMI_ERR(err) == IPMI_TIMEOUT_CC) {
216 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` timed out.",
217 list_item->sensor_name, st->name);
219 char errbuf[ERR_BUF_SIZE] = {0};
220 ipmi_get_error_string(err, errbuf, sizeof(errbuf) - 1);
222 if (IPMI_IS_IPMI_ERR(err))
223 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
225 list_item->sensor_name, st->name, errbuf);
226 else if (IPMI_IS_OS_ERR(err))
227 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
229 list_item->sensor_name, st->name, errbuf, IPMI_GET_OS_ERR(err));
230 else if (IPMI_IS_RMCPP_ERR(err))
231 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
233 list_item->sensor_name, st->name, errbuf);
234 else if (IPMI_IS_SOL_ERR(err))
235 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
237 list_item->sensor_name, st->name, errbuf, IPMI_GET_SOL_ERR(err));
239 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed "
240 "with error %#x. of class %#x",
241 list_item->sensor_name, st->name, err & 0xff, err & 0xffffff00);
244 } else if (list_item->sensor_not_present == 1) {
245 list_item->sensor_not_present = 0;
247 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` present.",
248 list_item->sensor_name, st->name);
250 if (st->notify_notpresent) {
251 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
253 sstrncpy(n.type_instance, list_item->type_instance,
254 sizeof(n.type_instance));
255 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
256 snprintf(n.message, sizeof(n.message), "sensor %s present",
257 list_item->sensor_name);
259 plugin_dispatch_notification(&n);
263 if (value_present != IPMI_BOTH_VALUES_PRESENT) {
264 INFO("ipmi plugin: sensor_read_handler: Removing sensor `%s` of `%s`, "
265 "because it provides %s. If you need this sensor, "
266 "please file a bug report.",
267 list_item->sensor_name, st->name,
268 (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
270 sensor_list_remove(st, sensor);
274 if (!ipmi_is_sensor_scanning_enabled(states)) {
275 DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
276 "it is in 'scanning disabled' state.",
277 list_item->sensor_name, st->name);
281 if (ipmi_is_initial_update_in_progress(states)) {
282 DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
283 "it is in 'initial update in progress' state.",
284 list_item->sensor_name, st->name);
288 vl.values = &(value_t){.gauge = value};
291 if (st->host != NULL)
292 sstrncpy(vl.host, st->host, sizeof(vl.host));
293 sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
294 sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
295 sstrncpy(vl.type_instance, list_item->type_instance,
296 sizeof(vl.type_instance));
298 plugin_dispatch_values(&vl);
299 } /* void sensor_read_handler */
301 static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) {
302 char temp[DATA_MAX_NAME_LEN] = {0};
303 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
304 const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
305 char sensor_name[DATA_MAX_NAME_LEN] = "";
306 char *sensor_name_ptr;
308 if ((buffer == NULL) || (buf_len == 0))
311 ipmi_sensor_get_name(sensor, temp, sizeof(temp));
312 temp[sizeof(temp) - 1] = 0;
314 if (entity_id_string != NULL && strlen(temp))
315 snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string);
316 else if (entity_id_string != NULL)
317 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
319 sstrncpy(sensor_name, temp, sizeof(sensor_name));
322 sstrncpy(temp, sensor_name, sizeof(temp));
323 sensor_name_ptr = strstr(temp, ").");
324 if (sensor_name_ptr != NULL) {
325 /* If name is something like "foo (123).bar",
326 * change that to "bar (123)".
327 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
328 * `temp' array, which holds a copy of the current `sensor_name'. */
331 /* `sensor_name_ptr' points to ").bar". */
332 sensor_name_ptr[1] = 0;
333 /* `temp' holds "foo (123)\0bar\0". */
334 sensor_name_ptr += 2;
335 /* `sensor_name_ptr' now points to "bar". */
337 sensor_id_ptr = strstr(temp, "(");
338 if (sensor_id_ptr != NULL) {
339 /* `sensor_id_ptr' now points to "(123)". */
340 snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
343 /* else: don't touch sensor_name. */
346 sstrncpy(buffer, sensor_name, buf_len);
349 static const char *sensor_unit_to_type(ipmi_sensor_t *sensor) {
350 static const c_ipmi_db_type_map_t ipmi_db_type_map[] = {
351 {IPMI_UNIT_TYPE_WATTS, "power"}, {IPMI_UNIT_TYPE_CFM, "flow"}};
353 /* check the modifier and rate of the sensor value */
354 if ((ipmi_sensor_get_modifier_unit_use(sensor) != IPMI_MODIFIER_UNIT_NONE) ||
355 (ipmi_sensor_get_rate_unit(sensor) != IPMI_RATE_UNIT_NONE))
358 /* find the db type by using sensor base unit type */
359 enum ipmi_unit_type_e ipmi_type = ipmi_sensor_get_base_unit(sensor);
360 for (int i = 0; i < STATIC_ARRAY_SIZE(ipmi_db_type_map); i++)
361 if (ipmi_db_type_map[i].type == ipmi_type)
362 return ipmi_db_type_map[i].type_name;
365 } /* const char* sensor_unit_to_type */
367 static int sensor_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
368 ipmi_sensor_id_t sensor_id;
369 c_ipmi_sensor_list_t *list_item;
370 c_ipmi_sensor_list_t *list_prev;
372 char buffer[DATA_MAX_NAME_LEN] = {0};
373 char *sensor_name_ptr = buffer;
377 sensor_id = ipmi_sensor_convert_to_id(sensor);
378 sensor_get_name(sensor, buffer, sizeof(buffer));
380 DEBUG("ipmi plugin: sensor_list_add: Found sensor `%s` of `%s`,"
382 " Event reading type: %#x"
384 " Event support: %#x",
385 sensor_name_ptr, st->name, ipmi_sensor_get_sensor_type(sensor),
386 ipmi_sensor_get_event_reading_type(sensor),
387 ipmi_sensor_get_sensor_direction(sensor),
388 ipmi_sensor_get_event_support(sensor));
390 /* Both `ignorelist' and `sensor_name_ptr' may be NULL. */
391 if (ignorelist_match(st->ignorelist, sensor_name_ptr) != 0)
394 /* FIXME: Use rate unit or base unit to scale the value */
396 sensor_type = ipmi_sensor_get_sensor_type(sensor);
399 * ipmitool/lib/ipmi_sdr.c sdr_sensor_has_analog_reading() has a notice
400 * about 'Threshold sensors' and 'analog readings'. Discrete sensor may
401 * have analog data, but discrete sensors support is not implemented
404 * ipmi_sensor_id_get_reading() supports only 'Threshold' sensors.
405 * See lib/sensor.c:4842, stand_ipmi_sensor_get_reading() for details.
407 if (!ipmi_sensor_get_is_readable(sensor)) {
408 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
409 "because it isn't readable! Its type: (%#x, %s). ",
410 sensor_name_ptr, st->name, sensor_type,
411 ipmi_sensor_get_sensor_type_string(sensor));
415 if (ipmi_sensor_get_event_reading_type(sensor) !=
416 IPMI_EVENT_READING_TYPE_THRESHOLD) {
417 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
418 "because it is discrete (%#x)! Its type: (%#x, %s). ",
419 sensor_name_ptr, st->name, sensor_type,
420 ipmi_sensor_get_event_reading_type(sensor),
421 ipmi_sensor_get_sensor_type_string(sensor));
425 switch (sensor_type) {
426 case IPMI_SENSOR_TYPE_TEMPERATURE:
427 type = "temperature";
430 case IPMI_SENSOR_TYPE_VOLTAGE:
434 case IPMI_SENSOR_TYPE_CURRENT:
438 case IPMI_SENSOR_TYPE_FAN:
442 case IPMI_SENSOR_TYPE_MEMORY:
447 /* try to get collectd DB type based on sensor base unit type */
448 if ((type = sensor_unit_to_type(sensor)) != NULL)
451 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
452 "because I don't know how to handle its units (%#x, %#x, %#x). "
453 "Sensor type: (%#x, %s). If you need this sensor, please file "
454 "a bug report at http://collectd.org/.",
455 sensor_name_ptr, st->name, ipmi_sensor_get_base_unit(sensor),
456 ipmi_sensor_get_modifier_unit(sensor),
457 ipmi_sensor_get_rate_unit(sensor), sensor_type,
458 ipmi_sensor_get_sensor_type_string(sensor));
461 } /* switch (sensor_type) */
463 pthread_mutex_lock(&st->sensor_list_lock);
466 for (list_item = st->sensor_list; list_item != NULL;
467 list_item = list_item->next) {
468 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
470 list_prev = list_item;
471 } /* for (list_item) */
473 if (list_item != NULL) {
474 pthread_mutex_unlock(&st->sensor_list_lock);
478 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
479 if (list_item == NULL) {
480 pthread_mutex_unlock(&st->sensor_list_lock);
484 list_item->instance = st;
485 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
487 if (list_prev != NULL)
488 list_prev->next = list_item;
490 st->sensor_list = list_item;
492 /* if sensor provides the percentage value, use "percent" collectd type
493 and add the `percent` to the type instance of the reported value */
494 if (ipmi_sensor_get_percentage(sensor)) {
495 snprintf(list_item->type_instance, sizeof(list_item->type_instance),
496 "percent-%s", sensor_name_ptr);
499 /* use type instance as a name of the sensor */
500 sstrncpy(list_item->type_instance, sensor_name_ptr,
501 sizeof(list_item->type_instance));
504 sstrncpy(list_item->sensor_name, sensor_name_ptr,
505 sizeof(list_item->sensor_name));
506 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
508 pthread_mutex_unlock(&st->sensor_list_lock);
510 if (st->notify_add && (st->init_in_progress == 0)) {
511 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
513 sstrncpy(n.type_instance, list_item->type_instance,
514 sizeof(n.type_instance));
515 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
516 snprintf(n.message, sizeof(n.message), "sensor %s added",
517 list_item->sensor_name);
519 plugin_dispatch_notification(&n);
523 } /* int sensor_list_add */
525 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
526 ipmi_sensor_id_t sensor_id;
527 c_ipmi_sensor_list_t *list_item;
528 c_ipmi_sensor_list_t *list_prev;
530 sensor_id = ipmi_sensor_convert_to_id(sensor);
532 pthread_mutex_lock(&st->sensor_list_lock);
535 for (list_item = st->sensor_list; list_item != NULL;
536 list_item = list_item->next) {
537 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
539 list_prev = list_item;
540 } /* for (list_item) */
542 if (list_item == NULL) {
543 pthread_mutex_unlock(&st->sensor_list_lock);
547 if (list_prev == NULL)
548 st->sensor_list = list_item->next;
550 list_prev->next = list_item->next;
553 list_item->next = NULL;
555 pthread_mutex_unlock(&st->sensor_list_lock);
557 if (st->notify_remove && st->active) {
558 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
560 sstrncpy(n.type_instance, list_item->type_instance,
561 sizeof(n.type_instance));
562 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
563 snprintf(n.message, sizeof(n.message), "sensor %s removed",
564 list_item->sensor_name);
566 plugin_dispatch_notification(&n);
571 } /* int sensor_list_remove */
573 static int sensor_list_read_all(c_ipmi_instance_t *st) {
574 pthread_mutex_lock(&st->sensor_list_lock);
576 for (c_ipmi_sensor_list_t *list_item = st->sensor_list; list_item != NULL;
577 list_item = list_item->next) {
578 DEBUG("ipmi plugin: try read sensor `%s` of `%s`, use: %d",
579 list_item->sensor_name, st->name, list_item->use);
581 /* Reading already initiated */
586 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
587 /* user data = */ (void *)list_item);
588 } /* for (list_item) */
590 pthread_mutex_unlock(&st->sensor_list_lock);
593 } /* int sensor_list_read_all */
595 static int sensor_list_remove_all(c_ipmi_instance_t *st) {
596 c_ipmi_sensor_list_t *list_item;
598 pthread_mutex_lock(&st->sensor_list_lock);
600 list_item = st->sensor_list;
601 st->sensor_list = NULL;
603 pthread_mutex_unlock(&st->sensor_list_lock);
605 while (list_item != NULL) {
606 c_ipmi_sensor_list_t *list_next = list_item->next;
610 list_item = list_next;
611 } /* while (list_item) */
614 } /* int sensor_list_remove_all */
616 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
618 case IPMI_LOWER_NON_CRITICAL:
619 case IPMI_UPPER_NON_CRITICAL:
621 case IPMI_LOWER_CRITICAL:
622 case IPMI_UPPER_CRITICAL:
623 return NOTIF_WARNING;
624 case IPMI_LOWER_NON_RECOVERABLE:
625 case IPMI_UPPER_NON_RECOVERABLE:
626 return NOTIF_FAILURE;
629 } /* switch (severity) */
630 } /* int sensor_convert_threshold_severity */
632 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
633 enum ipmi_event_dir_e dir,
634 ipmi_event_t *event) {
635 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
637 plugin_notification_meta_add_string(n, "entity_name",
638 ipmi_entity_get_entity_id_string(ent));
639 plugin_notification_meta_add_signed_int(n, "entity_id",
640 ipmi_entity_get_entity_id(ent));
641 plugin_notification_meta_add_signed_int(n, "entity_instance",
642 ipmi_entity_get_entity_instance(ent));
643 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
646 plugin_notification_meta_add_signed_int(n, "event_type",
647 ipmi_event_get_type(event));
648 } /* void add_event_sensor_meta_data */
650 static int sensor_threshold_event_handler(
651 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
652 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
653 enum ipmi_value_present_e value_present, unsigned int raw_value,
654 double value, void *cb_data, ipmi_event_t *event) {
656 c_ipmi_instance_t *st = cb_data;
658 /* From the IPMI specification Chapter 2: Events.
659 * If a callback handles the event, then all future callbacks called due to
660 * the event will receive a NULL for the event. So be ready to handle a NULL
661 * event in all your event handlers. A NULL may also be passed to an event
662 * handler if the callback was not due to an event. */
664 return IPMI_EVENT_NOT_HANDLED;
666 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
667 /* offset is a table index and it's represented as enum of strings that are
668 organized in the way - high and low for each threshold severity level */
669 unsigned int offset = (2 * threshold) + high_low;
670 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
671 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
672 const char *event_state =
673 ipmi_get_reading_name(event_type, sensor_type, offset);
674 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
675 if (value_present != IPMI_NO_VALUES_PRESENT)
676 snprintf(n.message, sizeof(n.message),
677 "sensor %s received event: %s, value is %f", n.type_instance,
680 snprintf(n.message, sizeof(n.message),
681 "sensor %s received event: %s, value not provided",
682 n.type_instance, event_state);
684 DEBUG("Threshold event received for sensor %s", n.type_instance);
686 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
687 n.severity = sensor_convert_threshold_severity(threshold);
688 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
690 plugin_notification_meta_add_string(&n, "severity",
691 ipmi_get_threshold_string(threshold));
692 plugin_notification_meta_add_string(&n, "direction",
693 ipmi_get_value_dir_string(high_low));
695 switch (value_present) {
696 case IPMI_BOTH_VALUES_PRESENT:
697 plugin_notification_meta_add_double(&n, "val", value);
698 /* both values present, so fall-through to add raw value too */
699 case IPMI_RAW_VALUE_PRESENT: {
700 char buf[DATA_MAX_NAME_LEN] = {0};
701 snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
702 plugin_notification_meta_add_string(&n, "raw", buf);
706 } /* switch (value_present) */
708 add_event_common_data(&n, sensor, dir, event);
710 plugin_dispatch_notification(&n);
711 plugin_notification_meta_free(n.meta);
713 /* Delete handled ipmi event from the list */
714 if (st->sel_clear_event) {
715 ipmi_event_delete(event, NULL, NULL);
716 return IPMI_EVENT_HANDLED;
719 return IPMI_EVENT_NOT_HANDLED;
720 } /* int sensor_threshold_event_handler */
722 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
723 enum ipmi_event_dir_e dir, int offset,
724 int severity, int prev_severity,
725 void *cb_data, ipmi_event_t *event) {
727 c_ipmi_instance_t *st = cb_data;
729 /* From the IPMI specification Chapter 2: Events.
730 * If a callback handles the event, then all future callbacks called due to
731 * the event will receive a NULL for the event. So be ready to handle a NULL
732 * event in all your event handlers. A NULL may also be passed to an event
733 * handler if the callback was not due to an event. */
735 return IPMI_EVENT_NOT_HANDLED;
737 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
738 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
739 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
740 const char *event_state =
741 ipmi_get_reading_name(event_type, sensor_type, offset);
742 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
743 snprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
744 n.type_instance, event_state);
746 DEBUG("Discrete event received for sensor %s", n.type_instance);
748 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
749 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
751 plugin_notification_meta_add_signed_int(&n, "offset", offset);
754 plugin_notification_meta_add_signed_int(&n, "severity", severity);
756 if (prev_severity != -1)
757 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
759 add_event_common_data(&n, sensor, dir, event);
761 plugin_dispatch_notification(&n);
762 plugin_notification_meta_free(n.meta);
764 /* Delete handled ipmi event from the list */
765 if (st->sel_clear_event) {
766 ipmi_event_delete(event, NULL, NULL);
767 return IPMI_EVENT_HANDLED;
770 return IPMI_EVENT_NOT_HANDLED;
771 } /* int sensor_discrete_event_handler */
777 entity_sensor_update_handler(enum ipmi_update_e op,
778 ipmi_entity_t __attribute__((unused)) * entity,
779 ipmi_sensor_t *sensor, void *user_data) {
780 c_ipmi_instance_t *st = user_data;
782 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
783 /* Will check for duplicate entries.. */
784 sensor_list_add(st, sensor);
786 if (st->sel_enabled) {
788 /* register threshold event if threshold sensor support events */
789 if ((ipmi_sensor_get_event_reading_type(sensor) ==
790 IPMI_EVENT_READING_TYPE_THRESHOLD) &&
791 (ipmi_sensor_get_threshold_access(sensor) !=
792 IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
793 status = ipmi_sensor_add_threshold_event_handler(
794 sensor, sensor_threshold_event_handler, st);
795 /* register discrete handler if discrete/specific sensor support events */
796 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
797 status = ipmi_sensor_add_discrete_event_handler(
798 sensor, sensor_discrete_event_handler, st);
801 char buf[DATA_MAX_NAME_LEN] = {0};
802 sensor_get_name(sensor, buf, sizeof(buf));
803 ERROR("Unable to add sensor %s event handler, status: %d", buf, status);
806 } else if (op == IPMI_DELETED) {
807 sensor_list_remove(st, sensor);
809 if (st->sel_enabled) {
810 if (ipmi_sensor_get_event_reading_type(sensor) ==
811 IPMI_EVENT_READING_TYPE_THRESHOLD)
812 ipmi_sensor_remove_threshold_event_handler(
813 sensor, sensor_threshold_event_handler, st);
815 ipmi_sensor_remove_discrete_event_handler(
816 sensor, sensor_discrete_event_handler, st);
819 } /* void entity_sensor_update_handler */
825 domain_entity_update_handler(enum ipmi_update_e op,
826 ipmi_domain_t __attribute__((unused)) * domain,
827 ipmi_entity_t *entity, void *user_data) {
829 c_ipmi_instance_t *st = user_data;
831 if (op == IPMI_ADDED) {
832 status = ipmi_entity_add_sensor_update_handler(
833 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
835 c_ipmi_error(st, "ipmi_entity_add_sensor_update_handler", status);
837 } else if (op == IPMI_DELETED) {
838 status = ipmi_entity_remove_sensor_update_handler(
839 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
841 c_ipmi_error(st, "ipmi_entity_remove_sensor_update_handler", status);
844 } /* void domain_entity_update_handler */
846 static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi,
847 const ipmi_addr_t __attribute__((unused)) * addr,
848 unsigned int __attribute__((unused)) addr_len,
849 ipmi_event_t *event, void *cb_data) {
850 unsigned int type = ipmi_event_get_type(event);
851 ipmi_domain_t *domain = cb_data;
853 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
856 /* It's not a standard IPMI event. */
859 /* force domain to reread SELs */
860 ipmi_domain_reread_sels(domain, NULL, NULL);
863 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
864 unsigned int conn_num,
865 unsigned int port_num,
869 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
870 "conn_num = %u, port_num = %u, still_connected = %i, "
872 (void *)domain, err, conn_num, port_num, still_connected, user_data);
874 c_ipmi_instance_t *st = user_data;
877 c_ipmi_error(st, "domain_connection_change_handler", err);
879 if (!still_connected) {
881 if (st->notify_conn && st->connected && st->init_in_progress == 0) {
882 notification_t n = c_ipmi_notification_init(st, NOTIF_FAILURE);
884 sstrncpy(n.message, "IPMI connection lost", sizeof(n.plugin));
886 plugin_dispatch_notification(&n);
893 if (st->notify_conn && !st->connected && st->init_in_progress == 0) {
894 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
896 sstrncpy(n.message, "IPMI connection restored", sizeof(n.plugin));
898 plugin_dispatch_notification(&n);
903 int status = ipmi_domain_add_entity_update_handler(
904 domain, domain_entity_update_handler, /* user data = */ st);
906 c_ipmi_error(st, "ipmi_domain_add_entity_update_handler", status);
909 status = st->connection->add_event_handler(st->connection, smi_event_handler,
913 c_ipmi_error(st, "Failed to register smi event handler", status);
914 } /* void domain_connection_change_handler */
916 static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
917 ipmi_domain_id_t domain_id;
920 if (st->connaddr != NULL) {
921 status = ipmi_ip_setup_con(
922 &st->connaddr, &(char *){IPMI_LAN_STD_PORT_STR}, 1, st->authtype,
923 (unsigned int)IPMI_PRIVILEGE_USER, st->username, strlen(st->username),
924 st->password, strlen(st->password), os_handler,
925 /* user data = */ NULL, &st->connection);
927 c_ipmi_error(st, "ipmi_ip_setup_con", status);
931 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
932 /* user data = */ NULL, &st->connection);
934 c_ipmi_error(st, "ipmi_smi_setup_con", status);
939 ipmi_open_option_t opts[] = {
940 {.option = IPMI_OPEN_OPTION_ALL, {.ival = 1}},
941 #ifdef IPMI_OPEN_OPTION_USE_CACHE
942 /* OpenIPMI-2.0.17 and later: Disable SDR cache in local file */
943 {.option = IPMI_OPEN_OPTION_USE_CACHE, {.ival = 0}},
948 * NOTE: Domain names must be unique. There is static `domains_list` common
949 * to all threads inside lib/domain.c and some ops are done by name.
951 status = ipmi_open_domain(
952 st->name, &st->connection, /* num_con = */ 1,
953 domain_connection_change_handler, /* user data = */ (void *)st,
954 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, opts,
955 STATIC_ARRAY_SIZE(opts), &domain_id);
957 c_ipmi_error(st, "ipmi_open_domain", status);
962 } /* int c_ipmi_thread_init */
964 static void *c_ipmi_thread_main(void *user_data) {
965 c_ipmi_instance_t *st = user_data;
967 int status = c_ipmi_thread_init(st);
969 ERROR("ipmi plugin: c_ipmi_thread_init failed.");
974 while (st->active != 0) {
975 struct timeval tv = {1, 0};
976 os_handler->perform_one_op(os_handler, &tv);
979 } /* void *c_ipmi_thread_main */
981 static c_ipmi_instance_t *c_ipmi_init_instance() {
982 c_ipmi_instance_t *st;
984 st = calloc(1, sizeof(*st));
986 ERROR("ipmi plugin: calloc failed.");
990 st->name = strdup("main");
991 if (st->name == NULL) {
993 ERROR("ipmi plugin: strdup() failed.");
997 st->ignorelist = ignorelist_create(/* invert = */ 1);
998 if (st->ignorelist == NULL) {
1001 ERROR("ipmi plugin: ignorelist_create() failed.");
1005 st->sensor_list = NULL;
1006 pthread_mutex_init(&st->sensor_list_lock, /* attr = */ NULL);
1009 st->connaddr = NULL;
1010 st->username = NULL;
1011 st->password = NULL;
1012 st->authtype = IPMI_AUTHTYPE_DEFAULT;
1017 } /* c_ipmi_instance_t *c_ipmi_init_instance */
1019 static void c_ipmi_free_instance(c_ipmi_instance_t *st) {
1023 assert(st->next == NULL);
1027 sfree(st->connaddr);
1028 sfree(st->username);
1029 sfree(st->password);
1031 ignorelist_free(st->ignorelist);
1032 pthread_mutex_destroy(&st->sensor_list_lock);
1034 } /* void c_ipmi_free_instance */
1036 static void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
1037 if (instances == NULL) {
1038 instances = instance;
1042 c_ipmi_instance_t *last = instances;
1044 while (last->next != NULL)
1047 last->next = instance;
1048 } /* void c_ipmi_add_instance */
1050 static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
1052 c_ipmi_instance_t *st = c_ipmi_init_instance();
1056 if (strcasecmp(ci->key, "Instance") == 0)
1057 status = cf_util_get_string(ci, &st->name);
1060 c_ipmi_free_instance(st);
1064 for (int i = 0; i < ci->children_num; i++) {
1065 oconfig_item_t *child = ci->children + i;
1067 if (strcasecmp("Sensor", child->key) == 0)
1068 ignorelist_add(st->ignorelist, child->values[0].value.string);
1069 else if (strcasecmp("IgnoreSelected", child->key) == 0) {
1071 status = cf_util_get_boolean(child, &t);
1074 ignorelist_set_invert(st->ignorelist, /* invert = */ !t);
1075 } else if (strcasecmp("NotifyIPMIConnectionState", child->key) == 0) {
1076 status = cf_util_get_boolean(child, &st->notify_conn);
1077 } else if (strcasecmp("NotifySensorAdd", child->key) == 0) {
1078 status = cf_util_get_boolean(child, &st->notify_add);
1079 } else if (strcasecmp("NotifySensorRemove", child->key) == 0) {
1080 status = cf_util_get_boolean(child, &st->notify_remove);
1081 } else if (strcasecmp("NotifySensorNotPresent", child->key) == 0) {
1082 status = cf_util_get_boolean(child, &st->notify_notpresent);
1083 } else if (strcasecmp("SELEnabled", child->key) == 0) {
1084 status = cf_util_get_boolean(child, &st->sel_enabled);
1085 } else if (strcasecmp("SELClearEvent", child->key) == 0) {
1086 status = cf_util_get_boolean(child, &st->sel_clear_event);
1087 } else if (strcasecmp("Host", child->key) == 0)
1088 status = cf_util_get_string(child, &st->host);
1089 else if (strcasecmp("Address", child->key) == 0)
1090 status = cf_util_get_string(child, &st->connaddr);
1091 else if (strcasecmp("Username", child->key) == 0)
1092 status = cf_util_get_string(child, &st->username);
1093 else if (strcasecmp("Password", child->key) == 0)
1094 status = cf_util_get_string(child, &st->password);
1095 else if (strcasecmp("AuthType", child->key) == 0) {
1097 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
1101 if (strcasecmp("MD5", tmp) == 0)
1102 st->authtype = IPMI_AUTHTYPE_MD5;
1103 else if (strcasecmp("rmcp+", tmp) == 0)
1104 st->authtype = IPMI_AUTHTYPE_RMCP_PLUS;
1106 WARNING("ipmi plugin: The value \"%s\" is not valid for the "
1107 "\"AuthType\" option.",
1110 WARNING("ipmi plugin: Option `%s' not allowed here.", child->key);
1119 c_ipmi_free_instance(st);
1123 c_ipmi_add_instance(st);
1126 } /* int c_ipmi_config_add_instance */
1128 static int c_ipmi_config(oconfig_item_t *ci) {
1129 _Bool have_instance_block = 0;
1131 for (int i = 0; i < ci->children_num; i++) {
1132 oconfig_item_t *child = ci->children + i;
1134 if (strcasecmp("Instance", child->key) == 0) {
1135 int status = c_ipmi_config_add_instance(child);
1139 have_instance_block = 1;
1140 } else if (!have_instance_block) {
1141 /* Non-instance option: Assume legacy configuration (without <Instance />
1142 * blocks) and call c_ipmi_config_add_instance with the <Plugin /> block.
1144 WARNING("ipmi plugin: Legacy configuration found! Please update your "
1146 return c_ipmi_config_add_instance(ci);
1148 WARNING("ipmi plugin: The configuration option "
1149 "\"%s\" is not allowed here. Did you "
1150 "forget to add an <Instance /> block "
1151 "around the configuration?",
1155 } /* for (ci->children) */
1158 } /* int c_ipmi_config */
1160 static int c_ipmi_read(user_data_t *user_data) {
1161 c_ipmi_instance_t *st = user_data->data;
1163 if (st->active == 0) {
1164 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
1168 if (st->connected == 0)
1171 sensor_list_read_all(st);
1173 if (st->init_in_progress > 0)
1174 st->init_in_progress--;
1176 st->init_in_progress = 0;
1179 } /* int c_ipmi_read */
1181 static int c_ipmi_init(void) {
1182 c_ipmi_instance_t *st;
1183 char callback_name[3 * DATA_MAX_NAME_LEN];
1185 if (os_handler != NULL) {
1189 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
1190 if (os_handler == NULL) {
1191 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
1195 os_handler->set_log_handler(os_handler, c_ipmi_log);
1197 if (ipmi_init(os_handler) != 0) {
1198 ERROR("ipmi plugin: ipmi_init() failed.");
1199 os_handler->free_os_handler(os_handler);
1203 if (instances == NULL) {
1204 /* No instances were configured, let's start a default instance. */
1205 st = c_ipmi_init_instance();
1209 c_ipmi_add_instance(st);
1212 /* Don't send `ADD' notifications during startup (~ 1 minute) */
1213 int cycles = 1 + (60 / CDTIME_T_TO_TIME_T(plugin_get_interval()));
1216 while (NULL != st) {
1217 /* The `st->name` is used as "domain name" for ipmi_open_domain().
1218 * That value should be unique, so we do plugin_register_complex_read()
1219 * at first as it checks the uniqueness. */
1220 snprintf(callback_name, sizeof(callback_name), "ipmi/%s", st->name);
1226 int status = plugin_register_complex_read(
1227 /* group = */ "ipmi",
1228 /* name = */ callback_name,
1229 /* callback = */ c_ipmi_read,
1231 /* user_data = */ &ud);
1238 st->init_in_progress = cycles;
1241 status = plugin_thread_create(&st->thread_id, /* attr = */ NULL,
1243 /* user data = */ (void *)st, "ipmi");
1247 st->thread_id = (pthread_t){0};
1249 plugin_unregister_read(callback_name);
1251 ERROR("ipmi plugin: pthread_create failed for `%s`.", callback_name);
1258 } /* int c_ipmi_init */
1260 static int c_ipmi_shutdown(void) {
1261 c_ipmi_instance_t *st = instances;
1264 while (st != NULL) {
1265 c_ipmi_instance_t *next = st->next;
1270 if (!pthread_equal(st->thread_id, (pthread_t){0})) {
1271 pthread_join(st->thread_id, NULL);
1272 st->thread_id = (pthread_t){0};
1275 sensor_list_remove_all(st);
1276 c_ipmi_free_instance(st);
1281 os_handler->free_os_handler(os_handler);
1285 } /* int c_ipmi_shutdown */
1287 void module_register(void) {
1288 plugin_register_complex_config("ipmi", c_ipmi_config);
1289 plugin_register_init("ipmi", c_ipmi_init);
1290 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
1291 } /* void module_register */