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 snprintf(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 snprintf(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 snprintf(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 snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string);
224 else if (entity_id_string != NULL)
225 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
227 sstrncpy(sensor_name, temp, sizeof(sensor_name));
230 sstrncpy(temp, sensor_name, sizeof(temp));
231 sensor_name_ptr = strstr(temp, ").");
232 if (sensor_name_ptr != NULL) {
233 /* If name is something like "foo (123).bar",
234 * change that to "bar (123)".
235 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
236 * `temp' array, which holds a copy of the current `sensor_name'. */
239 /* `sensor_name_ptr' points to ").bar". */
240 sensor_name_ptr[1] = 0;
241 /* `temp' holds "foo (123)\0bar\0". */
242 sensor_name_ptr += 2;
243 /* `sensor_name_ptr' now points to "bar". */
245 sensor_id_ptr = strstr(temp, "(");
246 if (sensor_id_ptr != NULL) {
247 /* `sensor_id_ptr' now points to "(123)". */
248 snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
251 /* else: don't touch sensor_name. */
254 sstrncpy(buffer, sensor_name, buf_len);
257 static int sensor_list_add(ipmi_sensor_t *sensor) {
258 ipmi_sensor_id_t sensor_id;
259 c_ipmi_sensor_list_t *list_item;
260 c_ipmi_sensor_list_t *list_prev;
262 char buffer[DATA_MAX_NAME_LEN] = {0};
263 char *sensor_name_ptr = buffer;
267 sensor_id = ipmi_sensor_convert_to_id(sensor);
268 sensor_get_name(sensor, buffer, sizeof(buffer));
270 /* Both `ignorelist' and `plugin_instance' may be NULL. */
271 if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
274 /* FIXME: Use rate unit or base unit to scale the value */
276 sensor_type = ipmi_sensor_get_sensor_type(sensor);
277 switch (sensor_type) {
278 case IPMI_SENSOR_TYPE_TEMPERATURE:
279 type = "temperature";
282 case IPMI_SENSOR_TYPE_VOLTAGE:
286 case IPMI_SENSOR_TYPE_CURRENT:
290 case IPMI_SENSOR_TYPE_FAN:
295 const char *sensor_type_str;
297 sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
298 INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
299 "because I don't know how to handle its type (%#x, %s). "
300 "If you need this sensor, please file a bug report.",
301 sensor_name_ptr, sensor_type, sensor_type_str);
304 } /* switch (sensor_type) */
306 pthread_mutex_lock(&sensor_list_lock);
309 for (list_item = sensor_list; list_item != NULL;
310 list_item = list_item->next) {
311 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
313 list_prev = list_item;
314 } /* for (list_item) */
316 if (list_item != NULL) {
317 pthread_mutex_unlock(&sensor_list_lock);
321 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
322 if (list_item == NULL) {
323 pthread_mutex_unlock(&sensor_list_lock);
327 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
329 if (list_prev != NULL)
330 list_prev->next = list_item;
332 sensor_list = list_item;
334 sstrncpy(list_item->sensor_name, sensor_name_ptr,
335 sizeof(list_item->sensor_name));
336 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
338 pthread_mutex_unlock(&sensor_list_lock);
340 if (c_ipmi_notify_add && (c_ipmi_init_in_progress == 0)) {
341 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
343 sstrncpy(n.host, hostname_g, sizeof(n.host));
344 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
345 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
346 snprintf(n.message, sizeof(n.message), "sensor %s added",
347 list_item->sensor_name);
349 plugin_dispatch_notification(&n);
353 } /* int sensor_list_add */
355 static int sensor_list_remove(ipmi_sensor_t *sensor) {
356 ipmi_sensor_id_t sensor_id;
357 c_ipmi_sensor_list_t *list_item;
358 c_ipmi_sensor_list_t *list_prev;
360 sensor_id = ipmi_sensor_convert_to_id(sensor);
362 pthread_mutex_lock(&sensor_list_lock);
365 for (list_item = sensor_list; list_item != NULL;
366 list_item = list_item->next) {
367 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
369 list_prev = list_item;
370 } /* for (list_item) */
372 if (list_item == NULL) {
373 pthread_mutex_unlock(&sensor_list_lock);
377 if (list_prev == NULL)
378 sensor_list = list_item->next;
380 list_prev->next = list_item->next;
383 list_item->next = NULL;
385 pthread_mutex_unlock(&sensor_list_lock);
387 if (c_ipmi_notify_remove && c_ipmi_active) {
388 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "",
391 sstrncpy(n.host, hostname_g, sizeof(n.host));
392 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
393 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
394 snprintf(n.message, sizeof(n.message), "sensor %s removed",
395 list_item->sensor_name);
397 plugin_dispatch_notification(&n);
402 } /* int sensor_list_remove */
404 static int sensor_list_read_all(void) {
405 pthread_mutex_lock(&sensor_list_lock);
407 for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
408 list_item = list_item->next) {
409 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
410 /* user data = */ list_item);
411 } /* for (list_item) */
413 pthread_mutex_unlock(&sensor_list_lock);
416 } /* int sensor_list_read_all */
418 static int sensor_list_remove_all(void) {
419 c_ipmi_sensor_list_t *list_item;
421 pthread_mutex_lock(&sensor_list_lock);
423 list_item = sensor_list;
426 pthread_mutex_unlock(&sensor_list_lock);
428 while (list_item != NULL) {
429 c_ipmi_sensor_list_t *list_next = list_item->next;
433 list_item = list_next;
434 } /* while (list_item) */
437 } /* int sensor_list_remove_all */
439 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
441 case IPMI_LOWER_NON_CRITICAL:
442 case IPMI_UPPER_NON_CRITICAL:
444 case IPMI_LOWER_CRITICAL:
445 case IPMI_UPPER_CRITICAL:
446 return NOTIF_WARNING;
447 case IPMI_LOWER_NON_RECOVERABLE:
448 case IPMI_UPPER_NON_RECOVERABLE:
449 return NOTIF_FAILURE;
452 } /* switch (severity) */
453 } /* int sensor_convert_threshold_severity */
455 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
456 enum ipmi_event_dir_e dir,
457 ipmi_event_t *event) {
458 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
460 plugin_notification_meta_add_string(n, "entity_name",
461 ipmi_entity_get_entity_id_string(ent));
462 plugin_notification_meta_add_signed_int(n, "entity_id",
463 ipmi_entity_get_entity_id(ent));
464 plugin_notification_meta_add_signed_int(n, "entity_instance",
465 ipmi_entity_get_entity_instance(ent));
466 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
469 plugin_notification_meta_add_signed_int(n, "event_type",
470 ipmi_event_get_type(event));
471 } /* void add_event_sensor_meta_data */
473 static int sensor_threshold_event_handler(
474 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
475 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
476 enum ipmi_value_present_e value_present, unsigned int raw_value,
477 double value, void *cb_data, ipmi_event_t *event) {
479 /* From the IPMI specification Chapter 2: Events.
480 * If a callback handles the event, then all future callbacks called due to
481 * the event will receive a NULL for the event. So be ready to handle a NULL
482 * event in all your event handlers. A NULL may also be passed to an event
483 * handler if the callback was not due to an event. */
485 return IPMI_EVENT_NOT_HANDLED;
487 /* offset is a table index and it's represented as enum of strings that are
488 organized in the way - high and low for each threshold severity level */
489 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
490 unsigned int offset = (2 * threshold) + high_low;
491 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
492 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
493 const char *event_state =
494 ipmi_get_reading_name(event_type, sensor_type, offset);
495 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
496 if (value_present != IPMI_NO_VALUES_PRESENT)
497 snprintf(n.message, sizeof(n.message),
498 "sensor %s received event: %s, value is %f", n.type_instance,
501 snprintf(n.message, sizeof(n.message),
502 "sensor %s received event: %s, value not provided",
503 n.type_instance, event_state);
505 DEBUG("Threshold event received for sensor %s", n.type_instance);
507 sstrncpy(n.host, hostname_g, sizeof(n.host));
508 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
509 n.severity = sensor_convert_threshold_severity(threshold);
510 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
512 plugin_notification_meta_add_string(&n, "severity",
513 ipmi_get_threshold_string(threshold));
514 plugin_notification_meta_add_string(&n, "direction",
515 ipmi_get_value_dir_string(high_low));
517 switch (value_present) {
518 case IPMI_BOTH_VALUES_PRESENT:
519 plugin_notification_meta_add_double(&n, "val", value);
520 /* both values present, so fall-through to add raw value too */
521 case IPMI_RAW_VALUE_PRESENT: {
522 char buf[DATA_MAX_NAME_LEN] = {0};
523 snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
524 plugin_notification_meta_add_string(&n, "raw", buf);
528 } /* switch (value_present) */
530 add_event_common_data(&n, sensor, dir, event);
532 plugin_dispatch_notification(&n);
533 plugin_notification_meta_free(n.meta);
535 /* Delete handled ipmi event from the list */
536 if (c_ipmi_sel_clear_event) {
537 ipmi_event_delete(event, NULL, NULL);
538 return IPMI_EVENT_HANDLED;
541 return IPMI_EVENT_NOT_HANDLED;
542 } /* int sensor_threshold_event_handler */
544 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
545 enum ipmi_event_dir_e dir, int offset,
546 int severity, int prev_severity,
547 void *cb_data, ipmi_event_t *event) {
548 /* From the IPMI specification Chapter 2: Events.
549 * If a callback handles the event, then all future callbacks called due to
550 * the event will receive a NULL for the event. So be ready to handle a NULL
551 * event in all your event handlers. A NULL may also be passed to an event
552 * handler if the callback was not due to an event. */
554 return IPMI_EVENT_NOT_HANDLED;
556 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
557 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
558 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
559 const char *event_state =
560 ipmi_get_reading_name(event_type, sensor_type, offset);
561 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
562 snprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
563 n.type_instance, event_state);
565 DEBUG("Discrete event received for sensor %s", n.type_instance);
567 sstrncpy(n.host, hostname_g, sizeof(n.host));
568 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
569 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
571 plugin_notification_meta_add_signed_int(&n, "offset", offset);
574 plugin_notification_meta_add_signed_int(&n, "severity", severity);
576 if (prev_severity != -1)
577 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
579 add_event_common_data(&n, sensor, dir, event);
581 plugin_dispatch_notification(&n);
582 plugin_notification_meta_free(n.meta);
584 /* Delete handled ipmi event from the list */
585 if (c_ipmi_sel_clear_event) {
586 ipmi_event_delete(event, NULL, NULL);
587 return IPMI_EVENT_HANDLED;
590 return IPMI_EVENT_NOT_HANDLED;
591 } /* int sensor_discrete_event_handler */
596 static void entity_sensor_update_handler(
597 enum ipmi_update_e op, ipmi_entity_t __attribute__((unused)) * entity,
598 ipmi_sensor_t *sensor, void __attribute__((unused)) * user_data) {
599 /* TODO: Ignore sensors we cannot read */
601 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
602 /* Will check for duplicate entries.. */
603 sensor_list_add(sensor);
605 if (c_ipmi_sel_enabled) {
607 /* register threshold event if threshold sensor support events */
608 if ((ipmi_sensor_get_event_reading_type(sensor) ==
609 IPMI_EVENT_READING_TYPE_THRESHOLD) &&
610 (ipmi_sensor_get_threshold_access(sensor) !=
611 IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
612 status = ipmi_sensor_add_threshold_event_handler(
613 sensor, sensor_threshold_event_handler, NULL);
614 /* register discrete handler if discrete/specific sensor support events */
615 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
616 status = ipmi_sensor_add_discrete_event_handler(
617 sensor, sensor_discrete_event_handler, NULL);
620 char buf[DATA_MAX_NAME_LEN] = {0};
621 sensor_get_name(sensor, buf, sizeof(buf));
622 ERROR("Unable to add sensor %s event handler, status: %d", buf, status);
625 } else if (op == IPMI_DELETED) {
626 sensor_list_remove(sensor);
628 if (c_ipmi_sel_enabled) {
629 if (ipmi_sensor_get_event_reading_type(sensor) ==
630 IPMI_EVENT_READING_TYPE_THRESHOLD)
631 ipmi_sensor_remove_threshold_event_handler(
632 sensor, sensor_threshold_event_handler, NULL);
634 ipmi_sensor_remove_discrete_event_handler(
635 sensor, sensor_discrete_event_handler, NULL);
638 } /* void entity_sensor_update_handler */
643 static void domain_entity_update_handler(
644 enum ipmi_update_e op, ipmi_domain_t __attribute__((unused)) * domain,
645 ipmi_entity_t *entity, void __attribute__((unused)) * user_data) {
648 if (op == IPMI_ADDED) {
649 status = ipmi_entity_add_sensor_update_handler(
650 entity, entity_sensor_update_handler, /* user data = */ NULL);
652 c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
654 } else if (op == IPMI_DELETED) {
655 status = ipmi_entity_remove_sensor_update_handler(
656 entity, entity_sensor_update_handler, /* user data = */ NULL);
658 c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
661 } /* void domain_entity_update_handler */
663 static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi,
664 const ipmi_addr_t __attribute__((unused)) * addr,
665 unsigned int __attribute__((unused)) addr_len,
666 ipmi_event_t *event, void *cb_data) {
667 unsigned int type = ipmi_event_get_type(event);
668 ipmi_domain_t *domain = cb_data;
670 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
673 /* It's not a standard IPMI event. */
676 /* force domain to reread SELs */
677 ipmi_domain_reread_sels(domain, NULL, NULL);
680 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
681 unsigned int conn_num,
682 unsigned int port_num,
687 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
688 "conn_num = %u, port_num = %u, still_connected = %i, "
689 "user_data = %p);\n",
690 (void *)domain, err, conn_num, port_num, still_connected, user_data);
692 status = ipmi_domain_add_entity_update_handler(
693 domain, domain_entity_update_handler, /* user data = */ NULL);
695 c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
698 ipmi_con_t *smi_connection = user_data;
699 status = smi_connection->add_event_handler(smi_connection, smi_event_handler,
703 c_ipmi_error("Failed to register smi event handler", status);
704 } /* void domain_connection_change_handler */
706 static int thread_init(os_handler_t **ret_os_handler) {
707 os_handler_t *os_handler;
708 ipmi_con_t *smi_connection = NULL;
709 ipmi_domain_id_t domain_id;
712 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
713 if (os_handler == NULL) {
714 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
718 ipmi_init(os_handler);
720 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
721 /* user data = */ NULL, &smi_connection);
723 c_ipmi_error("ipmi_smi_setup_con", status);
727 ipmi_open_option_t open_option[1] = {[0] = {.option = IPMI_OPEN_OPTION_ALL,
730 status = ipmi_open_domain(
731 "mydomain", &smi_connection, /* num_con = */ 1,
732 domain_connection_change_handler,
733 /* user data = */ (void *)smi_connection,
734 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
735 sizeof(open_option) / sizeof(open_option[0]), &domain_id);
737 c_ipmi_error("ipmi_open_domain", status);
741 *ret_os_handler = os_handler;
743 } /* int thread_init */
745 static void *thread_main(void __attribute__((unused)) * user_data) {
747 os_handler_t *os_handler = NULL;
749 status = thread_init(&os_handler);
751 ERROR("ipmi plugin: thread_init failed.\n");
755 while (c_ipmi_active != 0) {
756 struct timeval tv = {1, 0};
757 os_handler->perform_one_op(os_handler, &tv);
760 ipmi_posix_thread_free_os_handler(os_handler);
763 } /* void *thread_main */
765 static int c_ipmi_config(const char *key, const char *value) {
766 if (ignorelist == NULL)
767 ignorelist = ignorelist_create(/* invert = */ 1);
768 if (ignorelist == NULL)
771 if (strcasecmp("Sensor", key) == 0) {
772 ignorelist_add(ignorelist, value);
773 } else if (strcasecmp("IgnoreSelected", key) == 0) {
774 ignorelist_set_invert(ignorelist, !IS_TRUE(value));
775 } else if (strcasecmp("NotifySensorAdd", key) == 0) {
776 c_ipmi_notify_add = IS_TRUE(value);
777 } else if (strcasecmp("NotifySensorRemove", key) == 0) {
778 c_ipmi_notify_remove = IS_TRUE(value);
779 } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
780 c_ipmi_notify_notpresent = IS_TRUE(value);
781 } else if (strcasecmp("SELEnabled", key) == 0) {
782 c_ipmi_sel_enabled = IS_TRUE(value);
783 } else if (strcasecmp("SELClearEvent", key) == 0) {
784 c_ipmi_sel_clear_event = IS_TRUE(value);
790 } /* int c_ipmi_config */
792 static int c_ipmi_init(void) {
795 /* Don't send `ADD' notifications during startup (~ 1 minute) */
796 time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
797 c_ipmi_init_in_progress = 1 + (60 / iv);
801 status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
802 /* user data = */ NULL, "ipmi");
805 thread_id = (pthread_t)0;
806 ERROR("ipmi plugin: pthread_create failed.");
811 } /* int c_ipmi_init */
813 static int c_ipmi_read(void) {
814 if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
815 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
819 sensor_list_read_all();
821 if (c_ipmi_init_in_progress > 0)
822 c_ipmi_init_in_progress--;
824 c_ipmi_init_in_progress = 0;
827 } /* int c_ipmi_read */
829 static int c_ipmi_shutdown(void) {
832 if (thread_id != (pthread_t)0) {
833 pthread_join(thread_id, NULL);
834 thread_id = (pthread_t)0;
837 sensor_list_remove_all();
840 } /* int c_ipmi_shutdown */
842 void module_register(void) {
843 plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
844 plugin_register_init("ipmi", c_ipmi_init);
845 plugin_register_read("ipmi", c_ipmi_read);
846 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
847 } /* void module_register */