2 * collectd - src/ipmi.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_ignorelist.h"
29 #include <OpenIPMI/ipmiif.h>
30 #include <OpenIPMI/ipmi_err.h>
31 #include <OpenIPMI/ipmi_posix.h>
32 #include <OpenIPMI/ipmi_conn.h>
33 #include <OpenIPMI/ipmi_smi.h>
38 struct c_ipmi_sensor_list_s;
39 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
41 struct c_ipmi_sensor_list_s
43 ipmi_sensor_id_t sensor_id;
44 char sensor_name[DATA_MAX_NAME_LEN];
45 char sensor_type[DATA_MAX_NAME_LEN];
46 int sensor_not_present;
47 c_ipmi_sensor_list_t *next;
51 * Module global variables
53 static pthread_mutex_t sensor_list_lock = PTHREAD_MUTEX_INITIALIZER;
54 static c_ipmi_sensor_list_t *sensor_list = NULL;
56 static int c_ipmi_init_in_progress = 0;
57 static int c_ipmi_active = 0;
58 static pthread_t thread_id = (pthread_t) 0;
60 static const char *config_keys[] =
66 "NotifySensorNotPresent"
68 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
70 static ignorelist_t *ignorelist = NULL;
72 static int c_ipmi_nofiy_add = 0;
73 static int c_ipmi_nofiy_remove = 0;
74 static int c_ipmi_nofiy_notpresent = 0;
77 * Misc private functions
79 static void c_ipmi_error (const char *func, int status)
83 memset (errbuf, 0, sizeof (errbuf));
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 raw_value,
114 ipmi_states_t *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, time(NULL), "", "", "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);
151 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
152 "because it failed with status %#x.",
153 list_item->sensor_name, err);
154 sensor_list_remove (sensor);
158 else if (list_item->sensor_not_present == 1)
160 list_item->sensor_not_present = 0;
162 INFO ("ipmi plugin: sensor_read_handler: sensor %s present.",
163 list_item->sensor_name);
165 if (c_ipmi_nofiy_notpresent)
167 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
170 sstrncpy (n.host, hostname_g, sizeof (n.host));
171 sstrncpy (n.type_instance, list_item->sensor_name,
172 sizeof (n.type_instance));
173 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
174 ssnprintf (n.message, sizeof (n.message),
175 "sensor %s present", list_item->sensor_name);
177 plugin_dispatch_notification (&n);
181 if (value_present != IPMI_BOTH_VALUES_PRESENT)
183 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
184 "because it provides %s. If you need this sensor, "
185 "please file a bug report.",
186 list_item->sensor_name,
187 (value_present == IPMI_RAW_VALUE_PRESENT)
188 ? "only the raw value"
190 sensor_list_remove (sensor);
194 values[0].gauge = value;
198 vl.time = time (NULL);
200 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
201 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
202 sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
203 sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
205 plugin_dispatch_values (&vl);
206 } /* void sensor_read_handler */
208 static int sensor_list_add (ipmi_sensor_t *sensor)
210 ipmi_sensor_id_t sensor_id;
211 c_ipmi_sensor_list_t *list_item;
212 c_ipmi_sensor_list_t *list_prev;
214 char sensor_name[DATA_MAX_NAME_LEN];
215 char *sensor_name_ptr;
216 int sensor_type, len;
218 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
220 sensor_id = ipmi_sensor_convert_to_id (sensor);
222 memset (sensor_name, 0, sizeof (sensor_name));
223 ipmi_sensor_get_name (sensor, sensor_name, sizeof (sensor_name));
224 sensor_name[sizeof (sensor_name) - 1] = 0;
226 len = DATA_MAX_NAME_LEN - strlen(sensor_name);
227 strncat(sensor_name, " ", len--);
228 strncat(sensor_name, ipmi_entity_get_entity_id_string(ent), len);
230 sensor_name_ptr = strstr (sensor_name, ").");
231 if (sensor_name_ptr == NULL)
232 sensor_name_ptr = sensor_name;
235 char *sensor_name_ptr_id = strstr (sensor_name, "(");
237 sensor_name_ptr += 2;
238 len = DATA_MAX_NAME_LEN - strlen(sensor_name);
239 strncat(sensor_name, " ", len--);
240 strncat(sensor_name, sensor_name_ptr_id,
241 MIN(sensor_name_ptr - sensor_name_ptr_id - 1, len));
244 /* Both `ignorelist' and `plugin_instance' may be NULL. */
245 if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
248 /* FIXME: Use rate unit or base unit to scale the value */
250 sensor_type = ipmi_sensor_get_sensor_type (sensor);
253 case IPMI_SENSOR_TYPE_TEMPERATURE:
254 type = "temperature";
257 case IPMI_SENSOR_TYPE_VOLTAGE:
261 case IPMI_SENSOR_TYPE_CURRENT:
265 case IPMI_SENSOR_TYPE_FAN:
271 const char *sensor_type_str;
273 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
274 INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
275 "because I don't know how to handle its type (%#x, %s). "
276 "If you need this sensor, please file a bug report.",
277 sensor_name_ptr, sensor_type, sensor_type_str);
280 } /* switch (sensor_type) */
282 pthread_mutex_lock (&sensor_list_lock);
285 for (list_item = sensor_list;
287 list_item = list_item->next)
289 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
291 list_prev = list_item;
292 } /* for (list_item) */
294 if (list_item != NULL)
296 pthread_mutex_unlock (&sensor_list_lock);
300 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
301 if (list_item == NULL)
303 pthread_mutex_unlock (&sensor_list_lock);
307 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
309 if (list_prev != NULL)
310 list_prev->next = list_item;
312 sensor_list = list_item;
314 sstrncpy (list_item->sensor_name, sensor_name_ptr,
315 sizeof (list_item->sensor_name));
316 sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
318 pthread_mutex_unlock (&sensor_list_lock);
320 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
322 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
325 sstrncpy (n.host, hostname_g, sizeof (n.host));
326 sstrncpy (n.type_instance, list_item->sensor_name,
327 sizeof (n.type_instance));
328 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
329 ssnprintf (n.message, sizeof (n.message),
330 "sensor %s added", list_item->sensor_name);
332 plugin_dispatch_notification (&n);
336 } /* int sensor_list_add */
338 static int sensor_list_remove (ipmi_sensor_t *sensor)
340 ipmi_sensor_id_t sensor_id;
341 c_ipmi_sensor_list_t *list_item;
342 c_ipmi_sensor_list_t *list_prev;
344 sensor_id = ipmi_sensor_convert_to_id (sensor);
346 pthread_mutex_lock (&sensor_list_lock);
349 for (list_item = sensor_list;
351 list_item = list_item->next)
353 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
355 list_prev = list_item;
356 } /* for (list_item) */
358 if (list_item == NULL)
360 pthread_mutex_unlock (&sensor_list_lock);
364 if (list_prev == NULL)
365 sensor_list = list_item->next;
367 list_prev->next = list_item->next;
370 list_item->next = NULL;
372 pthread_mutex_unlock (&sensor_list_lock);
374 if (c_ipmi_nofiy_remove && c_ipmi_active)
376 notification_t n = { NOTIF_WARNING, time(NULL), "", "",
377 "ipmi", "", "", "", NULL };
379 sstrncpy (n.host, hostname_g, sizeof (n.host));
380 sstrncpy (n.type_instance, list_item->sensor_name,
381 sizeof (n.type_instance));
382 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
383 ssnprintf (n.message, sizeof (n.message),
384 "sensor %s removed", list_item->sensor_name);
386 plugin_dispatch_notification (&n);
391 } /* int sensor_list_remove */
393 static int sensor_list_read_all (void)
395 c_ipmi_sensor_list_t *list_item;
397 pthread_mutex_lock (&sensor_list_lock);
399 for (list_item = sensor_list;
401 list_item = list_item->next)
403 ipmi_sensor_id_get_reading (list_item->sensor_id,
404 sensor_read_handler, /* user data = */ list_item);
405 } /* for (list_item) */
407 pthread_mutex_unlock (&sensor_list_lock);
410 } /* int sensor_list_read_all */
412 static int sensor_list_remove_all (void)
414 c_ipmi_sensor_list_t *list_item;
416 pthread_mutex_lock (&sensor_list_lock);
418 list_item = sensor_list;
421 pthread_mutex_unlock (&sensor_list_lock);
423 while (list_item != NULL)
425 c_ipmi_sensor_list_t *list_next = list_item->next;
429 list_item = list_next;
430 } /* while (list_item) */
433 } /* int sensor_list_remove_all */
438 static void entity_sensor_update_handler (enum ipmi_update_e op,
439 ipmi_entity_t *entity,
440 ipmi_sensor_t *sensor,
443 /* TODO: Ignore sensors we cannot read */
445 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
447 /* Will check for duplicate entries.. */
448 sensor_list_add (sensor);
450 else if (op == IPMI_DELETED)
452 sensor_list_remove (sensor);
454 } /* void entity_sensor_update_handler */
459 static void domain_entity_update_handler (enum ipmi_update_e op,
460 ipmi_domain_t *domain,
461 ipmi_entity_t *entity,
466 if (op == IPMI_ADDED)
468 status = ipmi_entity_add_sensor_update_handler (entity,
469 entity_sensor_update_handler, /* user data = */ NULL);
472 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
475 else if (op == IPMI_DELETED)
477 status = ipmi_entity_remove_sensor_update_handler (entity,
478 entity_sensor_update_handler, /* user data = */ NULL);
481 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
484 } /* void domain_entity_update_handler */
486 static void domain_connection_change_handler (ipmi_domain_t *domain,
488 unsigned int conn_num,
489 unsigned int port_num,
495 printf ("domain_connection_change_handler (domain = %p, err = %i, "
496 "conn_num = %u, port_num = %u, still_connected = %i, "
497 "user_data = %p);\n",
498 (void *) domain, err, conn_num, port_num, still_connected, user_data);
500 status = ipmi_domain_add_entity_update_handler (domain,
501 domain_entity_update_handler, /* user data = */ NULL);
504 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
506 } /* void domain_connection_change_handler */
508 static int thread_init (os_handler_t **ret_os_handler)
510 os_handler_t *os_handler;
511 ipmi_open_option_t open_option[1];
512 ipmi_con_t *smi_connection = NULL;
513 ipmi_domain_id_t domain_id;
516 os_handler = ipmi_posix_thread_setup_os_handler (SIGUSR2);
517 if (os_handler == NULL)
519 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
523 ipmi_init (os_handler);
525 status = ipmi_smi_setup_con (/* if_num = */ 0,
527 /* user data = */ NULL,
531 c_ipmi_error ("ipmi_smi_setup_con", status);
535 memset (open_option, 0, sizeof (open_option));
536 open_option[0].option = IPMI_OPEN_OPTION_ALL;
537 open_option[0].ival = 1;
539 status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
540 domain_connection_change_handler, /* user data = */ NULL,
541 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
542 open_option, sizeof (open_option) / sizeof (open_option[0]),
546 c_ipmi_error ("ipmi_open_domain", status);
550 *ret_os_handler = os_handler;
552 } /* int thread_init */
554 static void *thread_main (void *user_data)
557 os_handler_t *os_handler = NULL;
559 status = thread_init (&os_handler);
562 fprintf (stderr, "ipmi plugin: thread_init failed.\n");
563 return ((void *) -1);
566 while (c_ipmi_active != 0)
568 struct timeval tv = { 1, 0 };
569 os_handler->perform_one_op (os_handler, &tv);
572 ipmi_posix_thread_free_os_handler (os_handler);
575 } /* void *thread_main */
577 static int c_ipmi_config (const char *key, const char *value)
579 if (ignorelist == NULL)
580 ignorelist = ignorelist_create (/* invert = */ 1);
581 if (ignorelist == NULL)
584 if (strcasecmp ("Sensor", key) == 0)
586 ignorelist_add (ignorelist, value);
588 else if (strcasecmp ("IgnoreSelected", key) == 0)
591 if ((strcasecmp ("True", value) == 0)
592 || (strcasecmp ("Yes", value) == 0)
593 || (strcasecmp ("On", value) == 0))
595 ignorelist_set_invert (ignorelist, invert);
597 else if (strcasecmp ("NotifySensorAdd", key) == 0)
599 if ((strcasecmp ("True", value) == 0)
600 || (strcasecmp ("Yes", value) == 0)
601 || (strcasecmp ("On", value) == 0))
602 c_ipmi_nofiy_add = 1;
604 else if (strcasecmp ("NotifySensorRemove", key) == 0)
606 if ((strcasecmp ("True", value) == 0)
607 || (strcasecmp ("Yes", value) == 0)
608 || (strcasecmp ("On", value) == 0))
609 c_ipmi_nofiy_remove = 1;
611 else if (strcasecmp ("NotifySensorNotPresent", key) == 0)
613 if ((strcasecmp ("True", value) == 0)
614 || (strcasecmp ("Yes", value) == 0)
615 || (strcasecmp ("On", value) == 0))
616 c_ipmi_nofiy_notpresent = 1;
624 } /* int c_ipmi_config */
626 static int c_ipmi_init (void)
630 /* Don't send `ADD' notifications during startup (~ 1 minute) */
631 c_ipmi_init_in_progress = 1 + (60 / interval_g);
635 status = pthread_create (&thread_id, /* attr = */ NULL, thread_main,
636 /* user data = */ NULL);
640 thread_id = (pthread_t) 0;
641 ERROR ("ipmi plugin: pthread_create failed.");
646 } /* int c_ipmi_init */
648 static int c_ipmi_read (void)
650 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
652 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
656 sensor_list_read_all ();
658 if (c_ipmi_init_in_progress > 0)
659 c_ipmi_init_in_progress--;
661 c_ipmi_init_in_progress = 0;
664 } /* int c_ipmi_read */
666 static int c_ipmi_shutdown (void)
670 if (thread_id != (pthread_t) 0)
672 pthread_join (thread_id, NULL);
673 thread_id = (pthread_t) 0;
676 sensor_list_remove_all ();
679 } /* int c_ipmi_shutdown */
681 void module_register (void)
683 plugin_register_config ("ipmi", c_ipmi_config,
684 config_keys, config_keys_num);
685 plugin_register_init ("ipmi", c_ipmi_init);
686 plugin_register_read ("ipmi", c_ipmi_read);
687 plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
688 } /* void module_register */
690 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */