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"
27 #include "common.h"
28 #include "plugin.h"
29 #include "utils_ignorelist.h"
31 #include <OpenIPMI/ipmiif.h>
32 #include <OpenIPMI/ipmi_err.h>
33 #include <OpenIPMI/ipmi_posix.h>
34 #include <OpenIPMI/ipmi_conn.h>
35 #include <OpenIPMI/ipmi_smi.h>
37 /*
38 * Private data types
39 */
40 struct c_ipmi_sensor_list_s;
41 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
43 struct c_ipmi_sensor_list_s
44 {
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[] =
63 {
64 "Sensor",
65 "IgnoreSelected",
66 "NotifySensorAdd",
67 "NotifySensorRemove",
68 "NotifySensorNotPresent"
69 };
70 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
72 static ignorelist_t *ignorelist = NULL;
74 static int c_ipmi_nofiy_add = 0;
75 static int c_ipmi_nofiy_remove = 0;
76 static int c_ipmi_nofiy_notpresent = 0;
78 /*
79 * Misc private functions
80 */
81 static void c_ipmi_error (const char *func, int status)
82 {
83 char errbuf[4096];
85 memset (errbuf, 0, sizeof (errbuf));
87 if (IPMI_IS_OS_ERR (status))
88 {
89 sstrerror (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
90 }
91 else if (IPMI_IS_IPMI_ERR (status))
92 {
93 ipmi_get_error_string (IPMI_GET_IPMI_ERR (status), errbuf, sizeof (errbuf));
94 }
96 if (errbuf[0] == 0)
97 {
98 ssnprintf (errbuf, sizeof (errbuf), "Unknown error %#x", status);
99 }
100 errbuf[sizeof (errbuf) - 1] = 0;
102 ERROR ("ipmi plugin: %s failed: %s", func, errbuf);
103 } /* void c_ipmi_error */
105 /*
106 * Sensor handlers
107 */
108 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
109 static int sensor_list_remove (ipmi_sensor_t *sensor);
111 static void sensor_read_handler (ipmi_sensor_t *sensor,
112 int err,
113 enum ipmi_value_present_e value_present,
114 unsigned int __attribute__((unused)) raw_value,
115 double value,
116 ipmi_states_t __attribute__((unused)) *states,
117 void *user_data)
118 {
119 value_t values[1];
120 value_list_t vl = VALUE_LIST_INIT;
122 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
124 if (err != 0)
125 {
126 if ((err & 0xff) == IPMI_NOT_PRESENT_CC)
127 {
128 if (list_item->sensor_not_present == 0)
129 {
130 list_item->sensor_not_present = 1;
132 INFO ("ipmi plugin: sensor_read_handler: sensor %s "
133 "not present.", list_item->sensor_name);
135 if (c_ipmi_nofiy_notpresent)
136 {
137 notification_t n = { NOTIF_WARNING, cdtime (), "", "", "ipmi",
138 "", "", "", NULL };
140 sstrncpy (n.host, hostname_g, sizeof (n.host));
141 sstrncpy (n.type_instance, list_item->sensor_name,
142 sizeof (n.type_instance));
143 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
144 ssnprintf (n.message, sizeof (n.message),
145 "sensor %s not present", list_item->sensor_name);
147 plugin_dispatch_notification (&n);
148 }
149 }
150 }
151 else if (IPMI_IS_IPMI_ERR(err) && IPMI_GET_IPMI_ERR(err) == IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC)
152 {
153 INFO ("ipmi plugin: sensor_read_handler: Sensor %s not ready",
154 list_item->sensor_name);
155 }
156 else
157 {
158 if (IPMI_IS_IPMI_ERR(err))
159 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
160 "because it failed with IPMI error %#x.",
161 list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
162 else if (IPMI_IS_OS_ERR(err))
163 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
164 "because it failed with OS error %#x.",
165 list_item->sensor_name, IPMI_GET_OS_ERR(err));
166 else if (IPMI_IS_RMCPP_ERR(err))
167 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
168 "because it failed with RMCPP error %#x.",
169 list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
170 else if (IPMI_IS_SOL_ERR(err))
171 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
172 "because it failed with RMCPP error %#x.",
173 list_item->sensor_name, IPMI_GET_SOL_ERR(err));
174 else
175 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
176 "because it failed with error %#x. of class %#x",
177 list_item->sensor_name, err & 0xff, err & 0xffffff00);
178 sensor_list_remove (sensor);
179 }
180 return;
181 }
182 else if (list_item->sensor_not_present == 1)
183 {
184 list_item->sensor_not_present = 0;
186 INFO ("ipmi plugin: sensor_read_handler: sensor %s present.",
187 list_item->sensor_name);
189 if (c_ipmi_nofiy_notpresent)
190 {
191 notification_t n = { NOTIF_OKAY, cdtime (), "", "", "ipmi",
192 "", "", "", NULL };
194 sstrncpy (n.host, hostname_g, sizeof (n.host));
195 sstrncpy (n.type_instance, list_item->sensor_name,
196 sizeof (n.type_instance));
197 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
198 ssnprintf (n.message, sizeof (n.message),
199 "sensor %s present", list_item->sensor_name);
201 plugin_dispatch_notification (&n);
202 }
203 }
205 if (value_present != IPMI_BOTH_VALUES_PRESENT)
206 {
207 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
208 "because it provides %s. If you need this sensor, "
209 "please file a bug report.",
210 list_item->sensor_name,
211 (value_present == IPMI_RAW_VALUE_PRESENT)
212 ? "only the raw value"
213 : "no value");
214 sensor_list_remove (sensor);
215 return;
216 }
218 values[0].gauge = value;
220 vl.values = values;
221 vl.values_len = 1;
223 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
224 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
225 sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
226 sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
228 plugin_dispatch_values (&vl);
229 } /* void sensor_read_handler */
231 static int sensor_list_add (ipmi_sensor_t *sensor)
232 {
233 ipmi_sensor_id_t sensor_id;
234 c_ipmi_sensor_list_t *list_item;
235 c_ipmi_sensor_list_t *list_prev;
237 char buffer[DATA_MAX_NAME_LEN];
238 const char *entity_id_string;
239 char sensor_name[DATA_MAX_NAME_LEN];
240 char *sensor_name_ptr;
241 int sensor_type;
242 const char *type;
243 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
245 sensor_id = ipmi_sensor_convert_to_id (sensor);
247 memset (buffer, 0, sizeof (buffer));
248 ipmi_sensor_get_name (sensor, buffer, sizeof (buffer));
249 buffer[sizeof (buffer) - 1] = 0;
251 entity_id_string = ipmi_entity_get_entity_id_string (ent);
253 if (entity_id_string == NULL)
254 sstrncpy (sensor_name, buffer, sizeof (sensor_name));
255 else
256 ssnprintf (sensor_name, sizeof (sensor_name),
257 "%s %s", buffer, entity_id_string);
259 sstrncpy (buffer, sensor_name, sizeof (buffer));
260 sensor_name_ptr = strstr (buffer, ").");
261 if (sensor_name_ptr != NULL)
262 {
263 /* If name is something like "foo (123).bar",
264 * change that to "bar (123)".
265 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
266 * `buffer' array, which holds a copy of the current `sensor_name'. */
267 char *sensor_id_ptr;
269 /* `sensor_name_ptr' points to ").bar". */
270 sensor_name_ptr[1] = 0;
271 /* `buffer' holds "foo (123)\0bar\0". */
272 sensor_name_ptr += 2;
273 /* `sensor_name_ptr' now points to "bar". */
275 sensor_id_ptr = strstr (buffer, "(");
276 if (sensor_id_ptr != NULL)
277 {
278 /* `sensor_id_ptr' now points to "(123)". */
279 ssnprintf (sensor_name, sizeof (sensor_name),
280 "%s %s", sensor_name_ptr, sensor_id_ptr);
281 }
282 /* else: don't touch sensor_name. */
283 }
284 sensor_name_ptr = sensor_name;
286 /* Both `ignorelist' and `plugin_instance' may be NULL. */
287 if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
288 return (0);
290 /* FIXME: Use rate unit or base unit to scale the value */
292 sensor_type = ipmi_sensor_get_sensor_type (sensor);
293 switch (sensor_type)
294 {
295 case IPMI_SENSOR_TYPE_TEMPERATURE:
296 type = "temperature";
297 break;
299 case IPMI_SENSOR_TYPE_VOLTAGE:
300 type = "voltage";
301 break;
303 case IPMI_SENSOR_TYPE_CURRENT:
304 type = "current";
305 break;
307 case IPMI_SENSOR_TYPE_FAN:
308 type = "fanspeed";
309 break;
311 default:
312 {
313 const char *sensor_type_str;
315 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
316 INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
317 "because I don't know how to handle its type (%#x, %s). "
318 "If you need this sensor, please file a bug report.",
319 sensor_name_ptr, sensor_type, sensor_type_str);
320 return (-1);
321 }
322 } /* switch (sensor_type) */
324 pthread_mutex_lock (&sensor_list_lock);
326 list_prev = NULL;
327 for (list_item = sensor_list;
328 list_item != NULL;
329 list_item = list_item->next)
330 {
331 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
332 break;
333 list_prev = list_item;
334 } /* for (list_item) */
336 if (list_item != NULL)
337 {
338 pthread_mutex_unlock (&sensor_list_lock);
339 return (0);
340 }
342 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
343 if (list_item == NULL)
344 {
345 pthread_mutex_unlock (&sensor_list_lock);
346 return (-1);
347 }
349 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
351 if (list_prev != NULL)
352 list_prev->next = list_item;
353 else
354 sensor_list = list_item;
356 sstrncpy (list_item->sensor_name, sensor_name_ptr,
357 sizeof (list_item->sensor_name));
358 sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
360 pthread_mutex_unlock (&sensor_list_lock);
362 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
363 {
364 notification_t n = { NOTIF_OKAY, cdtime (), "", "", "ipmi",
365 "", "", "", NULL };
367 sstrncpy (n.host, hostname_g, sizeof (n.host));
368 sstrncpy (n.type_instance, list_item->sensor_name,
369 sizeof (n.type_instance));
370 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
371 ssnprintf (n.message, sizeof (n.message),
372 "sensor %s added", list_item->sensor_name);
374 plugin_dispatch_notification (&n);
375 }
377 return (0);
378 } /* int sensor_list_add */
380 static int sensor_list_remove (ipmi_sensor_t *sensor)
381 {
382 ipmi_sensor_id_t sensor_id;
383 c_ipmi_sensor_list_t *list_item;
384 c_ipmi_sensor_list_t *list_prev;
386 sensor_id = ipmi_sensor_convert_to_id (sensor);
388 pthread_mutex_lock (&sensor_list_lock);
390 list_prev = NULL;
391 for (list_item = sensor_list;
392 list_item != NULL;
393 list_item = list_item->next)
394 {
395 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
396 break;
397 list_prev = list_item;
398 } /* for (list_item) */
400 if (list_item == NULL)
401 {
402 pthread_mutex_unlock (&sensor_list_lock);
403 return (-1);
404 }
406 if (list_prev == NULL)
407 sensor_list = list_item->next;
408 else
409 list_prev->next = list_item->next;
411 list_prev = NULL;
412 list_item->next = NULL;
414 pthread_mutex_unlock (&sensor_list_lock);
416 if (c_ipmi_nofiy_remove && c_ipmi_active)
417 {
418 notification_t n = { NOTIF_WARNING, cdtime (), "", "",
419 "ipmi", "", "", "", NULL };
421 sstrncpy (n.host, hostname_g, sizeof (n.host));
422 sstrncpy (n.type_instance, list_item->sensor_name,
423 sizeof (n.type_instance));
424 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
425 ssnprintf (n.message, sizeof (n.message),
426 "sensor %s removed", list_item->sensor_name);
428 plugin_dispatch_notification (&n);
429 }
431 free (list_item);
432 return (0);
433 } /* int sensor_list_remove */
435 static int sensor_list_read_all (void)
436 {
437 c_ipmi_sensor_list_t *list_item;
439 pthread_mutex_lock (&sensor_list_lock);
441 for (list_item = sensor_list;
442 list_item != NULL;
443 list_item = list_item->next)
444 {
445 ipmi_sensor_id_get_reading (list_item->sensor_id,
446 sensor_read_handler, /* user data = */ list_item);
447 } /* for (list_item) */
449 pthread_mutex_unlock (&sensor_list_lock);
451 return (0);
452 } /* int sensor_list_read_all */
454 static int sensor_list_remove_all (void)
455 {
456 c_ipmi_sensor_list_t *list_item;
458 pthread_mutex_lock (&sensor_list_lock);
460 list_item = sensor_list;
461 sensor_list = NULL;
463 pthread_mutex_unlock (&sensor_list_lock);
465 while (list_item != NULL)
466 {
467 c_ipmi_sensor_list_t *list_next = list_item->next;
469 free (list_item);
471 list_item = list_next;
472 } /* while (list_item) */
474 return (0);
475 } /* int sensor_list_remove_all */
477 /*
478 * Entity handlers
479 */
480 static void entity_sensor_update_handler (enum ipmi_update_e op,
481 ipmi_entity_t __attribute__((unused)) *entity,
482 ipmi_sensor_t *sensor,
483 void __attribute__((unused)) *user_data)
484 {
485 /* TODO: Ignore sensors we cannot read */
487 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
488 {
489 /* Will check for duplicate entries.. */
490 sensor_list_add (sensor);
491 }
492 else if (op == IPMI_DELETED)
493 {
494 sensor_list_remove (sensor);
495 }
496 } /* void entity_sensor_update_handler */
498 /*
499 * Domain handlers
500 */
501 static void domain_entity_update_handler (enum ipmi_update_e op,
502 ipmi_domain_t __attribute__((unused)) *domain,
503 ipmi_entity_t *entity,
504 void __attribute__((unused)) *user_data)
505 {
506 int status;
508 if (op == IPMI_ADDED)
509 {
510 status = ipmi_entity_add_sensor_update_handler (entity,
511 entity_sensor_update_handler, /* user data = */ NULL);
512 if (status != 0)
513 {
514 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
515 }
516 }
517 else if (op == IPMI_DELETED)
518 {
519 status = ipmi_entity_remove_sensor_update_handler (entity,
520 entity_sensor_update_handler, /* user data = */ NULL);
521 if (status != 0)
522 {
523 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
524 }
525 }
526 } /* void domain_entity_update_handler */
528 static void domain_connection_change_handler (ipmi_domain_t *domain,
529 int err,
530 unsigned int conn_num,
531 unsigned int port_num,
532 int still_connected,
533 void *user_data)
534 {
535 int status;
537 DEBUG ("domain_connection_change_handler (domain = %p, err = %i, "
538 "conn_num = %u, port_num = %u, still_connected = %i, "
539 "user_data = %p);\n",
540 (void *) domain, err, conn_num, port_num, still_connected, user_data);
542 status = ipmi_domain_add_entity_update_handler (domain,
543 domain_entity_update_handler, /* user data = */ NULL);
544 if (status != 0)
545 {
546 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
547 }
548 } /* void domain_connection_change_handler */
550 static int thread_init (os_handler_t **ret_os_handler)
551 {
552 os_handler_t *os_handler;
553 ipmi_open_option_t open_option[1];
554 ipmi_con_t *smi_connection = NULL;
555 ipmi_domain_id_t domain_id;
556 int status;
558 os_handler = ipmi_posix_thread_setup_os_handler (SIGIO);
559 if (os_handler == NULL)
560 {
561 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
562 return (-1);
563 }
565 ipmi_init (os_handler);
567 status = ipmi_smi_setup_con (/* if_num = */ 0,
568 os_handler,
569 /* user data = */ NULL,
570 &smi_connection);
571 if (status != 0)
572 {
573 c_ipmi_error ("ipmi_smi_setup_con", status);
574 return (-1);
575 }
577 memset (open_option, 0, sizeof (open_option));
578 open_option[0].option = IPMI_OPEN_OPTION_ALL;
579 open_option[0].ival = 1;
581 status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
582 domain_connection_change_handler, /* user data = */ NULL,
583 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
584 open_option, sizeof (open_option) / sizeof (open_option[0]),
585 &domain_id);
586 if (status != 0)
587 {
588 c_ipmi_error ("ipmi_open_domain", status);
589 return (-1);
590 }
592 *ret_os_handler = os_handler;
593 return (0);
594 } /* int thread_init */
596 static void *thread_main (void __attribute__((unused)) *user_data)
597 {
598 int status;
599 os_handler_t *os_handler = NULL;
601 status = thread_init (&os_handler);
602 if (status != 0)
603 {
604 ERROR ("ipmi plugin: thread_init failed.\n");
605 return ((void *) -1);
606 }
608 while (c_ipmi_active != 0)
609 {
610 struct timeval tv = { 1, 0 };
611 os_handler->perform_one_op (os_handler, &tv);
612 }
614 ipmi_posix_thread_free_os_handler (os_handler);
616 return ((void *) 0);
617 } /* void *thread_main */
619 static int c_ipmi_config (const char *key, const char *value)
620 {
621 if (ignorelist == NULL)
622 ignorelist = ignorelist_create (/* invert = */ 1);
623 if (ignorelist == NULL)
624 return (1);
626 if (strcasecmp ("Sensor", key) == 0)
627 {
628 ignorelist_add (ignorelist, value);
629 }
630 else if (strcasecmp ("IgnoreSelected", key) == 0)
631 {
632 int invert = 1;
633 if (IS_TRUE (value))
634 invert = 0;
635 ignorelist_set_invert (ignorelist, invert);
636 }
637 else if (strcasecmp ("NotifySensorAdd", key) == 0)
638 {
639 if (IS_TRUE (value))
640 c_ipmi_nofiy_add = 1;
641 }
642 else if (strcasecmp ("NotifySensorRemove", key) == 0)
643 {
644 if (IS_TRUE (value))
645 c_ipmi_nofiy_remove = 1;
646 }
647 else if (strcasecmp ("NotifySensorNotPresent", key) == 0)
648 {
649 if (IS_TRUE (value))
650 c_ipmi_nofiy_notpresent = 1;
651 }
652 else
653 {
654 return (-1);
655 }
657 return (0);
658 } /* int c_ipmi_config */
660 static int c_ipmi_init (void)
661 {
662 int status;
664 /* Don't send `ADD' notifications during startup (~ 1 minute) */
665 time_t iv = CDTIME_T_TO_TIME_T (plugin_get_interval ());
666 c_ipmi_init_in_progress = 1 + (60 / iv);
668 c_ipmi_active = 1;
670 status = plugin_thread_create (&thread_id, /* attr = */ NULL, thread_main,
671 /* user data = */ NULL);
672 if (status != 0)
673 {
674 c_ipmi_active = 0;
675 thread_id = (pthread_t) 0;
676 ERROR ("ipmi plugin: pthread_create failed.");
677 return (-1);
678 }
680 return (0);
681 } /* int c_ipmi_init */
683 static int c_ipmi_read (void)
684 {
685 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
686 {
687 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
688 return (-1);
689 }
691 sensor_list_read_all ();
693 if (c_ipmi_init_in_progress > 0)
694 c_ipmi_init_in_progress--;
695 else
696 c_ipmi_init_in_progress = 0;
698 return (0);
699 } /* int c_ipmi_read */
701 static int c_ipmi_shutdown (void)
702 {
703 c_ipmi_active = 0;
705 if (thread_id != (pthread_t) 0)
706 {
707 pthread_join (thread_id, NULL);
708 thread_id = (pthread_t) 0;
709 }
711 sensor_list_remove_all ();
713 return (0);
714 } /* int c_ipmi_shutdown */
716 void module_register (void)
717 {
718 plugin_register_config ("ipmi", c_ipmi_config,
719 config_keys, config_keys_num);
720 plugin_register_init ("ipmi", c_ipmi_init);
721 plugin_register_read ("ipmi", c_ipmi_read);
722 plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
723 } /* void module_register */
725 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */