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 snprintf(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 snprintf(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 snprintf(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 snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string);
317 else if (entity_id_string != NULL)
318 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
320 sstrncpy(sensor_name, temp, sizeof(sensor_name));
323 sstrncpy(temp, sensor_name, sizeof(temp));
324 sensor_name_ptr = strstr(temp, ").");
325 if (sensor_name_ptr != NULL) {
326 /* If name is something like "foo (123).bar",
327 * change that to "bar (123)".
328 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
329 * `temp' array, which holds a copy of the current `sensor_name'. */
332 /* `sensor_name_ptr' points to ").bar". */
333 sensor_name_ptr[1] = 0;
334 /* `temp' holds "foo (123)\0bar\0". */
335 sensor_name_ptr += 2;
336 /* `sensor_name_ptr' now points to "bar". */
338 sensor_id_ptr = strstr(temp, "(");
339 if (sensor_id_ptr != NULL) {
340 /* `sensor_id_ptr' now points to "(123)". */
341 snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
344 /* else: don't touch sensor_name. */
347 sstrncpy(buffer, sensor_name, buf_len);
350 static const char *sensor_unit_to_type(ipmi_sensor_t *sensor) {
351 static const c_ipmi_db_type_map_t ipmi_db_type_map[] = {
352 {IPMI_UNIT_TYPE_WATTS, "power"}, {IPMI_UNIT_TYPE_CFM, "flow"}};
354 /* check the modifier and rate of the sensor value */
355 if ((ipmi_sensor_get_modifier_unit_use(sensor) != IPMI_MODIFIER_UNIT_NONE) ||
356 (ipmi_sensor_get_rate_unit(sensor) != IPMI_RATE_UNIT_NONE))
359 /* find the db type by using sensor base unit type */
360 enum ipmi_unit_type_e ipmi_type = ipmi_sensor_get_base_unit(sensor);
361 for (size_t i = 0; i < STATIC_ARRAY_SIZE(ipmi_db_type_map); i++)
362 if (ipmi_db_type_map[i].type == ipmi_type)
363 return ipmi_db_type_map[i].type_name;
366 } /* const char* sensor_unit_to_type */
368 static int sensor_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
369 ipmi_sensor_id_t sensor_id;
370 c_ipmi_sensor_list_t *list_item;
371 c_ipmi_sensor_list_t *list_prev;
373 char buffer[DATA_MAX_NAME_LEN] = {0};
374 char *sensor_name_ptr = buffer;
378 sensor_id = ipmi_sensor_convert_to_id(sensor);
379 sensor_get_name(sensor, buffer, sizeof(buffer));
381 DEBUG("ipmi plugin: sensor_list_add: Found sensor `%s` of `%s`,"
383 " Event reading type: %#x"
385 " Event support: %#x",
386 sensor_name_ptr, st->name, ipmi_sensor_get_sensor_type(sensor),
387 ipmi_sensor_get_event_reading_type(sensor),
388 ipmi_sensor_get_sensor_direction(sensor),
389 ipmi_sensor_get_event_support(sensor));
391 /* Both `ignorelist' and `sensor_name_ptr' may be NULL. */
392 if (ignorelist_match(st->ignorelist, sensor_name_ptr) != 0)
395 /* FIXME: Use rate unit or base unit to scale the value */
397 sensor_type = ipmi_sensor_get_sensor_type(sensor);
400 * ipmitool/lib/ipmi_sdr.c sdr_sensor_has_analog_reading() has a notice
401 * about 'Threshold sensors' and 'analog readings'. Discrete sensor may
402 * have analog data, but discrete sensors support is not implemented
405 * ipmi_sensor_id_get_reading() supports only 'Threshold' sensors.
406 * See lib/sensor.c:4842, stand_ipmi_sensor_get_reading() for details.
408 if (!ipmi_sensor_get_is_readable(sensor)) {
409 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
410 "because it isn't readable! Its type: (%#x, %s). ",
411 sensor_name_ptr, st->name, sensor_type,
412 ipmi_sensor_get_sensor_type_string(sensor));
416 if (ipmi_sensor_get_event_reading_type(sensor) !=
417 IPMI_EVENT_READING_TYPE_THRESHOLD) {
418 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
419 "because it is discrete (%#x)! Its type: (%#x, %s). ",
420 sensor_name_ptr, st->name, sensor_type,
421 ipmi_sensor_get_event_reading_type(sensor),
422 ipmi_sensor_get_sensor_type_string(sensor));
426 switch (sensor_type) {
427 case IPMI_SENSOR_TYPE_TEMPERATURE:
428 type = "temperature";
431 case IPMI_SENSOR_TYPE_VOLTAGE:
435 case IPMI_SENSOR_TYPE_CURRENT:
439 case IPMI_SENSOR_TYPE_FAN:
443 case IPMI_SENSOR_TYPE_MEMORY:
448 /* try to get collectd DB type based on sensor base unit type */
449 if ((type = sensor_unit_to_type(sensor)) != NULL)
452 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
453 "because I don't know how to handle its units (%#x, %#x, %#x). "
454 "Sensor type: (%#x, %s). If you need this sensor, please file "
455 "a bug report at http://collectd.org/.",
456 sensor_name_ptr, st->name, ipmi_sensor_get_base_unit(sensor),
457 ipmi_sensor_get_modifier_unit(sensor),
458 ipmi_sensor_get_rate_unit(sensor), sensor_type,
459 ipmi_sensor_get_sensor_type_string(sensor));
462 } /* switch (sensor_type) */
464 pthread_mutex_lock(&st->sensor_list_lock);
467 for (list_item = st->sensor_list; list_item != NULL;
468 list_item = list_item->next) {
469 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
471 list_prev = list_item;
472 } /* for (list_item) */
474 if (list_item != NULL) {
475 pthread_mutex_unlock(&st->sensor_list_lock);
479 list_item = calloc(1, sizeof(*list_item));
480 if (list_item == NULL) {
481 pthread_mutex_unlock(&st->sensor_list_lock);
485 list_item->instance = st;
486 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
488 if (list_prev != NULL)
489 list_prev->next = list_item;
491 st->sensor_list = list_item;
493 /* if sensor provides the percentage value, use "percent" collectd type
494 and add the `percent` to the type instance of the reported value */
495 if (ipmi_sensor_get_percentage(sensor)) {
496 snprintf(list_item->type_instance, sizeof(list_item->type_instance),
497 "percent-%s", sensor_name_ptr);
500 /* use type instance as a name of the sensor */
501 sstrncpy(list_item->type_instance, sensor_name_ptr,
502 sizeof(list_item->type_instance));
505 sstrncpy(list_item->sensor_name, sensor_name_ptr,
506 sizeof(list_item->sensor_name));
507 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
509 pthread_mutex_unlock(&st->sensor_list_lock);
511 if (st->notify_add && (st->init_in_progress == 0)) {
512 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
514 sstrncpy(n.type_instance, list_item->type_instance,
515 sizeof(n.type_instance));
516 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
517 snprintf(n.message, sizeof(n.message), "sensor %s added",
518 list_item->sensor_name);
520 plugin_dispatch_notification(&n);
524 } /* int sensor_list_add */
526 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
527 ipmi_sensor_id_t sensor_id;
528 c_ipmi_sensor_list_t *list_item;
529 c_ipmi_sensor_list_t *list_prev;
531 sensor_id = ipmi_sensor_convert_to_id(sensor);
533 pthread_mutex_lock(&st->sensor_list_lock);
536 for (list_item = st->sensor_list; list_item != NULL;
537 list_item = list_item->next) {
538 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
540 list_prev = list_item;
541 } /* for (list_item) */
543 if (list_item == NULL) {
544 pthread_mutex_unlock(&st->sensor_list_lock);
548 if (list_prev == NULL)
549 st->sensor_list = list_item->next;
551 list_prev->next = list_item->next;
554 list_item->next = NULL;
556 pthread_mutex_unlock(&st->sensor_list_lock);
558 if (st->notify_remove && st->active) {
559 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
561 sstrncpy(n.type_instance, list_item->type_instance,
562 sizeof(n.type_instance));
563 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
564 snprintf(n.message, sizeof(n.message), "sensor %s removed",
565 list_item->sensor_name);
567 plugin_dispatch_notification(&n);
572 } /* int sensor_list_remove */
574 static int sensor_list_read_all(c_ipmi_instance_t *st) {
575 pthread_mutex_lock(&st->sensor_list_lock);
577 for (c_ipmi_sensor_list_t *list_item = st->sensor_list; list_item != NULL;
578 list_item = list_item->next) {
579 DEBUG("ipmi plugin: try read sensor `%s` of `%s`, use: %d",
580 list_item->sensor_name, st->name, list_item->use);
582 /* Reading already initiated */
587 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
588 /* user data = */ (void *)list_item);
589 } /* for (list_item) */
591 pthread_mutex_unlock(&st->sensor_list_lock);
594 } /* int sensor_list_read_all */
596 static int sensor_list_remove_all(c_ipmi_instance_t *st) {
597 c_ipmi_sensor_list_t *list_item;
599 pthread_mutex_lock(&st->sensor_list_lock);
601 list_item = st->sensor_list;
602 st->sensor_list = NULL;
604 pthread_mutex_unlock(&st->sensor_list_lock);
606 while (list_item != NULL) {
607 c_ipmi_sensor_list_t *list_next = list_item->next;
611 list_item = list_next;
612 } /* while (list_item) */
615 } /* int sensor_list_remove_all */
617 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
619 case IPMI_LOWER_NON_CRITICAL:
620 case IPMI_UPPER_NON_CRITICAL:
622 case IPMI_LOWER_CRITICAL:
623 case IPMI_UPPER_CRITICAL:
624 return NOTIF_WARNING;
625 case IPMI_LOWER_NON_RECOVERABLE:
626 case IPMI_UPPER_NON_RECOVERABLE:
627 return NOTIF_FAILURE;
630 } /* switch (severity) */
631 } /* int sensor_convert_threshold_severity */
633 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
634 enum ipmi_event_dir_e dir,
635 ipmi_event_t *event) {
636 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
638 plugin_notification_meta_add_string(n, "entity_name",
639 ipmi_entity_get_entity_id_string(ent));
640 plugin_notification_meta_add_signed_int(n, "entity_id",
641 ipmi_entity_get_entity_id(ent));
642 plugin_notification_meta_add_signed_int(n, "entity_instance",
643 ipmi_entity_get_entity_instance(ent));
644 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
647 plugin_notification_meta_add_signed_int(n, "event_type",
648 ipmi_event_get_type(event));
649 } /* void add_event_sensor_meta_data */
651 static int sensor_threshold_event_handler(
652 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
653 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
654 enum ipmi_value_present_e value_present, unsigned int raw_value,
655 double value, void *cb_data, ipmi_event_t *event) {
657 c_ipmi_instance_t *st = cb_data;
659 /* From the IPMI specification Chapter 2: Events.
660 * If a callback handles the event, then all future callbacks called due to
661 * the event will receive a NULL for the event. So be ready to handle a NULL
662 * event in all your event handlers. A NULL may also be passed to an event
663 * handler if the callback was not due to an event. */
665 return IPMI_EVENT_NOT_HANDLED;
667 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
668 /* offset is a table index and it's represented as enum of strings that are
669 organized in the way - high and low for each threshold severity level */
670 unsigned int offset = (2 * threshold) + high_low;
671 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
672 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
673 const char *event_state =
674 ipmi_get_reading_name(event_type, sensor_type, offset);
675 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
676 if (value_present != IPMI_NO_VALUES_PRESENT)
677 snprintf(n.message, sizeof(n.message),
678 "sensor %s received event: %s, value is %f", n.type_instance,
681 snprintf(n.message, sizeof(n.message),
682 "sensor %s received event: %s, value not provided",
683 n.type_instance, event_state);
685 DEBUG("Threshold event received for sensor %s", n.type_instance);
687 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
688 n.severity = sensor_convert_threshold_severity(threshold);
689 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
691 plugin_notification_meta_add_string(&n, "severity",
692 ipmi_get_threshold_string(threshold));
693 plugin_notification_meta_add_string(&n, "direction",
694 ipmi_get_value_dir_string(high_low));
696 switch (value_present) {
697 case IPMI_BOTH_VALUES_PRESENT:
698 plugin_notification_meta_add_double(&n, "val", value);
699 /* both values present, so fall-through to add raw value too */
700 case IPMI_RAW_VALUE_PRESENT: {
701 char buf[DATA_MAX_NAME_LEN] = {0};
702 snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
703 plugin_notification_meta_add_string(&n, "raw", buf);
707 } /* switch (value_present) */
709 add_event_common_data(&n, sensor, dir, event);
711 plugin_dispatch_notification(&n);
712 plugin_notification_meta_free(n.meta);
714 /* Delete handled ipmi event from the list */
715 if (st->sel_clear_event) {
716 ipmi_event_delete(event, NULL, NULL);
717 return IPMI_EVENT_HANDLED;
720 return IPMI_EVENT_NOT_HANDLED;
721 } /* int sensor_threshold_event_handler */
723 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
724 enum ipmi_event_dir_e dir, int offset,
725 int severity, int prev_severity,
726 void *cb_data, ipmi_event_t *event) {
728 c_ipmi_instance_t *st = cb_data;
730 /* From the IPMI specification Chapter 2: Events.
731 * If a callback handles the event, then all future callbacks called due to
732 * the event will receive a NULL for the event. So be ready to handle a NULL
733 * event in all your event handlers. A NULL may also be passed to an event
734 * handler if the callback was not due to an event. */
736 return IPMI_EVENT_NOT_HANDLED;
738 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
739 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
740 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
741 const char *event_state =
742 ipmi_get_reading_name(event_type, sensor_type, offset);
743 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
744 snprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
745 n.type_instance, event_state);
747 DEBUG("Discrete event received for sensor %s", n.type_instance);
749 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
750 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
752 plugin_notification_meta_add_signed_int(&n, "offset", offset);
755 plugin_notification_meta_add_signed_int(&n, "severity", severity);
757 if (prev_severity != -1)
758 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
760 add_event_common_data(&n, sensor, dir, event);
762 plugin_dispatch_notification(&n);
763 plugin_notification_meta_free(n.meta);
765 /* Delete handled ipmi event from the list */
766 if (st->sel_clear_event) {
767 ipmi_event_delete(event, NULL, NULL);
768 return IPMI_EVENT_HANDLED;
771 return IPMI_EVENT_NOT_HANDLED;
772 } /* int sensor_discrete_event_handler */
774 static int sel_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
775 char sensor_name[DATA_MAX_NAME_LEN] = {0};
778 /* Check if sensor on sel_ignorelist */
779 sensor_get_name(sensor, sensor_name, sizeof(sensor_name));
780 if (ignorelist_match(st->sel_ignorelist, sensor_name) != 0)
783 /* register threshold event if threshold sensor support events */
784 if (ipmi_sensor_get_event_reading_type(sensor) ==
785 IPMI_EVENT_READING_TYPE_THRESHOLD)
786 status = ipmi_sensor_add_threshold_event_handler(
787 sensor, sensor_threshold_event_handler, st);
788 /* register discrete handler if discrete/specific sensor support events */
789 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
790 status = ipmi_sensor_add_discrete_event_handler(
791 sensor, sensor_discrete_event_handler, st);
794 ERROR("Unable to add sensor %s event handler, status: %d", sensor_name,
799 static void sel_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
800 if (ipmi_sensor_get_event_reading_type(sensor) ==
801 IPMI_EVENT_READING_TYPE_THRESHOLD)
802 ipmi_sensor_remove_threshold_event_handler(
803 sensor, sensor_threshold_event_handler, st);
805 ipmi_sensor_remove_discrete_event_handler(
806 sensor, sensor_discrete_event_handler, st);
812 entity_sensor_update_handler(enum ipmi_update_e op,
813 ipmi_entity_t __attribute__((unused)) * entity,
814 ipmi_sensor_t *sensor, void *user_data) {
815 c_ipmi_instance_t *st = user_data;
817 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
818 /* Will check for duplicate entries.. */
819 sensor_list_add(st, sensor);
821 sel_list_add(st, sensor);
822 } else if (op == IPMI_DELETED) {
823 sensor_list_remove(st, sensor);
825 sel_list_remove(st, sensor);
827 } /* void entity_sensor_update_handler */
833 domain_entity_update_handler(enum ipmi_update_e op,
834 ipmi_domain_t __attribute__((unused)) * domain,
835 ipmi_entity_t *entity, void *user_data) {
837 c_ipmi_instance_t *st = user_data;
839 if (op == IPMI_ADDED) {
840 status = ipmi_entity_add_sensor_update_handler(
841 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
843 c_ipmi_error(st, "ipmi_entity_add_sensor_update_handler", status);
845 } else if (op == IPMI_DELETED) {
846 status = ipmi_entity_remove_sensor_update_handler(
847 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
849 c_ipmi_error(st, "ipmi_entity_remove_sensor_update_handler", status);
852 } /* void domain_entity_update_handler */
854 static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi,
855 const ipmi_addr_t __attribute__((unused)) * addr,
856 unsigned int __attribute__((unused)) addr_len,
857 ipmi_event_t *event, void *cb_data) {
858 unsigned int type = ipmi_event_get_type(event);
859 ipmi_domain_t *domain = cb_data;
861 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
864 /* It's not a standard IPMI event. */
867 /* force domain to reread SELs */
868 ipmi_domain_reread_sels(domain, NULL, NULL);
871 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
872 unsigned int conn_num,
873 unsigned int port_num,
877 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
878 "conn_num = %u, port_num = %u, still_connected = %i, "
880 (void *)domain, err, conn_num, port_num, still_connected, user_data);
882 c_ipmi_instance_t *st = user_data;
885 c_ipmi_error(st, "domain_connection_change_handler", err);
887 if (!still_connected) {
889 if (st->notify_conn && st->connected && st->init_in_progress == 0) {
890 notification_t n = c_ipmi_notification_init(st, NOTIF_FAILURE);
892 sstrncpy(n.message, "IPMI connection lost", sizeof(n.plugin));
894 plugin_dispatch_notification(&n);
897 st->connected = false;
901 if (st->notify_conn && !st->connected && st->init_in_progress == 0) {
902 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
904 sstrncpy(n.message, "IPMI connection restored", sizeof(n.plugin));
906 plugin_dispatch_notification(&n);
909 st->connected = true;
911 int status = ipmi_domain_add_entity_update_handler(
912 domain, domain_entity_update_handler, /* user data = */ st);
914 c_ipmi_error(st, "ipmi_domain_add_entity_update_handler", status);
917 status = st->connection->add_event_handler(st->connection, smi_event_handler,
921 c_ipmi_error(st, "Failed to register smi event handler", status);
922 } /* void domain_connection_change_handler */
924 static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
925 ipmi_domain_id_t domain_id;
928 if (st->connaddr != NULL) {
929 status = ipmi_ip_setup_con(
930 &st->connaddr, &(char *){IPMI_LAN_STD_PORT_STR}, 1, st->authtype,
931 (unsigned int)IPMI_PRIVILEGE_USER, st->username, strlen(st->username),
932 st->password, strlen(st->password), os_handler,
933 /* user data = */ NULL, &st->connection);
935 c_ipmi_error(st, "ipmi_ip_setup_con", status);
939 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
940 /* user data = */ NULL, &st->connection);
942 c_ipmi_error(st, "ipmi_smi_setup_con", status);
947 ipmi_open_option_t opts[] = {
948 {.option = IPMI_OPEN_OPTION_ALL, {.ival = 1}},
949 #ifdef IPMI_OPEN_OPTION_USE_CACHE
950 /* OpenIPMI-2.0.17 and later: Disable SDR cache in local file */
951 {.option = IPMI_OPEN_OPTION_USE_CACHE, {.ival = 0}},
956 * NOTE: Domain names must be unique. There is static `domains_list` common
957 * to all threads inside lib/domain.c and some ops are done by name.
959 status = ipmi_open_domain(
960 st->name, &st->connection, /* num_con = */ 1,
961 domain_connection_change_handler, /* user data = */ (void *)st,
962 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, opts,
963 STATIC_ARRAY_SIZE(opts), &domain_id);
965 c_ipmi_error(st, "ipmi_open_domain", status);
970 } /* int c_ipmi_thread_init */
972 static void *c_ipmi_thread_main(void *user_data) {
973 c_ipmi_instance_t *st = user_data;
975 int status = c_ipmi_thread_init(st);
977 ERROR("ipmi plugin: c_ipmi_thread_init failed.");
983 struct timeval tv = {1, 0};
984 os_handler->perform_one_op(os_handler, &tv);
987 } /* void *c_ipmi_thread_main */
989 static c_ipmi_instance_t *c_ipmi_init_instance() {
990 c_ipmi_instance_t *st;
992 st = calloc(1, sizeof(*st));
994 ERROR("ipmi plugin: calloc failed.");
998 st->name = strdup("main");
999 if (st->name == NULL) {
1001 ERROR("ipmi plugin: strdup() failed.");
1005 st->ignorelist = ignorelist_create(/* invert = */ 1);
1006 if (st->ignorelist == NULL) {
1009 ERROR("ipmi plugin: ignorelist_create() failed.");
1013 st->sel_ignorelist = ignorelist_create(/* invert = */ 1);
1014 if (st->sel_ignorelist == NULL) {
1015 ignorelist_free(st->ignorelist);
1018 ERROR("ipmi plugin: SEL ignorelist_create() failed.");
1022 st->sensor_list = NULL;
1023 pthread_mutex_init(&st->sensor_list_lock, /* attr = */ NULL);
1026 st->connaddr = NULL;
1027 st->username = NULL;
1028 st->password = NULL;
1029 st->authtype = IPMI_AUTHTYPE_DEFAULT;
1034 } /* c_ipmi_instance_t *c_ipmi_init_instance */
1036 static void c_ipmi_free_instance(c_ipmi_instance_t *st) {
1040 assert(st->next == NULL);
1044 sfree(st->connaddr);
1045 sfree(st->username);
1046 sfree(st->password);
1048 ignorelist_free(st->sel_ignorelist);
1049 ignorelist_free(st->ignorelist);
1050 pthread_mutex_destroy(&st->sensor_list_lock);
1052 } /* void c_ipmi_free_instance */
1054 static void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
1055 if (instances == NULL) {
1056 instances = instance;
1060 c_ipmi_instance_t *last = instances;
1062 while (last->next != NULL)
1065 last->next = instance;
1066 } /* void c_ipmi_add_instance */
1068 static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
1070 c_ipmi_instance_t *st = c_ipmi_init_instance();
1074 if (strcasecmp(ci->key, "Instance") == 0)
1075 status = cf_util_get_string(ci, &st->name);
1078 c_ipmi_free_instance(st);
1082 for (int i = 0; i < ci->children_num; i++) {
1083 oconfig_item_t *child = ci->children + i;
1085 if (strcasecmp("Sensor", child->key) == 0) {
1087 status = cf_util_get_string(child, &value);
1090 ignorelist_add(st->ignorelist, value);
1092 } else if (strcasecmp("IgnoreSelected", child->key) == 0) {
1094 status = cf_util_get_boolean(child, &t);
1097 ignorelist_set_invert(st->ignorelist, /* invert = */ !t);
1098 } else if (strcasecmp("NotifyIPMIConnectionState", child->key) == 0) {
1099 status = cf_util_get_boolean(child, &st->notify_conn);
1100 } else if (strcasecmp("NotifySensorAdd", child->key) == 0) {
1101 status = cf_util_get_boolean(child, &st->notify_add);
1102 } else if (strcasecmp("NotifySensorRemove", child->key) == 0) {
1103 status = cf_util_get_boolean(child, &st->notify_remove);
1104 } else if (strcasecmp("NotifySensorNotPresent", child->key) == 0) {
1105 status = cf_util_get_boolean(child, &st->notify_notpresent);
1106 } else if (strcasecmp("SELSensor", child->key) == 0) {
1108 status = cf_util_get_string(child, &value);
1111 ignorelist_add(st->sel_ignorelist, value);
1113 } else if (strcasecmp("SELIgnoreSelected", child->key) == 0) {
1115 status = cf_util_get_boolean(child, &t);
1118 ignorelist_set_invert(st->sel_ignorelist, /* invert = */ !t);
1119 } else if (strcasecmp("SELEnabled", child->key) == 0) {
1120 status = cf_util_get_boolean(child, &st->sel_enabled);
1121 } else if (strcasecmp("SELClearEvent", child->key) == 0) {
1122 status = cf_util_get_boolean(child, &st->sel_clear_event);
1123 } else if (strcasecmp("Host", child->key) == 0)
1124 status = cf_util_get_string(child, &st->host);
1125 else if (strcasecmp("Address", child->key) == 0)
1126 status = cf_util_get_string(child, &st->connaddr);
1127 else if (strcasecmp("Username", child->key) == 0)
1128 status = cf_util_get_string(child, &st->username);
1129 else if (strcasecmp("Password", child->key) == 0)
1130 status = cf_util_get_string(child, &st->password);
1131 else if (strcasecmp("AuthType", child->key) == 0) {
1133 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
1137 if (strcasecmp("MD5", tmp) == 0)
1138 st->authtype = IPMI_AUTHTYPE_MD5;
1139 else if (strcasecmp("rmcp+", tmp) == 0)
1140 st->authtype = IPMI_AUTHTYPE_RMCP_PLUS;
1142 WARNING("ipmi plugin: The value \"%s\" is not valid for the "
1143 "\"AuthType\" option.",
1146 WARNING("ipmi plugin: Option `%s' not allowed here.", child->key);
1155 c_ipmi_free_instance(st);
1159 c_ipmi_add_instance(st);
1162 } /* int c_ipmi_config_add_instance */
1164 static int c_ipmi_config(oconfig_item_t *ci) {
1165 bool have_instance_block = 0;
1167 for (int i = 0; i < ci->children_num; i++) {
1168 oconfig_item_t *child = ci->children + i;
1170 if (strcasecmp("Instance", child->key) == 0) {
1171 int status = c_ipmi_config_add_instance(child);
1175 have_instance_block = 1;
1176 } else if (!have_instance_block) {
1177 /* Non-instance option: Assume legacy configuration (without <Instance />
1178 * blocks) and call c_ipmi_config_add_instance with the <Plugin /> block.
1180 WARNING("ipmi plugin: Legacy configuration found! Please update your "
1182 return c_ipmi_config_add_instance(ci);
1184 WARNING("ipmi plugin: The configuration option "
1185 "\"%s\" is not allowed here. Did you "
1186 "forget to add an <Instance /> block "
1187 "around the configuration?",
1191 } /* for (ci->children) */
1194 } /* int c_ipmi_config */
1196 static int c_ipmi_read(user_data_t *user_data) {
1197 c_ipmi_instance_t *st = user_data->data;
1199 if (st->active == false) {
1200 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
1204 if (st->connected == false)
1207 sensor_list_read_all(st);
1209 if (st->init_in_progress > 0)
1210 st->init_in_progress--;
1212 st->init_in_progress = 0;
1215 } /* int c_ipmi_read */
1217 static int c_ipmi_init(void) {
1218 c_ipmi_instance_t *st;
1219 char callback_name[3 * DATA_MAX_NAME_LEN];
1221 if (os_handler != NULL) {
1225 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
1226 if (os_handler == NULL) {
1227 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
1231 os_handler->set_log_handler(os_handler, c_ipmi_log);
1233 if (ipmi_init(os_handler) != 0) {
1234 ERROR("ipmi plugin: ipmi_init() failed.");
1235 os_handler->free_os_handler(os_handler);
1239 if (instances == NULL) {
1240 /* No instances were configured, let's start a default instance. */
1241 st = c_ipmi_init_instance();
1245 c_ipmi_add_instance(st);
1248 /* Don't send `ADD' notifications during startup (~ 1 minute) */
1249 int cycles = 1 + (int)(TIME_T_TO_CDTIME_T(60) / plugin_get_interval());
1252 while (NULL != st) {
1253 /* The `st->name` is used as "domain name" for ipmi_open_domain().
1254 * That value should be unique, so we do plugin_register_complex_read()
1255 * at first as it checks the uniqueness. */
1256 snprintf(callback_name, sizeof(callback_name), "ipmi/%s", st->name);
1262 int status = plugin_register_complex_read(
1263 /* group = */ "ipmi",
1264 /* name = */ callback_name,
1265 /* callback = */ c_ipmi_read,
1267 /* user_data = */ &ud);
1274 st->init_in_progress = cycles;
1277 status = plugin_thread_create(&st->thread_id, /* attr = */ NULL,
1279 /* user data = */ (void *)st, "ipmi");
1283 st->thread_id = (pthread_t){0};
1285 plugin_unregister_read(callback_name);
1287 ERROR("ipmi plugin: pthread_create failed for `%s`.", callback_name);
1294 } /* int c_ipmi_init */
1296 static int c_ipmi_shutdown(void) {
1297 c_ipmi_instance_t *st = instances;
1300 while (st != NULL) {
1301 c_ipmi_instance_t *next = st->next;
1306 if (!pthread_equal(st->thread_id, (pthread_t){0})) {
1307 pthread_join(st->thread_id, NULL);
1308 st->thread_id = (pthread_t){0};
1311 sensor_list_remove_all(st);
1312 c_ipmi_free_instance(st);
1317 os_handler->free_os_handler(os_handler);
1321 } /* int c_ipmi_shutdown */
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 */