1 /**
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
6 *
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.
10 *
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.
15 *
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
19 *
20 * Authors:
21 * Florian octo Forster <octo at collectd.org>
22 * Peter Holik <peter at holik.at>
23 * Bruno Prémont <bonbons at linux-vserver.org>
24 **/
26 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
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>
38 /*
39 * Private data types
40 */
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;
50 };
52 /*
53 * Module global variables
54 */
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;
73 /*
74 * Misc private functions
75 */
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));
83 }
85 if (errbuf[0] == 0) {
86 ssnprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
87 }
88 errbuf[sizeof(errbuf) - 1] = 0;
90 ERROR("ipmi plugin: %s failed: %s", func, errbuf);
91 } /* void c_ipmi_error */
93 /*
94 * Sensor handlers
95 */
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,
102 double value,
103 ipmi_states_t __attribute__((unused)) * states,
104 void *user_data) {
105 value_t values[1];
106 value_list_t vl = VALUE_LIST_INIT;
108 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
110 if (err != 0) {
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 "
116 "not present.",
117 list_item->sensor_name);
119 if (c_ipmi_nofiy_notpresent) {
120 notification_t n = {
121 NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "", NULL};
123 sstrncpy(n.host, hostname_g, sizeof(n.host));
124 sstrncpy(n.type_instance, list_item->sensor_name,
125 sizeof(n.type_instance));
126 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
127 ssnprintf(n.message, sizeof(n.message), "sensor %s not present",
128 list_item->sensor_name);
130 plugin_dispatch_notification(&n);
131 }
132 }
133 } else if (IPMI_IS_IPMI_ERR(err) &&
134 IPMI_GET_IPMI_ERR(err) ==
135 IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
136 INFO("ipmi plugin: sensor_read_handler: Sensor %s not ready",
137 list_item->sensor_name);
138 } else {
139 if (IPMI_IS_IPMI_ERR(err))
140 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
141 "because it failed with IPMI error %#x.",
142 list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
143 else if (IPMI_IS_OS_ERR(err))
144 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
145 "because it failed with OS error %#x.",
146 list_item->sensor_name, IPMI_GET_OS_ERR(err));
147 else if (IPMI_IS_RMCPP_ERR(err))
148 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
149 "because it failed with RMCPP error %#x.",
150 list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
151 else if (IPMI_IS_SOL_ERR(err))
152 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
153 "because it failed with RMCPP error %#x.",
154 list_item->sensor_name, IPMI_GET_SOL_ERR(err));
155 else
156 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
157 "because it failed with error %#x. of class %#x",
158 list_item->sensor_name, err & 0xff, err & 0xffffff00);
159 sensor_list_remove(sensor);
160 }
161 return;
162 } else if (list_item->sensor_not_present == 1) {
163 list_item->sensor_not_present = 0;
165 INFO("ipmi plugin: sensor_read_handler: sensor %s present.",
166 list_item->sensor_name);
168 if (c_ipmi_nofiy_notpresent) {
169 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi",
170 "", "", "", NULL};
172 sstrncpy(n.host, hostname_g, sizeof(n.host));
173 sstrncpy(n.type_instance, list_item->sensor_name,
174 sizeof(n.type_instance));
175 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
176 ssnprintf(n.message, sizeof(n.message), "sensor %s present",
177 list_item->sensor_name);
179 plugin_dispatch_notification(&n);
180 }
181 }
183 if (value_present != IPMI_BOTH_VALUES_PRESENT) {
184 INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
185 "because it provides %s. If you need this sensor, "
186 "please file a bug report.",
187 list_item->sensor_name,
188 (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
189 : "no value");
190 sensor_list_remove(sensor);
191 return;
192 }
194 values[0].gauge = value;
196 vl.values = values;
197 vl.values_len = 1;
199 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
200 sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
201 sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
202 sstrncpy(vl.type_instance, list_item->sensor_name, sizeof(vl.type_instance));
204 plugin_dispatch_values(&vl);
205 } /* void sensor_read_handler */
207 static int sensor_list_add(ipmi_sensor_t *sensor) {
208 ipmi_sensor_id_t sensor_id;
209 c_ipmi_sensor_list_t *list_item;
210 c_ipmi_sensor_list_t *list_prev;
212 char buffer[DATA_MAX_NAME_LEN] = {0};
213 const char *entity_id_string;
214 char sensor_name[DATA_MAX_NAME_LEN];
215 char *sensor_name_ptr;
216 int sensor_type;
217 const char *type;
218 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
220 sensor_id = ipmi_sensor_convert_to_id(sensor);
222 ipmi_sensor_get_name(sensor, buffer, sizeof(buffer));
223 buffer[sizeof(buffer) - 1] = 0;
225 entity_id_string = ipmi_entity_get_entity_id_string(ent);
227 if (entity_id_string == NULL)
228 sstrncpy(sensor_name, buffer, sizeof(sensor_name));
229 else
230 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", buffer,
231 entity_id_string);
233 sstrncpy(buffer, sensor_name, sizeof(buffer));
234 sensor_name_ptr = strstr(buffer, ").");
235 if (sensor_name_ptr != NULL) {
236 /* If name is something like "foo (123).bar",
237 * change that to "bar (123)".
238 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
239 * `buffer' array, which holds a copy of the current `sensor_name'. */
240 char *sensor_id_ptr;
242 /* `sensor_name_ptr' points to ").bar". */
243 sensor_name_ptr[1] = 0;
244 /* `buffer' holds "foo (123)\0bar\0". */
245 sensor_name_ptr += 2;
246 /* `sensor_name_ptr' now points to "bar". */
248 sensor_id_ptr = strstr(buffer, "(");
249 if (sensor_id_ptr != NULL) {
250 /* `sensor_id_ptr' now points to "(123)". */
251 ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
252 sensor_id_ptr);
253 }
254 /* else: don't touch sensor_name. */
255 }
256 sensor_name_ptr = sensor_name;
258 /* Both `ignorelist' and `plugin_instance' may be NULL. */
259 if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
260 return (0);
262 /* FIXME: Use rate unit or base unit to scale the value */
264 sensor_type = ipmi_sensor_get_sensor_type(sensor);
265 switch (sensor_type) {
266 case IPMI_SENSOR_TYPE_TEMPERATURE:
267 type = "temperature";
268 break;
270 case IPMI_SENSOR_TYPE_VOLTAGE:
271 type = "voltage";
272 break;
274 case IPMI_SENSOR_TYPE_CURRENT:
275 type = "current";
276 break;
278 case IPMI_SENSOR_TYPE_FAN:
279 type = "fanspeed";
280 break;
282 default: {
283 const char *sensor_type_str;
285 sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
286 INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
287 "because I don't know how to handle its type (%#x, %s). "
288 "If you need this sensor, please file a bug report.",
289 sensor_name_ptr, sensor_type, sensor_type_str);
290 return (-1);
291 }
292 } /* switch (sensor_type) */
294 pthread_mutex_lock(&sensor_list_lock);
296 list_prev = NULL;
297 for (list_item = sensor_list; list_item != NULL;
298 list_item = list_item->next) {
299 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
300 break;
301 list_prev = list_item;
302 } /* for (list_item) */
304 if (list_item != NULL) {
305 pthread_mutex_unlock(&sensor_list_lock);
306 return (0);
307 }
309 list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
310 if (list_item == NULL) {
311 pthread_mutex_unlock(&sensor_list_lock);
312 return (-1);
313 }
315 list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
317 if (list_prev != NULL)
318 list_prev->next = list_item;
319 else
320 sensor_list = list_item;
322 sstrncpy(list_item->sensor_name, sensor_name_ptr,
323 sizeof(list_item->sensor_name));
324 sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
326 pthread_mutex_unlock(&sensor_list_lock);
328 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0)) {
329 notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
331 sstrncpy(n.host, hostname_g, sizeof(n.host));
332 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
333 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
334 ssnprintf(n.message, sizeof(n.message), "sensor %s added",
335 list_item->sensor_name);
337 plugin_dispatch_notification(&n);
338 }
340 return (0);
341 } /* int sensor_list_add */
343 static int sensor_list_remove(ipmi_sensor_t *sensor) {
344 ipmi_sensor_id_t sensor_id;
345 c_ipmi_sensor_list_t *list_item;
346 c_ipmi_sensor_list_t *list_prev;
348 sensor_id = ipmi_sensor_convert_to_id(sensor);
350 pthread_mutex_lock(&sensor_list_lock);
352 list_prev = NULL;
353 for (list_item = sensor_list; list_item != NULL;
354 list_item = list_item->next) {
355 if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
356 break;
357 list_prev = list_item;
358 } /* for (list_item) */
360 if (list_item == NULL) {
361 pthread_mutex_unlock(&sensor_list_lock);
362 return (-1);
363 }
365 if (list_prev == NULL)
366 sensor_list = list_item->next;
367 else
368 list_prev->next = list_item->next;
370 list_prev = NULL;
371 list_item->next = NULL;
373 pthread_mutex_unlock(&sensor_list_lock);
375 if (c_ipmi_nofiy_remove && c_ipmi_active) {
376 notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "",
377 NULL};
379 sstrncpy(n.host, hostname_g, sizeof(n.host));
380 sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
381 sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
382 ssnprintf(n.message, sizeof(n.message), "sensor %s removed",
383 list_item->sensor_name);
385 plugin_dispatch_notification(&n);
386 }
388 free(list_item);
389 return (0);
390 } /* int sensor_list_remove */
392 static int sensor_list_read_all(void) {
393 pthread_mutex_lock(&sensor_list_lock);
395 for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
396 list_item = list_item->next) {
397 ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
398 /* user data = */ list_item);
399 } /* for (list_item) */
401 pthread_mutex_unlock(&sensor_list_lock);
403 return (0);
404 } /* int sensor_list_read_all */
406 static int sensor_list_remove_all(void) {
407 c_ipmi_sensor_list_t *list_item;
409 pthread_mutex_lock(&sensor_list_lock);
411 list_item = sensor_list;
412 sensor_list = NULL;
414 pthread_mutex_unlock(&sensor_list_lock);
416 while (list_item != NULL) {
417 c_ipmi_sensor_list_t *list_next = list_item->next;
419 free(list_item);
421 list_item = list_next;
422 } /* while (list_item) */
424 return (0);
425 } /* int sensor_list_remove_all */
427 /*
428 * Entity handlers
429 */
430 static void entity_sensor_update_handler(
431 enum ipmi_update_e op, ipmi_entity_t __attribute__((unused)) * entity,
432 ipmi_sensor_t *sensor, void __attribute__((unused)) * user_data) {
433 /* TODO: Ignore sensors we cannot read */
435 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
436 /* Will check for duplicate entries.. */
437 sensor_list_add(sensor);
438 } else if (op == IPMI_DELETED) {
439 sensor_list_remove(sensor);
440 }
441 } /* void entity_sensor_update_handler */
443 /*
444 * Domain handlers
445 */
446 static void domain_entity_update_handler(
447 enum ipmi_update_e op, ipmi_domain_t __attribute__((unused)) * domain,
448 ipmi_entity_t *entity, void __attribute__((unused)) * user_data) {
449 int status;
451 if (op == IPMI_ADDED) {
452 status = ipmi_entity_add_sensor_update_handler(
453 entity, entity_sensor_update_handler, /* user data = */ NULL);
454 if (status != 0) {
455 c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
456 }
457 } else if (op == IPMI_DELETED) {
458 status = ipmi_entity_remove_sensor_update_handler(
459 entity, entity_sensor_update_handler, /* user data = */ NULL);
460 if (status != 0) {
461 c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
462 }
463 }
464 } /* void domain_entity_update_handler */
466 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
467 unsigned int conn_num,
468 unsigned int port_num,
469 int still_connected,
470 void *user_data) {
471 int status;
473 DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
474 "conn_num = %u, port_num = %u, still_connected = %i, "
475 "user_data = %p);\n",
476 (void *)domain, err, conn_num, port_num, still_connected, user_data);
478 status = ipmi_domain_add_entity_update_handler(
479 domain, domain_entity_update_handler, /* user data = */ NULL);
480 if (status != 0) {
481 c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
482 }
483 } /* void domain_connection_change_handler */
485 static int thread_init(os_handler_t **ret_os_handler) {
486 os_handler_t *os_handler;
487 ipmi_con_t *smi_connection = NULL;
488 ipmi_domain_id_t domain_id;
489 int status;
491 os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
492 if (os_handler == NULL) {
493 ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
494 return (-1);
495 }
497 ipmi_init(os_handler);
499 status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
500 /* user data = */ NULL, &smi_connection);
501 if (status != 0) {
502 c_ipmi_error("ipmi_smi_setup_con", status);
503 return (-1);
504 }
506 ipmi_open_option_t open_option[1] = {[0] = {.option = IPMI_OPEN_OPTION_ALL,
507 {.ival = 1}}};
509 status = ipmi_open_domain(
510 "mydomain", &smi_connection, /* num_con = */ 1,
511 domain_connection_change_handler, /* user data = */ NULL,
512 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
513 sizeof(open_option) / sizeof(open_option[0]), &domain_id);
514 if (status != 0) {
515 c_ipmi_error("ipmi_open_domain", status);
516 return (-1);
517 }
519 *ret_os_handler = os_handler;
520 return (0);
521 } /* int thread_init */
523 static void *thread_main(void __attribute__((unused)) * user_data) {
524 int status;
525 os_handler_t *os_handler = NULL;
527 status = thread_init(&os_handler);
528 if (status != 0) {
529 ERROR("ipmi plugin: thread_init failed.\n");
530 return ((void *)-1);
531 }
533 while (c_ipmi_active != 0) {
534 struct timeval tv = {1, 0};
535 os_handler->perform_one_op(os_handler, &tv);
536 }
538 ipmi_posix_thread_free_os_handler(os_handler);
540 return ((void *)0);
541 } /* void *thread_main */
543 static int c_ipmi_config(const char *key, const char *value) {
544 if (ignorelist == NULL)
545 ignorelist = ignorelist_create(/* invert = */ 1);
546 if (ignorelist == NULL)
547 return (1);
549 if (strcasecmp("Sensor", key) == 0) {
550 ignorelist_add(ignorelist, value);
551 } else if (strcasecmp("IgnoreSelected", key) == 0) {
552 int invert = 1;
553 if (IS_TRUE(value))
554 invert = 0;
555 ignorelist_set_invert(ignorelist, invert);
556 } else if (strcasecmp("NotifySensorAdd", key) == 0) {
557 if (IS_TRUE(value))
558 c_ipmi_nofiy_add = 1;
559 } else if (strcasecmp("NotifySensorRemove", key) == 0) {
560 if (IS_TRUE(value))
561 c_ipmi_nofiy_remove = 1;
562 } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
563 if (IS_TRUE(value))
564 c_ipmi_nofiy_notpresent = 1;
565 } else {
566 return (-1);
567 }
569 return (0);
570 } /* int c_ipmi_config */
572 static int c_ipmi_init(void) {
573 int status;
575 /* Don't send `ADD' notifications during startup (~ 1 minute) */
576 time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
577 c_ipmi_init_in_progress = 1 + (60 / iv);
579 c_ipmi_active = 1;
581 status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
582 /* user data = */ NULL);
583 if (status != 0) {
584 c_ipmi_active = 0;
585 thread_id = (pthread_t)0;
586 ERROR("ipmi plugin: pthread_create failed.");
587 return (-1);
588 }
590 return (0);
591 } /* int c_ipmi_init */
593 static int c_ipmi_read(void) {
594 if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
595 INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
596 return (-1);
597 }
599 sensor_list_read_all();
601 if (c_ipmi_init_in_progress > 0)
602 c_ipmi_init_in_progress--;
603 else
604 c_ipmi_init_in_progress = 0;
606 return (0);
607 } /* int c_ipmi_read */
609 static int c_ipmi_shutdown(void) {
610 c_ipmi_active = 0;
612 if (thread_id != (pthread_t)0) {
613 pthread_join(thread_id, NULL);
614 thread_id = (pthread_t)0;
615 }
617 sensor_list_remove_all();
619 return (0);
620 } /* int c_ipmi_shutdown */
622 void module_register(void) {
623 plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
624 plugin_register_init("ipmi", c_ipmi_init);
625 plugin_register_read("ipmi", c_ipmi_read);
626 plugin_register_shutdown("ipmi", c_ipmi_shutdown);
627 } /* void module_register */
629 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */