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"};
65 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
67 static ignorelist_t *ignorelist = NULL;
69 static int c_ipmi_notify_add = 0;
70 static int c_ipmi_notify_remove = 0;
71 static int c_ipmi_notify_notpresent = 0;
72 static int c_ipmi_sel_enabled = 0;
75 * Misc private functions
77 static void c_ipmi_error(const char *func, int status) {
78 char errbuf[4096] = {0};
80 if (IPMI_IS_OS_ERR(status)) {
81 sstrerror(IPMI_GET_OS_ERR(status), errbuf, sizeof(errbuf));
82 } else if (IPMI_IS_IPMI_ERR(status)) {
83 ipmi_get_error_string(IPMI_GET_IPMI_ERR(status), errbuf, sizeof(errbuf));
87 ssnprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
89 errbuf[sizeof(errbuf) - 1] = 0;
91 ERROR("ipmi plugin: %s failed: %s", func, errbuf);
92 } /* void c_ipmi_error */
97 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
98 static int sensor_list_remove(ipmi_sensor_t *sensor);
100 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
101 enum ipmi_value_present_e value_present,
102 unsigned int __attribute__((unused)) raw_value,
104 ipmi_states_t __attribute__((unused)) * states,
106 value_list_t vl = VALUE_LIST_INIT;
108 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
111 if ((err & 0xff) == IPMI_NOT_PRESENT_CC) {
112 if (list_item->sensor_not_present == 0) {
113 list_item->sensor_not_present = 1;
115 INFO("ipmi plugin: sensor_read_handler: sensor %s "
117 list_item->sensor_name);
119 if (c_ipmi_notify_notpresent) {
120 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "", NULL};
122 sstrncpy(n.host, hostname_g, sizeof(n.host));
123 sstrncpy(n.type_instance, list_item->sensor_name,
124 sizeof(n.type_instance));
125 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
126 ssnprintf(n.message, sizeof(n.message), "sensor %s not present",
127 list_item->sensor_name);
129 plugin_dispatch_notification(&n);
132 } else if (IPMI_IS_IPMI_ERR(err) &&
133 IPMI_GET_IPMI_ERR(err) ==
134 IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
135 INFO("ipmi plugin: sensor_read_handler: Sensor %s not ready",
136 list_item->sensor_name);
138 if (IPMI_IS_IPMI_ERR(err))
139 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
140 "because it failed with IPMI error %#x.",
141 list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
142 else if (IPMI_IS_OS_ERR(err))
143 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
144 "because it failed with OS error %#x.",
145 list_item->sensor_name, IPMI_GET_OS_ERR(err));
146 else if (IPMI_IS_RMCPP_ERR(err))
147 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
148 "because it failed with RMCPP error %#x.",
149 list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
150 else if (IPMI_IS_SOL_ERR(err))
151 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
152 "because it failed with RMCPP error %#x.",
153 list_item->sensor_name, IPMI_GET_SOL_ERR(err));
155 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
156 "because it failed with error %#x. of class %#x",
157 list_item->sensor_name, err & 0xff, err & 0xffffff00);
158 sensor_list_remove(sensor);
161 } else if (list_item->sensor_not_present == 1) {
162 list_item->sensor_not_present = 0;
164 INFO("ipmi plugin: sensor_read_handler: sensor %s present.",
165 list_item->sensor_name);
167 if (c_ipmi_notify_notpresent) {
168 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "",
171 sstrncpy(n.host, hostname_g, sizeof(n.host));
172 sstrncpy(n.type_instance, list_item->sensor_name,
173 sizeof(n.type_instance));
174 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
175 ssnprintf(n.message, sizeof(n.message), "sensor %s present",
176 list_item->sensor_name);
178 plugin_dispatch_notification(&n);
182 if (value_present != IPMI_BOTH_VALUES_PRESENT) {
183 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
184 "because it provides %s. If you need this sensor, "
185 "please file a bug report.",
186 list_item->sensor_name,
187 (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
189 sensor_list_remove(sensor);
193 vl.values = &(value_t){.gauge = value};
196 sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
197 sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
198 sstrncpy(vl.type_instance, list_item->sensor_name, sizeof(vl.type_instance));
200 plugin_dispatch_values(&vl);
201 } /* void sensor_read_handler */
203 static void sensor_get_name(ipmi_sensor_t *sensor, char *buf, int buf_len) {
204 char buffer[DATA_MAX_NAME_LEN] = {0};
205 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
206 const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
207 char sensor_name[DATA_MAX_NAME_LEN] = "";
208 char *sensor_name_ptr;
210 ipmi_sensor_get_name(sensor, buffer, sizeof(buffer));
211 buffer[sizeof(buffer) - 1] = 0;
213 if (entity_id_string != NULL && strlen(buffer))
214 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", buffer,
216 else if (entity_id_string != NULL)
217 sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
219 sstrncpy(sensor_name, buffer, sizeof(sensor_name));
221 if (strlen(buffer)) {
222 sstrncpy(buffer, sensor_name, sizeof(buffer));
223 sensor_name_ptr = strstr(buffer, ").");
224 if (sensor_name_ptr != NULL) {
225 /* If name is something like "foo (123).bar",
226 * change that to "bar (123)".
227 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
228 * `buffer' array, which holds a copy of the current `sensor_name'. */
231 /* `sensor_name_ptr' points to ").bar". */
232 sensor_name_ptr[1] = 0;
233 /* `buffer' holds "foo (123)\0bar\0". */
234 sensor_name_ptr += 2;
235 /* `sensor_name_ptr' now points to "bar". */
237 sensor_id_ptr = strstr(buffer, "(");
238 if (sensor_id_ptr != NULL) {
239 /* `sensor_id_ptr' now points to "(123)". */
240 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
243 /* else: don't touch sensor_name. */
248 sstrncpy(buf, sensor_name, buf_len);
251 static int sensor_list_add(ipmi_sensor_t *sensor) {
252 ipmi_sensor_id_t sensor_id;
253 c_ipmi_sensor_list_t *list_item;
254 c_ipmi_sensor_list_t *list_prev;
256 char buffer[DATA_MAX_NAME_LEN] = {0};
257 char *sensor_name_ptr = buffer;
261 sensor_id = ipmi_sensor_convert_to_id(sensor);
262 sensor_get_name(sensor, buffer, sizeof(buffer));
264 /* Both `ignorelist' and `plugin_instance' may be NULL. */
265 if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
268 /* FIXME: Use rate unit or base unit to scale the value */
270 sensor_type = ipmi_sensor_get_sensor_type(sensor);
271 switch (sensor_type) {
272 case IPMI_SENSOR_TYPE_TEMPERATURE:
273 type = "temperature";
276 case IPMI_SENSOR_TYPE_VOLTAGE:
280 case IPMI_SENSOR_TYPE_CURRENT:
284 case IPMI_SENSOR_TYPE_FAN:
289 const char *sensor_type_str;
291 sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
292 INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
293 "because I don't know how to handle its type (%#x, %s). "
294 "If you need this sensor, please file a bug report.",
295 sensor_name_ptr, sensor_type, sensor_type_str);
298 } /* switch (sensor_type) */
300 pthread_mutex_lock(&sensor_list_lock);
303 for (list_item = sensor_list; list_item != NULL;
304 list_item = list_item->next) {
305 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
307 list_prev = list_item;
308 } /* for (list_item) */
310 if (list_item != NULL) {
311 pthread_mutex_unlock(&sensor_list_lock);
315 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
316 if (list_item == NULL) {
317 pthread_mutex_unlock(&sensor_list_lock);
321 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
323 if (list_prev != NULL)
324 list_prev->next = list_item;
326 sensor_list = list_item;
328 sstrncpy(list_item->sensor_name, sensor_name_ptr,
329 sizeof(list_item->sensor_name));
330 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
332 pthread_mutex_unlock(&sensor_list_lock);
334 if (c_ipmi_notify_add && (c_ipmi_init_in_progress == 0)) {
335 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
337 sstrncpy(n.host, hostname_g, sizeof(n.host));
338 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
339 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
340 ssnprintf(n.message, sizeof(n.message), "sensor %s added",
341 list_item->sensor_name);
343 plugin_dispatch_notification(&n);
347 } /* int sensor_list_add */
349 static int sensor_list_remove(ipmi_sensor_t *sensor) {
350 ipmi_sensor_id_t sensor_id;
351 c_ipmi_sensor_list_t *list_item;
352 c_ipmi_sensor_list_t *list_prev;
354 sensor_id = ipmi_sensor_convert_to_id(sensor);
356 pthread_mutex_lock(&sensor_list_lock);
359 for (list_item = sensor_list; list_item != NULL;
360 list_item = list_item->next) {
361 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
363 list_prev = list_item;
364 } /* for (list_item) */
366 if (list_item == NULL) {
367 pthread_mutex_unlock(&sensor_list_lock);
371 if (list_prev == NULL)
372 sensor_list = list_item->next;
374 list_prev->next = list_item->next;
377 list_item->next = NULL;
379 pthread_mutex_unlock(&sensor_list_lock);
381 if (c_ipmi_notify_remove && c_ipmi_active) {
382 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi",
385 sstrncpy(n.host, hostname_g, sizeof(n.host));
386 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
387 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
388 ssnprintf(n.message, sizeof(n.message), "sensor %s removed",
389 list_item->sensor_name);
391 plugin_dispatch_notification(&n);
396 } /* int sensor_list_remove */
398 static int sensor_list_read_all(void) {
399 pthread_mutex_lock(&sensor_list_lock);
401 for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
402 list_item = list_item->next) {
403 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
404 /* user data = */ list_item);
405 } /* for (list_item) */
407 pthread_mutex_unlock(&sensor_list_lock);
410 } /* int sensor_list_read_all */
412 static int sensor_list_remove_all(void) {
413 c_ipmi_sensor_list_t *list_item;
415 pthread_mutex_lock(&sensor_list_lock);
417 list_item = sensor_list;
420 pthread_mutex_unlock(&sensor_list_lock);
422 while (list_item != NULL) {
423 c_ipmi_sensor_list_t *list_next = list_item->next;
427 list_item = list_next;
428 } /* while (list_item) */
431 } /* int sensor_list_remove_all */
433 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
434 int _severity = NOTIF_OKAY;
437 case IPMI_LOWER_NON_CRITICAL:
438 case IPMI_UPPER_NON_CRITICAL:
439 _severity = NOTIF_OKAY;
441 case IPMI_LOWER_CRITICAL:
442 case IPMI_UPPER_CRITICAL:
443 _severity = NOTIF_WARNING;
445 case IPMI_LOWER_NON_RECOVERABLE:
446 case IPMI_UPPER_NON_RECOVERABLE:
447 _severity = NOTIF_FAILURE;
451 } /* switch (severity) */
454 } /* int sensor_convert_threshold_severity */
456 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
457 enum ipmi_event_dir_e dir,
458 ipmi_event_t *event) {
459 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
461 plugin_notification_meta_add_string(n, "entity_name",
462 ipmi_entity_get_entity_id_string(ent));
463 plugin_notification_meta_add_signed_int(n, "entity_id",
464 ipmi_entity_get_entity_id(ent));
465 plugin_notification_meta_add_signed_int(n, "entity_instance",
466 ipmi_entity_get_entity_instance(ent));
467 plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
470 plugin_notification_meta_add_signed_int(n, "event_type",
471 ipmi_event_get_type(event));
472 } /* void add_event_sensor_meta_data */
474 static int sensor_threshold_event_handler(
475 ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
476 enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
477 enum ipmi_value_present_e value_present, unsigned int raw_value,
478 double value, void *cb_data, ipmi_event_t *event) {
479 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
480 /* offset is a table index and it's represented as enum of strings that are
481 organized in the way - high and low for each threshold severity level */
482 unsigned int offset = (2 * threshold) + high_low;
483 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
484 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
485 const char *event_state =
486 ipmi_get_reading_name(event_type, sensor_type, offset);
487 char buf[DATA_MAX_NAME_LEN] = {0};
489 /* From the IPMI specification Chapter 2: Events.
490 * If a callback handles the event, then all future callbacks called due to
491 * the event will receive a NULL for the event. So be ready to handle a NULL
492 * event in all your event handlers. A NULL may also be passed to an event
493 * handler if the callback was not due to an event. */
495 return (IPMI_EVENT_NOT_HANDLED);
497 sensor_get_name(sensor, buf, sizeof(buf));
498 sstrncpy(n.type_instance, buf, sizeof(n.type_instance));
499 if (value_present != IPMI_NO_VALUES_PRESENT)
500 ssnprintf(n.message, sizeof(n.message),
501 "sensor %s received event: %s, value is %f", buf, event_state,
504 ssnprintf(n.message, sizeof(n.message),
505 "sensor %s received event: %s, value not provided", buf,
508 DEBUG("Threshold event received for sensor %s", buf);
510 sstrncpy(n.host, hostname_g, sizeof(n.host));
511 sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
512 n.severity = sensor_convert_threshold_severity(threshold);
513 n.time = ipmi_event_get_timestamp(event);
515 plugin_notification_meta_add_string(&n, "severity",
516 ipmi_get_threshold_string(threshold));
517 plugin_notification_meta_add_string(&n, "direction",
518 ipmi_get_value_dir_string(high_low));
520 switch (value_present) {
521 case IPMI_BOTH_VALUES_PRESENT:
522 plugin_notification_meta_add_double(&n, "val", value);
523 case IPMI_RAW_VALUE_PRESENT:
524 snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
525 plugin_notification_meta_add_string(&n, "raw", buf);
529 } /* switch (value_present) */
531 add_event_common_data(&n, sensor, dir, event);
533 plugin_dispatch_notification(&n);
535 /* Delete handled ipmi event from the list */
536 ipmi_event_delete(event, NULL, NULL);
538 return (IPMI_EVENT_HANDLED);
539 } /* int sensor_threshold_event_handler */
541 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
542 enum ipmi_event_dir_e dir, int offset,
543 int severity, int prev_severity,
544 void *cb_data, ipmi_event_t *event) {
545 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
546 unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
547 unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
548 const char *event_state =
549 ipmi_get_reading_name(event_type, sensor_type, offset);
550 char buf[DATA_MAX_NAME_LEN] = {0};
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 sensor_get_name(sensor, buf, sizeof(buf));
561 sstrncpy(n.type_instance, buf, sizeof(n.type_instance));
562 ssnprintf(n.message, sizeof(n.message), "sensor %s received event: %s", buf,
565 DEBUG("Discrete event received for sensor %s", buf);
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 = 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);
583 /* Delete handled ipmi event from the list */
584 ipmi_event_delete(event, NULL, NULL);
586 return (IPMI_EVENT_HANDLED);
587 } /* int sensor_discrete_event_handler */
592 static void entity_sensor_update_handler(
593 enum ipmi_update_e op, ipmi_entity_t __attribute__((unused)) * entity,
594 ipmi_sensor_t *sensor, void __attribute__((unused)) * user_data) {
595 /* TODO: Ignore sensors we cannot read */
597 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
598 /* Will check for duplicate entries.. */
599 sensor_list_add(sensor);
601 if (c_ipmi_sel_enabled) {
603 /* register threshold event if threshold sensor support events */
604 if ((ipmi_sensor_get_event_reading_type(sensor) ==
605 IPMI_EVENT_READING_TYPE_THRESHOLD) &&
606 (ipmi_sensor_get_threshold_access(sensor) !=
607 IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
608 status = ipmi_sensor_add_threshold_event_handler(
609 sensor, sensor_threshold_event_handler, NULL);
610 /* register discrete handler if discrete/specific sensor support events */
611 else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
612 status = ipmi_sensor_add_discrete_event_handler(
613 sensor, sensor_discrete_event_handler, NULL);
616 char buf[DATA_MAX_NAME_LEN] = {0};
617 sensor_get_name(sensor, buf, sizeof(buf));
618 ERROR("Unable to add sensor %s event handler, status: %d", buf,
622 } else if (op == IPMI_DELETED) {
623 sensor_list_remove(sensor);
625 if (c_ipmi_sel_enabled) {
626 if (ipmi_sensor_get_event_reading_type(sensor) ==
627 IPMI_EVENT_READING_TYPE_THRESHOLD)
628 ipmi_sensor_remove_threshold_event_handler(
629 sensor, sensor_threshold_event_handler, NULL);
631 ipmi_sensor_remove_discrete_event_handler(
632 sensor, sensor_discrete_event_handler, NULL);
635 } /* void entity_sensor_update_handler */
640 static void domain_entity_update_handler(
641 enum ipmi_update_e op, ipmi_domain_t __attribute__((unused)) * domain,
642 ipmi_entity_t * entity, void __attribute__((unused)) * user_data) {
645 if (op == IPMI_ADDED) {
646 status = ipmi_entity_add_sensor_update_handler(
647 entity, entity_sensor_update_handler, /* user data = */ NULL);
649 c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
651 } else if (op == IPMI_DELETED) {
652 status = ipmi_entity_remove_sensor_update_handler(
653 entity, entity_sensor_update_handler, /* user data = */ NULL);
655 c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
658 } /* void domain_entity_update_handler */
660 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
661 unsigned int conn_num,
662 unsigned int port_num,
667 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
668 "conn_num = %u, port_num = %u, still_connected = %i, "
669 "user_data = %p);\n",
670 (void *)domain, err, conn_num, port_num, still_connected, user_data);
672 status = ipmi_domain_add_entity_update_handler(
673 domain, domain_entity_update_handler, /* user data = */ NULL);
675 c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
677 } /* void domain_connection_change_handler */
679 static int thread_init(os_handler_t **ret_os_handler) {
680 os_handler_t *os_handler;
681 ipmi_con_t *smi_connection = NULL;
682 ipmi_domain_id_t domain_id;
685 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
686 if (os_handler == NULL) {
687 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
691 ipmi_init(os_handler);
693 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
694 /* user data = */ NULL, &smi_connection);
696 c_ipmi_error("ipmi_smi_setup_con", status);
700 ipmi_open_option_t open_option[1] = {[0] = {.option = IPMI_OPEN_OPTION_ALL,
703 status = ipmi_open_domain(
704 "mydomain", &smi_connection, /* num_con = */ 1,
705 domain_connection_change_handler, /* user data = */ NULL,
706 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
707 sizeof(open_option) / sizeof(open_option[0]), &domain_id);
709 c_ipmi_error("ipmi_open_domain", status);
713 *ret_os_handler = os_handler;
715 } /* int thread_init */
717 static void *thread_main(void __attribute__((unused)) * user_data) {
719 os_handler_t *os_handler = NULL;
721 status = thread_init(&os_handler);
723 ERROR("ipmi plugin: thread_init failed.\n");
727 while (c_ipmi_active != 0) {
728 struct timeval tv = {1, 0};
729 os_handler->perform_one_op(os_handler, &tv);
732 ipmi_posix_thread_free_os_handler(os_handler);
735 } /* void *thread_main */
737 static int c_ipmi_config(const char *key, const char *value) {
738 if (ignorelist == NULL)
739 ignorelist = ignorelist_create(/* invert = */ 1);
740 if (ignorelist == NULL)
743 if (strcasecmp("Sensor", key) == 0) {
744 ignorelist_add(ignorelist, value);
745 } else if (strcasecmp("IgnoreSelected", key) == 0) {
749 ignorelist_set_invert(ignorelist, invert);
750 } else if (strcasecmp("NotifySensorAdd", key) == 0) {
752 c_ipmi_notify_add = 1;
753 } else if (strcasecmp("NotifySensorRemove", key) == 0) {
755 c_ipmi_notify_remove = 1;
756 } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
758 c_ipmi_notify_notpresent = 1;
759 } else if (strcasecmp("SELEnabled", key) == 0) {
761 c_ipmi_sel_enabled = 1;
767 } /* int c_ipmi_config */
769 static int c_ipmi_init(void) {
772 /* Don't send `ADD' notifications during startup (~ 1 minute) */
773 time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
774 c_ipmi_init_in_progress = 1 + (60 / iv);
778 status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
779 /* user data = */ NULL, "ipmi");
782 thread_id = (pthread_t)0;
783 ERROR("ipmi plugin: pthread_create failed.");
788 } /* int c_ipmi_init */
790 static int c_ipmi_read(void) {
791 if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
792 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
796 sensor_list_read_all();
798 if (c_ipmi_init_in_progress > 0)
799 c_ipmi_init_in_progress--;
801 c_ipmi_init_in_progress = 0;
804 } /* int c_ipmi_read */
806 static int c_ipmi_shutdown(void) {
809 if (thread_id != (pthread_t)0) {
810 pthread_join(thread_id, NULL);
811 thread_id = (pthread_t)0;
814 sensor_list_remove_all();
817 } /* int c_ipmi_shutdown */
819 void module_register(void) {
820 plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
821 plugin_register_init("ipmi", c_ipmi_init);
822 plugin_register_read("ipmi", c_ipmi_read);
823 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
824 } /* void module_register */