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>
44 struct c_ipmi_sensor_list_s;
45 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
47 struct c_ipmi_instance_s {
49 ignorelist_t *ignorelist;
52 _Bool notify_notpresent;
54 _Bool sel_clear_event;
60 unsigned int authtype;
62 ipmi_con_t *connection;
63 pthread_mutex_t sensor_list_lock;
64 c_ipmi_sensor_list_t *sensor_list;
70 struct c_ipmi_instance_s *next;
72 typedef struct c_ipmi_instance_s c_ipmi_instance_t;
74 struct c_ipmi_sensor_list_s {
75 ipmi_sensor_id_t sensor_id;
76 char sensor_name[DATA_MAX_NAME_LEN];
77 char sensor_type[DATA_MAX_NAME_LEN];
78 int sensor_not_present;
79 c_ipmi_sensor_list_t *next;
80 c_ipmi_instance_t *instance;
85 * Module global variables
87 static os_handler_t *os_handler;
88 static c_ipmi_instance_t *instances = NULL;
91 * Misc private functions
93 static void c_ipmi_error(const char *func, int status) {
94 char errbuf[4096] = {0};
96 if (IPMI_IS_OS_ERR(status) || IPMI_IS_RMCPP_ERR(status) ||
97 IPMI_IS_IPMI_ERR(status)) {
98 ipmi_get_error_string(status, errbuf, sizeof(errbuf));
101 if (errbuf[0] == 0) {
102 snprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
104 errbuf[sizeof(errbuf) - 1] = 0;
106 ERROR("ipmi plugin: %s failed: %s", func, errbuf);
107 } /* void c_ipmi_error */
109 static void c_ipmi_log(os_handler_t *handler, const char *format,
110 enum ipmi_log_type_e log_type, va_list ap) {
113 vsnprintf(msg, sizeof(msg), format, ap);
117 INFO("ipmi plugin: %s", msg);
119 case IPMI_LOG_WARNING:
120 NOTICE("ipmi plugin: %s", msg);
122 case IPMI_LOG_SEVERE:
123 WARNING("ipmi plugin: %s", msg);
126 ERROR("ipmi plugin: %s", msg);
128 case IPMI_LOG_ERR_INFO:
129 ERROR("ipmi plugin: %s", msg);
132 case IPMI_LOG_DEBUG_START:
134 fprintf(stderr, "ipmi plugin: %s\n", msg);
136 case IPMI_LOG_DEBUG_CONT:
137 case IPMI_LOG_DEBUG_END:
138 fprintf(stderr, "%s\n", msg);
141 case IPMI_LOG_DEBUG_START:
143 case IPMI_LOG_DEBUG_CONT:
144 case IPMI_LOG_DEBUG_END:
148 } /* void c_ipmi_log */
153 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
154 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor);
156 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
157 enum ipmi_value_present_e value_present,
158 unsigned int __attribute__((unused)) raw_value,
159 double value, ipmi_states_t *states,
161 value_list_t vl = VALUE_LIST_INIT;
163 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
164 c_ipmi_instance_t *st = list_item->instance;
169 if (IPMI_IS_IPMI_ERR(err) &&
170 IPMI_GET_IPMI_ERR(err) == IPMI_NOT_PRESENT_CC) {
171 if (list_item->sensor_not_present == 0) {
172 list_item->sensor_not_present = 1;
174 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` "
176 list_item->sensor_name, st->name);
178 if (st->notify_notpresent) {
180 NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "", NULL};
182 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g,
184 sstrncpy(n.type_instance, list_item->sensor_name,
185 sizeof(n.type_instance));
186 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
187 snprintf(n.message, sizeof(n.message), "sensor %s not present",
188 list_item->sensor_name);
190 plugin_dispatch_notification(&n);
193 } else if (IPMI_IS_IPMI_ERR(err) &&
194 IPMI_GET_IPMI_ERR(err) ==
195 IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
196 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` not ready.",
197 list_item->sensor_name, st->name);
198 } else if (IPMI_IS_IPMI_ERR(err) &&
199 IPMI_GET_IPMI_ERR(err) == IPMI_TIMEOUT_CC) {
200 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` timed out.",
201 list_item->sensor_name, st->name);
203 char errbuf[128] = {0};
204 ipmi_get_error_string(err, errbuf, sizeof(errbuf) - 1);
206 if (IPMI_IS_IPMI_ERR(err))
207 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
209 list_item->sensor_name, st->name, errbuf);
210 else if (IPMI_IS_OS_ERR(err))
211 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
213 list_item->sensor_name, st->name, errbuf, IPMI_GET_OS_ERR(err));
214 else if (IPMI_IS_RMCPP_ERR(err))
215 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
217 list_item->sensor_name, st->name, errbuf);
218 else if (IPMI_IS_SOL_ERR(err))
219 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
221 list_item->sensor_name, st->name, errbuf, IPMI_GET_SOL_ERR(err));
223 INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed "
224 "with error %#x. of class %#x",
225 list_item->sensor_name, st->name, err & 0xff, err & 0xffffff00);
228 } else if (list_item->sensor_not_present == 1) {
229 list_item->sensor_not_present = 0;
231 INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` present.",
232 list_item->sensor_name, st->name);
234 if (st->notify_notpresent) {
235 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi",
238 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g,
240 sstrncpy(n.type_instance, list_item->sensor_name,
241 sizeof(n.type_instance));
242 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
243 snprintf(n.message, sizeof(n.message), "sensor %s present",
244 list_item->sensor_name);
246 plugin_dispatch_notification(&n);
250 if (value_present != IPMI_BOTH_VALUES_PRESENT) {
251 INFO("ipmi plugin: sensor_read_handler: Removing sensor `%s` of `%s`, "
252 "because it provides %s. If you need this sensor, "
253 "please file a bug report.",
254 list_item->sensor_name, st->name,
255 (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
257 sensor_list_remove(st, sensor);
261 if (!ipmi_is_sensor_scanning_enabled(states)) {
262 DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
263 "it is in 'scanning disabled' state.",
264 list_item->sensor_name, st->name);
268 if (ipmi_is_initial_update_in_progress(states)) {
269 DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
270 "it is in 'initial update in progress' state.",
271 list_item->sensor_name, st->name);
275 vl.values = &(value_t){.gauge = value};
278 if (st->host != NULL)
279 sstrncpy(vl.host, st->host, sizeof(vl.host));
280 sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
281 sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
282 sstrncpy(vl.type_instance, list_item->sensor_name, sizeof(vl.type_instance));
284 plugin_dispatch_values(&vl);
285 } /* void sensor_read_handler */
287 static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) {
288 char temp[DATA_MAX_NAME_LEN] = {0};
289 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
290 const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
291 char sensor_name[DATA_MAX_NAME_LEN] = "";
292 char *sensor_name_ptr;
294 if ((buffer == NULL) || (buf_len == 0))
297 ipmi_sensor_get_name(sensor, temp, sizeof(temp));
298 temp[sizeof(temp) - 1] = 0;
300 if (entity_id_string != NULL && strlen(temp))
301 snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string);
302 else if (entity_id_string != NULL)
303 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
305 sstrncpy(sensor_name, temp, sizeof(sensor_name));
308 sstrncpy(temp, sensor_name, sizeof(temp));
309 sensor_name_ptr = strstr(temp, ").");
310 if (sensor_name_ptr != NULL) {
311 /* If name is something like "foo (123).bar",
312 * change that to "bar (123)".
313 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
314 * `temp' array, which holds a copy of the current `sensor_name'. */
317 /* `sensor_name_ptr' points to ").bar". */
318 sensor_name_ptr[1] = 0;
319 /* `temp' holds "foo (123)\0bar\0". */
320 sensor_name_ptr += 2;
321 /* `sensor_name_ptr' now points to "bar". */
323 sensor_id_ptr = strstr(temp, "(");
324 if (sensor_id_ptr != NULL) {
325 /* `sensor_id_ptr' now points to "(123)". */
326 snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
329 /* else: don't touch sensor_name. */
332 sstrncpy(buffer, sensor_name, buf_len);
335 static int sensor_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
336 ipmi_sensor_id_t sensor_id;
337 c_ipmi_sensor_list_t *list_item;
338 c_ipmi_sensor_list_t *list_prev;
340 char buffer[DATA_MAX_NAME_LEN] = {0};
341 char *sensor_name_ptr = buffer;
345 sensor_id = ipmi_sensor_convert_to_id(sensor);
346 sensor_get_name(sensor, buffer, sizeof(buffer));
348 DEBUG("ipmi plugin: sensor_list_add: Found sensor `%s` of `%s`,"
350 " Event reading type: %#x"
352 " Event support: %#x",
353 sensor_name_ptr, st->name, ipmi_sensor_get_sensor_type(sensor),
354 ipmi_sensor_get_event_reading_type(sensor),
355 ipmi_sensor_get_sensor_direction(sensor),
356 ipmi_sensor_get_event_support(sensor));
358 /* Both `ignorelist' and `sensor_name_ptr' may be NULL. */
359 if (ignorelist_match(st->ignorelist, sensor_name_ptr) != 0)
362 /* FIXME: Use rate unit or base unit to scale the value */
364 sensor_type = ipmi_sensor_get_sensor_type(sensor);
367 * ipmitool/lib/ipmi_sdr.c sdr_sensor_has_analog_reading() has a notice
368 * about 'Threshold sensors' and 'analog readings'. Discrete sensor may
369 * have analog data, but discrete sensors support is not implemented
372 * ipmi_sensor_id_get_reading() supports only 'Threshold' sensors.
373 * See lib/sensor.c:4842, stand_ipmi_sensor_get_reading() for details.
375 if (!ipmi_sensor_get_is_readable(sensor)) {
376 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
377 "because it don't readable! Its type: (%#x, %s). ",
378 sensor_name_ptr, st->name, sensor_type,
379 ipmi_sensor_get_sensor_type_string(sensor));
383 if (ipmi_sensor_get_event_reading_type(sensor) !=
384 IPMI_EVENT_READING_TYPE_THRESHOLD) {
385 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
386 "because it is discrete (%#x)! Its type: (%#x, %s). ",
387 sensor_name_ptr, st->name, sensor_type,
388 ipmi_sensor_get_event_reading_type(sensor),
389 ipmi_sensor_get_sensor_type_string(sensor));
393 switch (sensor_type) {
394 case IPMI_SENSOR_TYPE_TEMPERATURE:
395 type = "temperature";
398 case IPMI_SENSOR_TYPE_VOLTAGE:
402 case IPMI_SENSOR_TYPE_CURRENT:
406 case IPMI_SENSOR_TYPE_FAN:
411 INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
412 "because I don't know how to handle its type (%#x, %s). "
413 "If you need this sensor, please file a bug report.",
414 sensor_name_ptr, st->name, sensor_type,
415 ipmi_sensor_get_sensor_type_string(sensor));
418 } /* switch (sensor_type) */
420 pthread_mutex_lock(&st->sensor_list_lock);
423 for (list_item = st->sensor_list; list_item != NULL;
424 list_item = list_item->next) {
425 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
427 list_prev = list_item;
428 } /* for (list_item) */
430 if (list_item != NULL) {
431 pthread_mutex_unlock(&st->sensor_list_lock);
435 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
436 if (list_item == NULL) {
437 pthread_mutex_unlock(&st->sensor_list_lock);
441 list_item->instance = st;
442 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
444 if (list_prev != NULL)
445 list_prev->next = list_item;
447 st->sensor_list = list_item;
449 sstrncpy(list_item->sensor_name, sensor_name_ptr,
450 sizeof(list_item->sensor_name));
451 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
453 pthread_mutex_unlock(&st->sensor_list_lock);
455 if (st->notify_add && (st->init_in_progress == 0)) {
456 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
458 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g,
460 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
461 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
462 snprintf(n.message, sizeof(n.message), "sensor %s added",
463 list_item->sensor_name);
465 plugin_dispatch_notification(&n);
469 } /* int sensor_list_add */
471 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
472 ipmi_sensor_id_t sensor_id;
473 c_ipmi_sensor_list_t *list_item;
474 c_ipmi_sensor_list_t *list_prev;
476 sensor_id = ipmi_sensor_convert_to_id(sensor);
478 pthread_mutex_lock(&st->sensor_list_lock);
481 for (list_item = st->sensor_list; list_item != NULL;
482 list_item = list_item->next) {
483 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
485 list_prev = list_item;
486 } /* for (list_item) */
488 if (list_item == NULL) {
489 pthread_mutex_unlock(&st->sensor_list_lock);
493 if (list_prev == NULL)
494 st->sensor_list = list_item->next;
496 list_prev->next = list_item->next;
499 list_item->next = NULL;
501 pthread_mutex_unlock(&st->sensor_list_lock);
503 if (st->notify_remove && st->active) {
504 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "",
507 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g,
509 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
510 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
511 snprintf(n.message, sizeof(n.message), "sensor %s removed",
512 list_item->sensor_name);
514 plugin_dispatch_notification(&n);
519 } /* int sensor_list_remove */
521 static int sensor_list_read_all(c_ipmi_instance_t *st) {
522 pthread_mutex_lock(&st->sensor_list_lock);
524 for (c_ipmi_sensor_list_t *list_item = st->sensor_list; list_item != NULL;
525 list_item = list_item->next) {
526 DEBUG("ipmi plugin: try read sensor `%s` of `%s`, use: %d",
527 list_item->sensor_name, st->name, list_item->use);
529 /* Reading already initiated */
534 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
535 /* user data = */ (void *)list_item);
536 } /* for (list_item) */
538 pthread_mutex_unlock(&st->sensor_list_lock);
541 } /* int sensor_list_read_all */
543 static int sensor_list_remove_all(c_ipmi_instance_t *st) {
544 c_ipmi_sensor_list_t *list_item;
546 pthread_mutex_lock(&st->sensor_list_lock);
548 list_item = st->sensor_list;
549 st->sensor_list = NULL;
551 pthread_mutex_unlock(&st->sensor_list_lock);
553 while (list_item != NULL) {
554 c_ipmi_sensor_list_t *list_next = list_item->next;
558 list_item = list_next;
559 } /* while (list_item) */
562 } /* int sensor_list_remove_all */
564 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
566 case IPMI_LOWER_NON_CRITICAL:
567 case IPMI_UPPER_NON_CRITICAL:
569 case IPMI_LOWER_CRITICAL:
570 case IPMI_UPPER_CRITICAL:
571 return NOTIF_WARNING;
572 case IPMI_LOWER_NON_RECOVERABLE:
573 case IPMI_UPPER_NON_RECOVERABLE:
574 return NOTIF_FAILURE;
577 } /* switch (severity) */
578 } /* int sensor_convert_threshold_severity */
580 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
581 enum ipmi_event_dir_e dir,
582 ipmi_event_t *event) {
583 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
585 plugin_notification_meta_add_string(n, "entity_name",
586 ipmi_entity_get_entity_id_string(ent));
587 plugin_notification_meta_add_signed_int(n, "entity_id",
588 ipmi_entity_get_entity_id(ent));
589 plugin_notification_meta_add_signed_int(n, "entity_instance",
590 ipmi_entity_get_entity_instance(ent));
591 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
594 plugin_notification_meta_add_signed_int(n, "event_type",
595 ipmi_event_get_type(event));
596 } /* void add_event_sensor_meta_data */
598 static int sensor_threshold_event_handler(
599 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
600 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
601 enum ipmi_value_present_e value_present, unsigned int raw_value,
602 double value, void *cb_data, ipmi_event_t *event) {
604 c_ipmi_instance_t *st = (c_ipmi_instance_t *)cb_data;
606 /* From the IPMI specification Chapter 2: Events.
607 * If a callback handles the event, then all future callbacks called due to
608 * the event will receive a NULL for the event. So be ready to handle a NULL
609 * event in all your event handlers. A NULL may also be passed to an event
610 * handler if the callback was not due to an event. */
612 return IPMI_EVENT_NOT_HANDLED;
614 /* offset is a table index and it's represented as enum of strings that are
615 organized in the way - high and low for each threshold severity level */
616 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
617 unsigned int offset = (2 * threshold) + high_low;
618 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
619 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
620 const char *event_state =
621 ipmi_get_reading_name(event_type, sensor_type, offset);
622 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
623 if (value_present != IPMI_NO_VALUES_PRESENT)
624 snprintf(n.message, sizeof(n.message),
625 "sensor %s received event: %s, value is %f", n.type_instance,
628 snprintf(n.message, sizeof(n.message),
629 "sensor %s received event: %s, value not provided",
630 n.type_instance, event_state);
632 DEBUG("Threshold event received for sensor %s", n.type_instance);
634 sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
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 = (c_ipmi_instance_t *)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 = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
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.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
698 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
699 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
701 plugin_notification_meta_add_signed_int(&n, "offset", offset);
704 plugin_notification_meta_add_signed_int(&n, "severity", severity);
706 if (prev_severity != -1)
707 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
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_discrete_event_handler */
727 entity_sensor_update_handler(enum ipmi_update_e op,
728 ipmi_entity_t __attribute__((unused)) * entity,
729 ipmi_sensor_t *sensor, void *user_data) {
730 c_ipmi_instance_t *st = (c_ipmi_instance_t *)user_data;
732 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
733 /* Will check for duplicate entries.. */
734 sensor_list_add(st, sensor);
736 if (st->sel_enabled) {
738 /* register threshold event if threshold sensor support events */
739 if ((ipmi_sensor_get_event_reading_type(sensor) ==
740 IPMI_EVENT_READING_TYPE_THRESHOLD) &&
741 (ipmi_sensor_get_threshold_access(sensor) !=
742 IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
743 status = ipmi_sensor_add_threshold_event_handler(
744 sensor, sensor_threshold_event_handler, st);
745 /* register discrete handler if discrete/specific sensor support events */
746 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
747 status = ipmi_sensor_add_discrete_event_handler(
748 sensor, sensor_discrete_event_handler, st);
751 char buf[DATA_MAX_NAME_LEN] = {0};
752 sensor_get_name(sensor, buf, sizeof(buf));
753 ERROR("Unable to add sensor %s event handler, status: %d", buf, status);
756 } else if (op == IPMI_DELETED) {
757 sensor_list_remove(st, sensor);
759 if (st->sel_enabled) {
760 if (ipmi_sensor_get_event_reading_type(sensor) ==
761 IPMI_EVENT_READING_TYPE_THRESHOLD)
762 ipmi_sensor_remove_threshold_event_handler(
763 sensor, sensor_threshold_event_handler, st);
765 ipmi_sensor_remove_discrete_event_handler(
766 sensor, sensor_discrete_event_handler, st);
769 } /* void entity_sensor_update_handler */
775 domain_entity_update_handler(enum ipmi_update_e op,
776 ipmi_domain_t __attribute__((unused)) * domain,
777 ipmi_entity_t *entity, void *user_data) {
779 c_ipmi_instance_t *st = (c_ipmi_instance_t *)user_data;
781 if (op == IPMI_ADDED) {
782 status = ipmi_entity_add_sensor_update_handler(
783 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
785 c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
787 } else if (op == IPMI_DELETED) {
788 status = ipmi_entity_remove_sensor_update_handler(
789 entity, entity_sensor_update_handler, /* user data = */ (void *)st);
791 c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
794 } /* void domain_entity_update_handler */
796 static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi,
797 const ipmi_addr_t __attribute__((unused)) * addr,
798 unsigned int __attribute__((unused)) addr_len,
799 ipmi_event_t *event, void *cb_data) {
800 unsigned int type = ipmi_event_get_type(event);
801 ipmi_domain_t *domain = cb_data;
803 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
806 /* It's not a standard IPMI event. */
809 /* force domain to reread SELs */
810 ipmi_domain_reread_sels(domain, NULL, NULL);
813 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
814 unsigned int conn_num,
815 unsigned int port_num,
819 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
820 "conn_num = %u, port_num = %u, still_connected = %i, "
822 (void *)domain, err, conn_num, port_num, still_connected, user_data);
825 c_ipmi_error("domain_connection_change_handler", err);
827 if (!still_connected)
830 c_ipmi_instance_t *st = (c_ipmi_instance_t *)user_data;
832 int status = ipmi_domain_add_entity_update_handler(
833 domain, domain_entity_update_handler, /* user data = */ st);
835 c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
838 status = st->connection->add_event_handler(st->connection, smi_event_handler,
842 c_ipmi_error("Failed to register smi event handler", status);
843 } /* void domain_connection_change_handler */
845 static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
846 ipmi_domain_id_t domain_id;
849 if (st->connaddr != NULL) {
850 char *ip_addrs[1] = {NULL}, *ports[1] = {NULL};
852 ip_addrs[0] = strdup(st->connaddr);
853 ports[0] = strdup(IPMI_LAN_STD_PORT_STR);
855 status = ipmi_ip_setup_con(ip_addrs, ports, 1, st->authtype,
856 (unsigned int)IPMI_PRIVILEGE_USER, st->username,
857 strlen(st->username), st->password,
858 strlen(st->password), os_handler,
859 /* user data = */ NULL, &st->connection);
861 c_ipmi_error("ipmi_ip_setup_con", status);
865 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
866 /* user data = */ NULL, &st->connection);
868 c_ipmi_error("ipmi_smi_setup_con", status);
873 size_t open_option_num = 0;
874 ipmi_open_option_t open_option[2];
876 open_option[open_option_num].option = IPMI_OPEN_OPTION_ALL;
877 open_option[open_option_num].ival = 1;
880 #ifdef IPMI_OPEN_OPTION_USE_CACHE
881 // This option appeared in OpenIPMI-2.0.17
882 open_option[open_option_num].option = IPMI_OPEN_OPTION_USE_CACHE;
883 open_option[open_option_num].ival = 0; /* Disable SDR cache in local file */
888 * NOTE: Domain names must be unique. There is static `domains_list` common
889 * to all threads inside lib/domain.c and some ops are done by name.
891 status = ipmi_open_domain(
892 st->name, &st->connection, /* num_con = */ 1,
893 domain_connection_change_handler, /* user data = */ (void *)st,
894 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
895 open_option_num, &domain_id);
897 c_ipmi_error("ipmi_open_domain", status);
902 } /* int c_ipmi_thread_init */
904 static void *c_ipmi_thread_main(void *user_data) {
906 c_ipmi_instance_t *st = (c_ipmi_instance_t *)user_data;
908 status = c_ipmi_thread_init(st);
910 ERROR("ipmi plugin: c_ipmi_thread_init failed.");
915 while (st->active != 0) {
916 struct timeval tv = {1, 0};
917 os_handler->perform_one_op(os_handler, &tv);
920 } /* void *c_ipmi_thread_main */
922 static c_ipmi_instance_t *c_ipmi_init_instance() {
923 c_ipmi_instance_t *st;
925 st = calloc(1, sizeof(*st));
927 ERROR("ipmi plugin: calloc failed.");
931 st->name = strdup("main");
932 if (st->name == NULL) {
934 ERROR("ipmi plugin: strdup() failed.");
938 st->ignorelist = ignorelist_create(/* invert = */ 1);
939 if (st->ignorelist == NULL) {
942 ERROR("ipmi plugin: ignorelist_create() failed.");
946 st->sensor_list = NULL;
947 pthread_mutex_init(&st->sensor_list_lock, /* attr = */ NULL);
953 st->authtype = IPMI_AUTHTYPE_DEFAULT;
958 } /* c_ipmi_instance_t *c_ipmi_init_instance */
960 static void c_ipmi_free_instance(c_ipmi_instance_t *st) {
964 assert(st->next == NULL);
972 ignorelist_free(st->ignorelist);
973 pthread_mutex_destroy(&st->sensor_list_lock);
975 } /* void c_ipmi_free_instance */
977 void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
978 if (instances == NULL) {
979 instances = instance;
983 c_ipmi_instance_t *last = instances;
985 while (last->next != NULL)
988 last->next = instance;
991 } /* void c_ipmi_add_instance */
993 static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
995 c_ipmi_instance_t *st = c_ipmi_init_instance();
999 if (strcasecmp(ci->key, "Instance") == 0)
1000 status = cf_util_get_string(ci, &st->name);
1003 c_ipmi_free_instance(st);
1007 for (int i = 0; i < ci->children_num; i++) {
1008 oconfig_item_t *child = ci->children + i;
1010 if (strcasecmp("Sensor", child->key) == 0)
1011 ignorelist_add(st->ignorelist, ci->values[0].value.string);
1012 else if (strcasecmp("IgnoreSelected", child->key) == 0) {
1013 if (ci->values[0].value.boolean)
1014 ignorelist_set_invert(st->ignorelist, /* invert = */ 0);
1016 ignorelist_set_invert(st->ignorelist, /* invert = */ 1);
1017 } else if (strcasecmp("NotifySensorAdd", child->key) == 0) {
1018 if (ci->values[0].value.boolean)
1020 } else if (strcasecmp("NotifySensorRemove", child->key) == 0) {
1021 if (ci->values[0].value.boolean)
1022 st->notify_remove = 1;
1023 } else if (strcasecmp("NotifySensorNotPresent", child->key) == 0) {
1024 if (ci->values[0].value.boolean)
1025 st->notify_notpresent = 1;
1026 } else if (strcasecmp("SELEnabled", child->key) == 0) {
1027 if (ci->values[0].value.boolean)
1028 st->sel_enabled = 1;
1029 } else if (strcasecmp("SELClearEvent", child->key) == 0) {
1030 if (ci->values[0].value.boolean)
1031 st->sel_clear_event = 1;
1032 } else if (strcasecmp("Host", child->key) == 0)
1033 status = cf_util_get_string(child, &st->host);
1034 else if (strcasecmp("Address", child->key) == 0)
1035 status = cf_util_get_string(child, &st->connaddr);
1036 else if (strcasecmp("Username", child->key) == 0)
1037 status = cf_util_get_string(child, &st->username);
1038 else if (strcasecmp("Password", child->key) == 0)
1039 status = cf_util_get_string(child, &st->password);
1040 else if (strcasecmp("AuthType", child->key) == 0) {
1042 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
1046 if (strcasecmp("MD5", tmp) == 0)
1047 st->authtype = IPMI_AUTHTYPE_MD5;
1048 else if (strcasecmp("rmcp+", tmp) == 0)
1049 st->authtype = IPMI_AUTHTYPE_RMCP_PLUS;
1051 WARNING("ipmi plugin: The value \"%s\" is not valid for the "
1052 "\"AuthType\" option.",
1055 WARNING("ipmi plugin: Option `%s' not allowed here.", child->key);
1064 c_ipmi_free_instance(st);
1068 c_ipmi_add_instance(st);
1071 } /* int c_ipmi_config_add_instance */
1073 static int c_ipmi_config(oconfig_item_t *ci) {
1074 _Bool have_instance_block = 0;
1076 for (int i = 0; i < ci->children_num; i++) {
1077 oconfig_item_t *child = ci->children + i;
1079 if (strcasecmp("Instance", child->key) == 0) {
1080 c_ipmi_config_add_instance(child);
1081 have_instance_block = 1;
1082 } else if (!have_instance_block) {
1083 /* Non-instance option: Assume legacy configuration (without <Instance />
1084 * blocks) and call c_ipmi_config_add_instance with the <Plugin /> block.
1086 return c_ipmi_config_add_instance(ci);
1088 WARNING("ipmi plugin: The configuration option "
1089 "\"%s\" is not allowed here. Did you "
1090 "forget to add an <Instance /> block "
1091 "around the configuration?",
1093 } /* for (ci->children) */
1096 } /* int c_ipmi_config */
1098 static int c_ipmi_read(user_data_t *user_data) {
1099 c_ipmi_instance_t *st;
1100 st = user_data->data;
1102 if ((st->active == 0) || (st->thread_id == (pthread_t)0)) {
1103 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
1107 sensor_list_read_all(st);
1109 if (st->init_in_progress > 0)
1110 st->init_in_progress--;
1112 st->init_in_progress = 0;
1115 } /* int c_ipmi_read */
1117 static int c_ipmi_init(void) {
1119 c_ipmi_instance_t *st;
1120 char callback_name[3 * DATA_MAX_NAME_LEN];
1122 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
1123 if (os_handler == NULL) {
1124 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
1128 os_handler->set_log_handler(os_handler, c_ipmi_log);
1130 if (ipmi_init(os_handler) != 0) {
1131 ERROR("ipmi plugin: ipmi_init() failed.");
1132 os_handler->free_os_handler(os_handler);
1136 /* Don't send `ADD' notifications during startup (~ 1 minute) */
1137 time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
1139 if (instances == NULL) {
1140 /* No instances were configured, let's start a default instance. */
1141 st = c_ipmi_init_instance();
1145 c_ipmi_add_instance(st);
1149 while (NULL != st) {
1150 /* The `st->name` is used as "domain name" for ipmi_open_domain().
1151 * That value should be unique, so we do plugin_register_complex_read()
1152 * at first as it checks the uniqueness. */
1153 snprintf(callback_name, sizeof(callback_name), "ipmi/%s", st->name);
1159 status = plugin_register_complex_read(
1160 /* group = */ "ipmi",
1161 /* name = */ callback_name,
1162 /* callback = */ c_ipmi_read,
1164 /* user_data = */ &ud);
1172 st->init_in_progress = 1 + (60 / iv);
1175 status = plugin_thread_create(&st->thread_id, /* attr = */ NULL,
1177 /* user data = */ (void *)st, "ipmi");
1181 st->thread_id = (pthread_t)0;
1183 plugin_unregister_read(callback_name);
1185 ERROR("ipmi plugin: pthread_create failed for `%s`.", callback_name);
1192 } /* int c_ipmi_init */
1194 static int c_ipmi_shutdown(void) {
1195 c_ipmi_instance_t *st = instances;
1198 while (st != NULL) {
1199 c_ipmi_instance_t *next = st->next;
1204 if (st->thread_id != (pthread_t)0) {
1205 pthread_join(st->thread_id, NULL);
1206 st->thread_id = (pthread_t)0;
1209 sensor_list_remove_all(st);
1210 c_ipmi_free_instance(st);
1215 os_handler->free_os_handler(os_handler);
1218 } /* int c_ipmi_shutdown */
1220 void module_register(void) {
1221 plugin_register_complex_config("ipmi", c_ipmi_config);
1222 plugin_register_init("ipmi", c_ipmi_init);
1223 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
1224 } /* void module_register */