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 raw_value,
117 double value,
118 ipmi_states_t *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
154 {
155 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
156 "because it failed with status %#x.",
157 list_item->sensor_name, err);
158 sensor_list_remove (sensor);
159 }
160 return;
161 }
162 else if (list_item->sensor_not_present == 1)
163 {
164 list_item->sensor_not_present = 0;
166 INFO ("ipmi plugin: sensor_read_handler: sensor %s present.",
167 list_item->sensor_name);
169 if (c_ipmi_nofiy_notpresent)
170 {
171 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
172 "", "", "", NULL };
174 sstrncpy (n.host, hostname_g, sizeof (n.host));
175 sstrncpy (n.type_instance, list_item->sensor_name,
176 sizeof (n.type_instance));
177 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
178 ssnprintf (n.message, sizeof (n.message),
179 "sensor %s present", list_item->sensor_name);
181 plugin_dispatch_notification (&n);
182 }
183 }
185 if (value_present != IPMI_BOTH_VALUES_PRESENT)
186 {
187 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
188 "because it provides %s. If you need this sensor, "
189 "please file a bug report.",
190 list_item->sensor_name,
191 (value_present == IPMI_RAW_VALUE_PRESENT)
192 ? "only the raw value"
193 : "no value");
194 sensor_list_remove (sensor);
195 return;
196 }
198 values[0].gauge = value;
200 vl.values = values;
201 vl.values_len = 1;
202 vl.time = time (NULL);
204 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
205 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
206 sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
207 sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
209 plugin_dispatch_values (&vl);
210 } /* void sensor_read_handler */
212 static int sensor_list_add (ipmi_sensor_t *sensor)
213 {
214 ipmi_sensor_id_t sensor_id;
215 c_ipmi_sensor_list_t *list_item;
216 c_ipmi_sensor_list_t *list_prev;
218 char buffer[DATA_MAX_NAME_LEN];
219 const char *entity_id_string;
220 char sensor_name[DATA_MAX_NAME_LEN];
221 char *sensor_name_ptr;
222 int sensor_type;
223 const char *type;
224 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
226 sensor_id = ipmi_sensor_convert_to_id (sensor);
228 memset (buffer, 0, sizeof (buffer));
229 ipmi_sensor_get_name (sensor, buffer, sizeof (buffer));
230 buffer[sizeof (buffer) - 1] = 0;
232 entity_id_string = ipmi_entity_get_entity_id_string (ent);
234 if (entity_id_string == NULL)
235 sstrncpy (sensor_name, buffer, sizeof (sensor_name));
236 else
237 ssnprintf (sensor_name, sizeof (sensor_name),
238 "%s %s", buffer, entity_id_string);
240 sstrncpy (buffer, sensor_name, sizeof (buffer));
241 sensor_name_ptr = strstr (buffer, ").");
242 if (sensor_name_ptr != NULL)
243 {
244 /* If name is something like "foo (123).bar",
245 * change that to "bar (123)".
246 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
247 * `buffer' array, which holds a copy of the current `sensor_name'. */
248 char *sensor_id_ptr;
250 /* `sensor_name_ptr' points to ").bar". */
251 sensor_name_ptr[1] = 0;
252 /* `buffer' holds "foo (123)\0bar\0". */
253 sensor_name_ptr += 2;
254 /* `sensor_name_ptr' now points to "bar". */
256 sensor_id_ptr = strstr (buffer, "(");
257 if (sensor_id_ptr != NULL)
258 {
259 /* `sensor_id_ptr' now points to "(123)". */
260 ssnprintf (sensor_name, sizeof (sensor_name),
261 "%s %s", sensor_name_ptr, sensor_id_ptr);
262 }
263 /* else: don't touch sensor_name. */
264 }
265 sensor_name_ptr = sensor_name;
267 /* Both `ignorelist' and `plugin_instance' may be NULL. */
268 if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
269 return (0);
271 /* FIXME: Use rate unit or base unit to scale the value */
273 sensor_type = ipmi_sensor_get_sensor_type (sensor);
274 switch (sensor_type)
275 {
276 case IPMI_SENSOR_TYPE_TEMPERATURE:
277 type = "temperature";
278 break;
280 case IPMI_SENSOR_TYPE_VOLTAGE:
281 type = "voltage";
282 break;
284 case IPMI_SENSOR_TYPE_CURRENT:
285 type = "current";
286 break;
288 case IPMI_SENSOR_TYPE_FAN:
289 type = "fanspeed";
290 break;
292 default:
293 {
294 const char *sensor_type_str;
296 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
297 INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
298 "because I don't know how to handle its type (%#x, %s). "
299 "If you need this sensor, please file a bug report.",
300 sensor_name_ptr, sensor_type, sensor_type_str);
301 return (-1);
302 }
303 } /* switch (sensor_type) */
305 pthread_mutex_lock (&sensor_list_lock);
307 list_prev = NULL;
308 for (list_item = sensor_list;
309 list_item != NULL;
310 list_item = list_item->next)
311 {
312 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
313 break;
314 list_prev = list_item;
315 } /* for (list_item) */
317 if (list_item != NULL)
318 {
319 pthread_mutex_unlock (&sensor_list_lock);
320 return (0);
321 }
323 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
324 if (list_item == NULL)
325 {
326 pthread_mutex_unlock (&sensor_list_lock);
327 return (-1);
328 }
330 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
332 if (list_prev != NULL)
333 list_prev->next = list_item;
334 else
335 sensor_list = list_item;
337 sstrncpy (list_item->sensor_name, sensor_name_ptr,
338 sizeof (list_item->sensor_name));
339 sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
341 pthread_mutex_unlock (&sensor_list_lock);
343 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
344 {
345 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
346 "", "", "", NULL };
348 sstrncpy (n.host, hostname_g, sizeof (n.host));
349 sstrncpy (n.type_instance, list_item->sensor_name,
350 sizeof (n.type_instance));
351 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
352 ssnprintf (n.message, sizeof (n.message),
353 "sensor %s added", list_item->sensor_name);
355 plugin_dispatch_notification (&n);
356 }
358 return (0);
359 } /* int sensor_list_add */
361 static int sensor_list_remove (ipmi_sensor_t *sensor)
362 {
363 ipmi_sensor_id_t sensor_id;
364 c_ipmi_sensor_list_t *list_item;
365 c_ipmi_sensor_list_t *list_prev;
367 sensor_id = ipmi_sensor_convert_to_id (sensor);
369 pthread_mutex_lock (&sensor_list_lock);
371 list_prev = NULL;
372 for (list_item = sensor_list;
373 list_item != NULL;
374 list_item = list_item->next)
375 {
376 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
377 break;
378 list_prev = list_item;
379 } /* for (list_item) */
381 if (list_item == NULL)
382 {
383 pthread_mutex_unlock (&sensor_list_lock);
384 return (-1);
385 }
387 if (list_prev == NULL)
388 sensor_list = list_item->next;
389 else
390 list_prev->next = list_item->next;
392 list_prev = NULL;
393 list_item->next = NULL;
395 pthread_mutex_unlock (&sensor_list_lock);
397 if (c_ipmi_nofiy_remove && c_ipmi_active)
398 {
399 notification_t n = { NOTIF_WARNING, time(NULL), "", "",
400 "ipmi", "", "", "", NULL };
402 sstrncpy (n.host, hostname_g, sizeof (n.host));
403 sstrncpy (n.type_instance, list_item->sensor_name,
404 sizeof (n.type_instance));
405 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
406 ssnprintf (n.message, sizeof (n.message),
407 "sensor %s removed", list_item->sensor_name);
409 plugin_dispatch_notification (&n);
410 }
412 free (list_item);
413 return (0);
414 } /* int sensor_list_remove */
416 static int sensor_list_read_all (void)
417 {
418 c_ipmi_sensor_list_t *list_item;
420 pthread_mutex_lock (&sensor_list_lock);
422 for (list_item = sensor_list;
423 list_item != NULL;
424 list_item = list_item->next)
425 {
426 ipmi_sensor_id_get_reading (list_item->sensor_id,
427 sensor_read_handler, /* user data = */ list_item);
428 } /* for (list_item) */
430 pthread_mutex_unlock (&sensor_list_lock);
432 return (0);
433 } /* int sensor_list_read_all */
435 static int sensor_list_remove_all (void)
436 {
437 c_ipmi_sensor_list_t *list_item;
439 pthread_mutex_lock (&sensor_list_lock);
441 list_item = sensor_list;
442 sensor_list = NULL;
444 pthread_mutex_unlock (&sensor_list_lock);
446 while (list_item != NULL)
447 {
448 c_ipmi_sensor_list_t *list_next = list_item->next;
450 free (list_item);
452 list_item = list_next;
453 } /* while (list_item) */
455 return (0);
456 } /* int sensor_list_remove_all */
458 /*
459 * Entity handlers
460 */
461 static void entity_sensor_update_handler (enum ipmi_update_e op,
462 ipmi_entity_t *entity,
463 ipmi_sensor_t *sensor,
464 void *user_data)
465 {
466 /* TODO: Ignore sensors we cannot read */
468 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
469 {
470 /* Will check for duplicate entries.. */
471 sensor_list_add (sensor);
472 }
473 else if (op == IPMI_DELETED)
474 {
475 sensor_list_remove (sensor);
476 }
477 } /* void entity_sensor_update_handler */
479 /*
480 * Domain handlers
481 */
482 static void domain_entity_update_handler (enum ipmi_update_e op,
483 ipmi_domain_t *domain,
484 ipmi_entity_t *entity,
485 void *user_data)
486 {
487 int status;
489 if (op == IPMI_ADDED)
490 {
491 status = ipmi_entity_add_sensor_update_handler (entity,
492 entity_sensor_update_handler, /* user data = */ NULL);
493 if (status != 0)
494 {
495 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
496 }
497 }
498 else if (op == IPMI_DELETED)
499 {
500 status = ipmi_entity_remove_sensor_update_handler (entity,
501 entity_sensor_update_handler, /* user data = */ NULL);
502 if (status != 0)
503 {
504 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
505 }
506 }
507 } /* void domain_entity_update_handler */
509 static void domain_connection_change_handler (ipmi_domain_t *domain,
510 int err,
511 unsigned int conn_num,
512 unsigned int port_num,
513 int still_connected,
514 void *user_data)
515 {
516 int status;
518 DEBUG ("domain_connection_change_handler (domain = %p, err = %i, "
519 "conn_num = %u, port_num = %u, still_connected = %i, "
520 "user_data = %p);\n",
521 (void *) domain, err, conn_num, port_num, still_connected, user_data);
523 status = ipmi_domain_add_entity_update_handler (domain,
524 domain_entity_update_handler, /* user data = */ NULL);
525 if (status != 0)
526 {
527 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
528 }
529 } /* void domain_connection_change_handler */
531 static int thread_init (os_handler_t **ret_os_handler)
532 {
533 os_handler_t *os_handler;
534 ipmi_open_option_t open_option[1];
535 ipmi_con_t *smi_connection = NULL;
536 ipmi_domain_id_t domain_id;
537 int status;
539 os_handler = ipmi_posix_thread_setup_os_handler (SIGUSR2);
540 if (os_handler == NULL)
541 {
542 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
543 return (-1);
544 }
546 ipmi_init (os_handler);
548 status = ipmi_smi_setup_con (/* if_num = */ 0,
549 os_handler,
550 /* user data = */ NULL,
551 &smi_connection);
552 if (status != 0)
553 {
554 c_ipmi_error ("ipmi_smi_setup_con", status);
555 return (-1);
556 }
558 memset (open_option, 0, sizeof (open_option));
559 open_option[0].option = IPMI_OPEN_OPTION_ALL;
560 open_option[0].ival = 1;
562 status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
563 domain_connection_change_handler, /* user data = */ NULL,
564 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
565 open_option, sizeof (open_option) / sizeof (open_option[0]),
566 &domain_id);
567 if (status != 0)
568 {
569 c_ipmi_error ("ipmi_open_domain", status);
570 return (-1);
571 }
573 *ret_os_handler = os_handler;
574 return (0);
575 } /* int thread_init */
577 static void *thread_main (void *user_data)
578 {
579 int status;
580 os_handler_t *os_handler = NULL;
582 status = thread_init (&os_handler);
583 if (status != 0)
584 {
585 ERROR ("ipmi plugin: thread_init failed.\n");
586 return ((void *) -1);
587 }
589 while (c_ipmi_active != 0)
590 {
591 struct timeval tv = { 1, 0 };
592 os_handler->perform_one_op (os_handler, &tv);
593 }
595 ipmi_posix_thread_free_os_handler (os_handler);
597 return ((void *) 0);
598 } /* void *thread_main */
600 static int c_ipmi_config (const char *key, const char *value)
601 {
602 if (ignorelist == NULL)
603 ignorelist = ignorelist_create (/* invert = */ 1);
604 if (ignorelist == NULL)
605 return (1);
607 if (strcasecmp ("Sensor", key) == 0)
608 {
609 ignorelist_add (ignorelist, value);
610 }
611 else if (strcasecmp ("IgnoreSelected", key) == 0)
612 {
613 int invert = 1;
614 if ((strcasecmp ("True", value) == 0)
615 || (strcasecmp ("Yes", value) == 0)
616 || (strcasecmp ("On", value) == 0))
617 invert = 0;
618 ignorelist_set_invert (ignorelist, invert);
619 }
620 else if (strcasecmp ("NotifySensorAdd", key) == 0)
621 {
622 if ((strcasecmp ("True", value) == 0)
623 || (strcasecmp ("Yes", value) == 0)
624 || (strcasecmp ("On", value) == 0))
625 c_ipmi_nofiy_add = 1;
626 }
627 else if (strcasecmp ("NotifySensorRemove", key) == 0)
628 {
629 if ((strcasecmp ("True", value) == 0)
630 || (strcasecmp ("Yes", value) == 0)
631 || (strcasecmp ("On", value) == 0))
632 c_ipmi_nofiy_remove = 1;
633 }
634 else if (strcasecmp ("NotifySensorNotPresent", key) == 0)
635 {
636 if ((strcasecmp ("True", value) == 0)
637 || (strcasecmp ("Yes", value) == 0)
638 || (strcasecmp ("On", value) == 0))
639 c_ipmi_nofiy_notpresent = 1;
640 }
641 else
642 {
643 return (-1);
644 }
646 return (0);
647 } /* int c_ipmi_config */
649 static int c_ipmi_init (void)
650 {
651 int status;
653 /* Don't send `ADD' notifications during startup (~ 1 minute) */
654 c_ipmi_init_in_progress = 1 + (60 / interval_g);
656 c_ipmi_active = 1;
658 status = pthread_create (&thread_id, /* attr = */ NULL, thread_main,
659 /* user data = */ NULL);
660 if (status != 0)
661 {
662 c_ipmi_active = 0;
663 thread_id = (pthread_t) 0;
664 ERROR ("ipmi plugin: pthread_create failed.");
665 return (-1);
666 }
668 return (0);
669 } /* int c_ipmi_init */
671 static int c_ipmi_read (void)
672 {
673 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
674 {
675 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
676 return (-1);
677 }
679 sensor_list_read_all ();
681 if (c_ipmi_init_in_progress > 0)
682 c_ipmi_init_in_progress--;
683 else
684 c_ipmi_init_in_progress = 0;
686 return (0);
687 } /* int c_ipmi_read */
689 static int c_ipmi_shutdown (void)
690 {
691 c_ipmi_active = 0;
693 if (thread_id != (pthread_t) 0)
694 {
695 pthread_join (thread_id, NULL);
696 thread_id = (pthread_t) 0;
697 }
699 sensor_list_remove_all ();
701 return (0);
702 } /* int c_ipmi_shutdown */
704 void module_register (void)
705 {
706 plugin_register_config ("ipmi", c_ipmi_config,
707 config_keys, config_keys_num);
708 plugin_register_init ("ipmi", c_ipmi_init);
709 plugin_register_read ("ipmi", c_ipmi_read);
710 plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
711 } /* void module_register */
713 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */