1 /**
2 * collectd - src/ipmi.c
3 * Copyright (C) 2008 Florian octo Forster
4 * Copyright (C) 2008 Peter Holik
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 * Peter Holik <peter at holik.at>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "utils_ignorelist.h"
29 #include <pthread.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 raw_value,
115 double value,
116 ipmi_states_t *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, time(NULL), "", "", "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
152 {
153 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
154 "because it failed with status %#x.",
155 list_item->sensor_name, err);
156 sensor_list_remove (sensor);
157 }
158 return;
159 }
160 else if (list_item->sensor_not_present == 1)
161 {
162 list_item->sensor_not_present = 0;
164 INFO ("ipmi plugin: sensor_read_handler: sensor %s present.",
165 list_item->sensor_name);
167 if (c_ipmi_nofiy_notpresent)
168 {
169 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "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),
177 "sensor %s present", list_item->sensor_name);
179 plugin_dispatch_notification (&n);
180 }
181 }
183 if (value_present != IPMI_BOTH_VALUES_PRESENT)
184 {
185 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
186 "because it provides %s. If you need this sensor, "
187 "please file a bug report.",
188 list_item->sensor_name,
189 (value_present == IPMI_RAW_VALUE_PRESENT)
190 ? "only the raw value"
191 : "no value");
192 sensor_list_remove (sensor);
193 return;
194 }
196 values[0].gauge = value;
198 vl.values = values;
199 vl.values_len = 1;
200 vl.time = time (NULL);
202 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
203 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
204 sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
205 sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
207 plugin_dispatch_values (&vl);
208 } /* void sensor_read_handler */
210 static int sensor_list_add (ipmi_sensor_t *sensor)
211 {
212 ipmi_sensor_id_t sensor_id;
213 c_ipmi_sensor_list_t *list_item;
214 c_ipmi_sensor_list_t *list_prev;
216 char buffer[DATA_MAX_NAME_LEN];
217 const char *entity_id_string;
218 char sensor_name[DATA_MAX_NAME_LEN];
219 char *sensor_name_ptr;
220 int sensor_type;
221 const char *type;
222 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
224 sensor_id = ipmi_sensor_convert_to_id (sensor);
226 memset (buffer, 0, sizeof (buffer));
227 ipmi_sensor_get_name (sensor, buffer, sizeof (buffer));
228 buffer[sizeof (buffer) - 1] = 0;
230 entity_id_string = ipmi_entity_get_entity_id_string (ent);
232 if (entity_id_string == NULL)
233 sstrncpy (sensor_name, buffer, sizeof (sensor_name));
234 else
235 ssnprintf (sensor_name, sizeof (sensor_name),
236 "%s %s", buffer, entity_id_string);
238 sstrncpy (buffer, sensor_name, sizeof (buffer));
239 sensor_name_ptr = strstr (buffer, ").");
240 if (sensor_name_ptr != NULL)
241 {
242 /* If name is something like "foo (123).bar",
243 * change that to "bar (123)".
244 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
245 * `buffer' array, which holds a copy of the current `sensor_name'. */
246 char *sensor_id_ptr;
248 /* `sensor_name_ptr' points to ").bar". */
249 sensor_name_ptr[1] = 0;
250 /* `buffer' holds "foo (123)\0bar\0". */
251 sensor_name_ptr += 2;
252 /* `sensor_name_ptr' now points to "bar". */
254 sensor_id_ptr = strstr (buffer, "(");
255 if (sensor_id_ptr != NULL)
256 {
257 /* `sensor_id_ptr' now points to "(123)". */
258 ssnprintf (sensor_name, sizeof (sensor_name),
259 "%s %s", sensor_name_ptr, sensor_id_ptr);
260 }
261 /* else: don't touch sensor_name. */
262 }
263 sensor_name_ptr = sensor_name;
265 /* Both `ignorelist' and `plugin_instance' may be NULL. */
266 if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
267 return (0);
269 /* FIXME: Use rate unit or base unit to scale the value */
271 sensor_type = ipmi_sensor_get_sensor_type (sensor);
272 switch (sensor_type)
273 {
274 case IPMI_SENSOR_TYPE_TEMPERATURE:
275 type = "temperature";
276 break;
278 case IPMI_SENSOR_TYPE_VOLTAGE:
279 type = "voltage";
280 break;
282 case IPMI_SENSOR_TYPE_CURRENT:
283 type = "current";
284 break;
286 case IPMI_SENSOR_TYPE_FAN:
287 type = "fanspeed";
288 break;
290 default:
291 {
292 const char *sensor_type_str;
294 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
295 INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
296 "because I don't know how to handle its type (%#x, %s). "
297 "If you need this sensor, please file a bug report.",
298 sensor_name_ptr, sensor_type, sensor_type_str);
299 return (-1);
300 }
301 } /* switch (sensor_type) */
303 pthread_mutex_lock (&sensor_list_lock);
305 list_prev = NULL;
306 for (list_item = sensor_list;
307 list_item != NULL;
308 list_item = list_item->next)
309 {
310 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
311 break;
312 list_prev = list_item;
313 } /* for (list_item) */
315 if (list_item != NULL)
316 {
317 pthread_mutex_unlock (&sensor_list_lock);
318 return (0);
319 }
321 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
322 if (list_item == NULL)
323 {
324 pthread_mutex_unlock (&sensor_list_lock);
325 return (-1);
326 }
328 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
330 if (list_prev != NULL)
331 list_prev->next = list_item;
332 else
333 sensor_list = list_item;
335 sstrncpy (list_item->sensor_name, sensor_name_ptr,
336 sizeof (list_item->sensor_name));
337 sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
339 pthread_mutex_unlock (&sensor_list_lock);
341 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
342 {
343 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
344 "", "", "", NULL };
346 sstrncpy (n.host, hostname_g, sizeof (n.host));
347 sstrncpy (n.type_instance, list_item->sensor_name,
348 sizeof (n.type_instance));
349 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
350 ssnprintf (n.message, sizeof (n.message),
351 "sensor %s added", list_item->sensor_name);
353 plugin_dispatch_notification (&n);
354 }
356 return (0);
357 } /* int sensor_list_add */
359 static int sensor_list_remove (ipmi_sensor_t *sensor)
360 {
361 ipmi_sensor_id_t sensor_id;
362 c_ipmi_sensor_list_t *list_item;
363 c_ipmi_sensor_list_t *list_prev;
365 sensor_id = ipmi_sensor_convert_to_id (sensor);
367 pthread_mutex_lock (&sensor_list_lock);
369 list_prev = NULL;
370 for (list_item = sensor_list;
371 list_item != NULL;
372 list_item = list_item->next)
373 {
374 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
375 break;
376 list_prev = list_item;
377 } /* for (list_item) */
379 if (list_item == NULL)
380 {
381 pthread_mutex_unlock (&sensor_list_lock);
382 return (-1);
383 }
385 if (list_prev == NULL)
386 sensor_list = list_item->next;
387 else
388 list_prev->next = list_item->next;
390 list_prev = NULL;
391 list_item->next = NULL;
393 pthread_mutex_unlock (&sensor_list_lock);
395 if (c_ipmi_nofiy_remove && c_ipmi_active)
396 {
397 notification_t n = { NOTIF_WARNING, time(NULL), "", "",
398 "ipmi", "", "", "", NULL };
400 sstrncpy (n.host, hostname_g, sizeof (n.host));
401 sstrncpy (n.type_instance, list_item->sensor_name,
402 sizeof (n.type_instance));
403 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
404 ssnprintf (n.message, sizeof (n.message),
405 "sensor %s removed", list_item->sensor_name);
407 plugin_dispatch_notification (&n);
408 }
410 free (list_item);
411 return (0);
412 } /* int sensor_list_remove */
414 static int sensor_list_read_all (void)
415 {
416 c_ipmi_sensor_list_t *list_item;
418 pthread_mutex_lock (&sensor_list_lock);
420 for (list_item = sensor_list;
421 list_item != NULL;
422 list_item = list_item->next)
423 {
424 ipmi_sensor_id_get_reading (list_item->sensor_id,
425 sensor_read_handler, /* user data = */ list_item);
426 } /* for (list_item) */
428 pthread_mutex_unlock (&sensor_list_lock);
430 return (0);
431 } /* int sensor_list_read_all */
433 static int sensor_list_remove_all (void)
434 {
435 c_ipmi_sensor_list_t *list_item;
437 pthread_mutex_lock (&sensor_list_lock);
439 list_item = sensor_list;
440 sensor_list = NULL;
442 pthread_mutex_unlock (&sensor_list_lock);
444 while (list_item != NULL)
445 {
446 c_ipmi_sensor_list_t *list_next = list_item->next;
448 free (list_item);
450 list_item = list_next;
451 } /* while (list_item) */
453 return (0);
454 } /* int sensor_list_remove_all */
456 /*
457 * Entity handlers
458 */
459 static void entity_sensor_update_handler (enum ipmi_update_e op,
460 ipmi_entity_t *entity,
461 ipmi_sensor_t *sensor,
462 void *user_data)
463 {
464 /* TODO: Ignore sensors we cannot read */
466 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
467 {
468 /* Will check for duplicate entries.. */
469 sensor_list_add (sensor);
470 }
471 else if (op == IPMI_DELETED)
472 {
473 sensor_list_remove (sensor);
474 }
475 } /* void entity_sensor_update_handler */
477 /*
478 * Domain handlers
479 */
480 static void domain_entity_update_handler (enum ipmi_update_e op,
481 ipmi_domain_t *domain,
482 ipmi_entity_t *entity,
483 void *user_data)
484 {
485 int status;
487 if (op == IPMI_ADDED)
488 {
489 status = ipmi_entity_add_sensor_update_handler (entity,
490 entity_sensor_update_handler, /* user data = */ NULL);
491 if (status != 0)
492 {
493 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
494 }
495 }
496 else if (op == IPMI_DELETED)
497 {
498 status = ipmi_entity_remove_sensor_update_handler (entity,
499 entity_sensor_update_handler, /* user data = */ NULL);
500 if (status != 0)
501 {
502 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
503 }
504 }
505 } /* void domain_entity_update_handler */
507 static void domain_connection_change_handler (ipmi_domain_t *domain,
508 int err,
509 unsigned int conn_num,
510 unsigned int port_num,
511 int still_connected,
512 void *user_data)
513 {
514 int status;
516 DEBUG ("domain_connection_change_handler (domain = %p, err = %i, "
517 "conn_num = %u, port_num = %u, still_connected = %i, "
518 "user_data = %p);\n",
519 (void *) domain, err, conn_num, port_num, still_connected, user_data);
521 status = ipmi_domain_add_entity_update_handler (domain,
522 domain_entity_update_handler, /* user data = */ NULL);
523 if (status != 0)
524 {
525 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
526 }
527 } /* void domain_connection_change_handler */
529 static int thread_init (os_handler_t **ret_os_handler)
530 {
531 os_handler_t *os_handler;
532 ipmi_open_option_t open_option[1];
533 ipmi_con_t *smi_connection = NULL;
534 ipmi_domain_id_t domain_id;
535 int status;
537 os_handler = ipmi_posix_thread_setup_os_handler (SIGUSR2);
538 if (os_handler == NULL)
539 {
540 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
541 return (-1);
542 }
544 ipmi_init (os_handler);
546 status = ipmi_smi_setup_con (/* if_num = */ 0,
547 os_handler,
548 /* user data = */ NULL,
549 &smi_connection);
550 if (status != 0)
551 {
552 c_ipmi_error ("ipmi_smi_setup_con", status);
553 return (-1);
554 }
556 memset (open_option, 0, sizeof (open_option));
557 open_option[0].option = IPMI_OPEN_OPTION_ALL;
558 open_option[0].ival = 1;
560 status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
561 domain_connection_change_handler, /* user data = */ NULL,
562 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
563 open_option, sizeof (open_option) / sizeof (open_option[0]),
564 &domain_id);
565 if (status != 0)
566 {
567 c_ipmi_error ("ipmi_open_domain", status);
568 return (-1);
569 }
571 *ret_os_handler = os_handler;
572 return (0);
573 } /* int thread_init */
575 static void *thread_main (void *user_data)
576 {
577 int status;
578 os_handler_t *os_handler = NULL;
580 status = thread_init (&os_handler);
581 if (status != 0)
582 {
583 ERROR ("ipmi plugin: thread_init failed.\n");
584 return ((void *) -1);
585 }
587 while (c_ipmi_active != 0)
588 {
589 struct timeval tv = { 1, 0 };
590 os_handler->perform_one_op (os_handler, &tv);
591 }
593 ipmi_posix_thread_free_os_handler (os_handler);
595 return ((void *) 0);
596 } /* void *thread_main */
598 static int c_ipmi_config (const char *key, const char *value)
599 {
600 if (ignorelist == NULL)
601 ignorelist = ignorelist_create (/* invert = */ 1);
602 if (ignorelist == NULL)
603 return (1);
605 if (strcasecmp ("Sensor", key) == 0)
606 {
607 ignorelist_add (ignorelist, value);
608 }
609 else if (strcasecmp ("IgnoreSelected", key) == 0)
610 {
611 int invert = 1;
612 if ((strcasecmp ("True", value) == 0)
613 || (strcasecmp ("Yes", value) == 0)
614 || (strcasecmp ("On", value) == 0))
615 invert = 0;
616 ignorelist_set_invert (ignorelist, invert);
617 }
618 else if (strcasecmp ("NotifySensorAdd", key) == 0)
619 {
620 if ((strcasecmp ("True", value) == 0)
621 || (strcasecmp ("Yes", value) == 0)
622 || (strcasecmp ("On", value) == 0))
623 c_ipmi_nofiy_add = 1;
624 }
625 else if (strcasecmp ("NotifySensorRemove", key) == 0)
626 {
627 if ((strcasecmp ("True", value) == 0)
628 || (strcasecmp ("Yes", value) == 0)
629 || (strcasecmp ("On", value) == 0))
630 c_ipmi_nofiy_remove = 1;
631 }
632 else if (strcasecmp ("NotifySensorNotPresent", key) == 0)
633 {
634 if ((strcasecmp ("True", value) == 0)
635 || (strcasecmp ("Yes", value) == 0)
636 || (strcasecmp ("On", value) == 0))
637 c_ipmi_nofiy_notpresent = 1;
638 }
639 else
640 {
641 return (-1);
642 }
644 return (0);
645 } /* int c_ipmi_config */
647 static int c_ipmi_init (void)
648 {
649 int status;
651 /* Don't send `ADD' notifications during startup (~ 1 minute) */
652 c_ipmi_init_in_progress = 1 + (60 / interval_g);
654 c_ipmi_active = 1;
656 status = pthread_create (&thread_id, /* attr = */ NULL, thread_main,
657 /* user data = */ NULL);
658 if (status != 0)
659 {
660 c_ipmi_active = 0;
661 thread_id = (pthread_t) 0;
662 ERROR ("ipmi plugin: pthread_create failed.");
663 return (-1);
664 }
666 return (0);
667 } /* int c_ipmi_init */
669 static int c_ipmi_read (void)
670 {
671 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
672 {
673 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
674 return (-1);
675 }
677 sensor_list_read_all ();
679 if (c_ipmi_init_in_progress > 0)
680 c_ipmi_init_in_progress--;
681 else
682 c_ipmi_init_in_progress = 0;
684 return (0);
685 } /* int c_ipmi_read */
687 static int c_ipmi_shutdown (void)
688 {
689 c_ipmi_active = 0;
691 if (thread_id != (pthread_t) 0)
692 {
693 pthread_join (thread_id, NULL);
694 thread_id = (pthread_t) 0;
695 }
697 sensor_list_remove_all ();
699 return (0);
700 } /* int c_ipmi_shutdown */
702 void module_register (void)
703 {
704 plugin_register_config ("ipmi", c_ipmi_config,
705 config_keys, config_keys_num);
706 plugin_register_init ("ipmi", c_ipmi_init);
707 plugin_register_read ("ipmi", c_ipmi_read);
708 plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
709 } /* void module_register */
711 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */