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 _Bool c_ipmi_notify_add = 0;
71 static _Bool c_ipmi_notify_remove = 0;
72 static _Bool c_ipmi_notify_notpresent = 0;
73 static _Bool c_ipmi_sel_enabled = 0;
74 static _Bool 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 *buffer, int buf_len) {
206 char temp[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 if ((buffer == NULL) || (buf_len == 0))
215 ipmi_sensor_get_name(sensor, temp, sizeof(temp));
216 temp[sizeof(temp) - 1] = 0;
218 if (entity_id_string != NULL && strlen(temp))
219 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", temp,
221 else if (entity_id_string != NULL)
222 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
224 sstrncpy(sensor_name, temp, sizeof(sensor_name));
227 sstrncpy(temp, sensor_name, sizeof(temp));
228 sensor_name_ptr = strstr(temp, ").");
229 if (sensor_name_ptr != NULL) {
230 /* If name is something like "foo (123).bar",
231 * change that to "bar (123)".
232 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
233 * `temp' array, which holds a copy of the current `sensor_name'. */
236 /* `sensor_name_ptr' points to ").bar". */
237 sensor_name_ptr[1] = 0;
238 /* `temp' holds "foo (123)\0bar\0". */
239 sensor_name_ptr += 2;
240 /* `sensor_name_ptr' now points to "bar". */
242 sensor_id_ptr = strstr(temp, "(");
243 if (sensor_id_ptr != NULL) {
244 /* `sensor_id_ptr' now points to "(123)". */
245 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
248 /* else: don't touch sensor_name. */
251 sstrncpy(buffer, sensor_name, buf_len);
254 static int sensor_list_add(ipmi_sensor_t *sensor) {
255 ipmi_sensor_id_t sensor_id;
256 c_ipmi_sensor_list_t *list_item;
257 c_ipmi_sensor_list_t *list_prev;
259 char buffer[DATA_MAX_NAME_LEN] = {0};
260 char *sensor_name_ptr = buffer;
264 sensor_id = ipmi_sensor_convert_to_id(sensor);
265 sensor_get_name(sensor, buffer, sizeof(buffer));
267 /* Both `ignorelist' and `plugin_instance' may be NULL. */
268 if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
271 /* FIXME: Use rate unit or base unit to scale the value */
273 sensor_type = ipmi_sensor_get_sensor_type(sensor);
274 switch (sensor_type) {
275 case IPMI_SENSOR_TYPE_TEMPERATURE:
276 type = "temperature";
279 case IPMI_SENSOR_TYPE_VOLTAGE:
283 case IPMI_SENSOR_TYPE_CURRENT:
287 case IPMI_SENSOR_TYPE_FAN:
292 const char *sensor_type_str;
294 sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
295 INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
296 "because I don't know how to handle its type (%#x, %s). "
297 "If you need this sensor, please file a bug report.",
298 sensor_name_ptr, sensor_type, sensor_type_str);
301 } /* switch (sensor_type) */
303 pthread_mutex_lock(&sensor_list_lock);
306 for (list_item = sensor_list; list_item != NULL;
307 list_item = list_item->next) {
308 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
310 list_prev = list_item;
311 } /* for (list_item) */
313 if (list_item != NULL) {
314 pthread_mutex_unlock(&sensor_list_lock);
318 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
319 if (list_item == NULL) {
320 pthread_mutex_unlock(&sensor_list_lock);
324 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
326 if (list_prev != NULL)
327 list_prev->next = list_item;
329 sensor_list = list_item;
331 sstrncpy(list_item->sensor_name, sensor_name_ptr,
332 sizeof(list_item->sensor_name));
333 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
335 pthread_mutex_unlock(&sensor_list_lock);
337 if (c_ipmi_notify_add && (c_ipmi_init_in_progress == 0)) {
338 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
340 sstrncpy(n.host, hostname_g, sizeof(n.host));
341 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
342 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
343 ssnprintf(n.message, sizeof(n.message), "sensor %s added",
344 list_item->sensor_name);
346 plugin_dispatch_notification(&n);
350 } /* int sensor_list_add */
352 static int sensor_list_remove(ipmi_sensor_t *sensor) {
353 ipmi_sensor_id_t sensor_id;
354 c_ipmi_sensor_list_t *list_item;
355 c_ipmi_sensor_list_t *list_prev;
357 sensor_id = ipmi_sensor_convert_to_id(sensor);
359 pthread_mutex_lock(&sensor_list_lock);
362 for (list_item = sensor_list; list_item != NULL;
363 list_item = list_item->next) {
364 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
366 list_prev = list_item;
367 } /* for (list_item) */
369 if (list_item == NULL) {
370 pthread_mutex_unlock(&sensor_list_lock);
374 if (list_prev == NULL)
375 sensor_list = list_item->next;
377 list_prev->next = list_item->next;
380 list_item->next = NULL;
382 pthread_mutex_unlock(&sensor_list_lock);
384 if (c_ipmi_notify_remove && c_ipmi_active) {
385 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi",
388 sstrncpy(n.host, hostname_g, sizeof(n.host));
389 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
390 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
391 ssnprintf(n.message, sizeof(n.message), "sensor %s removed",
392 list_item->sensor_name);
394 plugin_dispatch_notification(&n);
399 } /* int sensor_list_remove */
401 static int sensor_list_read_all(void) {
402 pthread_mutex_lock(&sensor_list_lock);
404 for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
405 list_item = list_item->next) {
406 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
407 /* user data = */ list_item);
408 } /* for (list_item) */
410 pthread_mutex_unlock(&sensor_list_lock);
413 } /* int sensor_list_read_all */
415 static int sensor_list_remove_all(void) {
416 c_ipmi_sensor_list_t *list_item;
418 pthread_mutex_lock(&sensor_list_lock);
420 list_item = sensor_list;
423 pthread_mutex_unlock(&sensor_list_lock);
425 while (list_item != NULL) {
426 c_ipmi_sensor_list_t *list_next = list_item->next;
430 list_item = list_next;
431 } /* while (list_item) */
434 } /* int sensor_list_remove_all */
436 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
437 int _severity = NOTIF_OKAY;
440 case IPMI_LOWER_NON_CRITICAL:
441 case IPMI_UPPER_NON_CRITICAL:
442 _severity = NOTIF_OKAY;
444 case IPMI_LOWER_CRITICAL:
445 case IPMI_UPPER_CRITICAL:
446 _severity = NOTIF_WARNING;
448 case IPMI_LOWER_NON_RECOVERABLE:
449 case IPMI_UPPER_NON_RECOVERABLE:
450 _severity = NOTIF_FAILURE;
454 } /* switch (severity) */
457 } /* int sensor_convert_threshold_severity */
459 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
460 enum ipmi_event_dir_e dir,
461 ipmi_event_t *event) {
462 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
464 plugin_notification_meta_add_string(n, "entity_name",
465 ipmi_entity_get_entity_id_string(ent));
466 plugin_notification_meta_add_signed_int(n, "entity_id",
467 ipmi_entity_get_entity_id(ent));
468 plugin_notification_meta_add_signed_int(n, "entity_instance",
469 ipmi_entity_get_entity_instance(ent));
470 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
473 plugin_notification_meta_add_signed_int(n, "event_type",
474 ipmi_event_get_type(event));
475 } /* void add_event_sensor_meta_data */
477 static int sensor_threshold_event_handler(
478 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
479 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
480 enum ipmi_value_present_e value_present, unsigned int raw_value,
481 double value, void *cb_data, ipmi_event_t *event) {
483 /* From the IPMI specification Chapter 2: Events.
484 * If a callback handles the event, then all future callbacks called due to
485 * the event will receive a NULL for the event. So be ready to handle a NULL
486 * event in all your event handlers. A NULL may also be passed to an event
487 * handler if the callback was not due to an event. */
489 return (IPMI_EVENT_NOT_HANDLED);
491 /* offset is a table index and it's represented as enum of strings that are
492 organized in the way - high and low for each threshold severity level */
493 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
494 unsigned int offset = (2 * threshold) + high_low;
495 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
496 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
497 const char *event_state =
498 ipmi_get_reading_name(event_type, sensor_type, offset);
499 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
500 if (value_present != IPMI_NO_VALUES_PRESENT)
501 ssnprintf(n.message, sizeof(n.message),
502 "sensor %s received event: %s, value is %f", n.type_instance,
505 ssnprintf(n.message, sizeof(n.message),
506 "sensor %s received event: %s, value not provided",
507 n.type_instance, event_state);
509 DEBUG("Threshold event received for sensor %s", n.type_instance);
511 sstrncpy(n.host, hostname_g, sizeof(n.host));
512 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
513 n.severity = sensor_convert_threshold_severity(threshold);
514 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
516 plugin_notification_meta_add_string(&n, "severity",
517 ipmi_get_threshold_string(threshold));
518 plugin_notification_meta_add_string(&n, "direction",
519 ipmi_get_value_dir_string(high_low));
521 switch (value_present) {
522 case IPMI_BOTH_VALUES_PRESENT:
523 plugin_notification_meta_add_double(&n, "val", value);
524 /* both values present, so fall-through to add raw value too */
525 case IPMI_RAW_VALUE_PRESENT: {
526 char buf[DATA_MAX_NAME_LEN] = {0};
527 snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
528 plugin_notification_meta_add_string(&n, "raw", buf);
532 } /* switch (value_present) */
534 add_event_common_data(&n, sensor, dir, event);
536 plugin_dispatch_notification(&n);
537 plugin_notification_meta_free(n.meta);
539 /* Delete handled ipmi event from the list */
540 if (c_ipmi_sel_clear_event) {
541 ipmi_event_delete(event, NULL, NULL);
542 return (IPMI_EVENT_HANDLED);
545 return (IPMI_EVENT_NOT_HANDLED);
546 } /* int sensor_threshold_event_handler */
548 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
549 enum ipmi_event_dir_e dir, int offset,
550 int severity, int prev_severity,
551 void *cb_data, ipmi_event_t *event) {
552 /* From the IPMI specification Chapter 2: Events.
553 * If a callback handles the event, then all future callbacks called due to
554 * the event will receive a NULL for the event. So be ready to handle a NULL
555 * event in all your event handlers. A NULL may also be passed to an event
556 * handler if the callback was not due to an event. */
558 return (IPMI_EVENT_NOT_HANDLED);
560 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
561 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
562 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
563 const char *event_state =
564 ipmi_get_reading_name(event_type, sensor_type, offset);
565 sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
566 ssnprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
567 n.type_instance, event_state);
569 DEBUG("Discrete event received for sensor %s", n.type_instance);
571 sstrncpy(n.host, hostname_g, sizeof(n.host));
572 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
573 n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
575 plugin_notification_meta_add_signed_int(&n, "offset", offset);
578 plugin_notification_meta_add_signed_int(&n, "severity", severity);
580 if (prev_severity != -1)
581 plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
583 add_event_common_data(&n, sensor, dir, event);
585 plugin_dispatch_notification(&n);
586 plugin_notification_meta_free(n.meta);
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) {
780 ignorelist_set_invert(ignorelist, !IS_TRUE(value));
781 } else if (strcasecmp("NotifySensorAdd", key) == 0) {
782 c_ipmi_notify_add = IS_TRUE(value);
783 } else if (strcasecmp("NotifySensorRemove", key) == 0) {
784 c_ipmi_notify_remove = IS_TRUE(value);
785 } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
786 c_ipmi_notify_notpresent = IS_TRUE(value);
787 } else if (strcasecmp("SELEnabled", key) == 0) {
788 c_ipmi_sel_enabled = IS_TRUE(value);
789 } else if (strcasecmp("SELClearEvent", key) == 0) {
790 c_ipmi_sel_clear_event = IS_TRUE(value);
796 } /* int c_ipmi_config */
798 static int c_ipmi_init(void) {
801 /* Don't send `ADD' notifications during startup (~ 1 minute) */
802 time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
803 c_ipmi_init_in_progress = 1 + (60 / iv);
807 status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
808 /* user data = */ NULL, "ipmi");
811 thread_id = (pthread_t)0;
812 ERROR("ipmi plugin: pthread_create failed.");
817 } /* int c_ipmi_init */
819 static int c_ipmi_read(void) {
820 if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
821 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
825 sensor_list_read_all();
827 if (c_ipmi_init_in_progress > 0)
828 c_ipmi_init_in_progress--;
830 c_ipmi_init_in_progress = 0;
833 } /* int c_ipmi_read */
835 static int c_ipmi_shutdown(void) {
838 if (thread_id != (pthread_t)0) {
839 pthread_join(thread_id, NULL);
840 thread_id = (pthread_t)0;
843 sensor_list_remove_all();
846 } /* int c_ipmi_shutdown */
848 void module_register(void) {
849 plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
850 plugin_register_init("ipmi", c_ipmi_init);
851 plugin_register_read("ipmi", c_ipmi_read);
852 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
853 } /* void module_register */