2 * collectd - src/ipmi.c
3 * Copyright (C) 2008 Florian octo Forster
4 * Copyright (C) 2008 Peter Holik
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
21 * Peter Holik <peter at holik.at>
27 #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)
85 memset (errbuf, 0, sizeof (errbuf));
87 if (IPMI_IS_OS_ERR (status))
89 sstrerror (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
91 else if (IPMI_IS_IPMI_ERR (status))
93 ipmi_get_error_string (IPMI_GET_IPMI_ERR (status), errbuf, sizeof (errbuf));
98 ssnprintf (errbuf, sizeof (errbuf), "Unknown error %#x", status);
100 errbuf[sizeof (errbuf) - 1] = 0;
102 ERROR ("ipmi plugin: %s failed: %s", func, errbuf);
103 } /* void c_ipmi_error */
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,
113 enum ipmi_value_present_e value_present,
114 unsigned int raw_value,
116 ipmi_states_t *states,
120 value_list_t vl = VALUE_LIST_INIT;
122 c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
126 if ((err & 0xff) == IPMI_NOT_PRESENT_CC)
128 if (list_item->sensor_not_present == 0)
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)
137 notification_t n = { NOTIF_WARNING, time(NULL), "", "", "ipmi",
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);
151 else if (IPMI_IS_IPMI_ERR(err) && IPMI_GET_IPMI_ERR(err) == IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC)
153 INFO ("ipmi plugin: sensor_read_handler: Sensor %s not ready",
154 list_item->sensor_name);
158 if (IPMI_IS_IPMI_ERR(err))
159 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
160 "because it failed with IPMI error %#x.",
161 list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
162 else if (IPMI_IS_OS_ERR(err))
163 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
164 "because it failed with OS error %#x.",
165 list_item->sensor_name, IPMI_GET_OS_ERR(err));
166 else if (IPMI_IS_RMCPP_ERR(err))
167 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
168 "because it failed with RMCPP error %#x.",
169 list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
170 else if (IPMI_IS_SOL_ERR(err))
171 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
172 "because it failed with RMCPP error %#x.",
173 list_item->sensor_name, IPMI_GET_SOL_ERR(err));
175 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
176 "because it failed with error %#x. of class %#x",
177 list_item->sensor_name, err & 0xff, err & 0xffffff00);
178 sensor_list_remove (sensor);
182 else if (list_item->sensor_not_present == 1)
184 list_item->sensor_not_present = 0;
186 INFO ("ipmi plugin: sensor_read_handler: sensor %s present.",
187 list_item->sensor_name);
189 if (c_ipmi_nofiy_notpresent)
191 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
194 sstrncpy (n.host, hostname_g, sizeof (n.host));
195 sstrncpy (n.type_instance, list_item->sensor_name,
196 sizeof (n.type_instance));
197 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
198 ssnprintf (n.message, sizeof (n.message),
199 "sensor %s present", list_item->sensor_name);
201 plugin_dispatch_notification (&n);
205 if (value_present != IPMI_BOTH_VALUES_PRESENT)
207 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
208 "because it provides %s. If you need this sensor, "
209 "please file a bug report.",
210 list_item->sensor_name,
211 (value_present == IPMI_RAW_VALUE_PRESENT)
212 ? "only the raw value"
214 sensor_list_remove (sensor);
218 values[0].gauge = value;
222 vl.time = time (NULL);
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
226 sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
227 sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
229 plugin_dispatch_values (&vl);
230 } /* void sensor_read_handler */
232 static int sensor_list_add (ipmi_sensor_t *sensor)
234 ipmi_sensor_id_t sensor_id;
235 c_ipmi_sensor_list_t *list_item;
236 c_ipmi_sensor_list_t *list_prev;
238 char sensor_name[DATA_MAX_NAME_LEN];
239 char *sensor_name_ptr;
240 int sensor_type, len;
242 ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
244 sensor_id = ipmi_sensor_convert_to_id (sensor);
246 memset (sensor_name, 0, sizeof (sensor_name));
247 ipmi_sensor_get_name (sensor, sensor_name, sizeof (sensor_name));
248 sensor_name[sizeof (sensor_name) - 1] = 0;
250 len = DATA_MAX_NAME_LEN - strlen(sensor_name);
251 strncat(sensor_name, " ", len--);
252 strncat(sensor_name, ipmi_entity_get_entity_id_string(ent), len);
254 sensor_name_ptr = strstr (sensor_name, ").");
255 if (sensor_name_ptr == NULL)
256 sensor_name_ptr = sensor_name;
259 char *sensor_name_ptr_id = strstr (sensor_name, "(");
261 sensor_name_ptr += 2;
262 len = DATA_MAX_NAME_LEN - strlen(sensor_name);
263 strncat(sensor_name, " ", len--);
264 strncat(sensor_name, sensor_name_ptr_id,
265 MIN(sensor_name_ptr - sensor_name_ptr_id - 1, len));
268 /* Both `ignorelist' and `plugin_instance' may be NULL. */
269 if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
272 /* FIXME: Use rate unit or base unit to scale the value */
274 sensor_type = ipmi_sensor_get_sensor_type (sensor);
277 case IPMI_SENSOR_TYPE_TEMPERATURE:
278 type = "temperature";
281 case IPMI_SENSOR_TYPE_VOLTAGE:
285 case IPMI_SENSOR_TYPE_CURRENT:
289 case IPMI_SENSOR_TYPE_FAN:
295 const char *sensor_type_str;
297 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
298 INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
299 "because I don't know how to handle its type (%#x, %s). "
300 "If you need this sensor, please file a bug report.",
301 sensor_name_ptr, sensor_type, sensor_type_str);
304 } /* switch (sensor_type) */
306 pthread_mutex_lock (&sensor_list_lock);
309 for (list_item = sensor_list;
311 list_item = list_item->next)
313 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
315 list_prev = list_item;
316 } /* for (list_item) */
318 if (list_item != NULL)
320 pthread_mutex_unlock (&sensor_list_lock);
324 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
325 if (list_item == NULL)
327 pthread_mutex_unlock (&sensor_list_lock);
331 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
333 if (list_prev != NULL)
334 list_prev->next = list_item;
336 sensor_list = list_item;
338 sstrncpy (list_item->sensor_name, sensor_name_ptr,
339 sizeof (list_item->sensor_name));
340 sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
342 pthread_mutex_unlock (&sensor_list_lock);
344 if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0))
346 notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi",
349 sstrncpy (n.host, hostname_g, sizeof (n.host));
350 sstrncpy (n.type_instance, list_item->sensor_name,
351 sizeof (n.type_instance));
352 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
353 ssnprintf (n.message, sizeof (n.message),
354 "sensor %s added", list_item->sensor_name);
356 plugin_dispatch_notification (&n);
360 } /* int sensor_list_add */
362 static int sensor_list_remove (ipmi_sensor_t *sensor)
364 ipmi_sensor_id_t sensor_id;
365 c_ipmi_sensor_list_t *list_item;
366 c_ipmi_sensor_list_t *list_prev;
368 sensor_id = ipmi_sensor_convert_to_id (sensor);
370 pthread_mutex_lock (&sensor_list_lock);
373 for (list_item = sensor_list;
375 list_item = list_item->next)
377 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
379 list_prev = list_item;
380 } /* for (list_item) */
382 if (list_item == NULL)
384 pthread_mutex_unlock (&sensor_list_lock);
388 if (list_prev == NULL)
389 sensor_list = list_item->next;
391 list_prev->next = list_item->next;
394 list_item->next = NULL;
396 pthread_mutex_unlock (&sensor_list_lock);
398 if (c_ipmi_nofiy_remove && c_ipmi_active)
400 notification_t n = { NOTIF_WARNING, time(NULL), "", "",
401 "ipmi", "", "", "", NULL };
403 sstrncpy (n.host, hostname_g, sizeof (n.host));
404 sstrncpy (n.type_instance, list_item->sensor_name,
405 sizeof (n.type_instance));
406 sstrncpy (n.type, list_item->sensor_type, sizeof (n.type));
407 ssnprintf (n.message, sizeof (n.message),
408 "sensor %s removed", list_item->sensor_name);
410 plugin_dispatch_notification (&n);
415 } /* int sensor_list_remove */
417 static int sensor_list_read_all (void)
419 c_ipmi_sensor_list_t *list_item;
421 pthread_mutex_lock (&sensor_list_lock);
423 for (list_item = sensor_list;
425 list_item = list_item->next)
427 ipmi_sensor_id_get_reading (list_item->sensor_id,
428 sensor_read_handler, /* user data = */ list_item);
429 } /* for (list_item) */
431 pthread_mutex_unlock (&sensor_list_lock);
434 } /* int sensor_list_read_all */
436 static int sensor_list_remove_all (void)
438 c_ipmi_sensor_list_t *list_item;
440 pthread_mutex_lock (&sensor_list_lock);
442 list_item = sensor_list;
445 pthread_mutex_unlock (&sensor_list_lock);
447 while (list_item != NULL)
449 c_ipmi_sensor_list_t *list_next = list_item->next;
453 list_item = list_next;
454 } /* while (list_item) */
457 } /* int sensor_list_remove_all */
462 static void entity_sensor_update_handler (enum ipmi_update_e op,
463 ipmi_entity_t *entity,
464 ipmi_sensor_t *sensor,
467 /* TODO: Ignore sensors we cannot read */
469 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
471 /* Will check for duplicate entries.. */
472 sensor_list_add (sensor);
474 else if (op == IPMI_DELETED)
476 sensor_list_remove (sensor);
478 } /* void entity_sensor_update_handler */
483 static void domain_entity_update_handler (enum ipmi_update_e op,
484 ipmi_domain_t *domain,
485 ipmi_entity_t *entity,
490 if (op == IPMI_ADDED)
492 status = ipmi_entity_add_sensor_update_handler (entity,
493 entity_sensor_update_handler, /* user data = */ NULL);
496 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
499 else if (op == IPMI_DELETED)
501 status = ipmi_entity_remove_sensor_update_handler (entity,
502 entity_sensor_update_handler, /* user data = */ NULL);
505 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
508 } /* void domain_entity_update_handler */
510 static void domain_connection_change_handler (ipmi_domain_t *domain,
512 unsigned int conn_num,
513 unsigned int port_num,
519 DEBUG ("domain_connection_change_handler (domain = %p, err = %i, "
520 "conn_num = %u, port_num = %u, still_connected = %i, "
521 "user_data = %p);\n",
522 (void *) domain, err, conn_num, port_num, still_connected, user_data);
524 status = ipmi_domain_add_entity_update_handler (domain,
525 domain_entity_update_handler, /* user data = */ NULL);
528 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
530 } /* void domain_connection_change_handler */
532 static int thread_init (os_handler_t **ret_os_handler)
534 os_handler_t *os_handler;
535 ipmi_open_option_t open_option[1];
536 ipmi_con_t *smi_connection = NULL;
537 ipmi_domain_id_t domain_id;
540 os_handler = ipmi_posix_thread_setup_os_handler (SIGUSR2);
541 if (os_handler == NULL)
543 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
547 ipmi_init (os_handler);
549 status = ipmi_smi_setup_con (/* if_num = */ 0,
551 /* user data = */ NULL,
555 c_ipmi_error ("ipmi_smi_setup_con", status);
559 memset (open_option, 0, sizeof (open_option));
560 open_option[0].option = IPMI_OPEN_OPTION_ALL;
561 open_option[0].ival = 1;
563 status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
564 domain_connection_change_handler, /* user data = */ NULL,
565 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
566 open_option, sizeof (open_option) / sizeof (open_option[0]),
570 c_ipmi_error ("ipmi_open_domain", status);
574 *ret_os_handler = os_handler;
576 } /* int thread_init */
578 static void *thread_main (void *user_data)
581 os_handler_t *os_handler = NULL;
583 status = thread_init (&os_handler);
586 ERROR ("ipmi plugin: thread_init failed.\n");
587 return ((void *) -1);
590 while (c_ipmi_active != 0)
592 struct timeval tv = { 1, 0 };
593 os_handler->perform_one_op (os_handler, &tv);
596 ipmi_posix_thread_free_os_handler (os_handler);
599 } /* void *thread_main */
601 static int c_ipmi_config (const char *key, const char *value)
603 if (ignorelist == NULL)
604 ignorelist = ignorelist_create (/* invert = */ 1);
605 if (ignorelist == NULL)
608 if (strcasecmp ("Sensor", key) == 0)
610 ignorelist_add (ignorelist, value);
612 else if (strcasecmp ("IgnoreSelected", key) == 0)
615 if ((strcasecmp ("True", value) == 0)
616 || (strcasecmp ("Yes", value) == 0)
617 || (strcasecmp ("On", value) == 0))
619 ignorelist_set_invert (ignorelist, invert);
621 else if (strcasecmp ("NotifySensorAdd", key) == 0)
623 if ((strcasecmp ("True", value) == 0)
624 || (strcasecmp ("Yes", value) == 0)
625 || (strcasecmp ("On", value) == 0))
626 c_ipmi_nofiy_add = 1;
628 else if (strcasecmp ("NotifySensorRemove", key) == 0)
630 if ((strcasecmp ("True", value) == 0)
631 || (strcasecmp ("Yes", value) == 0)
632 || (strcasecmp ("On", value) == 0))
633 c_ipmi_nofiy_remove = 1;
635 else if (strcasecmp ("NotifySensorNotPresent", key) == 0)
637 if ((strcasecmp ("True", value) == 0)
638 || (strcasecmp ("Yes", value) == 0)
639 || (strcasecmp ("On", value) == 0))
640 c_ipmi_nofiy_notpresent = 1;
648 } /* int c_ipmi_config */
650 static int c_ipmi_init (void)
654 /* Don't send `ADD' notifications during startup (~ 1 minute) */
655 c_ipmi_init_in_progress = 1 + (60 / interval_g);
659 status = pthread_create (&thread_id, /* attr = */ NULL, thread_main,
660 /* user data = */ NULL);
664 thread_id = (pthread_t) 0;
665 ERROR ("ipmi plugin: pthread_create failed.");
670 } /* int c_ipmi_init */
672 static int c_ipmi_read (void)
674 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
676 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
680 sensor_list_read_all ();
682 if (c_ipmi_init_in_progress > 0)
683 c_ipmi_init_in_progress--;
685 c_ipmi_init_in_progress = 0;
688 } /* int c_ipmi_read */
690 static int c_ipmi_shutdown (void)
694 if (thread_id != (pthread_t) 0)
696 pthread_join (thread_id, NULL);
697 thread_id = (pthread_t) 0;
700 sensor_list_remove_all ();
703 } /* int c_ipmi_shutdown */
705 void module_register (void)
707 plugin_register_config ("ipmi", c_ipmi_config,
708 config_keys, config_keys_num);
709 plugin_register_init ("ipmi", c_ipmi_init);
710 plugin_register_read ("ipmi", c_ipmi_read);
711 plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
712 } /* void module_register */
714 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */