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"};
65 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
67 static ignorelist_t *ignorelist = NULL;
69 static int c_ipmi_nofiy_add = 0;
70 static int c_ipmi_nofiy_remove = 0;
71 static int c_ipmi_nofiy_notpresent = 0;
74 * Misc private functions
76 static void c_ipmi_error(const char *func, int status) {
77 char errbuf[4096] = {0};
79 if (IPMI_IS_OS_ERR(status)) {
80 sstrerror(IPMI_GET_OS_ERR(status), errbuf, sizeof(errbuf));
81 } else if (IPMI_IS_IPMI_ERR(status)) {
82 ipmi_get_error_string(IPMI_GET_IPMI_ERR(status), errbuf, sizeof(errbuf));
86 snprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
88 errbuf[sizeof(errbuf) - 1] = 0;
90 ERROR("ipmi plugin: %s failed: %s", func, errbuf);
91 } /* void c_ipmi_error */
96 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
97 static int sensor_list_remove(ipmi_sensor_t *sensor);
99 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
100 enum ipmi_value_present_e value_present,
101 unsigned int __attribute__((unused)) raw_value,
103 ipmi_states_t __attribute__((unused)) * states,
105 value_list_t vl = VALUE_LIST_INIT;
107 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
110 if ((err & 0xff) == IPMI_NOT_PRESENT_CC) {
111 if (list_item->sensor_not_present == 0) {
112 list_item->sensor_not_present = 1;
114 INFO("ipmi plugin: sensor_read_handler: sensor %s "
116 list_item->sensor_name);
118 if (c_ipmi_nofiy_notpresent) {
120 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 snprintf(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_nofiy_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 snprintf(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 int sensor_list_add(ipmi_sensor_t *sensor) {
204 ipmi_sensor_id_t sensor_id;
205 c_ipmi_sensor_list_t *list_item;
206 c_ipmi_sensor_list_t *list_prev;
208 char buffer[DATA_MAX_NAME_LEN] = {0};
209 const char *entity_id_string;
210 char sensor_name[DATA_MAX_NAME_LEN];
211 char *sensor_name_ptr;
214 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
216 sensor_id = ipmi_sensor_convert_to_id(sensor);
218 ipmi_sensor_get_name(sensor, buffer, sizeof(buffer));
219 buffer[sizeof(buffer) - 1] = 0;
221 entity_id_string = ipmi_entity_get_entity_id_string(ent);
223 if (entity_id_string == NULL)
224 sstrncpy(sensor_name, buffer, sizeof(sensor_name));
226 snprintf(sensor_name, sizeof(sensor_name), "%s %s", buffer,
229 sstrncpy(buffer, sensor_name, sizeof(buffer));
230 sensor_name_ptr = strstr(buffer, ").");
231 if (sensor_name_ptr != NULL) {
232 /* If name is something like "foo (123).bar",
233 * change that to "bar (123)".
234 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
235 * `buffer' array, which holds a copy of the current `sensor_name'. */
238 /* `sensor_name_ptr' points to ").bar". */
239 sensor_name_ptr[1] = 0;
240 /* `buffer' holds "foo (123)\0bar\0". */
241 sensor_name_ptr += 2;
242 /* `sensor_name_ptr' now points to "bar". */
244 sensor_id_ptr = strstr(buffer, "(");
245 if (sensor_id_ptr != NULL) {
246 /* `sensor_id_ptr' now points to "(123)". */
247 snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
250 /* else: don't touch sensor_name. */
252 sensor_name_ptr = sensor_name;
254 /* Both `ignorelist' and `plugin_instance' may be NULL. */
255 if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
258 /* FIXME: Use rate unit or base unit to scale the value */
260 sensor_type = ipmi_sensor_get_sensor_type(sensor);
261 switch (sensor_type) {
262 case IPMI_SENSOR_TYPE_TEMPERATURE:
263 type = "temperature";
266 case IPMI_SENSOR_TYPE_VOLTAGE:
270 case IPMI_SENSOR_TYPE_CURRENT:
274 case IPMI_SENSOR_TYPE_FAN:
279 const char *sensor_type_str;
281 sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
282 INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
283 "because I don't know how to handle its type (%#x, %s). "
284 "If you need this sensor, please file a bug report.",
285 sensor_name_ptr, sensor_type, sensor_type_str);
288 } /* switch (sensor_type) */
290 pthread_mutex_lock(&sensor_list_lock);
293 for (list_item = sensor_list; list_item != NULL;
294 list_item = list_item->next) {
295 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
297 list_prev = list_item;
298 } /* for (list_item) */
300 if (list_item != NULL) {
301 pthread_mutex_unlock(&sensor_list_lock);
305 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
306 if (list_item == NULL) {
307 pthread_mutex_unlock(&sensor_list_lock);
311 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
313 if (list_prev != NULL)
314 list_prev->next = list_item;
316 sensor_list = list_item;
318 sstrncpy(list_item->sensor_name, sensor_name_ptr,
319 sizeof(list_item->sensor_name));
320 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
322 pthread_mutex_unlock(&sensor_list_lock);
324 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0)) {
325 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
327 sstrncpy(n.host, hostname_g, sizeof(n.host));
328 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
329 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
330 snprintf(n.message, sizeof(n.message), "sensor %s added",
331 list_item->sensor_name);
333 plugin_dispatch_notification(&n);
337 } /* int sensor_list_add */
339 static int sensor_list_remove(ipmi_sensor_t *sensor) {
340 ipmi_sensor_id_t sensor_id;
341 c_ipmi_sensor_list_t *list_item;
342 c_ipmi_sensor_list_t *list_prev;
344 sensor_id = ipmi_sensor_convert_to_id(sensor);
346 pthread_mutex_lock(&sensor_list_lock);
349 for (list_item = sensor_list; list_item != NULL;
350 list_item = list_item->next) {
351 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
353 list_prev = list_item;
354 } /* for (list_item) */
356 if (list_item == NULL) {
357 pthread_mutex_unlock(&sensor_list_lock);
361 if (list_prev == NULL)
362 sensor_list = list_item->next;
364 list_prev->next = list_item->next;
367 list_item->next = NULL;
369 pthread_mutex_unlock(&sensor_list_lock);
371 if (c_ipmi_nofiy_remove && c_ipmi_active) {
372 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "",
375 sstrncpy(n.host, hostname_g, sizeof(n.host));
376 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
377 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
378 snprintf(n.message, sizeof(n.message), "sensor %s removed",
379 list_item->sensor_name);
381 plugin_dispatch_notification(&n);
386 } /* int sensor_list_remove */
388 static int sensor_list_read_all(void) {
389 pthread_mutex_lock(&sensor_list_lock);
391 for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
392 list_item = list_item->next) {
393 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
394 /* user data = */ list_item);
395 } /* for (list_item) */
397 pthread_mutex_unlock(&sensor_list_lock);
400 } /* int sensor_list_read_all */
402 static int sensor_list_remove_all(void) {
403 c_ipmi_sensor_list_t *list_item;
405 pthread_mutex_lock(&sensor_list_lock);
407 list_item = sensor_list;
410 pthread_mutex_unlock(&sensor_list_lock);
412 while (list_item != NULL) {
413 c_ipmi_sensor_list_t *list_next = list_item->next;
417 list_item = list_next;
418 } /* while (list_item) */
421 } /* int sensor_list_remove_all */
426 static void entity_sensor_update_handler(
427 enum ipmi_update_e op, ipmi_entity_t __attribute__((unused)) * entity,
428 ipmi_sensor_t *sensor, void __attribute__((unused)) * user_data) {
429 /* TODO: Ignore sensors we cannot read */
431 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
432 /* Will check for duplicate entries.. */
433 sensor_list_add(sensor);
434 } else if (op == IPMI_DELETED) {
435 sensor_list_remove(sensor);
437 } /* void entity_sensor_update_handler */
442 static void domain_entity_update_handler(
443 enum ipmi_update_e op, ipmi_domain_t __attribute__((unused)) * domain,
444 ipmi_entity_t *entity, void __attribute__((unused)) * user_data) {
447 if (op == IPMI_ADDED) {
448 status = ipmi_entity_add_sensor_update_handler(
449 entity, entity_sensor_update_handler, /* user data = */ NULL);
451 c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
453 } else if (op == IPMI_DELETED) {
454 status = ipmi_entity_remove_sensor_update_handler(
455 entity, entity_sensor_update_handler, /* user data = */ NULL);
457 c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
460 } /* void domain_entity_update_handler */
462 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
463 unsigned int conn_num,
464 unsigned int port_num,
469 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
470 "conn_num = %u, port_num = %u, still_connected = %i, "
471 "user_data = %p);\n",
472 (void *)domain, err, conn_num, port_num, still_connected, user_data);
474 status = ipmi_domain_add_entity_update_handler(
475 domain, domain_entity_update_handler, /* user data = */ NULL);
477 c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
479 } /* void domain_connection_change_handler */
481 static int thread_init(os_handler_t **ret_os_handler) {
482 os_handler_t *os_handler;
483 ipmi_con_t *smi_connection = NULL;
484 ipmi_domain_id_t domain_id;
487 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
488 if (os_handler == NULL) {
489 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
493 ipmi_init(os_handler);
495 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
496 /* user data = */ NULL, &smi_connection);
498 c_ipmi_error("ipmi_smi_setup_con", status);
502 ipmi_open_option_t open_option[1] = {[0] = {.option = IPMI_OPEN_OPTION_ALL,
505 status = ipmi_open_domain(
506 "mydomain", &smi_connection, /* num_con = */ 1,
507 domain_connection_change_handler, /* user data = */ NULL,
508 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
509 sizeof(open_option) / sizeof(open_option[0]), &domain_id);
511 c_ipmi_error("ipmi_open_domain", status);
515 *ret_os_handler = os_handler;
517 } /* int thread_init */
519 static void *thread_main(void __attribute__((unused)) * user_data) {
521 os_handler_t *os_handler = NULL;
523 status = thread_init(&os_handler);
525 ERROR("ipmi plugin: thread_init failed.\n");
529 while (c_ipmi_active != 0) {
530 struct timeval tv = {1, 0};
531 os_handler->perform_one_op(os_handler, &tv);
534 ipmi_posix_thread_free_os_handler(os_handler);
537 } /* void *thread_main */
539 static int c_ipmi_config(const char *key, const char *value) {
540 if (ignorelist == NULL)
541 ignorelist = ignorelist_create(/* invert = */ 1);
542 if (ignorelist == NULL)
545 if (strcasecmp("Sensor", key) == 0) {
546 ignorelist_add(ignorelist, value);
547 } else if (strcasecmp("IgnoreSelected", key) == 0) {
551 ignorelist_set_invert(ignorelist, invert);
552 } else if (strcasecmp("NotifySensorAdd", key) == 0) {
554 c_ipmi_nofiy_add = 1;
555 } else if (strcasecmp("NotifySensorRemove", key) == 0) {
557 c_ipmi_nofiy_remove = 1;
558 } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
560 c_ipmi_nofiy_notpresent = 1;
566 } /* int c_ipmi_config */
568 static int c_ipmi_init(void) {
571 /* Don't send `ADD' notifications during startup (~ 1 minute) */
572 time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
573 c_ipmi_init_in_progress = 1 + (60 / iv);
577 status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
578 /* user data = */ NULL, "ipmi");
581 thread_id = (pthread_t)0;
582 ERROR("ipmi plugin: pthread_create failed.");
587 } /* int c_ipmi_init */
589 static int c_ipmi_read(void) {
590 if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
591 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
595 sensor_list_read_all();
597 if (c_ipmi_init_in_progress > 0)
598 c_ipmi_init_in_progress--;
600 c_ipmi_init_in_progress = 0;
603 } /* int c_ipmi_read */
605 static int c_ipmi_shutdown(void) {
608 if (thread_id != (pthread_t)0) {
609 pthread_join(thread_id, NULL);
610 thread_id = (pthread_t)0;
613 sensor_list_remove_all();
616 } /* int c_ipmi_shutdown */
618 void module_register(void) {
619 plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
620 plugin_register_init("ipmi", c_ipmi_init);
621 plugin_register_read("ipmi", c_ipmi_read);
622 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
623 } /* void module_register */