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>
28 #include <OpenIPMI/ipmiif.h>
29 #include <OpenIPMI/ipmi_err.h>
30 #include <OpenIPMI/ipmi_posix.h>
31 #include <OpenIPMI/ipmi_conn.h>
32 #include <OpenIPMI/ipmi_smi.h>
37 struct c_ipmi_sensor_list_s;
38 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
40 struct c_ipmi_sensor_list_s
42 ipmi_sensor_id_t sensor_id;
43 c_ipmi_sensor_list_t *next;
47 * Module global variables
49 static pthread_mutex_t sensor_list_lock = PTHREAD_MUTEX_INITIALIZER;
50 static c_ipmi_sensor_list_t *sensor_list = NULL;
52 static int c_ipmi_active = 0;
53 static pthread_t thread_id = (pthread_t) 0;
56 * Misc private functions
58 static void c_ipmi_error (const char *func, int status)
62 memset (errbuf, 0, sizeof (errbuf));
64 if (IPMI_IS_OS_ERR (status))
66 sstrerror_r (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
68 else if (IPMI_IS_IPMI_ERR (status))
70 ipmi_get_error_string (IPMI_GET_IPMI_ERR (status), errbuf, sizeof (errbuf));
75 ssnprintf (errbuf, sizeof (errbuf), "Unknown error %#x", status);
77 errbuf[sizeof (errbuf) - 1] = 0;
79 ERROR ("ipmi plugin: %s failed: %s", func, errbuf);
80 } /* void c_ipmi_error */
85 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
86 static int sensor_list_remove (ipmi_sensor_t *sensor);
88 static void sensor_read_handler (ipmi_sensor_t *sensor,
90 enum ipmi_value_present_e value_present,
91 unsigned int raw_value,
93 ipmi_states_t *states,
97 value_list_t vl = VALUE_LIST_INIT;
99 char sensor_name[IPMI_SENSOR_NAME_LEN];
100 char *sensor_name_ptr;
104 memset (sensor_name, 0, sizeof (sensor_name));
105 ipmi_sensor_get_name (sensor, sensor_name, sizeof (sensor_name));
106 sensor_name[sizeof (sensor_name) - 1] = 0;
108 sensor_name_ptr = strstr (sensor_name, ").");
109 if (sensor_name_ptr == NULL)
110 sensor_name_ptr = sensor_name;
112 sensor_name_ptr += 2;
116 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
117 "because it failed with status %#x.",
118 sensor_name_ptr, err);
119 sensor_list_remove (sensor);
123 if (value_present != IPMI_BOTH_VALUES_PRESENT)
125 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
126 "because it provides %s. If you need this sensor, "
127 "please file a bug report.",
129 (value_present == IPMI_RAW_VALUE_PRESENT)
130 ? "only the raw value"
132 sensor_list_remove (sensor);
136 /* FIXME: Use rate unit or base unit to scale the value */
138 sensor_type = ipmi_sensor_get_sensor_type (sensor);
141 case IPMI_SENSOR_TYPE_TEMPERATURE:
142 type = "temperature";
145 case IPMI_SENSOR_TYPE_VOLTAGE:
149 case IPMI_SENSOR_TYPE_CURRENT:
153 case IPMI_SENSOR_TYPE_FAN:
159 const char *sensor_type_str;
161 sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
162 INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
163 "because I don't know how to handle its type (%#x, %s). "
164 "If you need this sensor, please file a bug report.",
165 sensor_name_ptr, sensor_type, sensor_type_str);
166 sensor_list_remove (sensor);
169 } /* switch (sensor_type) */
171 values[0].gauge = value;
175 vl.time = time (NULL);
177 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
178 sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
179 sstrncpy (vl.type_instance, sensor_name_ptr, sizeof (vl.type_instance));
181 plugin_dispatch_values (type, &vl);
182 } /* void sensor_read_handler */
184 static int sensor_list_add (ipmi_sensor_t *sensor)
186 ipmi_sensor_id_t sensor_id;
187 c_ipmi_sensor_list_t *list_item;
188 c_ipmi_sensor_list_t *list_prev;
190 sensor_id = ipmi_sensor_convert_to_id (sensor);
192 pthread_mutex_lock (&sensor_list_lock);
195 for (list_item = sensor_list;
197 list_item = list_item->next)
199 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
201 list_prev = list_item;
202 } /* for (list_item) */
204 if (list_item != NULL)
206 pthread_mutex_unlock (&sensor_list_lock);
210 list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
211 if (list_item == NULL)
213 pthread_mutex_unlock (&sensor_list_lock);
217 list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
219 if (list_prev != NULL)
220 list_prev->next = list_item;
222 sensor_list = list_item;
224 pthread_mutex_unlock (&sensor_list_lock);
227 } /* int sensor_list_add */
229 static int sensor_list_remove (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 sensor_id = ipmi_sensor_convert_to_id (sensor);
237 pthread_mutex_lock (&sensor_list_lock);
240 for (list_item = sensor_list;
242 list_item = list_item->next)
244 if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
246 list_prev = list_item;
247 } /* for (list_item) */
249 if (list_item == NULL)
251 pthread_mutex_unlock (&sensor_list_lock);
255 if (list_prev == NULL)
256 sensor_list = list_item->next;
258 list_prev->next = list_item->next;
261 list_item->next = NULL;
263 pthread_mutex_unlock (&sensor_list_lock);
267 } /* int sensor_list_remove */
269 static int sensor_list_read_all (void)
271 c_ipmi_sensor_list_t *list_item;
273 pthread_mutex_lock (&sensor_list_lock);
275 for (list_item = sensor_list;
277 list_item = list_item->next)
279 ipmi_sensor_id_get_reading (list_item->sensor_id,
280 sensor_read_handler, /* user data = */ NULL);
281 } /* for (list_item) */
283 pthread_mutex_unlock (&sensor_list_lock);
286 } /* int sensor_list_read_all */
288 static int sensor_list_remove_all (void)
290 c_ipmi_sensor_list_t *list_item;
292 pthread_mutex_lock (&sensor_list_lock);
294 list_item = sensor_list;
297 pthread_mutex_unlock (&sensor_list_lock);
299 while (list_item != NULL)
301 c_ipmi_sensor_list_t *list_next = list_item->next;
305 list_item = list_next;
306 } /* while (list_item) */
309 } /* int sensor_list_remove_all */
314 static void entity_sensor_update_handler (enum ipmi_update_e op,
315 ipmi_entity_t *entity,
316 ipmi_sensor_t *sensor,
319 /* TODO: Ignore sensors we cannot read */
321 if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
323 /* Will check for duplicate entries.. */
324 sensor_list_add (sensor);
326 else if (op == IPMI_DELETED)
328 sensor_list_remove (sensor);
330 } /* void entity_sensor_update_handler */
335 static void domain_entity_update_handler (enum ipmi_update_e op,
336 ipmi_domain_t *domain,
337 ipmi_entity_t *entity,
342 if (op == IPMI_ADDED)
344 status = ipmi_entity_add_sensor_update_handler (entity,
345 entity_sensor_update_handler, /* user data = */ NULL);
348 c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
351 else if (op == IPMI_DELETED)
353 status = ipmi_entity_remove_sensor_update_handler (entity,
354 entity_sensor_update_handler, /* user data = */ NULL);
357 c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
360 } /* void domain_entity_update_handler */
362 static void domain_connection_change_handler (ipmi_domain_t *domain,
364 unsigned int conn_num,
365 unsigned int port_num,
371 printf ("domain_connection_change_handler (domain = %p, err = %i, "
372 "conn_num = %u, port_num = %u, still_connected = %i, "
373 "user_data = %p);\n",
374 (void *) domain, err, conn_num, port_num, still_connected, user_data);
376 status = ipmi_domain_add_entity_update_handler (domain,
377 domain_entity_update_handler, /* user data = */ NULL);
380 c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
382 } /* void domain_connection_change_handler */
384 static int thread_init (os_handler_t **ret_os_handler)
386 os_handler_t *os_handler;
387 ipmi_open_option_t open_option[1];
388 ipmi_con_t *smi_connection = NULL;
389 ipmi_domain_id_t domain_id;
392 os_handler = ipmi_posix_thread_setup_os_handler (SIGUSR2);
393 if (os_handler == NULL)
395 ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
399 ipmi_init (os_handler);
401 status = ipmi_smi_setup_con (/* if_num = */ 0,
403 /* user data = */ NULL,
407 c_ipmi_error ("ipmi_smi_setup_con", status);
411 memset (open_option, 0, sizeof (open_option));
412 open_option[0].option = IPMI_OPEN_OPTION_ALL;
413 open_option[0].ival = 1;
415 status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
416 domain_connection_change_handler, /* user data = */ NULL,
417 /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
418 open_option, sizeof (open_option) / sizeof (open_option[0]),
422 c_ipmi_error ("ipmi_open_domain", status);
426 *ret_os_handler = os_handler;
428 } /* int thread_init */
430 static void *thread_main (void *user_data)
433 os_handler_t *os_handler = NULL;
435 status = thread_init (&os_handler);
438 fprintf (stderr, "ipmi plugin: thread_init failed.\n");
439 return ((void *) -1);
442 while (c_ipmi_active != 0)
444 struct timeval tv = { 1, 0 };
445 os_handler->perform_one_op (os_handler, &tv);
448 ipmi_posix_thread_free_os_handler (os_handler);
451 } /* void *thread_main */
453 static int c_ipmi_init (void)
459 status = pthread_create (&thread_id, /* attr = */ NULL, thread_main,
460 /* user data = */ NULL);
464 thread_id = (pthread_t) 0;
465 ERROR ("ipmi plugin: pthread_create failed.");
470 } /* int c_ipmi_init */
472 static int c_ipmi_read (void)
474 if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
476 INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
480 sensor_list_read_all ();
483 } /* int c_ipmi_read */
485 static int c_ipmi_shutdown (void)
489 if (thread_id != (pthread_t) 0)
491 pthread_join (thread_id, NULL);
492 thread_id = (pthread_t) 0;
495 sensor_list_remove_all ();
498 } /* int c_ipmi_shutdown */
500 void module_register (void)
502 plugin_register_init ("ipmi", c_ipmi_init);
503 plugin_register_read ("ipmi", c_ipmi_read);
504 plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
505 } /* void module_register */
507 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */