262c439148843256afa3f7885e586751fceff5ca
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 verplant.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 <pthread.h>
33 #include <OpenIPMI/ipmiif.h>
34 #include <OpenIPMI/ipmi_err.h>
35 #include <OpenIPMI/ipmi_posix.h>
36 #include <OpenIPMI/ipmi_conn.h>
37 #include <OpenIPMI/ipmi_smi.h>
39 /*
40 * Private data types
41 */
42 struct c_ipmi_sensor_list_s;
43 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
45 struct c_ipmi_sensor_list_s
46 {
47 ipmi_sensor_id_t sensor_id;
48 char sensor_name[DATA_MAX_NAME_LEN];
49 char sensor_type[DATA_MAX_NAME_LEN];
50 int sensor_not_present;
51 c_ipmi_sensor_list_t *next;
52 };
54 /*
55 * Module global variables
56 */
57 static pthread_mutex_t sensor_list_lock = PTHREAD_MUTEX_INITIALIZER;
58 static c_ipmi_sensor_list_t *sensor_list = NULL;
60 static int c_ipmi_init_in_progress = 0;
61 static int c_ipmi_active = 0;
62 static pthread_t thread_id = (pthread_t) 0;
64 static const char *config_keys[] =
65 {
66 "Sensor",
67 "IgnoreSelected",
68 "NotifySensorAdd",
69 "NotifySensorRemove",
70 "NotifySensorNotPresent"
71 };
72 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
74 static ignorelist_t *ignorelist = NULL;
76 static int c_ipmi_nofiy_add = 0;
77 static int c_ipmi_nofiy_remove = 0;
78 static int c_ipmi_nofiy_notpresent = 0;
80 /*
81 * Misc private functions
82 */
83 static void c_ipmi_error (const char *func, int status)
84 {
85 char errbuf[4096];
87 memset (errbuf, 0, sizeof (errbuf));
89 if (IPMI_IS_OS_ERR (status))
90 {
91 sstrerror (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
92 }
93 else if (IPMI_IS_IPMI_ERR (status))
94 {
95 ipmi_get_error_string (IPMI_GET_IPMI_ERR (status), errbuf, sizeof (errbuf));
96 }
98 if (errbuf[0] == 0)
99 {
100 ssnprintf (errbuf, sizeof (errbuf), "Unknown error %#x", status);
101 }
102 errbuf[sizeof (errbuf) - 1] = 0;
104 ERROR ("ipmi plugin: %s failed: %s", func, errbuf);
105 } /* void c_ipmi_error */
107 /*
108 * Sensor handlers
109 */
110 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
111 static int sensor_list_remove (ipmi_sensor_t *sensor);
113 static void sensor_read_handler (ipmi_sensor_t *sensor,
114 int err,
115 enum ipmi_value_present_e value_present,
116 unsigned int __attribute__((unused)) raw_value,
117 double value,
118 ipmi_states_t __attribute__((unused)) *states,
119 void *user_data)
120 {
121 value_t values[1];
122 value_list_t vl = VALUE_LIST_INIT;
124 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
126 if (err != 0)
127 {
128 if ((err & 0xff) == IPMI_NOT_PRESENT_CC)
129 {
130 if (list_item->sensor_not_present == 0)
131 {
132 list_item->sensor_not_present = 1;
134 INFO ("ipmi plugin: sensor_read_handler: sensor %s "
135 "not present.", list_item->sensor_name);
137 if (c_ipmi_nofiy_notpresent)
138 {
139 notification_t n = { NOTIF_WARNING, time(NULL), "", "", "ipmi",
140 "", "", "", NULL };
142 sstrncpy (n.host, hostname_g, sizeof (n.host));
143 sstrncpy (n.type_instance, list_item->sensor_name,
144 sizeof (n.type_instance));
145 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
146 ssnprintf (n.message, sizeof (n.message),
147 "sensor %s not present", list_item->sensor_name);
149 plugin_dispatch_notification (&n);
150 }
151 }
152 }
153 else if (IPMI_IS_IPMI_ERR(err) && IPMI_GET_IPMI_ERR(err) == IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC)
154 {
155 INFO ("ipmi plugin: sensor_read_handler: Sensor %s not ready",
156 list_item->sensor_name);
157 }
158 else
159 {
160 if (IPMI_IS_IPMI_ERR(err))
161 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
162 "because it failed with IPMI error %#x.",
163 list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
164 else if (IPMI_IS_OS_ERR(err))
165 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
166 "because it failed with OS error %#x.",
167 list_item->sensor_name, IPMI_GET_OS_ERR(err));
168 else if (IPMI_IS_RMCPP_ERR(err))
169 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
170 "because it failed with RMCPP error %#x.",
171 list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
172 else if (IPMI_IS_SOL_ERR(err))
173 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
174 "because it failed with RMCPP error %#x.",
175 list_item->sensor_name, IPMI_GET_SOL_ERR(err));
176 else
177 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
178 "because it failed with error %#x. of class %#x",
179 list_item->sensor_name, err & 0xff, err & 0xffffff00);
180 sensor_list_remove (sensor);
181 }
182 return;
183 }
184 else if (list_item->sensor_not_present == 1)
185 {
186 list_item->sensor_not_present = 0;
188 INFO ("ipmi plugin: sensor_read_handler: sensor %s present.",
189 list_item->sensor_name);
191 if (c_ipmi_nofiy_notpresent)
192 {
193 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
194 "", "", "", NULL };
196 sstrncpy (n.host, hostname_g, sizeof (n.host));
197 sstrncpy (n.type_instance, list_item->sensor_name,
198 sizeof (n.type_instance));
199 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
200 ssnprintf (n.message, sizeof (n.message),
201 "sensor %s present", list_item->sensor_name);
203 plugin_dispatch_notification (&n);
204 }
205 }
207 if (value_present != IPMI_BOTH_VALUES_PRESENT)
208 {
209 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
210 "because it provides %s. If you need this sensor, "
211 "please file a bug report.",
212 list_item->sensor_name,
213 (value_present == IPMI_RAW_VALUE_PRESENT)
214 ? "only the raw value"
215 : "no value");
216 sensor_list_remove (sensor);
217 return;
218 }
220 values[0].gauge = value;
222 vl.values = values;
223 vl.values_len = 1;
225 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
226 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
227 sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
228 sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
230 plugin_dispatch_values (&vl);
231 } /* void sensor_read_handler */
233 static int sensor_list_add (ipmi_sensor_t *sensor)
234 {
235 ipmi_sensor_id_t sensor_id;
236 c_ipmi_sensor_list_t *list_item;
237 c_ipmi_sensor_list_t *list_prev;
239 char buffer[DATA_MAX_NAME_LEN];
240 const char *entity_id_string;
241 char sensor_name[DATA_MAX_NAME_LEN];
242 char *sensor_name_ptr;
243 int sensor_type;
244 const char *type;
245 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
247 sensor_id = ipmi_sensor_convert_to_id (sensor);
249 memset (buffer, 0, sizeof (buffer));
250 ipmi_sensor_get_name (sensor, buffer, sizeof (buffer));
251 buffer[sizeof (buffer) - 1] = 0;
253 entity_id_string = ipmi_entity_get_entity_id_string (ent);
255 if (entity_id_string == NULL)
256 sstrncpy (sensor_name, buffer, sizeof (sensor_name));
257 else
258 ssnprintf (sensor_name, sizeof (sensor_name),
259 "%s %s", buffer, entity_id_string);
261 sstrncpy (buffer, sensor_name, sizeof (buffer));
262 sensor_name_ptr = strstr (buffer, ").");
263 if (sensor_name_ptr != NULL)
264 {
265 /* If name is something like "foo (123).bar",
266 * change that to "bar (123)".
267 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
268 * `buffer' array, which holds a copy of the current `sensor_name'. */
269 char *sensor_id_ptr;
271 /* `sensor_name_ptr' points to ").bar". */
272 sensor_name_ptr[1] = 0;
273 /* `buffer' holds "foo (123)\0bar\0". */
274 sensor_name_ptr += 2;
275 /* `sensor_name_ptr' now points to "bar". */
277 sensor_id_ptr = strstr (buffer, "(");
278 if (sensor_id_ptr != NULL)
279 {
280 /* `sensor_id_ptr' now points to "(123)". */
281 ssnprintf (sensor_name, sizeof (sensor_name),
282 "%s %s", sensor_name_ptr, sensor_id_ptr);
283 }
284 /* else: don't touch sensor_name. */
285 }
286 sensor_name_ptr = sensor_name;
288 /* Both `ignorelist' and `plugin_instance' may be NULL. */
289 if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
290 return (0);
292 /* FIXME: Use rate unit or base unit to scale the value */
294 sensor_type = ipmi_sensor_get_sensor_type (sensor);
295 switch (sensor_type)
296 {
297 case IPMI_SENSOR_TYPE_TEMPERATURE:
298 type = "temperature";
299 break;
301 case IPMI_SENSOR_TYPE_VOLTAGE:
302 type = "voltage";
303 break;
305 case IPMI_SENSOR_TYPE_CURRENT:
306 type = "current";
307 break;
309 case IPMI_SENSOR_TYPE_FAN:
310 type = "fanspeed";
311 break;
313 default:
314 {
315 const char *sensor_type_str;
317 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
318 INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
319 "because I don't know how to handle its type (%#x, %s). "
320 "If you need this sensor, please file a bug report.",
321 sensor_name_ptr, sensor_type, sensor_type_str);
322 return (-1);
323 }
324 } /* switch (sensor_type) */
326 pthread_mutex_lock (&sensor_list_lock);
328 list_prev = NULL;
329 for (list_item = sensor_list;
330 list_item != NULL;
331 list_item = list_item->next)
332 {
333 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
334 break;
335 list_prev = list_item;
336 } /* for (list_item) */
338 if (list_item != NULL)
339 {
340 pthread_mutex_unlock (&sensor_list_lock);
341 return (0);
342 }
344 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
345 if (list_item == NULL)
346 {
347 pthread_mutex_unlock (&sensor_list_lock);
348 return (-1);
349 }
351 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
353 if (list_prev != NULL)
354 list_prev->next = list_item;
355 else
356 sensor_list = list_item;
358 sstrncpy (list_item->sensor_name, sensor_name_ptr,
359 sizeof (list_item->sensor_name));
360 sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
362 pthread_mutex_unlock (&sensor_list_lock);
364 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
365 {
366 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
367 "", "", "", NULL };
369 sstrncpy (n.host, hostname_g, sizeof (n.host));
370 sstrncpy (n.type_instance, list_item->sensor_name,
371 sizeof (n.type_instance));
372 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
373 ssnprintf (n.message, sizeof (n.message),
374 "sensor %s added", list_item->sensor_name);
376 plugin_dispatch_notification (&n);
377 }
379 return (0);
380 } /* int sensor_list_add */
382 static int sensor_list_remove (ipmi_sensor_t *sensor)
383 {
384 ipmi_sensor_id_t sensor_id;
385 c_ipmi_sensor_list_t *list_item;
386 c_ipmi_sensor_list_t *list_prev;
388 sensor_id = ipmi_sensor_convert_to_id (sensor);
390 pthread_mutex_lock (&sensor_list_lock);
392 list_prev = NULL;
393 for (list_item = sensor_list;
394 list_item != NULL;
395 list_item = list_item->next)
396 {
397 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
398 break;
399 list_prev = list_item;
400 } /* for (list_item) */
402 if (list_item == NULL)
403 {
404 pthread_mutex_unlock (&sensor_list_lock);
405 return (-1);
406 }
408 if (list_prev == NULL)
409 sensor_list = list_item->next;
410 else
411 list_prev->next = list_item->next;
413 list_prev = NULL;
414 list_item->next = NULL;
416 pthread_mutex_unlock (&sensor_list_lock);
418 if (c_ipmi_nofiy_remove && c_ipmi_active)
419 {
420 notification_t n = { NOTIF_WARNING, time(NULL), "", "",
421 "ipmi", "", "", "", NULL };
423 sstrncpy (n.host, hostname_g, sizeof (n.host));
424 sstrncpy (n.type_instance, list_item->sensor_name,
425 sizeof (n.type_instance));
426 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
427 ssnprintf (n.message, sizeof (n.message),
428 "sensor %s removed", list_item->sensor_name);
430 plugin_dispatch_notification (&n);
431 }
433 free (list_item);
434 return (0);
435 } /* int sensor_list_remove */
437 static int sensor_list_read_all (void)
438 {
439 c_ipmi_sensor_list_t *list_item;
441 pthread_mutex_lock (&sensor_list_lock);
443 for (list_item = sensor_list;
444 list_item != NULL;
445 list_item = list_item->next)
446 {
447 ipmi_sensor_id_get_reading (list_item->sensor_id,
448 sensor_read_handler, /* user data = */ list_item);
449 } /* for (list_item) */
451 pthread_mutex_unlock (&sensor_list_lock);
453 return (0);
454 } /* int sensor_list_read_all */
456 static int sensor_list_remove_all (void)
457 {
458 c_ipmi_sensor_list_t *list_item;
460 pthread_mutex_lock (&sensor_list_lock);
462 list_item = sensor_list;
463 sensor_list = NULL;
465 pthread_mutex_unlock (&sensor_list_lock);
467 while (list_item != NULL)
468 {
469 c_ipmi_sensor_list_t *list_next = list_item->next;
471 free (list_item);
473 list_item = list_next;
474 } /* while (list_item) */
476 return (0);
477 } /* int sensor_list_remove_all */
479 /*
480 * Entity handlers
481 */
482 static void entity_sensor_update_handler (enum ipmi_update_e op,
483 ipmi_entity_t __attribute__((unused)) *entity,
484 ipmi_sensor_t *sensor,
485 void __attribute__((unused)) *user_data)
486 {
487 /* TODO: Ignore sensors we cannot read */
489 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
490 {
491 /* Will check for duplicate entries.. */
492 sensor_list_add (sensor);
493 }
494 else if (op == IPMI_DELETED)
495 {
496 sensor_list_remove (sensor);
497 }
498 } /* void entity_sensor_update_handler */
500 /*
501 * Domain handlers
502 */
503 static void domain_entity_update_handler (enum ipmi_update_e op,
504 ipmi_domain_t __attribute__((unused)) *domain,
505 ipmi_entity_t *entity,
506 void __attribute__((unused)) *user_data)
507 {
508 int status;
510 if (op == IPMI_ADDED)
511 {
512 status = ipmi_entity_add_sensor_update_handler (entity,
513 entity_sensor_update_handler, /* user data = */ NULL);
514 if (status != 0)
515 {
516 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
517 }
518 }
519 else if (op == IPMI_DELETED)
520 {
521 status = ipmi_entity_remove_sensor_update_handler (entity,
522 entity_sensor_update_handler, /* user data = */ NULL);
523 if (status != 0)
524 {
525 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
526 }
527 }
528 } /* void domain_entity_update_handler */
530 static void domain_connection_change_handler (ipmi_domain_t *domain,
531 int err,
532 unsigned int conn_num,
533 unsigned int port_num,
534 int still_connected,
535 void *user_data)
536 {
537 int status;
539 DEBUG ("domain_connection_change_handler (domain = %p, err = %i, "
540 "conn_num = %u, port_num = %u, still_connected = %i, "
541 "user_data = %p);\n",
542 (void *) domain, err, conn_num, port_num, still_connected, user_data);
544 status = ipmi_domain_add_entity_update_handler (domain,
545 domain_entity_update_handler, /* user data = */ NULL);
546 if (status != 0)
547 {
548 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
549 }
550 } /* void domain_connection_change_handler */
552 static int thread_init (os_handler_t **ret_os_handler)
553 {
554 os_handler_t *os_handler;
555 ipmi_open_option_t open_option[1];
556 ipmi_con_t *smi_connection = NULL;
557 ipmi_domain_id_t domain_id;
558 int status;
560 os_handler = ipmi_posix_thread_setup_os_handler (SIGIO);
561 if (os_handler == NULL)
562 {
563 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
564 return (-1);
565 }
567 ipmi_init (os_handler);
569 status = ipmi_smi_setup_con (/* if_num = */ 0,
570 os_handler,
571 /* user data = */ NULL,
572 &smi_connection);
573 if (status != 0)
574 {
575 c_ipmi_error ("ipmi_smi_setup_con", status);
576 return (-1);
577 }
579 memset (open_option, 0, sizeof (open_option));
580 open_option[0].option = IPMI_OPEN_OPTION_ALL;
581 open_option[0].ival = 1;
583 status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
584 domain_connection_change_handler, /* user data = */ NULL,
585 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
586 open_option, sizeof (open_option) / sizeof (open_option[0]),
587 &domain_id);
588 if (status != 0)
589 {
590 c_ipmi_error ("ipmi_open_domain", status);
591 return (-1);
592 }
594 *ret_os_handler = os_handler;
595 return (0);
596 } /* int thread_init */
598 static void *thread_main (void __attribute__((unused)) *user_data)
599 {
600 int status;
601 os_handler_t *os_handler = NULL;
603 status = thread_init (&os_handler);
604 if (status != 0)
605 {
606 ERROR ("ipmi plugin: thread_init failed.\n");
607 return ((void *) -1);
608 }
610 while (c_ipmi_active != 0)
611 {
612 struct timeval tv = { 1, 0 };
613 os_handler->perform_one_op (os_handler, &tv);
614 }
616 ipmi_posix_thread_free_os_handler (os_handler);
618 return ((void *) 0);
619 } /* void *thread_main */
621 static int c_ipmi_config (const char *key, const char *value)
622 {
623 if (ignorelist == NULL)
624 ignorelist = ignorelist_create (/* invert = */ 1);
625 if (ignorelist == NULL)
626 return (1);
628 if (strcasecmp ("Sensor", key) == 0)
629 {
630 ignorelist_add (ignorelist, value);
631 }
632 else if (strcasecmp ("IgnoreSelected", key) == 0)
633 {
634 int invert = 1;
635 if (IS_TRUE (value))
636 invert = 0;
637 ignorelist_set_invert (ignorelist, invert);
638 }
639 else if (strcasecmp ("NotifySensorAdd", key) == 0)
640 {
641 if (IS_TRUE (value))
642 c_ipmi_nofiy_add = 1;
643 }
644 else if (strcasecmp ("NotifySensorRemove", key) == 0)
645 {
646 if (IS_TRUE (value))
647 c_ipmi_nofiy_remove = 1;
648 }
649 else if (strcasecmp ("NotifySensorNotPresent", key) == 0)
650 {
651 if (IS_TRUE (value))
652 c_ipmi_nofiy_notpresent = 1;
653 }
654 else
655 {
656 return (-1);
657 }
659 return (0);
660 } /* int c_ipmi_config */
662 static int c_ipmi_init (void)
663 {
664 int status;
666 /* Don't send `ADD' notifications during startup (~ 1 minute) */
667 c_ipmi_init_in_progress = 1 + (60 / interval_g);
669 c_ipmi_active = 1;
671 status = pthread_create (&thread_id, /* attr = */ NULL, thread_main,
672 /* user data = */ NULL);
673 if (status != 0)
674 {
675 c_ipmi_active = 0;
676 thread_id = (pthread_t) 0;
677 ERROR ("ipmi plugin: pthread_create failed.");
678 return (-1);
679 }
681 return (0);
682 } /* int c_ipmi_init */
684 static int c_ipmi_read (void)
685 {
686 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
687 {
688 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
689 return (-1);
690 }
692 sensor_list_read_all ();
694 if (c_ipmi_init_in_progress > 0)
695 c_ipmi_init_in_progress--;
696 else
697 c_ipmi_init_in_progress = 0;
699 return (0);
700 } /* int c_ipmi_read */
702 static int c_ipmi_shutdown (void)
703 {
704 c_ipmi_active = 0;
706 if (thread_id != (pthread_t) 0)
707 {
708 pthread_join (thread_id, NULL);
709 thread_id = (pthread_t) 0;
710 }
712 sensor_list_remove_all ();
714 return (0);
715 } /* int c_ipmi_shutdown */
717 void module_register (void)
718 {
719 plugin_register_config ("ipmi", c_ipmi_config,
720 config_keys, config_keys_num);
721 plugin_register_init ("ipmi", c_ipmi_init);
722 plugin_register_read ("ipmi", c_ipmi_read);
723 plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
724 } /* void module_register */
726 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */