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>
30 #include "utils_ignorelist.h"
32 #include <OpenIPMI/ipmi_conn.h>
33 #include <OpenIPMI/ipmi_err.h>
34 #include <OpenIPMI/ipmi_posix.h>
35 #include <OpenIPMI/ipmi_smi.h>
36 #include <OpenIPMI/ipmiif.h>
41 struct c_ipmi_sensor_list_s;
42 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
44 struct c_ipmi_sensor_list_s {
45 ipmi_sensor_id_t sensor_id;
46 char sensor_name[DATA_MAX_NAME_LEN];
47 char sensor_type[DATA_MAX_NAME_LEN];
48 int sensor_not_present;
49 c_ipmi_sensor_list_t *next;
53 * Module global variables
55 static pthread_mutex_t sensor_list_lock = PTHREAD_MUTEX_INITIALIZER;
56 static c_ipmi_sensor_list_t *sensor_list = NULL;
58 static int c_ipmi_init_in_progress = 0;
59 static int c_ipmi_active = 0;
60 static pthread_t thread_id = (pthread_t)0;
62 static const char *config_keys[] = {"Sensor",
66 "NotifySensorNotPresent",
69 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
71 static ignorelist_t *ignorelist = NULL;
73 static _Bool c_ipmi_notify_add = 0;
74 static _Bool c_ipmi_notify_remove = 0;
75 static _Bool c_ipmi_notify_notpresent = 0;
76 static _Bool c_ipmi_sel_enabled = 0;
77 static _Bool c_ipmi_sel_clear_event = 0;
80 * Misc private functions
82 static void c_ipmi_error(const char *func, int status) {
83 char errbuf[4096] = {0};
85 if (IPMI_IS_OS_ERR(status)) {
86 sstrerror(IPMI_GET_OS_ERR(status), errbuf, sizeof(errbuf));
87 } else if (IPMI_IS_IPMI_ERR(status)) {
88 ipmi_get_error_string(IPMI_GET_IPMI_ERR(status), errbuf, sizeof(errbuf));
92 ssnprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
94 errbuf[sizeof(errbuf) - 1] = 0;
96 ERROR("ipmi plugin: %s failed: %s", func, errbuf);
97 } /* void c_ipmi_error */
102 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
103 static int sensor_list_remove(ipmi_sensor_t *sensor);
105 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
106 enum ipmi_value_present_e value_present,
107 unsigned int __attribute__((unused)) raw_value,
109 ipmi_states_t __attribute__((unused)) * states,
111 value_list_t vl = VALUE_LIST_INIT;
113 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
116 if ((err & 0xff) == IPMI_NOT_PRESENT_CC) {
117 if (list_item->sensor_not_present == 0) {
118 list_item->sensor_not_present = 1;
120 INFO("ipmi plugin: sensor_read_handler: sensor %s "
122 list_item->sensor_name);
124 if (c_ipmi_notify_notpresent) {
126 NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "", NULL};
128 sstrncpy(n.host, hostname_g, sizeof(n.host));
129 sstrncpy(n.type_instance, list_item->sensor_name,
130 sizeof(n.type_instance));
131 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
132 ssnprintf(n.message, sizeof(n.message), "sensor %s not present",
133 list_item->sensor_name);
135 plugin_dispatch_notification(&n);
138 } else if (IPMI_IS_IPMI_ERR(err) &&
139 IPMI_GET_IPMI_ERR(err) ==
140 IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
141 INFO("ipmi plugin: sensor_read_handler: Sensor %s not ready",
142 list_item->sensor_name);
144 if (IPMI_IS_IPMI_ERR(err))
145 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
146 "because it failed with IPMI error %#x.",
147 list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
148 else if (IPMI_IS_OS_ERR(err))
149 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
150 "because it failed with OS error %#x.",
151 list_item->sensor_name, IPMI_GET_OS_ERR(err));
152 else if (IPMI_IS_RMCPP_ERR(err))
153 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
154 "because it failed with RMCPP error %#x.",
155 list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
156 else if (IPMI_IS_SOL_ERR(err))
157 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
158 "because it failed with RMCPP error %#x.",
159 list_item->sensor_name, IPMI_GET_SOL_ERR(err));
161 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
162 "because it failed with error %#x. of class %#x",
163 list_item->sensor_name, err & 0xff, err & 0xffffff00);
164 sensor_list_remove(sensor);
167 } else if (list_item->sensor_not_present == 1) {
168 list_item->sensor_not_present = 0;
170 INFO("ipmi plugin: sensor_read_handler: sensor %s present.",
171 list_item->sensor_name);
173 if (c_ipmi_notify_notpresent) {
174 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi",
177 sstrncpy(n.host, hostname_g, sizeof(n.host));
178 sstrncpy(n.type_instance, list_item->sensor_name,
179 sizeof(n.type_instance));
180 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
181 ssnprintf(n.message, sizeof(n.message), "sensor %s present",
182 list_item->sensor_name);
184 plugin_dispatch_notification(&n);
188 if (value_present != IPMI_BOTH_VALUES_PRESENT) {
189 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
190 "because it provides %s. If you need this sensor, "
191 "please file a bug report.",
192 list_item->sensor_name,
193 (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
195 sensor_list_remove(sensor);
199 vl.values = &(value_t){.gauge = value};
202 sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
203 sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
204 sstrncpy(vl.type_instance, list_item->sensor_name, sizeof(vl.type_instance));
206 plugin_dispatch_values(&vl);
207 } /* void sensor_read_handler */
209 static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) {
210 char temp[DATA_MAX_NAME_LEN] = {0};
211 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
212 const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
213 char sensor_name[DATA_MAX_NAME_LEN] = "";
214 char *sensor_name_ptr;
216 if ((buffer == NULL) || (buf_len == 0))
219 ipmi_sensor_get_name(sensor, temp, sizeof(temp));
220 temp[sizeof(temp) - 1] = 0;
222 if (entity_id_string != NULL && strlen(temp))
223 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", temp,
225 else if (entity_id_string != NULL)
226 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
228 sstrncpy(sensor_name, temp, sizeof(sensor_name));
231 sstrncpy(temp, sensor_name, sizeof(temp));
232 sensor_name_ptr = strstr(temp, ").");
233 if (sensor_name_ptr != NULL) {
234 /* If name is something like "foo (123).bar",
235 * change that to "bar (123)".
236 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
237 * `temp' array, which holds a copy of the current `sensor_name'. */
240 /* `sensor_name_ptr' points to ").bar". */
241 sensor_name_ptr[1] = 0;
242 /* `temp' holds "foo (123)\0bar\0". */
243 sensor_name_ptr += 2;
244 /* `sensor_name_ptr' now points to "bar". */
246 sensor_id_ptr = strstr(temp, "(");
247 if (sensor_id_ptr != NULL) {
248 /* `sensor_id_ptr' now points to "(123)". */
249 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
252 /* else: don't touch sensor_name. */
255 sstrncpy(buffer, sensor_name, buf_len);
258 static int sensor_list_add(ipmi_sensor_t *sensor) {
259 ipmi_sensor_id_t sensor_id;
260 c_ipmi_sensor_list_t *list_item;
261 c_ipmi_sensor_list_t *list_prev;
263 char buffer[DATA_MAX_NAME_LEN] = {0};
264 char *sensor_name_ptr = buffer;
268 sensor_id = ipmi_sensor_convert_to_id(sensor);
269 sensor_get_name(sensor, buffer, sizeof(buffer));
271 /* Both `ignorelist' and `plugin_instance' may be NULL. */
272 if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
275 /* FIXME: Use rate unit or base unit to scale the value */
277 sensor_type = ipmi_sensor_get_sensor_type(sensor);
278 switch (sensor_type) {
279 case IPMI_SENSOR_TYPE_TEMPERATURE:
280 type = "temperature";
283 case IPMI_SENSOR_TYPE_VOLTAGE:
287 case IPMI_SENSOR_TYPE_CURRENT:
291 case IPMI_SENSOR_TYPE_FAN:
296 const char *sensor_type_str;
298 sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
299 INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
300 "because I don't know how to handle its type (%#x, %s). "
301 "If you need this sensor, please file a bug report.",
302 sensor_name_ptr, sensor_type, sensor_type_str);
305 } /* switch (sensor_type) */
307 pthread_mutex_lock(&sensor_list_lock);
310 for (list_item = sensor_list; list_item != NULL;
311 list_item = list_item->next) {
312 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
314 list_prev = list_item;
315 } /* for (list_item) */
317 if (list_item != NULL) {
318 pthread_mutex_unlock(&sensor_list_lock);
322 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
323 if (list_item == NULL) {
324 pthread_mutex_unlock(&sensor_list_lock);
328 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
330 if (list_prev != NULL)
331 list_prev->next = list_item;
333 sensor_list = list_item;
335 sstrncpy(list_item->sensor_name, sensor_name_ptr,
336 sizeof(list_item->sensor_name));
337 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
339 pthread_mutex_unlock(&sensor_list_lock);
341 if (c_ipmi_notify_add && (c_ipmi_init_in_progress == 0)) {
342 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
344 sstrncpy(n.host, hostname_g, sizeof(n.host));
345 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
346 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
347 ssnprintf(n.message, sizeof(n.message), "sensor %s added",
348 list_item->sensor_name);
350 plugin_dispatch_notification(&n);
354 } /* int sensor_list_add */
356 static int sensor_list_remove(ipmi_sensor_t *sensor) {
357 ipmi_sensor_id_t sensor_id;
358 c_ipmi_sensor_list_t *list_item;
359 c_ipmi_sensor_list_t *list_prev;
361 sensor_id = ipmi_sensor_convert_to_id(sensor);
363 pthread_mutex_lock(&sensor_list_lock);
366 for (list_item = sensor_list; list_item != NULL;
367 list_item = list_item->next) {
368 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
370 list_prev = list_item;
371 } /* for (list_item) */
373 if (list_item == NULL) {
374 pthread_mutex_unlock(&sensor_list_lock);
378 if (list_prev == NULL)
379 sensor_list = list_item->next;
381 list_prev->next = list_item->next;
384 list_item->next = NULL;
386 pthread_mutex_unlock(&sensor_list_lock);
388 if (c_ipmi_notify_remove && c_ipmi_active) {
389 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "",
392 sstrncpy(n.host, hostname_g, sizeof(n.host));
393 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
394 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
395 ssnprintf(n.message, sizeof(n.message), "sensor %s removed",
396 list_item->sensor_name);
398 plugin_dispatch_notification(&n);
403 } /* int sensor_list_remove */
405 static int sensor_list_read_all(void) {
406 pthread_mutex_lock(&sensor_list_lock);
408 for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
409 list_item = list_item->next) {
410 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
411 /* user data = */ list_item);
412 } /* for (list_item) */
414 pthread_mutex_unlock(&sensor_list_lock);
417 } /* int sensor_list_read_all */
419 static int sensor_list_remove_all(void) {
420 c_ipmi_sensor_list_t *list_item;
422 pthread_mutex_lock(&sensor_list_lock);
424 list_item = sensor_list;
427 pthread_mutex_unlock(&sensor_list_lock);
429 while (list_item != NULL) {
430 c_ipmi_sensor_list_t *list_next = list_item->next;
434 list_item = list_next;
435 } /* while (list_item) */
438 } /* int sensor_list_remove_all */
440 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
441 int _severity = NOTIF_OKAY;
444 case IPMI_LOWER_NON_CRITICAL:
445 case IPMI_UPPER_NON_CRITICAL:
446 _severity = NOTIF_OKAY;
448 case IPMI_LOWER_CRITICAL:
449 case IPMI_UPPER_CRITICAL:
450 _severity = NOTIF_WARNING;
452 case IPMI_LOWER_NON_RECOVERABLE:
453 case IPMI_UPPER_NON_RECOVERABLE:
454 _severity = NOTIF_FAILURE;
458 } /* switch (severity) */
461 } /* int sensor_convert_threshold_severity */
463 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
464 enum ipmi_event_dir_e dir,
465 ipmi_event_t *event) {
466 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
468 plugin_notification_meta_add_string(n, "entity_name",
469 ipmi_entity_get_entity_id_string(ent));
470 plugin_notification_meta_add_signed_int(n, "entity_id",
471 ipmi_entity_get_entity_id(ent));
472 plugin_notification_meta_add_signed_int(n, "entity_instance",
473 ipmi_entity_get_entity_instance(ent));
474 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
477 plugin_notification_meta_add_signed_int(n, "event_type",
478 ipmi_event_get_type(event));
479 } /* void add_event_sensor_meta_data */
481 static int sensor_threshold_event_handler(
482 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
483 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
484 enum ipmi_value_present_e value_present, unsigned int raw_value,
485 double value, void *cb_data, ipmi_event_t *event) {
487 /* From the IPMI specification Chapter 2: Events.
488 * If a callback handles the event, then all future callbacks called due to
489 * the event will receive a NULL for the event. So be ready to handle a NULL
490 * event in all your event handlers. A NULL may also be passed to an event
491 * handler if the callback was not due to an event. */
493 return (IPMI_EVENT_NOT_HANDLED);
495 /* offset is a table index and it's represented as enum of strings that are
496 organized in the way - high and low for each threshold severity level */
497 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
498 unsigned int offset = (2 * threshold) + high_low;
499 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
500 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
501 const char *event_state =
502 ipmi_get_reading_name(event_type, sensor_type, offset);
503 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
504 if (value_present != IPMI_NO_VALUES_PRESENT)
505 ssnprintf(n.message, sizeof(n.message),
506 "sensor %s received event: %s, value is %f", n.type_instance,
509 ssnprintf(n.message, sizeof(n.message),
510 "sensor %s received event: %s, value not provided",
511 n.type_instance, event_state);
513 DEBUG("Threshold event received for sensor %s", n.type_instance);
515 sstrncpy(n.host, hostname_g, sizeof(n.host));
516 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
517 n.severity = sensor_convert_threshold_severity(threshold);
518 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
520 plugin_notification_meta_add_string(&n, "severity",
521 ipmi_get_threshold_string(threshold));
522 plugin_notification_meta_add_string(&n, "direction",
523 ipmi_get_value_dir_string(high_low));
525 switch (value_present) {
526 case IPMI_BOTH_VALUES_PRESENT:
527 plugin_notification_meta_add_double(&n, "val", value);
528 /* both values present, so fall-through to add raw value too */
529 case IPMI_RAW_VALUE_PRESENT: {
530 char buf[DATA_MAX_NAME_LEN] = {0};
531 snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
532 plugin_notification_meta_add_string(&n, "raw", buf);
536 } /* switch (value_present) */
538 add_event_common_data(&n, sensor, dir, event);
540 plugin_dispatch_notification(&n);
541 plugin_notification_meta_free(n.meta);
543 /* Delete handled ipmi event from the list */
544 if (c_ipmi_sel_clear_event) {
545 ipmi_event_delete(event, NULL, NULL);
546 return (IPMI_EVENT_HANDLED);
549 return (IPMI_EVENT_NOT_HANDLED);
550 } /* int sensor_threshold_event_handler */
552 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
553 enum ipmi_event_dir_e dir, int offset,
554 int severity, int prev_severity,
555 void *cb_data, ipmi_event_t *event) {
556 /* From the IPMI specification Chapter 2: Events.
557 * If a callback handles the event, then all future callbacks called due to
558 * the event will receive a NULL for the event. So be ready to handle a NULL
559 * event in all your event handlers. A NULL may also be passed to an event
560 * handler if the callback was not due to an event. */
562 return (IPMI_EVENT_NOT_HANDLED);
564 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
565 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
566 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
567 const char *event_state =
568 ipmi_get_reading_name(event_type, sensor_type, offset);
569 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
570 ssnprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
571 n.type_instance, event_state);
573 DEBUG("Discrete event received for sensor %s", n.type_instance);
575 sstrncpy(n.host, hostname_g, sizeof(n.host));
576 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
577 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
579 plugin_notification_meta_add_signed_int(&n, "offset", offset);
582 plugin_notification_meta_add_signed_int(&n, "severity", severity);
584 if (prev_severity != -1)
585 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
587 add_event_common_data(&n, sensor, dir, event);
589 plugin_dispatch_notification(&n);
590 plugin_notification_meta_free(n.meta);
592 /* Delete handled ipmi event from the list */
593 if (c_ipmi_sel_clear_event) {
594 ipmi_event_delete(event, NULL, NULL);
595 return (IPMI_EVENT_HANDLED);
598 return (IPMI_EVENT_NOT_HANDLED);
599 } /* int sensor_discrete_event_handler */
604 static void entity_sensor_update_handler(
605 enum ipmi_update_e op, ipmi_entity_t __attribute__((unused)) * entity,
606 ipmi_sensor_t *sensor, void __attribute__((unused)) * user_data) {
607 /* TODO: Ignore sensors we cannot read */
609 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
610 /* Will check for duplicate entries.. */
611 sensor_list_add(sensor);
613 if (c_ipmi_sel_enabled) {
615 /* register threshold event if threshold sensor support events */
616 if ((ipmi_sensor_get_event_reading_type(sensor) ==
617 IPMI_EVENT_READING_TYPE_THRESHOLD) &&
618 (ipmi_sensor_get_threshold_access(sensor) !=
619 IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
620 status = ipmi_sensor_add_threshold_event_handler(
621 sensor, sensor_threshold_event_handler, NULL);
622 /* register discrete handler if discrete/specific sensor support events */
623 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
624 status = ipmi_sensor_add_discrete_event_handler(
625 sensor, sensor_discrete_event_handler, NULL);
628 char buf[DATA_MAX_NAME_LEN] = {0};
629 sensor_get_name(sensor, buf, sizeof(buf));
630 ERROR("Unable to add sensor %s event handler, status: %d", buf, status);
633 } else if (op == IPMI_DELETED) {
634 sensor_list_remove(sensor);
636 if (c_ipmi_sel_enabled) {
637 if (ipmi_sensor_get_event_reading_type(sensor) ==
638 IPMI_EVENT_READING_TYPE_THRESHOLD)
639 ipmi_sensor_remove_threshold_event_handler(
640 sensor, sensor_threshold_event_handler, NULL);
642 ipmi_sensor_remove_discrete_event_handler(
643 sensor, sensor_discrete_event_handler, NULL);
646 } /* void entity_sensor_update_handler */
651 static void domain_entity_update_handler(
652 enum ipmi_update_e op, ipmi_domain_t __attribute__((unused)) * domain,
653 ipmi_entity_t *entity, void __attribute__((unused)) * user_data) {
656 if (op == IPMI_ADDED) {
657 status = ipmi_entity_add_sensor_update_handler(
658 entity, entity_sensor_update_handler, /* user data = */ NULL);
660 c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
662 } else if (op == IPMI_DELETED) {
663 status = ipmi_entity_remove_sensor_update_handler(
664 entity, entity_sensor_update_handler, /* user data = */ NULL);
666 c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
669 } /* void domain_entity_update_handler */
671 static void smi_event_handler(ipmi_con_t __attribute__((unused)) *ipmi,
672 const ipmi_addr_t __attribute__((unused)) *addr,
673 unsigned int __attribute__((unused)) addr_len,
674 ipmi_event_t *event, void *cb_data) {
675 unsigned int type = ipmi_event_get_type(event);
676 ipmi_domain_t *domain = cb_data;
678 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
681 /* It's not a standard IPMI event. */
684 /* force domain to reread SELs */
685 ipmi_domain_reread_sels(domain, NULL, NULL);
688 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
689 unsigned int conn_num,
690 unsigned int port_num,
695 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
696 "conn_num = %u, port_num = %u, still_connected = %i, "
697 "user_data = %p);\n",
698 (void *)domain, err, conn_num, port_num, still_connected, user_data);
700 status = ipmi_domain_add_entity_update_handler(
701 domain, domain_entity_update_handler, /* user data = */ NULL);
703 c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
706 ipmi_con_t *smi_connection = user_data;
707 status = smi_connection->add_event_handler(smi_connection, smi_event_handler,
711 c_ipmi_error("Failed to register smi event handler", status);
712 } /* void domain_connection_change_handler */
714 static int thread_init(os_handler_t **ret_os_handler) {
715 os_handler_t *os_handler;
716 ipmi_con_t *smi_connection = NULL;
717 ipmi_domain_id_t domain_id;
720 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
721 if (os_handler == NULL) {
722 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
726 ipmi_init(os_handler);
728 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
729 /* user data = */ NULL, &smi_connection);
731 c_ipmi_error("ipmi_smi_setup_con", status);
735 ipmi_open_option_t open_option[1] = {
736 [0] = {.option = IPMI_OPEN_OPTION_ALL, {.ival = 1}}};
738 status = ipmi_open_domain(
739 "mydomain", &smi_connection, /* num_con = */ 1,
740 domain_connection_change_handler,
741 /* user data = */ (void *)smi_connection,
742 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
743 sizeof(open_option) / sizeof(open_option[0]), &domain_id);
745 c_ipmi_error("ipmi_open_domain", status);
749 *ret_os_handler = os_handler;
751 } /* int thread_init */
753 static void *thread_main(void __attribute__((unused)) * user_data) {
755 os_handler_t *os_handler = NULL;
757 status = thread_init(&os_handler);
759 ERROR("ipmi plugin: thread_init failed.\n");
763 while (c_ipmi_active != 0) {
764 struct timeval tv = {1, 0};
765 os_handler->perform_one_op(os_handler, &tv);
768 ipmi_posix_thread_free_os_handler(os_handler);
771 } /* void *thread_main */
773 static int c_ipmi_config(const char *key, const char *value) {
774 if (ignorelist == NULL)
775 ignorelist = ignorelist_create(/* invert = */ 1);
776 if (ignorelist == NULL)
779 if (strcasecmp("Sensor", key) == 0) {
780 ignorelist_add(ignorelist, value);
781 } else if (strcasecmp("IgnoreSelected", key) == 0) {
782 ignorelist_set_invert(ignorelist, !IS_TRUE(value));
783 } else if (strcasecmp("NotifySensorAdd", key) == 0) {
784 c_ipmi_notify_add = IS_TRUE(value);
785 } else if (strcasecmp("NotifySensorRemove", key) == 0) {
786 c_ipmi_notify_remove = IS_TRUE(value);
787 } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
788 c_ipmi_notify_notpresent = IS_TRUE(value);
789 } else if (strcasecmp("SELEnabled", key) == 0) {
790 c_ipmi_sel_enabled = IS_TRUE(value);
791 } else if (strcasecmp("SELClearEvent", key) == 0) {
792 c_ipmi_sel_clear_event = IS_TRUE(value);
798 } /* int c_ipmi_config */
800 static int c_ipmi_init(void) {
803 /* Don't send `ADD' notifications during startup (~ 1 minute) */
804 time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
805 c_ipmi_init_in_progress = 1 + (60 / iv);
809 status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
810 /* user data = */ NULL, "ipmi");
813 thread_id = (pthread_t)0;
814 ERROR("ipmi plugin: pthread_create failed.");
819 } /* int c_ipmi_init */
821 static int c_ipmi_read(void) {
822 if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
823 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
827 sensor_list_read_all();
829 if (c_ipmi_init_in_progress > 0)
830 c_ipmi_init_in_progress--;
832 c_ipmi_init_in_progress = 0;
835 } /* int c_ipmi_read */
837 static int c_ipmi_shutdown(void) {
840 if (thread_id != (pthread_t)0) {
841 pthread_join(thread_id, NULL);
842 thread_id = (pthread_t)0;
845 sensor_list_remove_all();
848 } /* int c_ipmi_shutdown */
850 void module_register(void) {
851 plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
852 plugin_register_init("ipmi", c_ipmi_init);
853 plugin_register_read("ipmi", c_ipmi_read);
854 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
855 } /* void module_register */