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 int sensor_not_present;
83 c_ipmi_sensor_list_t *next;
84 c_ipmi_instance_t *instance;
89 * Module global variables
91 static os_handler_t *os_handler = NULL;
92 static c_ipmi_instance_t *instances = NULL;
95 * Misc private functions
97 static void c_ipmi_error(c_ipmi_instance_t *st, const char *func, int status) {
98 char errbuf[ERR_BUF_SIZE] = {0};
100 if (IPMI_IS_OS_ERR(status) || IPMI_IS_RMCPP_ERR(status) ||
101 IPMI_IS_IPMI_ERR(status)) {
102 ipmi_get_error_string(status, errbuf, sizeof(errbuf));
105 if (errbuf[0] == 0) {
106 snprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
108 errbuf[sizeof(errbuf) - 1] = 0;
110 ERROR("ipmi plugin: %s failed for `%s`: %s", func, st->name, errbuf);
111 } /* void c_ipmi_error */
113 static void c_ipmi_log(os_handler_t *handler, const char *format,
114 enum ipmi_log_type_e log_type, va_list ap) {
115 char msg[ERR_BUF_SIZE];
117 vsnprintf(msg, sizeof(msg), format, ap);
121 INFO("ipmi plugin: %s", msg);
123 case IPMI_LOG_WARNING:
124 NOTICE("ipmi plugin: %s", msg);
126 case IPMI_LOG_SEVERE:
127 WARNING("ipmi plugin: %s", msg);
130 ERROR("ipmi plugin: %s", msg);
132 case IPMI_LOG_ERR_INFO:
133 ERROR("ipmi plugin: %s", msg);
136 case IPMI_LOG_DEBUG_START:
138 DEBUG("ipmi plugin: %s", msg);
140 case IPMI_LOG_DEBUG_CONT:
141 case IPMI_LOG_DEBUG_END:
145 case IPMI_LOG_DEBUG_START:
147 case IPMI_LOG_DEBUG_CONT:
148 case IPMI_LOG_DEBUG_END:
152 } /* void c_ipmi_log */
154 static notification_t c_ipmi_notification_init(c_ipmi_instance_t const *st,
156 notification_t n = {severity, cdtime(), "", "", "ipmi", "", "", "", NULL};
158 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
160 } /* notification_t c_ipmi_notification_init */
165 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
166 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor);
168 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
169 enum ipmi_value_present_e value_present,
170 unsigned int __attribute__((unused)) raw_value,
171 double value, ipmi_states_t *states,
173 value_list_t vl = VALUE_LIST_INIT;
175 c_ipmi_sensor_list_t *list_item = user_data;
176 c_ipmi_instance_t *st = list_item->instance;
181 if (IPMI_IS_IPMI_ERR(err) &&
182 IPMI_GET_IPMI_ERR(err) == IPMI_NOT_PRESENT_CC) {
183 if (list_item->sensor_not_present == 0) {
184 list_item->sensor_not_present = 1;
186 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` "
188 list_item->sensor_name, st->name);
190 if (st->notify_notpresent) {
191 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
193 sstrncpy(n.type_instance, list_item->sensor_name,
194 sizeof(n.type_instance));
195 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
196 snprintf(n.message, sizeof(n.message), "sensor %s not present",
197 list_item->sensor_name);
199 plugin_dispatch_notification(&n);
202 } else if (IPMI_IS_IPMI_ERR(err) &&
203 IPMI_GET_IPMI_ERR(err) ==
204 IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
205 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` not ready.",
206 list_item->sensor_name, st->name);
207 } else if (IPMI_IS_IPMI_ERR(err) &&
208 IPMI_GET_IPMI_ERR(err) == IPMI_TIMEOUT_CC) {
209 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` timed out.",
210 list_item->sensor_name, st->name);
212 char errbuf[ERR_BUF_SIZE] = {0};
213 ipmi_get_error_string(err, errbuf, sizeof(errbuf) - 1);
215 if (IPMI_IS_IPMI_ERR(err))
216 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
218 list_item->sensor_name, st->name, errbuf);
219 else if (IPMI_IS_OS_ERR(err))
220 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
222 list_item->sensor_name, st->name, errbuf, IPMI_GET_OS_ERR(err));
223 else if (IPMI_IS_RMCPP_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_SOL_ERR(err))
228 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
230 list_item->sensor_name, st->name, errbuf, IPMI_GET_SOL_ERR(err));
232 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed "
233 "with error %#x. of class %#x",
234 list_item->sensor_name, st->name, err & 0xff, err & 0xffffff00);
237 } else if (list_item->sensor_not_present == 1) {
238 list_item->sensor_not_present = 0;
240 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` present.",
241 list_item->sensor_name, st->name);
243 if (st->notify_notpresent) {
244 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
246 sstrncpy(n.type_instance, list_item->sensor_name,
247 sizeof(n.type_instance));
248 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
249 snprintf(n.message, sizeof(n.message), "sensor %s present",
250 list_item->sensor_name);
252 plugin_dispatch_notification(&n);
256 if (value_present != IPMI_BOTH_VALUES_PRESENT) {
257 INFO("ipmi plugin: sensor_read_handler: Removing sensor `%s` of `%s`, "
258 "because it provides %s. If you need this sensor, "
259 "please file a bug report.",
260 list_item->sensor_name, st->name,
261 (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
263 sensor_list_remove(st, sensor);
267 if (!ipmi_is_sensor_scanning_enabled(states)) {
268 DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
269 "it is in 'scanning disabled' state.",
270 list_item->sensor_name, st->name);
274 if (ipmi_is_initial_update_in_progress(states)) {
275 DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
276 "it is in 'initial update in progress' state.",
277 list_item->sensor_name, st->name);
281 vl.values = &(value_t){.gauge = value};
284 if (st->host != NULL)
285 sstrncpy(vl.host, st->host, sizeof(vl.host));
286 sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
287 sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
288 sstrncpy(vl.type_instance, list_item->sensor_name, sizeof(vl.type_instance));
290 plugin_dispatch_values(&vl);
291 } /* void sensor_read_handler */
293 static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) {
294 char temp[DATA_MAX_NAME_LEN] = {0};
295 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
296 const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
297 char sensor_name[DATA_MAX_NAME_LEN] = "";
298 char *sensor_name_ptr;
300 if ((buffer == NULL) || (buf_len == 0))
303 ipmi_sensor_get_name(sensor, temp, sizeof(temp));
304 temp[sizeof(temp) - 1] = 0;
306 if (entity_id_string != NULL && strlen(temp))
307 snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string);
308 else if (entity_id_string != NULL)
309 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
311 sstrncpy(sensor_name, temp, sizeof(sensor_name));
314 sstrncpy(temp, sensor_name, sizeof(temp));
315 sensor_name_ptr = strstr(temp, ").");
316 if (sensor_name_ptr != NULL) {
317 /* If name is something like "foo (123).bar",
318 * change that to "bar (123)".
319 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
320 * `temp' array, which holds a copy of the current `sensor_name'. */
323 /* `sensor_name_ptr' points to ").bar". */
324 sensor_name_ptr[1] = 0;
325 /* `temp' holds "foo (123)\0bar\0". */
326 sensor_name_ptr += 2;
327 /* `sensor_name_ptr' now points to "bar". */
329 sensor_id_ptr = strstr(temp, "(");
330 if (sensor_id_ptr != NULL) {
331 /* `sensor_id_ptr' now points to "(123)". */
332 snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
335 /* else: don't touch sensor_name. */
338 sstrncpy(buffer, sensor_name, buf_len);
341 static int sensor_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
342 ipmi_sensor_id_t sensor_id;
343 c_ipmi_sensor_list_t *list_item;
344 c_ipmi_sensor_list_t *list_prev;
346 char buffer[DATA_MAX_NAME_LEN] = {0};
347 char *sensor_name_ptr = buffer;
351 sensor_id = ipmi_sensor_convert_to_id(sensor);
352 sensor_get_name(sensor, buffer, sizeof(buffer));
354 DEBUG("ipmi plugin: sensor_list_add: Found sensor `%s` of `%s`,"
356 " Event reading type: %#x"
358 " Event support: %#x",
359 sensor_name_ptr, st->name, ipmi_sensor_get_sensor_type(sensor),
360 ipmi_sensor_get_event_reading_type(sensor),
361 ipmi_sensor_get_sensor_direction(sensor),
362 ipmi_sensor_get_event_support(sensor));
364 /* Both `ignorelist' and `sensor_name_ptr' may be NULL. */
365 if (ignorelist_match(st->ignorelist, sensor_name_ptr) != 0)
368 /* FIXME: Use rate unit or base unit to scale the value */
370 sensor_type = ipmi_sensor_get_sensor_type(sensor);
373 * ipmitool/lib/ipmi_sdr.c sdr_sensor_has_analog_reading() has a notice
374 * about 'Threshold sensors' and 'analog readings'. Discrete sensor may
375 * have analog data, but discrete sensors support is not implemented
378 * ipmi_sensor_id_get_reading() supports only 'Threshold' sensors.
379 * See lib/sensor.c:4842, stand_ipmi_sensor_get_reading() for details.
381 if (!ipmi_sensor_get_is_readable(sensor)) {
382 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
383 "because it don't readable! Its type: (%#x, %s). ",
384 sensor_name_ptr, st->name, sensor_type,
385 ipmi_sensor_get_sensor_type_string(sensor));
389 if (ipmi_sensor_get_event_reading_type(sensor) !=
390 IPMI_EVENT_READING_TYPE_THRESHOLD) {
391 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
392 "because it is discrete (%#x)! Its type: (%#x, %s). ",
393 sensor_name_ptr, st->name, sensor_type,
394 ipmi_sensor_get_event_reading_type(sensor),
395 ipmi_sensor_get_sensor_type_string(sensor));
399 switch (sensor_type) {
400 case IPMI_SENSOR_TYPE_TEMPERATURE:
401 type = "temperature";
404 case IPMI_SENSOR_TYPE_VOLTAGE:
408 case IPMI_SENSOR_TYPE_CURRENT:
412 case IPMI_SENSOR_TYPE_FAN:
417 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
418 "because I don't know how to handle its type (%#x, %s). "
419 "If you need this sensor, please file a bug report.",
420 sensor_name_ptr, st->name, sensor_type,
421 ipmi_sensor_get_sensor_type_string(sensor));
424 } /* switch (sensor_type) */
426 pthread_mutex_lock(&st->sensor_list_lock);
429 for (list_item = st->sensor_list; list_item != NULL;
430 list_item = list_item->next) {
431 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
433 list_prev = list_item;
434 } /* for (list_item) */
436 if (list_item != NULL) {
437 pthread_mutex_unlock(&st->sensor_list_lock);
441 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
442 if (list_item == NULL) {
443 pthread_mutex_unlock(&st->sensor_list_lock);
447 list_item->instance = st;
448 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
450 if (list_prev != NULL)
451 list_prev->next = list_item;
453 st->sensor_list = list_item;
455 sstrncpy(list_item->sensor_name, sensor_name_ptr,
456 sizeof(list_item->sensor_name));
457 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
459 pthread_mutex_unlock(&st->sensor_list_lock);
461 if (st->notify_add && (st->init_in_progress == 0)) {
462 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
464 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
465 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
466 snprintf(n.message, sizeof(n.message), "sensor %s added",
467 list_item->sensor_name);
469 plugin_dispatch_notification(&n);
473 } /* int sensor_list_add */
475 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
476 ipmi_sensor_id_t sensor_id;
477 c_ipmi_sensor_list_t *list_item;
478 c_ipmi_sensor_list_t *list_prev;
480 sensor_id = ipmi_sensor_convert_to_id(sensor);
482 pthread_mutex_lock(&st->sensor_list_lock);
485 for (list_item = st->sensor_list; list_item != NULL;
486 list_item = list_item->next) {
487 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
489 list_prev = list_item;
490 } /* for (list_item) */
492 if (list_item == NULL) {
493 pthread_mutex_unlock(&st->sensor_list_lock);
497 if (list_prev == NULL)
498 st->sensor_list = list_item->next;
500 list_prev->next = list_item->next;
503 list_item->next = NULL;
505 pthread_mutex_unlock(&st->sensor_list_lock);
507 if (st->notify_remove && st->active) {
508 notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
510 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
511 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
512 snprintf(n.message, sizeof(n.message), "sensor %s removed",
513 list_item->sensor_name);
515 plugin_dispatch_notification(&n);
520 } /* int sensor_list_remove */
522 static int sensor_list_read_all(c_ipmi_instance_t *st) {
523 pthread_mutex_lock(&st->sensor_list_lock);
525 for (c_ipmi_sensor_list_t *list_item = st->sensor_list; list_item != NULL;
526 list_item = list_item->next) {
527 DEBUG("ipmi plugin: try read sensor `%s` of `%s`, use: %d",
528 list_item->sensor_name, st->name, list_item->use);
530 /* Reading already initiated */
535 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
536 /* user data = */ (void *)list_item);
537 } /* for (list_item) */
539 pthread_mutex_unlock(&st->sensor_list_lock);
542 } /* int sensor_list_read_all */
544 static int sensor_list_remove_all(c_ipmi_instance_t *st) {
545 c_ipmi_sensor_list_t *list_item;
547 pthread_mutex_lock(&st->sensor_list_lock);
549 list_item = st->sensor_list;
550 st->sensor_list = NULL;
552 pthread_mutex_unlock(&st->sensor_list_lock);
554 while (list_item != NULL) {
555 c_ipmi_sensor_list_t *list_next = list_item->next;
559 list_item = list_next;
560 } /* while (list_item) */
563 } /* int sensor_list_remove_all */
565 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
567 case IPMI_LOWER_NON_CRITICAL:
568 case IPMI_UPPER_NON_CRITICAL:
570 case IPMI_LOWER_CRITICAL:
571 case IPMI_UPPER_CRITICAL:
572 return NOTIF_WARNING;
573 case IPMI_LOWER_NON_RECOVERABLE:
574 case IPMI_UPPER_NON_RECOVERABLE:
575 return NOTIF_FAILURE;
578 } /* switch (severity) */
579 } /* int sensor_convert_threshold_severity */
581 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
582 enum ipmi_event_dir_e dir,
583 ipmi_event_t *event) {
584 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
586 plugin_notification_meta_add_string(n, "entity_name",
587 ipmi_entity_get_entity_id_string(ent));
588 plugin_notification_meta_add_signed_int(n, "entity_id",
589 ipmi_entity_get_entity_id(ent));
590 plugin_notification_meta_add_signed_int(n, "entity_instance",
591 ipmi_entity_get_entity_instance(ent));
592 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
595 plugin_notification_meta_add_signed_int(n, "event_type",
596 ipmi_event_get_type(event));
597 } /* void add_event_sensor_meta_data */
599 static int sensor_threshold_event_handler(
600 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
601 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
602 enum ipmi_value_present_e value_present, unsigned int raw_value,
603 double value, void *cb_data, ipmi_event_t *event) {
605 c_ipmi_instance_t *st = cb_data;
607 /* From the IPMI specification Chapter 2: Events.
608 * If a callback handles the event, then all future callbacks called due to
609 * the event will receive a NULL for the event. So be ready to handle a NULL
610 * event in all your event handlers. A NULL may also be passed to an event
611 * handler if the callback was not due to an event. */
613 return IPMI_EVENT_NOT_HANDLED;
615 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
616 /* offset is a table index and it's represented as enum of strings that are
617 organized in the way - high and low for each threshold severity level */
618 unsigned int offset = (2 * threshold) + high_low;
619 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
620 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
621 const char *event_state =
622 ipmi_get_reading_name(event_type, sensor_type, offset);
623 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
624 if (value_present != IPMI_NO_VALUES_PRESENT)
625 snprintf(n.message, sizeof(n.message),
626 "sensor %s received event: %s, value is %f", n.type_instance,
629 snprintf(n.message, sizeof(n.message),
630 "sensor %s received event: %s, value not provided",
631 n.type_instance, event_state);
633 DEBUG("Threshold event received for sensor %s", n.type_instance);
635 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
636 n.severity = sensor_convert_threshold_severity(threshold);
637 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
639 plugin_notification_meta_add_string(&n, "severity",
640 ipmi_get_threshold_string(threshold));
641 plugin_notification_meta_add_string(&n, "direction",
642 ipmi_get_value_dir_string(high_low));
644 switch (value_present) {
645 case IPMI_BOTH_VALUES_PRESENT:
646 plugin_notification_meta_add_double(&n, "val", value);
647 /* both values present, so fall-through to add raw value too */
648 case IPMI_RAW_VALUE_PRESENT: {
649 char buf[DATA_MAX_NAME_LEN] = {0};
650 snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
651 plugin_notification_meta_add_string(&n, "raw", buf);
655 } /* switch (value_present) */
657 add_event_common_data(&n, sensor, dir, event);
659 plugin_dispatch_notification(&n);
660 plugin_notification_meta_free(n.meta);
662 /* Delete handled ipmi event from the list */
663 if (st->sel_clear_event) {
664 ipmi_event_delete(event, NULL, NULL);
665 return IPMI_EVENT_HANDLED;
668 return IPMI_EVENT_NOT_HANDLED;
669 } /* int sensor_threshold_event_handler */
671 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
672 enum ipmi_event_dir_e dir, int offset,
673 int severity, int prev_severity,
674 void *cb_data, ipmi_event_t *event) {
676 c_ipmi_instance_t *st = cb_data;
678 /* From the IPMI specification Chapter 2: Events.
679 * If a callback handles the event, then all future callbacks called due to
680 * the event will receive a NULL for the event. So be ready to handle a NULL
681 * event in all your event handlers. A NULL may also be passed to an event
682 * handler if the callback was not due to an event. */
684 return IPMI_EVENT_NOT_HANDLED;
686 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
687 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
688 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
689 const char *event_state =
690 ipmi_get_reading_name(event_type, sensor_type, offset);
691 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
692 snprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
693 n.type_instance, event_state);
695 DEBUG("Discrete event received for sensor %s", n.type_instance);
697 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
698 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
700 plugin_notification_meta_add_signed_int(&n, "offset", offset);
703 plugin_notification_meta_add_signed_int(&n, "severity", severity);
705 if (prev_severity != -1)
706 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
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_discrete_event_handler */
726 entity_sensor_update_handler(enum ipmi_update_e op,
727 ipmi_entity_t __attribute__((unused)) * entity,
728 ipmi_sensor_t *sensor, void *user_data) {
729 c_ipmi_instance_t *st = user_data;
731 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
732 /* Will check for duplicate entries.. */
733 sensor_list_add(st, sensor);
735 if (st->sel_enabled) {
737 /* register threshold event if threshold sensor support events */
738 if ((ipmi_sensor_get_event_reading_type(sensor) ==
739 IPMI_EVENT_READING_TYPE_THRESHOLD) &&
740 (ipmi_sensor_get_threshold_access(sensor) !=
741 IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
742 status = ipmi_sensor_add_threshold_event_handler(
743 sensor, sensor_threshold_event_handler, st);
744 /* register discrete handler if discrete/specific sensor support events */
745 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
746 status = ipmi_sensor_add_discrete_event_handler(
747 sensor, sensor_discrete_event_handler, st);
750 char buf[DATA_MAX_NAME_LEN] = {0};
751 sensor_get_name(sensor, buf, sizeof(buf));
752 ERROR("Unable to add sensor %s event handler, status: %d", buf, status);
755 } else if (op == IPMI_DELETED) {
756 sensor_list_remove(st, sensor);
758 if (st->sel_enabled) {
759 if (ipmi_sensor_get_event_reading_type(sensor) ==
760 IPMI_EVENT_READING_TYPE_THRESHOLD)
761 ipmi_sensor_remove_threshold_event_handler(
762 sensor, sensor_threshold_event_handler, st);
764 ipmi_sensor_remove_discrete_event_handler(
765 sensor, sensor_discrete_event_handler, st);
768 } /* void entity_sensor_update_handler */
774 domain_entity_update_handler(enum ipmi_update_e op,
775 ipmi_domain_t __attribute__((unused)) * domain,
776 ipmi_entity_t *entity, void *user_data) {
778 c_ipmi_instance_t *st = user_data;
780 if (op == IPMI_ADDED) {
781 status = ipmi_entity_add_sensor_update_handler(
782 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
784 c_ipmi_error(st, "ipmi_entity_add_sensor_update_handler", status);
786 } else if (op == IPMI_DELETED) {
787 status = ipmi_entity_remove_sensor_update_handler(
788 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
790 c_ipmi_error(st, "ipmi_entity_remove_sensor_update_handler", status);
793 } /* void domain_entity_update_handler */
795 static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi,
796 const ipmi_addr_t __attribute__((unused)) * addr,
797 unsigned int __attribute__((unused)) addr_len,
798 ipmi_event_t *event, void *cb_data) {
799 unsigned int type = ipmi_event_get_type(event);
800 ipmi_domain_t *domain = cb_data;
802 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
805 /* It's not a standard IPMI event. */
808 /* force domain to reread SELs */
809 ipmi_domain_reread_sels(domain, NULL, NULL);
812 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
813 unsigned int conn_num,
814 unsigned int port_num,
818 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
819 "conn_num = %u, port_num = %u, still_connected = %i, "
821 (void *)domain, err, conn_num, port_num, still_connected, user_data);
823 c_ipmi_instance_t *st = user_data;
826 c_ipmi_error(st, "domain_connection_change_handler", err);
828 if (!still_connected) {
830 if (st->notify_conn && st->connected && st->init_in_progress == 0) {
831 notification_t n = c_ipmi_notification_init(st, NOTIF_FAILURE);
833 sstrncpy(n.message, "IPMI connection lost", sizeof(n.plugin));
835 plugin_dispatch_notification(&n);
842 if (st->notify_conn && !st->connected && st->init_in_progress == 0) {
843 notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
845 sstrncpy(n.message, "IPMI connection restored", sizeof(n.plugin));
847 plugin_dispatch_notification(&n);
852 int status = ipmi_domain_add_entity_update_handler(
853 domain, domain_entity_update_handler, /* user data = */ st);
855 c_ipmi_error(st, "ipmi_domain_add_entity_update_handler", status);
858 status = st->connection->add_event_handler(st->connection, smi_event_handler,
862 c_ipmi_error(st, "Failed to register smi event handler", status);
863 } /* void domain_connection_change_handler */
865 static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
866 ipmi_domain_id_t domain_id;
869 if (st->connaddr != NULL) {
870 status = ipmi_ip_setup_con(
871 &st->connaddr, &(char *){IPMI_LAN_STD_PORT_STR}, 1, st->authtype,
872 (unsigned int)IPMI_PRIVILEGE_USER, st->username, strlen(st->username),
873 st->password, strlen(st->password), os_handler,
874 /* user data = */ NULL, &st->connection);
876 c_ipmi_error(st, "ipmi_ip_setup_con", status);
880 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
881 /* user data = */ NULL, &st->connection);
883 c_ipmi_error(st, "ipmi_smi_setup_con", status);
888 ipmi_open_option_t opts[] = {
889 {.option = IPMI_OPEN_OPTION_ALL, {.ival = 1}},
890 #ifdef IPMI_OPEN_OPTION_USE_CACHE
891 /* OpenIPMI-2.0.17 and later: Disable SDR cache in local file */
892 {.option = IPMI_OPEN_OPTION_USE_CACHE, {.ival = 0}},
897 * NOTE: Domain names must be unique. There is static `domains_list` common
898 * to all threads inside lib/domain.c and some ops are done by name.
900 status = ipmi_open_domain(
901 st->name, &st->connection, /* num_con = */ 1,
902 domain_connection_change_handler, /* user data = */ (void *)st,
903 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, opts,
904 STATIC_ARRAY_SIZE(opts), &domain_id);
906 c_ipmi_error(st, "ipmi_open_domain", status);
911 } /* int c_ipmi_thread_init */
913 static void *c_ipmi_thread_main(void *user_data) {
914 c_ipmi_instance_t *st = user_data;
916 int status = c_ipmi_thread_init(st);
918 ERROR("ipmi plugin: c_ipmi_thread_init failed.");
923 while (st->active != 0) {
924 struct timeval tv = {1, 0};
925 os_handler->perform_one_op(os_handler, &tv);
928 } /* void *c_ipmi_thread_main */
930 static c_ipmi_instance_t *c_ipmi_init_instance() {
931 c_ipmi_instance_t *st;
933 st = calloc(1, sizeof(*st));
935 ERROR("ipmi plugin: calloc failed.");
939 st->name = strdup("main");
940 if (st->name == NULL) {
942 ERROR("ipmi plugin: strdup() failed.");
946 st->ignorelist = ignorelist_create(/* invert = */ 1);
947 if (st->ignorelist == NULL) {
950 ERROR("ipmi plugin: ignorelist_create() failed.");
954 st->sensor_list = NULL;
955 pthread_mutex_init(&st->sensor_list_lock, /* attr = */ NULL);
961 st->authtype = IPMI_AUTHTYPE_DEFAULT;
966 } /* c_ipmi_instance_t *c_ipmi_init_instance */
968 static void c_ipmi_free_instance(c_ipmi_instance_t *st) {
972 assert(st->next == NULL);
980 ignorelist_free(st->ignorelist);
981 pthread_mutex_destroy(&st->sensor_list_lock);
983 } /* void c_ipmi_free_instance */
985 static void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
986 if (instances == NULL) {
987 instances = instance;
991 c_ipmi_instance_t *last = instances;
993 while (last->next != NULL)
996 last->next = instance;
997 } /* void c_ipmi_add_instance */
999 static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
1001 c_ipmi_instance_t *st = c_ipmi_init_instance();
1005 if (strcasecmp(ci->key, "Instance") == 0)
1006 status = cf_util_get_string(ci, &st->name);
1009 c_ipmi_free_instance(st);
1013 for (int i = 0; i < ci->children_num; i++) {
1014 oconfig_item_t *child = ci->children + i;
1016 if (strcasecmp("Sensor", child->key) == 0)
1017 ignorelist_add(st->ignorelist, ci->values[0].value.string);
1018 else if (strcasecmp("IgnoreSelected", child->key) == 0) {
1020 status = cf_util_get_boolean(child, &t);
1023 ignorelist_set_invert(st->ignorelist, /* invert = */ !t);
1024 } else if (strcasecmp("NotifyIPMIConnectionState", child->key) == 0) {
1025 status = cf_util_get_boolean(child, &st->notify_conn);
1026 } else if (strcasecmp("NotifySensorAdd", child->key) == 0) {
1027 status = cf_util_get_boolean(child, &st->notify_add);
1028 } else if (strcasecmp("NotifySensorRemove", child->key) == 0) {
1029 status = cf_util_get_boolean(child, &st->notify_remove);
1030 } else if (strcasecmp("NotifySensorNotPresent", child->key) == 0) {
1031 status = cf_util_get_boolean(child, &st->notify_notpresent);
1032 } else if (strcasecmp("SELEnabled", child->key) == 0) {
1033 status = cf_util_get_boolean(child, &st->sel_enabled);
1034 } else if (strcasecmp("SELClearEvent", child->key) == 0) {
1035 status = cf_util_get_boolean(child, &st->sel_clear_event);
1036 } else if (strcasecmp("Host", child->key) == 0)
1037 status = cf_util_get_string(child, &st->host);
1038 else if (strcasecmp("Address", child->key) == 0)
1039 status = cf_util_get_string(child, &st->connaddr);
1040 else if (strcasecmp("Username", child->key) == 0)
1041 status = cf_util_get_string(child, &st->username);
1042 else if (strcasecmp("Password", child->key) == 0)
1043 status = cf_util_get_string(child, &st->password);
1044 else if (strcasecmp("AuthType", child->key) == 0) {
1046 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
1050 if (strcasecmp("MD5", tmp) == 0)
1051 st->authtype = IPMI_AUTHTYPE_MD5;
1052 else if (strcasecmp("rmcp+", tmp) == 0)
1053 st->authtype = IPMI_AUTHTYPE_RMCP_PLUS;
1055 WARNING("ipmi plugin: The value \"%s\" is not valid for the "
1056 "\"AuthType\" option.",
1059 WARNING("ipmi plugin: Option `%s' not allowed here.", child->key);
1068 c_ipmi_free_instance(st);
1072 c_ipmi_add_instance(st);
1075 } /* int c_ipmi_config_add_instance */
1077 static int c_ipmi_config(oconfig_item_t *ci) {
1078 _Bool have_instance_block = 0;
1080 for (int i = 0; i < ci->children_num; i++) {
1081 oconfig_item_t *child = ci->children + i;
1083 if (strcasecmp("Instance", child->key) == 0) {
1084 int status = c_ipmi_config_add_instance(child);
1088 have_instance_block = 1;
1089 } else if (!have_instance_block) {
1090 /* Non-instance option: Assume legacy configuration (without <Instance />
1091 * blocks) and call c_ipmi_config_add_instance with the <Plugin /> block.
1093 WARNING("ipmi plugin: Legacy configuration found! Please update your "
1095 return c_ipmi_config_add_instance(ci);
1097 WARNING("ipmi plugin: The configuration option "
1098 "\"%s\" is not allowed here. Did you "
1099 "forget to add an <Instance /> block "
1100 "around the configuration?",
1104 } /* for (ci->children) */
1107 } /* int c_ipmi_config */
1109 static int c_ipmi_read(user_data_t *user_data) {
1110 c_ipmi_instance_t *st = user_data->data;
1112 if (st->active == 0) {
1113 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
1117 if (st->connected == 0)
1120 sensor_list_read_all(st);
1122 if (st->init_in_progress > 0)
1123 st->init_in_progress--;
1125 st->init_in_progress = 0;
1128 } /* int c_ipmi_read */
1130 static int c_ipmi_init(void) {
1131 c_ipmi_instance_t *st;
1132 char callback_name[3 * DATA_MAX_NAME_LEN];
1134 if (os_handler != NULL) {
1138 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
1139 if (os_handler == NULL) {
1140 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
1144 os_handler->set_log_handler(os_handler, c_ipmi_log);
1146 if (ipmi_init(os_handler) != 0) {
1147 ERROR("ipmi plugin: ipmi_init() failed.");
1148 os_handler->free_os_handler(os_handler);
1152 if (instances == NULL) {
1153 /* No instances were configured, let's start a default instance. */
1154 st = c_ipmi_init_instance();
1158 c_ipmi_add_instance(st);
1161 /* Don't send `ADD' notifications during startup (~ 1 minute) */
1162 int cycles = 1 + (60 / CDTIME_T_TO_TIME_T(plugin_get_interval()));
1165 while (NULL != st) {
1166 /* The `st->name` is used as "domain name" for ipmi_open_domain().
1167 * That value should be unique, so we do plugin_register_complex_read()
1168 * at first as it checks the uniqueness. */
1169 snprintf(callback_name, sizeof(callback_name), "ipmi/%s", st->name);
1175 int status = plugin_register_complex_read(
1176 /* group = */ "ipmi",
1177 /* name = */ callback_name,
1178 /* callback = */ c_ipmi_read,
1180 /* user_data = */ &ud);
1187 st->init_in_progress = cycles;
1190 status = plugin_thread_create(&st->thread_id, /* attr = */ NULL,
1192 /* user data = */ (void *)st, "ipmi");
1196 st->thread_id = (pthread_t){0};
1198 plugin_unregister_read(callback_name);
1200 ERROR("ipmi plugin: pthread_create failed for `%s`.", callback_name);
1207 } /* int c_ipmi_init */
1209 static int c_ipmi_shutdown(void) {
1210 c_ipmi_instance_t *st = instances;
1213 while (st != NULL) {
1214 c_ipmi_instance_t *next = st->next;
1219 if (!pthread_equal(st->thread_id, (pthread_t){0})) {
1220 pthread_join(st->thread_id, NULL);
1221 st->thread_id = (pthread_t){0};
1224 sensor_list_remove_all(st);
1225 c_ipmi_free_instance(st);
1230 os_handler->free_os_handler(os_handler);
1234 } /* int c_ipmi_shutdown */
1236 void module_register(void) {
1237 plugin_register_complex_config("ipmi", c_ipmi_config);
1238 plugin_register_init("ipmi", c_ipmi_init);
1239 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
1240 } /* void module_register */