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
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.
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.
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
21 * Florian octo Forster <octo at collectd.org>
22 * Peter Holik <peter at holik.at>
23 * Bruno Prémont <bonbons at linux-vserver.org>
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>
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
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;
53 * Module global variables
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[] =
68 "NotifySensorNotPresent"
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;
79 * Misc private functions
81 static void c_ipmi_error (const char *func, int status)
83 char errbuf[4096] = { 0 };
85 if (IPMI_IS_OS_ERR (status))
87 sstrerror (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
89 else if (IPMI_IS_IPMI_ERR (status))
91 ipmi_get_error_string (IPMI_GET_IPMI_ERR (status), errbuf, sizeof (errbuf));
96 ssnprintf (errbuf, sizeof (errbuf), "Unknown error %#x", status);
98 errbuf[sizeof (errbuf) - 1] = 0;
100 ERROR ("ipmi plugin: %s failed: %s", func, errbuf);
101 } /* void c_ipmi_error */
106 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
107 static int sensor_list_remove (ipmi_sensor_t *sensor);
109 static void sensor_read_handler (ipmi_sensor_t *sensor,
111 enum ipmi_value_present_e value_present,
112 unsigned int __attribute__((unused)) raw_value,
114 ipmi_states_t __attribute__((unused)) *states,
118 value_list_t vl = VALUE_LIST_INIT;
120 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
124 if ((err & 0xff) == IPMI_NOT_PRESENT_CC)
126 if (list_item->sensor_not_present == 0)
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)
135 notification_t n = { NOTIF_WARNING, cdtime (), "", "", "ipmi",
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);
149 else if (IPMI_IS_IPMI_ERR(err) && IPMI_GET_IPMI_ERR(err) == IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC)
151 INFO ("ipmi plugin: sensor_read_handler: Sensor %s not ready",
152 list_item->sensor_name);
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));
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);
180 else if (list_item->sensor_not_present == 1)
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)
189 notification_t n = { NOTIF_OKAY, cdtime (), "", "", "ipmi",
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);
203 if (value_present != IPMI_BOTH_VALUES_PRESENT)
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"
212 sensor_list_remove (sensor);
216 values[0].gauge = value;
221 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
222 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
223 sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
224 sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
226 plugin_dispatch_values (&vl);
227 } /* void sensor_read_handler */
229 static int sensor_list_add (ipmi_sensor_t *sensor)
231 ipmi_sensor_id_t sensor_id;
232 c_ipmi_sensor_list_t *list_item;
233 c_ipmi_sensor_list_t *list_prev;
235 char buffer[DATA_MAX_NAME_LEN] = { 0 };
236 const char *entity_id_string;
237 char sensor_name[DATA_MAX_NAME_LEN];
238 char *sensor_name_ptr;
241 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
243 sensor_id = ipmi_sensor_convert_to_id (sensor);
245 ipmi_sensor_get_name (sensor, buffer, sizeof (buffer));
246 buffer[sizeof (buffer) - 1] = 0;
248 entity_id_string = ipmi_entity_get_entity_id_string (ent);
250 if (entity_id_string == NULL)
251 sstrncpy (sensor_name, buffer, sizeof (sensor_name));
253 ssnprintf (sensor_name, sizeof (sensor_name),
254 "%s %s", buffer, entity_id_string);
256 sstrncpy (buffer, sensor_name, sizeof (buffer));
257 sensor_name_ptr = strstr (buffer, ").");
258 if (sensor_name_ptr != NULL)
260 /* If name is something like "foo (123).bar",
261 * change that to "bar (123)".
262 * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
263 * `buffer' array, which holds a copy of the current `sensor_name'. */
266 /* `sensor_name_ptr' points to ").bar". */
267 sensor_name_ptr[1] = 0;
268 /* `buffer' holds "foo (123)\0bar\0". */
269 sensor_name_ptr += 2;
270 /* `sensor_name_ptr' now points to "bar". */
272 sensor_id_ptr = strstr (buffer, "(");
273 if (sensor_id_ptr != NULL)
275 /* `sensor_id_ptr' now points to "(123)". */
276 ssnprintf (sensor_name, sizeof (sensor_name),
277 "%s %s", sensor_name_ptr, sensor_id_ptr);
279 /* else: don't touch sensor_name. */
281 sensor_name_ptr = sensor_name;
283 /* Both `ignorelist' and `plugin_instance' may be NULL. */
284 if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
287 /* FIXME: Use rate unit or base unit to scale the value */
289 sensor_type = ipmi_sensor_get_sensor_type (sensor);
292 case IPMI_SENSOR_TYPE_TEMPERATURE:
293 type = "temperature";
296 case IPMI_SENSOR_TYPE_VOLTAGE:
300 case IPMI_SENSOR_TYPE_CURRENT:
304 case IPMI_SENSOR_TYPE_FAN:
310 const char *sensor_type_str;
312 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
313 INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
314 "because I don't know how to handle its type (%#x, %s). "
315 "If you need this sensor, please file a bug report.",
316 sensor_name_ptr, sensor_type, sensor_type_str);
319 } /* switch (sensor_type) */
321 pthread_mutex_lock (&sensor_list_lock);
324 for (list_item = sensor_list;
326 list_item = list_item->next)
328 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
330 list_prev = list_item;
331 } /* for (list_item) */
333 if (list_item != NULL)
335 pthread_mutex_unlock (&sensor_list_lock);
339 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
340 if (list_item == NULL)
342 pthread_mutex_unlock (&sensor_list_lock);
346 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
348 if (list_prev != NULL)
349 list_prev->next = list_item;
351 sensor_list = list_item;
353 sstrncpy (list_item->sensor_name, sensor_name_ptr,
354 sizeof (list_item->sensor_name));
355 sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
357 pthread_mutex_unlock (&sensor_list_lock);
359 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
361 notification_t n = { NOTIF_OKAY, cdtime (), "", "", "ipmi",
364 sstrncpy (n.host, hostname_g, sizeof (n.host));
365 sstrncpy (n.type_instance, list_item->sensor_name,
366 sizeof (n.type_instance));
367 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
368 ssnprintf (n.message, sizeof (n.message),
369 "sensor %s added", list_item->sensor_name);
371 plugin_dispatch_notification (&n);
375 } /* int sensor_list_add */
377 static int sensor_list_remove (ipmi_sensor_t *sensor)
379 ipmi_sensor_id_t sensor_id;
380 c_ipmi_sensor_list_t *list_item;
381 c_ipmi_sensor_list_t *list_prev;
383 sensor_id = ipmi_sensor_convert_to_id (sensor);
385 pthread_mutex_lock (&sensor_list_lock);
388 for (list_item = sensor_list;
390 list_item = list_item->next)
392 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
394 list_prev = list_item;
395 } /* for (list_item) */
397 if (list_item == NULL)
399 pthread_mutex_unlock (&sensor_list_lock);
403 if (list_prev == NULL)
404 sensor_list = list_item->next;
406 list_prev->next = list_item->next;
409 list_item->next = NULL;
411 pthread_mutex_unlock (&sensor_list_lock);
413 if (c_ipmi_nofiy_remove && c_ipmi_active)
415 notification_t n = { NOTIF_WARNING, cdtime (), "", "",
416 "ipmi", "", "", "", NULL };
418 sstrncpy (n.host, hostname_g, sizeof (n.host));
419 sstrncpy (n.type_instance, list_item->sensor_name,
420 sizeof (n.type_instance));
421 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
422 ssnprintf (n.message, sizeof (n.message),
423 "sensor %s removed", list_item->sensor_name);
425 plugin_dispatch_notification (&n);
430 } /* int sensor_list_remove */
432 static int sensor_list_read_all (void)
434 c_ipmi_sensor_list_t *list_item;
436 pthread_mutex_lock (&sensor_list_lock);
438 for (list_item = sensor_list;
440 list_item = list_item->next)
442 ipmi_sensor_id_get_reading (list_item->sensor_id,
443 sensor_read_handler, /* user data = */ list_item);
444 } /* for (list_item) */
446 pthread_mutex_unlock (&sensor_list_lock);
449 } /* int sensor_list_read_all */
451 static int sensor_list_remove_all (void)
453 c_ipmi_sensor_list_t *list_item;
455 pthread_mutex_lock (&sensor_list_lock);
457 list_item = sensor_list;
460 pthread_mutex_unlock (&sensor_list_lock);
462 while (list_item != NULL)
464 c_ipmi_sensor_list_t *list_next = list_item->next;
468 list_item = list_next;
469 } /* while (list_item) */
472 } /* int sensor_list_remove_all */
477 static void entity_sensor_update_handler (enum ipmi_update_e op,
478 ipmi_entity_t __attribute__((unused)) *entity,
479 ipmi_sensor_t *sensor,
480 void __attribute__((unused)) *user_data)
482 /* TODO: Ignore sensors we cannot read */
484 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
486 /* Will check for duplicate entries.. */
487 sensor_list_add (sensor);
489 else if (op == IPMI_DELETED)
491 sensor_list_remove (sensor);
493 } /* void entity_sensor_update_handler */
498 static void domain_entity_update_handler (enum ipmi_update_e op,
499 ipmi_domain_t __attribute__((unused)) *domain,
500 ipmi_entity_t *entity,
501 void __attribute__((unused)) *user_data)
505 if (op == IPMI_ADDED)
507 status = ipmi_entity_add_sensor_update_handler (entity,
508 entity_sensor_update_handler, /* user data = */ NULL);
511 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
514 else if (op == IPMI_DELETED)
516 status = ipmi_entity_remove_sensor_update_handler (entity,
517 entity_sensor_update_handler, /* user data = */ NULL);
520 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
523 } /* void domain_entity_update_handler */
525 static void domain_connection_change_handler (ipmi_domain_t *domain,
527 unsigned int conn_num,
528 unsigned int port_num,
534 DEBUG ("domain_connection_change_handler (domain = %p, err = %i, "
535 "conn_num = %u, port_num = %u, still_connected = %i, "
536 "user_data = %p);\n",
537 (void *) domain, err, conn_num, port_num, still_connected, user_data);
539 status = ipmi_domain_add_entity_update_handler (domain,
540 domain_entity_update_handler, /* user data = */ NULL);
543 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
545 } /* void domain_connection_change_handler */
547 static int thread_init (os_handler_t **ret_os_handler)
549 os_handler_t *os_handler;
550 ipmi_open_option_t open_option[1];
551 ipmi_con_t *smi_connection = NULL;
552 ipmi_domain_id_t domain_id;
555 os_handler = ipmi_posix_thread_setup_os_handler (SIGIO);
556 if (os_handler == NULL)
558 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
562 ipmi_init (os_handler);
564 status = ipmi_smi_setup_con (/* if_num = */ 0,
566 /* user data = */ NULL,
570 c_ipmi_error ("ipmi_smi_setup_con", status);
574 ipmi_open_option_t open_option[1] = {
576 .option = IPMI_OPEN_OPTION_ALL,
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]),
588 c_ipmi_error ("ipmi_open_domain", status);
592 *ret_os_handler = os_handler;
594 } /* int thread_init */
596 static void *thread_main (void __attribute__((unused)) *user_data)
599 os_handler_t *os_handler = NULL;
601 status = thread_init (&os_handler);
604 ERROR ("ipmi plugin: thread_init failed.\n");
605 return ((void *) -1);
608 while (c_ipmi_active != 0)
610 struct timeval tv = { 1, 0 };
611 os_handler->perform_one_op (os_handler, &tv);
614 ipmi_posix_thread_free_os_handler (os_handler);
617 } /* void *thread_main */
619 static int c_ipmi_config (const char *key, const char *value)
621 if (ignorelist == NULL)
622 ignorelist = ignorelist_create (/* invert = */ 1);
623 if (ignorelist == NULL)
626 if (strcasecmp ("Sensor", key) == 0)
628 ignorelist_add (ignorelist, value);
630 else if (strcasecmp ("IgnoreSelected", key) == 0)
635 ignorelist_set_invert (ignorelist, invert);
637 else if (strcasecmp ("NotifySensorAdd", key) == 0)
640 c_ipmi_nofiy_add = 1;
642 else if (strcasecmp ("NotifySensorRemove", key) == 0)
645 c_ipmi_nofiy_remove = 1;
647 else if (strcasecmp ("NotifySensorNotPresent", key) == 0)
650 c_ipmi_nofiy_notpresent = 1;
658 } /* int c_ipmi_config */
660 static int c_ipmi_init (void)
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);
670 status = plugin_thread_create (&thread_id, /* attr = */ NULL, thread_main,
671 /* user data = */ NULL);
675 thread_id = (pthread_t) 0;
676 ERROR ("ipmi plugin: pthread_create failed.");
681 } /* int c_ipmi_init */
683 static int c_ipmi_read (void)
685 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
687 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
691 sensor_list_read_all ();
693 if (c_ipmi_init_in_progress > 0)
694 c_ipmi_init_in_progress--;
696 c_ipmi_init_in_progress = 0;
699 } /* int c_ipmi_read */
701 static int c_ipmi_shutdown (void)
705 if (thread_id != (pthread_t) 0)
707 pthread_join (thread_id, NULL);
708 thread_id = (pthread_t) 0;
711 sensor_list_remove_all ();
714 } /* int c_ipmi_shutdown */
716 void module_register (void)
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 : */