a42b787fe5a40e1b67126a7eaa884371d91b026e
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/ipmiif.h>
33 #include <OpenIPMI/ipmi_err.h>
34 #include <OpenIPMI/ipmi_posix.h>
35 #include <OpenIPMI/ipmi_conn.h>
36 #include <OpenIPMI/ipmi_smi.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 {
46 ipmi_sensor_id_t sensor_id;
47 char sensor_name[DATA_MAX_NAME_LEN];
48 char sensor_type[DATA_MAX_NAME_LEN];
49 int sensor_not_present;
50 c_ipmi_sensor_list_t *next;
51 };
53 /*
54 * Module global variables
55 */
56 static pthread_mutex_t sensor_list_lock = PTHREAD_MUTEX_INITIALIZER;
57 static c_ipmi_sensor_list_t *sensor_list = NULL;
59 static int c_ipmi_init_in_progress = 0;
60 static int c_ipmi_active = 0;
61 static pthread_t thread_id = (pthread_t) 0;
63 static const char *config_keys[] =
64 {
65 "Sensor",
66 "IgnoreSelected",
67 "NotifySensorAdd",
68 "NotifySensorRemove",
69 "NotifySensorNotPresent"
70 };
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 static ignorelist_t *ignorelist = NULL;
75 static int c_ipmi_nofiy_add = 0;
76 static int c_ipmi_nofiy_remove = 0;
77 static int c_ipmi_nofiy_notpresent = 0;
79 /*
80 * Misc private functions
81 */
82 static void c_ipmi_error (const char *func, int status)
83 {
84 char errbuf[4096] = { 0 };
86 if (IPMI_IS_OS_ERR (status))
87 {
88 sstrerror (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
89 }
90 else if (IPMI_IS_IPMI_ERR (status))
91 {
92 ipmi_get_error_string (IPMI_GET_IPMI_ERR (status), errbuf, sizeof (errbuf));
93 }
95 if (errbuf[0] == 0)
96 {
97 ssnprintf (errbuf, sizeof (errbuf), "Unknown error %#x", status);
98 }
99 errbuf[sizeof (errbuf) - 1] = 0;
101 ERROR ("ipmi plugin: %s failed: %s", func, errbuf);
102 } /* void c_ipmi_error */
104 /*
105 * Sensor handlers
106 */
107 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
108 static int sensor_list_remove (ipmi_sensor_t *sensor);
110 static void sensor_read_handler (ipmi_sensor_t *sensor,
111 int err,
112 enum ipmi_value_present_e value_present,
113 unsigned int __attribute__((unused)) raw_value,
114 double value,
115 ipmi_states_t __attribute__((unused)) *states,
116 void *user_data)
117 {
118 value_list_t vl = VALUE_LIST_INIT;
120 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
122 if (err != 0)
123 {
124 if ((err & 0xff) == IPMI_NOT_PRESENT_CC)
125 {
126 if (list_item->sensor_not_present == 0)
127 {
128 list_item->sensor_not_present = 1;
130 INFO ("ipmi plugin: sensor_read_handler: sensor %s "
131 "not present.", list_item->sensor_name);
133 if (c_ipmi_nofiy_notpresent)
134 {
135 notification_t n = { NOTIF_WARNING, cdtime (), "", "", "ipmi",
136 "", "", "", NULL };
138 sstrncpy (n.host, hostname_g, sizeof (n.host));
139 sstrncpy (n.type_instance, list_item->sensor_name,
140 sizeof (n.type_instance));
141 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
142 ssnprintf (n.message, sizeof (n.message),
143 "sensor %s not present", list_item->sensor_name);
145 plugin_dispatch_notification (&n);
146 }
147 }
148 }
149 else if (IPMI_IS_IPMI_ERR(err) && IPMI_GET_IPMI_ERR(err) == IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC)
150 {
151 INFO ("ipmi plugin: sensor_read_handler: Sensor %s not ready",
152 list_item->sensor_name);
153 }
154 else
155 {
156 if (IPMI_IS_IPMI_ERR(err))
157 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
158 "because it failed with IPMI error %#x.",
159 list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
160 else if (IPMI_IS_OS_ERR(err))
161 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
162 "because it failed with OS error %#x.",
163 list_item->sensor_name, IPMI_GET_OS_ERR(err));
164 else if (IPMI_IS_RMCPP_ERR(err))
165 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
166 "because it failed with RMCPP error %#x.",
167 list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
168 else if (IPMI_IS_SOL_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_SOL_ERR(err));
172 else
173 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
174 "because it failed with error %#x. of class %#x",
175 list_item->sensor_name, err & 0xff, err & 0xffffff00);
176 sensor_list_remove (sensor);
177 }
178 return;
179 }
180 else if (list_item->sensor_not_present == 1)
181 {
182 list_item->sensor_not_present = 0;
184 INFO ("ipmi plugin: sensor_read_handler: sensor %s present.",
185 list_item->sensor_name);
187 if (c_ipmi_nofiy_notpresent)
188 {
189 notification_t n = { NOTIF_OKAY, cdtime (), "", "", "ipmi",
190 "", "", "", NULL };
192 sstrncpy (n.host, hostname_g, sizeof (n.host));
193 sstrncpy (n.type_instance, list_item->sensor_name,
194 sizeof (n.type_instance));
195 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
196 ssnprintf (n.message, sizeof (n.message),
197 "sensor %s present", list_item->sensor_name);
199 plugin_dispatch_notification (&n);
200 }
201 }
203 if (value_present != IPMI_BOTH_VALUES_PRESENT)
204 {
205 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
206 "because it provides %s. If you need this sensor, "
207 "please file a bug report.",
208 list_item->sensor_name,
209 (value_present == IPMI_RAW_VALUE_PRESENT)
210 ? "only the raw value"
211 : "no value");
212 sensor_list_remove (sensor);
213 return;
214 }
216 vl.values = &(value_t) { .gauge = value };
217 vl.values_len = 1;
219 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
220 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
221 sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
222 sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
224 plugin_dispatch_values (&vl);
225 } /* void sensor_read_handler */
227 static int sensor_list_add (ipmi_sensor_t *sensor)
228 {
229 ipmi_sensor_id_t sensor_id;
230 c_ipmi_sensor_list_t *list_item;
231 c_ipmi_sensor_list_t *list_prev;
233 char buffer[DATA_MAX_NAME_LEN] = { 0 };
234 const char *entity_id_string;
235 char sensor_name[DATA_MAX_NAME_LEN];
236 char *sensor_name_ptr;
237 int sensor_type;
238 const char *type;
239 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
241 sensor_id = ipmi_sensor_convert_to_id (sensor);
243 ipmi_sensor_get_name (sensor, buffer, sizeof (buffer));
244 buffer[sizeof (buffer) - 1] = 0;
246 entity_id_string = ipmi_entity_get_entity_id_string (ent);
248 if (entity_id_string == NULL)
249 sstrncpy (sensor_name, buffer, sizeof (sensor_name));
250 else
251 ssnprintf (sensor_name, sizeof (sensor_name),
252 "%s %s", buffer, entity_id_string);
254 sstrncpy (buffer, sensor_name, sizeof (buffer));
255 sensor_name_ptr = strstr (buffer, ").");
256 if (sensor_name_ptr != NULL)
257 {
258 /* If name is something like "foo (123).bar",
259 * change that to "bar (123)".
260 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
261 * `buffer' array, which holds a copy of the current `sensor_name'. */
262 char *sensor_id_ptr;
264 /* `sensor_name_ptr' points to ").bar". */
265 sensor_name_ptr[1] = 0;
266 /* `buffer' holds "foo (123)\0bar\0". */
267 sensor_name_ptr += 2;
268 /* `sensor_name_ptr' now points to "bar". */
270 sensor_id_ptr = strstr (buffer, "(");
271 if (sensor_id_ptr != NULL)
272 {
273 /* `sensor_id_ptr' now points to "(123)". */
274 ssnprintf (sensor_name, sizeof (sensor_name),
275 "%s %s", sensor_name_ptr, sensor_id_ptr);
276 }
277 /* else: don't touch sensor_name. */
278 }
279 sensor_name_ptr = sensor_name;
281 /* Both `ignorelist' and `plugin_instance' may be NULL. */
282 if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
283 return (0);
285 /* FIXME: Use rate unit or base unit to scale the value */
287 sensor_type = ipmi_sensor_get_sensor_type (sensor);
288 switch (sensor_type)
289 {
290 case IPMI_SENSOR_TYPE_TEMPERATURE:
291 type = "temperature";
292 break;
294 case IPMI_SENSOR_TYPE_VOLTAGE:
295 type = "voltage";
296 break;
298 case IPMI_SENSOR_TYPE_CURRENT:
299 type = "current";
300 break;
302 case IPMI_SENSOR_TYPE_FAN:
303 type = "fanspeed";
304 break;
306 default:
307 {
308 const char *sensor_type_str;
310 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
311 INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
312 "because I don't know how to handle its type (%#x, %s). "
313 "If you need this sensor, please file a bug report.",
314 sensor_name_ptr, sensor_type, sensor_type_str);
315 return (-1);
316 }
317 } /* switch (sensor_type) */
319 pthread_mutex_lock (&sensor_list_lock);
321 list_prev = NULL;
322 for (list_item = sensor_list;
323 list_item != NULL;
324 list_item = list_item->next)
325 {
326 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
327 break;
328 list_prev = list_item;
329 } /* for (list_item) */
331 if (list_item != NULL)
332 {
333 pthread_mutex_unlock (&sensor_list_lock);
334 return (0);
335 }
337 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
338 if (list_item == NULL)
339 {
340 pthread_mutex_unlock (&sensor_list_lock);
341 return (-1);
342 }
344 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
346 if (list_prev != NULL)
347 list_prev->next = list_item;
348 else
349 sensor_list = list_item;
351 sstrncpy (list_item->sensor_name, sensor_name_ptr,
352 sizeof (list_item->sensor_name));
353 sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
355 pthread_mutex_unlock (&sensor_list_lock);
357 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
358 {
359 notification_t n = { NOTIF_OKAY, cdtime (), "", "", "ipmi",
360 "", "", "", NULL };
362 sstrncpy (n.host, hostname_g, sizeof (n.host));
363 sstrncpy (n.type_instance, list_item->sensor_name,
364 sizeof (n.type_instance));
365 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
366 ssnprintf (n.message, sizeof (n.message),
367 "sensor %s added", list_item->sensor_name);
369 plugin_dispatch_notification (&n);
370 }
372 return (0);
373 } /* int sensor_list_add */
375 static int sensor_list_remove (ipmi_sensor_t *sensor)
376 {
377 ipmi_sensor_id_t sensor_id;
378 c_ipmi_sensor_list_t *list_item;
379 c_ipmi_sensor_list_t *list_prev;
381 sensor_id = ipmi_sensor_convert_to_id (sensor);
383 pthread_mutex_lock (&sensor_list_lock);
385 list_prev = NULL;
386 for (list_item = sensor_list;
387 list_item != NULL;
388 list_item = list_item->next)
389 {
390 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
391 break;
392 list_prev = list_item;
393 } /* for (list_item) */
395 if (list_item == NULL)
396 {
397 pthread_mutex_unlock (&sensor_list_lock);
398 return (-1);
399 }
401 if (list_prev == NULL)
402 sensor_list = list_item->next;
403 else
404 list_prev->next = list_item->next;
406 list_prev = NULL;
407 list_item->next = NULL;
409 pthread_mutex_unlock (&sensor_list_lock);
411 if (c_ipmi_nofiy_remove && c_ipmi_active)
412 {
413 notification_t n = { NOTIF_WARNING, cdtime (), "", "",
414 "ipmi", "", "", "", NULL };
416 sstrncpy (n.host, hostname_g, sizeof (n.host));
417 sstrncpy (n.type_instance, list_item->sensor_name,
418 sizeof (n.type_instance));
419 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
420 ssnprintf (n.message, sizeof (n.message),
421 "sensor %s removed", list_item->sensor_name);
423 plugin_dispatch_notification (&n);
424 }
426 free (list_item);
427 return (0);
428 } /* int sensor_list_remove */
430 static int sensor_list_read_all (void)
431 {
432 pthread_mutex_lock (&sensor_list_lock);
434 for (c_ipmi_sensor_list_t *list_item = sensor_list;
435 list_item != NULL;
436 list_item = list_item->next)
437 {
438 ipmi_sensor_id_get_reading (list_item->sensor_id,
439 sensor_read_handler, /* user data = */ list_item);
440 } /* for (list_item) */
442 pthread_mutex_unlock (&sensor_list_lock);
444 return (0);
445 } /* int sensor_list_read_all */
447 static int sensor_list_remove_all (void)
448 {
449 c_ipmi_sensor_list_t *list_item;
451 pthread_mutex_lock (&sensor_list_lock);
453 list_item = sensor_list;
454 sensor_list = NULL;
456 pthread_mutex_unlock (&sensor_list_lock);
458 while (list_item != NULL)
459 {
460 c_ipmi_sensor_list_t *list_next = list_item->next;
462 free (list_item);
464 list_item = list_next;
465 } /* while (list_item) */
467 return (0);
468 } /* int sensor_list_remove_all */
470 /*
471 * Entity handlers
472 */
473 static void entity_sensor_update_handler (enum ipmi_update_e op,
474 ipmi_entity_t __attribute__((unused)) *entity,
475 ipmi_sensor_t *sensor,
476 void __attribute__((unused)) *user_data)
477 {
478 /* TODO: Ignore sensors we cannot read */
480 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
481 {
482 /* Will check for duplicate entries.. */
483 sensor_list_add (sensor);
484 }
485 else if (op == IPMI_DELETED)
486 {
487 sensor_list_remove (sensor);
488 }
489 } /* void entity_sensor_update_handler */
491 /*
492 * Domain handlers
493 */
494 static void domain_entity_update_handler (enum ipmi_update_e op,
495 ipmi_domain_t __attribute__((unused)) *domain,
496 ipmi_entity_t *entity,
497 void __attribute__((unused)) *user_data)
498 {
499 int status;
501 if (op == IPMI_ADDED)
502 {
503 status = ipmi_entity_add_sensor_update_handler (entity,
504 entity_sensor_update_handler, /* user data = */ NULL);
505 if (status != 0)
506 {
507 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
508 }
509 }
510 else if (op == IPMI_DELETED)
511 {
512 status = ipmi_entity_remove_sensor_update_handler (entity,
513 entity_sensor_update_handler, /* user data = */ NULL);
514 if (status != 0)
515 {
516 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
517 }
518 }
519 } /* void domain_entity_update_handler */
521 static void domain_connection_change_handler (ipmi_domain_t *domain,
522 int err,
523 unsigned int conn_num,
524 unsigned int port_num,
525 int still_connected,
526 void *user_data)
527 {
528 int status;
530 DEBUG ("domain_connection_change_handler (domain = %p, err = %i, "
531 "conn_num = %u, port_num = %u, still_connected = %i, "
532 "user_data = %p);\n",
533 (void *) domain, err, conn_num, port_num, still_connected, user_data);
535 status = ipmi_domain_add_entity_update_handler (domain,
536 domain_entity_update_handler, /* user data = */ NULL);
537 if (status != 0)
538 {
539 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
540 }
541 } /* void domain_connection_change_handler */
543 static int thread_init (os_handler_t **ret_os_handler)
544 {
545 os_handler_t *os_handler;
546 ipmi_con_t *smi_connection = NULL;
547 ipmi_domain_id_t domain_id;
548 int status;
550 os_handler = ipmi_posix_thread_setup_os_handler (SIGIO);
551 if (os_handler == NULL)
552 {
553 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
554 return (-1);
555 }
557 ipmi_init (os_handler);
559 status = ipmi_smi_setup_con (/* if_num = */ 0,
560 os_handler,
561 /* user data = */ NULL,
562 &smi_connection);
563 if (status != 0)
564 {
565 c_ipmi_error ("ipmi_smi_setup_con", status);
566 return (-1);
567 }
569 ipmi_open_option_t open_option[1] = {
570 [0] = {
571 .option = IPMI_OPEN_OPTION_ALL,
572 { .ival = 1 }
573 }
574 };
576 status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
577 domain_connection_change_handler, /* user data = */ NULL,
578 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
579 open_option, sizeof (open_option) / sizeof (open_option[0]),
580 &domain_id);
581 if (status != 0)
582 {
583 c_ipmi_error ("ipmi_open_domain", status);
584 return (-1);
585 }
587 *ret_os_handler = os_handler;
588 return (0);
589 } /* int thread_init */
591 static void *thread_main (void __attribute__((unused)) *user_data)
592 {
593 int status;
594 os_handler_t *os_handler = NULL;
596 status = thread_init (&os_handler);
597 if (status != 0)
598 {
599 ERROR ("ipmi plugin: thread_init failed.\n");
600 return ((void *) -1);
601 }
603 while (c_ipmi_active != 0)
604 {
605 struct timeval tv = { 1, 0 };
606 os_handler->perform_one_op (os_handler, &tv);
607 }
609 ipmi_posix_thread_free_os_handler (os_handler);
611 return ((void *) 0);
612 } /* void *thread_main */
614 static int c_ipmi_config (const char *key, const char *value)
615 {
616 if (ignorelist == NULL)
617 ignorelist = ignorelist_create (/* invert = */ 1);
618 if (ignorelist == NULL)
619 return (1);
621 if (strcasecmp ("Sensor", key) == 0)
622 {
623 ignorelist_add (ignorelist, value);
624 }
625 else if (strcasecmp ("IgnoreSelected", key) == 0)
626 {
627 int invert = 1;
628 if (IS_TRUE (value))
629 invert = 0;
630 ignorelist_set_invert (ignorelist, invert);
631 }
632 else if (strcasecmp ("NotifySensorAdd", key) == 0)
633 {
634 if (IS_TRUE (value))
635 c_ipmi_nofiy_add = 1;
636 }
637 else if (strcasecmp ("NotifySensorRemove", key) == 0)
638 {
639 if (IS_TRUE (value))
640 c_ipmi_nofiy_remove = 1;
641 }
642 else if (strcasecmp ("NotifySensorNotPresent", key) == 0)
643 {
644 if (IS_TRUE (value))
645 c_ipmi_nofiy_notpresent = 1;
646 }
647 else
648 {
649 return (-1);
650 }
652 return (0);
653 } /* int c_ipmi_config */
655 static int c_ipmi_init (void)
656 {
657 int status;
659 /* Don't send `ADD' notifications during startup (~ 1 minute) */
660 time_t iv = CDTIME_T_TO_TIME_T (plugin_get_interval ());
661 c_ipmi_init_in_progress = 1 + (60 / iv);
663 c_ipmi_active = 1;
665 status = plugin_thread_create (&thread_id, /* attr = */ NULL, thread_main,
666 /* user data = */ NULL);
667 if (status != 0)
668 {
669 c_ipmi_active = 0;
670 thread_id = (pthread_t) 0;
671 ERROR ("ipmi plugin: pthread_create failed.");
672 return (-1);
673 }
675 return (0);
676 } /* int c_ipmi_init */
678 static int c_ipmi_read (void)
679 {
680 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
681 {
682 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
683 return (-1);
684 }
686 sensor_list_read_all ();
688 if (c_ipmi_init_in_progress > 0)
689 c_ipmi_init_in_progress--;
690 else
691 c_ipmi_init_in_progress = 0;
693 return (0);
694 } /* int c_ipmi_read */
696 static int c_ipmi_shutdown (void)
697 {
698 c_ipmi_active = 0;
700 if (thread_id != (pthread_t) 0)
701 {
702 pthread_join (thread_id, NULL);
703 thread_id = (pthread_t) 0;
704 }
706 sensor_list_remove_all ();
708 return (0);
709 } /* int c_ipmi_shutdown */
711 void module_register (void)
712 {
713 plugin_register_config ("ipmi", c_ipmi_config,
714 config_keys, config_keys_num);
715 plugin_register_init ("ipmi", c_ipmi_init);
716 plugin_register_read ("ipmi", c_ipmi_read);
717 plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
718 } /* void module_register */
720 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */