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", "IgnoreSelected",
63 "NotifySensorAdd", "NotifySensorRemove",
64 "NotifySensorNotPresent", "SELEnabled",
66 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
68 static ignorelist_t *ignorelist = NULL;
70 static int c_ipmi_notify_add = 0;
71 static int c_ipmi_notify_remove = 0;
72 static int c_ipmi_notify_notpresent = 0;
73 static int c_ipmi_sel_enabled = 0;
74 static int c_ipmi_sel_clear_event = 0;
77 * Misc private functions
79 static void c_ipmi_error(const char *func, int status) {
80 char errbuf[4096] = {0};
82 if (IPMI_IS_OS_ERR(status)) {
83 sstrerror(IPMI_GET_OS_ERR(status), errbuf, sizeof(errbuf));
84 } else if (IPMI_IS_IPMI_ERR(status)) {
85 ipmi_get_error_string(IPMI_GET_IPMI_ERR(status), errbuf, sizeof(errbuf));
89 ssnprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
91 errbuf[sizeof(errbuf) - 1] = 0;
93 ERROR("ipmi plugin: %s failed: %s", func, errbuf);
94 } /* void c_ipmi_error */
99 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
100 static int sensor_list_remove(ipmi_sensor_t *sensor);
102 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
103 enum ipmi_value_present_e value_present,
104 unsigned int __attribute__((unused)) raw_value,
106 ipmi_states_t __attribute__((unused)) * states,
108 value_list_t vl = VALUE_LIST_INIT;
110 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
113 if ((err & 0xff) == IPMI_NOT_PRESENT_CC) {
114 if (list_item->sensor_not_present == 0) {
115 list_item->sensor_not_present = 1;
117 INFO("ipmi plugin: sensor_read_handler: sensor %s "
119 list_item->sensor_name);
121 if (c_ipmi_notify_notpresent) {
122 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "", NULL};
124 sstrncpy(n.host, hostname_g, sizeof(n.host));
125 sstrncpy(n.type_instance, list_item->sensor_name,
126 sizeof(n.type_instance));
127 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
128 ssnprintf(n.message, sizeof(n.message), "sensor %s not present",
129 list_item->sensor_name);
131 plugin_dispatch_notification(&n);
134 } else if (IPMI_IS_IPMI_ERR(err) &&
135 IPMI_GET_IPMI_ERR(err) ==
136 IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
137 INFO("ipmi plugin: sensor_read_handler: Sensor %s not ready",
138 list_item->sensor_name);
140 if (IPMI_IS_IPMI_ERR(err))
141 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
142 "because it failed with IPMI error %#x.",
143 list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
144 else if (IPMI_IS_OS_ERR(err))
145 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
146 "because it failed with OS error %#x.",
147 list_item->sensor_name, IPMI_GET_OS_ERR(err));
148 else if (IPMI_IS_RMCPP_ERR(err))
149 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
150 "because it failed with RMCPP error %#x.",
151 list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
152 else if (IPMI_IS_SOL_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_SOL_ERR(err));
157 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
158 "because it failed with error %#x. of class %#x",
159 list_item->sensor_name, err & 0xff, err & 0xffffff00);
160 sensor_list_remove(sensor);
163 } else if (list_item->sensor_not_present == 1) {
164 list_item->sensor_not_present = 0;
166 INFO("ipmi plugin: sensor_read_handler: sensor %s present.",
167 list_item->sensor_name);
169 if (c_ipmi_notify_notpresent) {
170 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "",
173 sstrncpy(n.host, hostname_g, sizeof(n.host));
174 sstrncpy(n.type_instance, list_item->sensor_name,
175 sizeof(n.type_instance));
176 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
177 ssnprintf(n.message, sizeof(n.message), "sensor %s present",
178 list_item->sensor_name);
180 plugin_dispatch_notification(&n);
184 if (value_present != IPMI_BOTH_VALUES_PRESENT) {
185 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
186 "because it provides %s. If you need this sensor, "
187 "please file a bug report.",
188 list_item->sensor_name,
189 (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
191 sensor_list_remove(sensor);
195 vl.values = &(value_t){.gauge = value};
198 sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
199 sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
200 sstrncpy(vl.type_instance, list_item->sensor_name, sizeof(vl.type_instance));
202 plugin_dispatch_values(&vl);
203 } /* void sensor_read_handler */
205 static void sensor_get_name(ipmi_sensor_t *sensor, char *buf, int buf_len) {
206 char buffer[DATA_MAX_NAME_LEN] = {0};
207 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
208 const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
209 char sensor_name[DATA_MAX_NAME_LEN] = "";
210 char *sensor_name_ptr;
212 ipmi_sensor_get_name(sensor, buffer, sizeof(buffer));
213 buffer[sizeof(buffer) - 1] = 0;
215 if (entity_id_string != NULL && strlen(buffer))
216 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", buffer,
218 else if (entity_id_string != NULL)
219 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
221 sstrncpy(sensor_name, buffer, sizeof(sensor_name));
223 if (strlen(buffer)) {
224 sstrncpy(buffer, sensor_name, sizeof(buffer));
225 sensor_name_ptr = strstr(buffer, ").");
226 if (sensor_name_ptr != NULL) {
227 /* If name is something like "foo (123).bar",
228 * change that to "bar (123)".
229 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
230 * `buffer' array, which holds a copy of the current `sensor_name'. */
233 /* `sensor_name_ptr' points to ").bar". */
234 sensor_name_ptr[1] = 0;
235 /* `buffer' holds "foo (123)\0bar\0". */
236 sensor_name_ptr += 2;
237 /* `sensor_name_ptr' now points to "bar". */
239 sensor_id_ptr = strstr(buffer, "(");
240 if (sensor_id_ptr != NULL) {
241 /* `sensor_id_ptr' now points to "(123)". */
242 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
245 /* else: don't touch sensor_name. */
250 sstrncpy(buf, sensor_name, buf_len);
253 static int sensor_list_add(ipmi_sensor_t *sensor) {
254 ipmi_sensor_id_t sensor_id;
255 c_ipmi_sensor_list_t *list_item;
256 c_ipmi_sensor_list_t *list_prev;
258 char buffer[DATA_MAX_NAME_LEN] = {0};
259 char *sensor_name_ptr = buffer;
263 sensor_id = ipmi_sensor_convert_to_id(sensor);
264 sensor_get_name(sensor, buffer, sizeof(buffer));
266 /* Both `ignorelist' and `plugin_instance' may be NULL. */
267 if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
270 /* FIXME: Use rate unit or base unit to scale the value */
272 sensor_type = ipmi_sensor_get_sensor_type(sensor);
273 switch (sensor_type) {
274 case IPMI_SENSOR_TYPE_TEMPERATURE:
275 type = "temperature";
278 case IPMI_SENSOR_TYPE_VOLTAGE:
282 case IPMI_SENSOR_TYPE_CURRENT:
286 case IPMI_SENSOR_TYPE_FAN:
291 const char *sensor_type_str;
293 sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
294 INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
295 "because I don't know how to handle its type (%#x, %s). "
296 "If you need this sensor, please file a bug report.",
297 sensor_name_ptr, sensor_type, sensor_type_str);
300 } /* switch (sensor_type) */
302 pthread_mutex_lock(&sensor_list_lock);
305 for (list_item = sensor_list; list_item != NULL;
306 list_item = list_item->next) {
307 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
309 list_prev = list_item;
310 } /* for (list_item) */
312 if (list_item != NULL) {
313 pthread_mutex_unlock(&sensor_list_lock);
317 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
318 if (list_item == NULL) {
319 pthread_mutex_unlock(&sensor_list_lock);
323 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
325 if (list_prev != NULL)
326 list_prev->next = list_item;
328 sensor_list = list_item;
330 sstrncpy(list_item->sensor_name, sensor_name_ptr,
331 sizeof(list_item->sensor_name));
332 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
334 pthread_mutex_unlock(&sensor_list_lock);
336 if (c_ipmi_notify_add && (c_ipmi_init_in_progress == 0)) {
337 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
339 sstrncpy(n.host, hostname_g, sizeof(n.host));
340 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
341 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
342 ssnprintf(n.message, sizeof(n.message), "sensor %s added",
343 list_item->sensor_name);
345 plugin_dispatch_notification(&n);
349 } /* int sensor_list_add */
351 static int sensor_list_remove(ipmi_sensor_t *sensor) {
352 ipmi_sensor_id_t sensor_id;
353 c_ipmi_sensor_list_t *list_item;
354 c_ipmi_sensor_list_t *list_prev;
356 sensor_id = ipmi_sensor_convert_to_id(sensor);
358 pthread_mutex_lock(&sensor_list_lock);
361 for (list_item = sensor_list; list_item != NULL;
362 list_item = list_item->next) {
363 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
365 list_prev = list_item;
366 } /* for (list_item) */
368 if (list_item == NULL) {
369 pthread_mutex_unlock(&sensor_list_lock);
373 if (list_prev == NULL)
374 sensor_list = list_item->next;
376 list_prev->next = list_item->next;
379 list_item->next = NULL;
381 pthread_mutex_unlock(&sensor_list_lock);
383 if (c_ipmi_notify_remove && c_ipmi_active) {
384 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi",
387 sstrncpy(n.host, hostname_g, sizeof(n.host));
388 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
389 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
390 ssnprintf(n.message, sizeof(n.message), "sensor %s removed",
391 list_item->sensor_name);
393 plugin_dispatch_notification(&n);
398 } /* int sensor_list_remove */
400 static int sensor_list_read_all(void) {
401 pthread_mutex_lock(&sensor_list_lock);
403 for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
404 list_item = list_item->next) {
405 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
406 /* user data = */ list_item);
407 } /* for (list_item) */
409 pthread_mutex_unlock(&sensor_list_lock);
412 } /* int sensor_list_read_all */
414 static int sensor_list_remove_all(void) {
415 c_ipmi_sensor_list_t *list_item;
417 pthread_mutex_lock(&sensor_list_lock);
419 list_item = sensor_list;
422 pthread_mutex_unlock(&sensor_list_lock);
424 while (list_item != NULL) {
425 c_ipmi_sensor_list_t *list_next = list_item->next;
429 list_item = list_next;
430 } /* while (list_item) */
433 } /* int sensor_list_remove_all */
435 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
436 int _severity = NOTIF_OKAY;
439 case IPMI_LOWER_NON_CRITICAL:
440 case IPMI_UPPER_NON_CRITICAL:
441 _severity = NOTIF_OKAY;
443 case IPMI_LOWER_CRITICAL:
444 case IPMI_UPPER_CRITICAL:
445 _severity = NOTIF_WARNING;
447 case IPMI_LOWER_NON_RECOVERABLE:
448 case IPMI_UPPER_NON_RECOVERABLE:
449 _severity = NOTIF_FAILURE;
453 } /* switch (severity) */
456 } /* int sensor_convert_threshold_severity */
458 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
459 enum ipmi_event_dir_e dir,
460 ipmi_event_t *event) {
461 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
463 plugin_notification_meta_add_string(n, "entity_name",
464 ipmi_entity_get_entity_id_string(ent));
465 plugin_notification_meta_add_signed_int(n, "entity_id",
466 ipmi_entity_get_entity_id(ent));
467 plugin_notification_meta_add_signed_int(n, "entity_instance",
468 ipmi_entity_get_entity_instance(ent));
469 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
472 plugin_notification_meta_add_signed_int(n, "event_type",
473 ipmi_event_get_type(event));
474 } /* void add_event_sensor_meta_data */
476 static int sensor_threshold_event_handler(
477 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
478 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
479 enum ipmi_value_present_e value_present, unsigned int raw_value,
480 double value, void *cb_data, ipmi_event_t *event) {
481 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
482 /* offset is a table index and it's represented as enum of strings that are
483 organized in the way - high and low for each threshold severity level */
484 unsigned int offset = (2 * threshold) + high_low;
485 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
486 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
487 const char *event_state =
488 ipmi_get_reading_name(event_type, sensor_type, offset);
489 char buf[DATA_MAX_NAME_LEN] = {0};
491 /* From the IPMI specification Chapter 2: Events.
492 * If a callback handles the event, then all future callbacks called due to
493 * the event will receive a NULL for the event. So be ready to handle a NULL
494 * event in all your event handlers. A NULL may also be passed to an event
495 * handler if the callback was not due to an event. */
497 return (IPMI_EVENT_NOT_HANDLED);
499 sensor_get_name(sensor, buf, sizeof(buf));
500 sstrncpy(n.type_instance, buf, sizeof(n.type_instance));
501 if (value_present != IPMI_NO_VALUES_PRESENT)
502 ssnprintf(n.message, sizeof(n.message),
503 "sensor %s received event: %s, value is %f", buf, event_state,
506 ssnprintf(n.message, sizeof(n.message),
507 "sensor %s received event: %s, value not provided", buf,
510 DEBUG("Threshold event received for sensor %s", buf);
512 sstrncpy(n.host, hostname_g, sizeof(n.host));
513 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
514 n.severity = sensor_convert_threshold_severity(threshold);
515 n.time = ipmi_event_get_timestamp(event);
517 plugin_notification_meta_add_string(&n, "severity",
518 ipmi_get_threshold_string(threshold));
519 plugin_notification_meta_add_string(&n, "direction",
520 ipmi_get_value_dir_string(high_low));
522 switch (value_present) {
523 case IPMI_BOTH_VALUES_PRESENT:
524 plugin_notification_meta_add_double(&n, "val", value);
525 case IPMI_RAW_VALUE_PRESENT:
526 snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
527 plugin_notification_meta_add_string(&n, "raw", buf);
531 } /* switch (value_present) */
533 add_event_common_data(&n, sensor, dir, event);
535 plugin_dispatch_notification(&n);
537 /* Delete handled ipmi event from the list */
538 if (c_ipmi_sel_clear_event) {
539 ipmi_event_delete(event, NULL, NULL);
540 return (IPMI_EVENT_HANDLED);
543 return (IPMI_EVENT_NOT_HANDLED);
544 } /* int sensor_threshold_event_handler */
546 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
547 enum ipmi_event_dir_e dir, int offset,
548 int severity, int prev_severity,
549 void *cb_data, ipmi_event_t *event) {
550 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
551 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
552 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
553 const char *event_state =
554 ipmi_get_reading_name(event_type, sensor_type, offset);
555 char buf[DATA_MAX_NAME_LEN] = {0};
557 /* From the IPMI specification Chapter 2: Events.
558 * If a callback handles the event, then all future callbacks called due to
559 * the event will receive a NULL for the event. So be ready to handle a NULL
560 * event in all your event handlers. A NULL may also be passed to an event
561 * handler if the callback was not due to an event. */
563 return (IPMI_EVENT_NOT_HANDLED);
565 sensor_get_name(sensor, buf, sizeof(buf));
566 sstrncpy(n.type_instance, buf, sizeof(n.type_instance));
567 ssnprintf(n.message, sizeof(n.message), "sensor %s received event: %s", buf,
570 DEBUG("Discrete event received for sensor %s", buf);
572 sstrncpy(n.host, hostname_g, sizeof(n.host));
573 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
574 n.time = ipmi_event_get_timestamp(event);
576 plugin_notification_meta_add_signed_int(&n, "offset", offset);
579 plugin_notification_meta_add_signed_int(&n, "severity", severity);
581 if (prev_severity != -1)
582 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
584 add_event_common_data(&n, sensor, dir, event);
586 plugin_dispatch_notification(&n);
588 /* Delete handled ipmi event from the list */
589 if (c_ipmi_sel_clear_event) {
590 ipmi_event_delete(event, NULL, NULL);
591 return (IPMI_EVENT_HANDLED);
594 return (IPMI_EVENT_NOT_HANDLED);
595 } /* int sensor_discrete_event_handler */
600 static void entity_sensor_update_handler(
601 enum ipmi_update_e op, ipmi_entity_t __attribute__((unused)) * entity,
602 ipmi_sensor_t *sensor, void __attribute__((unused)) * user_data) {
603 /* TODO: Ignore sensors we cannot read */
605 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
606 /* Will check for duplicate entries.. */
607 sensor_list_add(sensor);
609 if (c_ipmi_sel_enabled) {
611 /* register threshold event if threshold sensor support events */
612 if ((ipmi_sensor_get_event_reading_type(sensor) ==
613 IPMI_EVENT_READING_TYPE_THRESHOLD) &&
614 (ipmi_sensor_get_threshold_access(sensor) !=
615 IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
616 status = ipmi_sensor_add_threshold_event_handler(
617 sensor, sensor_threshold_event_handler, NULL);
618 /* register discrete handler if discrete/specific sensor support events */
619 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
620 status = ipmi_sensor_add_discrete_event_handler(
621 sensor, sensor_discrete_event_handler, NULL);
624 char buf[DATA_MAX_NAME_LEN] = {0};
625 sensor_get_name(sensor, buf, sizeof(buf));
626 ERROR("Unable to add sensor %s event handler, status: %d", buf,
630 } else if (op == IPMI_DELETED) {
631 sensor_list_remove(sensor);
633 if (c_ipmi_sel_enabled) {
634 if (ipmi_sensor_get_event_reading_type(sensor) ==
635 IPMI_EVENT_READING_TYPE_THRESHOLD)
636 ipmi_sensor_remove_threshold_event_handler(
637 sensor, sensor_threshold_event_handler, NULL);
639 ipmi_sensor_remove_discrete_event_handler(
640 sensor, sensor_discrete_event_handler, NULL);
643 } /* void entity_sensor_update_handler */
648 static void domain_entity_update_handler(
649 enum ipmi_update_e op, ipmi_domain_t __attribute__((unused)) * domain,
650 ipmi_entity_t * entity, void __attribute__((unused)) * user_data) {
653 if (op == IPMI_ADDED) {
654 status = ipmi_entity_add_sensor_update_handler(
655 entity, entity_sensor_update_handler, /* user data = */ NULL);
657 c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
659 } else if (op == IPMI_DELETED) {
660 status = ipmi_entity_remove_sensor_update_handler(
661 entity, entity_sensor_update_handler, /* user data = */ NULL);
663 c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
666 } /* void domain_entity_update_handler */
668 static void smi_event_handler(ipmi_con_t __attribute__((unused)) *ipmi,
669 const ipmi_addr_t __attribute__((unused)) *addr,
670 unsigned int __attribute__((unused)) addr_len,
673 unsigned int type = ipmi_event_get_type(event);
674 ipmi_domain_t *domain = cb_data;
676 DEBUG("%s: Event received: type %u", __FUNCTION__, type);
679 /* It's not a standard IPMI event. */
682 /* force domain to reread SELs */
683 ipmi_domain_reread_sels(domain, NULL, NULL);
686 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
687 unsigned int conn_num,
688 unsigned int port_num,
693 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
694 "conn_num = %u, port_num = %u, still_connected = %i, "
695 "user_data = %p);\n",
696 (void *)domain, err, conn_num, port_num, still_connected, user_data);
698 status = ipmi_domain_add_entity_update_handler(
699 domain, domain_entity_update_handler, /* user data = */ NULL);
701 c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
704 ipmi_con_t *smi_connection = user_data;
705 status = smi_connection->add_event_handler(smi_connection, smi_event_handler,
709 c_ipmi_error("Failed to register smi event handler", status);
710 } /* void domain_connection_change_handler */
712 static int thread_init(os_handler_t **ret_os_handler) {
713 os_handler_t *os_handler;
714 ipmi_con_t *smi_connection = NULL;
715 ipmi_domain_id_t domain_id;
718 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
719 if (os_handler == NULL) {
720 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
724 ipmi_init(os_handler);
726 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
727 /* user data = */ NULL, &smi_connection);
729 c_ipmi_error("ipmi_smi_setup_con", status);
733 ipmi_open_option_t open_option[1] = {[0] = {.option = IPMI_OPEN_OPTION_ALL,
736 status = ipmi_open_domain(
737 "mydomain", &smi_connection, /* num_con = */ 1,
738 domain_connection_change_handler,
739 /* user data = */ (void*) smi_connection,
740 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
741 sizeof(open_option) / sizeof(open_option[0]), &domain_id);
743 c_ipmi_error("ipmi_open_domain", status);
747 *ret_os_handler = os_handler;
749 } /* int thread_init */
751 static void *thread_main(void __attribute__((unused)) * user_data) {
753 os_handler_t *os_handler = NULL;
755 status = thread_init(&os_handler);
757 ERROR("ipmi plugin: thread_init failed.\n");
761 while (c_ipmi_active != 0) {
762 struct timeval tv = {1, 0};
763 os_handler->perform_one_op(os_handler, &tv);
766 ipmi_posix_thread_free_os_handler(os_handler);
769 } /* void *thread_main */
771 static int c_ipmi_config(const char *key, const char *value) {
772 if (ignorelist == NULL)
773 ignorelist = ignorelist_create(/* invert = */ 1);
774 if (ignorelist == NULL)
777 if (strcasecmp("Sensor", key) == 0) {
778 ignorelist_add(ignorelist, value);
779 } else if (strcasecmp("IgnoreSelected", key) == 0) {
783 ignorelist_set_invert(ignorelist, invert);
784 } else if (strcasecmp("NotifySensorAdd", key) == 0) {
786 c_ipmi_notify_add = 1;
787 } else if (strcasecmp("NotifySensorRemove", key) == 0) {
789 c_ipmi_notify_remove = 1;
790 } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
792 c_ipmi_notify_notpresent = 1;
793 } else if (strcasecmp("SELEnabled", key) == 0) {
795 c_ipmi_sel_enabled = 1;
796 } else if (strcasecmp("SELClearEvent", key) == 0) {
798 c_ipmi_sel_clear_event = 1;
804 } /* int c_ipmi_config */
806 static int c_ipmi_init(void) {
809 /* Don't send `ADD' notifications during startup (~ 1 minute) */
810 time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
811 c_ipmi_init_in_progress = 1 + (60 / iv);
815 status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
816 /* user data = */ NULL, "ipmi");
819 thread_id = (pthread_t)0;
820 ERROR("ipmi plugin: pthread_create failed.");
825 } /* int c_ipmi_init */
827 static int c_ipmi_read(void) {
828 if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
829 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
833 sensor_list_read_all();
835 if (c_ipmi_init_in_progress > 0)
836 c_ipmi_init_in_progress--;
838 c_ipmi_init_in_progress = 0;
841 } /* int c_ipmi_read */
843 static int c_ipmi_shutdown(void) {
846 if (thread_id != (pthread_t)0) {
847 pthread_join(thread_id, NULL);
848 thread_id = (pthread_t)0;
851 sensor_list_remove_all();
854 } /* int c_ipmi_shutdown */
856 void module_register(void) {
857 plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
858 plugin_register_init("ipmi", c_ipmi_init);
859 plugin_register_read("ipmi", c_ipmi_read);
860 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
861 } /* void module_register */