ipmi plugin: Rename IPMI sensors so names are unique.
[collectd.git] / src / ipmi.c
1 /**
2  * collectd - src/ipmi.c
3  * Copyright (C) 2008  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_ignorelist.h"
26
27 #include <pthread.h>
28
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>
34
35 /*
36  * Private data types
37  */
38 struct c_ipmi_sensor_list_s;
39 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
40
41 struct c_ipmi_sensor_list_s
42 {
43   ipmi_sensor_id_t sensor_id;
44   char sensor_name[DATA_MAX_NAME_LEN];
45   char sensor_type[DATA_MAX_NAME_LEN];
46   c_ipmi_sensor_list_t *next;
47 };
48
49 /*
50  * Module global variables
51  */
52 static pthread_mutex_t sensor_list_lock = PTHREAD_MUTEX_INITIALIZER;
53 static c_ipmi_sensor_list_t *sensor_list = NULL;
54
55 static int c_ipmi_active = 0;
56 static pthread_t thread_id = (pthread_t) 0;
57
58 static const char *config_keys[] =
59 {
60         "Sensor",
61         "IgnoreSelected"
62 };
63 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
64
65 static ignorelist_t *ignorelist = NULL;
66
67 /*
68  * Misc private functions
69  */
70 static void c_ipmi_error (const char *func, int status)
71 {
72   char errbuf[4096];
73
74   memset (errbuf, 0, sizeof (errbuf));
75
76   if (IPMI_IS_OS_ERR (status))
77   {
78     sstrerror (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
79   }
80   else if (IPMI_IS_IPMI_ERR (status))
81   {
82     ipmi_get_error_string (IPMI_GET_IPMI_ERR (status), errbuf, sizeof (errbuf));
83   }
84
85   if (errbuf[0] == 0)
86   {
87     ssnprintf (errbuf, sizeof (errbuf), "Unknown error %#x", status);
88   }
89   errbuf[sizeof (errbuf) - 1] = 0;
90
91   ERROR ("ipmi plugin: %s failed: %s", func, errbuf);
92 } /* void c_ipmi_error */
93
94 /*
95  * Sensor handlers
96  */
97 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
98 static int sensor_list_remove (ipmi_sensor_t *sensor);
99
100 static void sensor_read_handler (ipmi_sensor_t *sensor,
101     int err,
102     enum ipmi_value_present_e value_present,
103     unsigned int raw_value,
104     double value,
105     ipmi_states_t *states,
106     void *user_data)
107 {
108   value_t values[1];
109   value_list_t vl = VALUE_LIST_INIT;
110
111   c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
112
113   if (err != 0)
114   {
115     INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
116         "because it failed with status %#x.",
117         list_item->sensor_name, err);
118     sensor_list_remove (sensor);
119     return;
120   }
121
122   if (value_present != IPMI_BOTH_VALUES_PRESENT)
123   {
124     INFO ("ipmi plugin: sensor_read_handler: Removing sensor %s, "
125         "because it provides %s. If you need this sensor, "
126         "please file a bug report.",
127         list_item->sensor_name,
128         (value_present == IPMI_RAW_VALUE_PRESENT)
129         ? "only the raw value"
130         : "no value");
131     sensor_list_remove (sensor);
132     return;
133   }
134
135   values[0].gauge = value;
136
137   vl.values = values;
138   vl.values_len = 1;
139   vl.time = time (NULL);
140
141   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
142   sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
143   sstrncpy (vl.type, list_item->sensor_type, sizeof (vl.type));
144   sstrncpy (vl.type_instance, list_item->sensor_name, sizeof (vl.type_instance));
145
146   plugin_dispatch_values (&vl);
147 } /* void sensor_read_handler */
148
149 static int sensor_list_add (ipmi_sensor_t *sensor)
150 {
151   ipmi_sensor_id_t sensor_id;
152   c_ipmi_sensor_list_t *list_item;
153   c_ipmi_sensor_list_t *list_prev;
154
155   char sensor_name[DATA_MAX_NAME_LEN];
156   char *sensor_name_ptr;
157   int sensor_type, len;
158   const char *type;
159   ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
160
161   sensor_id = ipmi_sensor_convert_to_id (sensor);
162
163   memset (sensor_name, 0, sizeof (sensor_name));
164   ipmi_sensor_get_name (sensor, sensor_name, sizeof (sensor_name));
165   sensor_name[sizeof (sensor_name) - 1] = 0;
166
167   len = DATA_MAX_NAME_LEN - strlen(sensor_name);
168   strncat(sensor_name, " ", len--);
169   strncat(sensor_name, ipmi_entity_get_entity_id_string(ent), len);
170
171   sensor_name_ptr = strstr (sensor_name, ").");
172   if (sensor_name_ptr == NULL)
173     sensor_name_ptr = sensor_name;
174   else
175   {
176     char *sensor_name_ptr_id = strstr (sensor_name, "(");
177
178     sensor_name_ptr += 2;
179     len = DATA_MAX_NAME_LEN - strlen(sensor_name);
180     strncat(sensor_name, " ", len--);
181     strncat(sensor_name, sensor_name_ptr_id, 
182       MIN(sensor_name_ptr - sensor_name_ptr_id - 1, len));
183   }
184
185   /* Both `ignorelist' and `plugin_instance' may be NULL. */
186   if (ignorelist_match (ignorelist, sensor_name_ptr) != 0)
187     return (0);
188
189   /* FIXME: Use rate unit or base unit to scale the value */
190
191   sensor_type = ipmi_sensor_get_sensor_type (sensor);
192   switch (sensor_type)
193   {
194     case IPMI_SENSOR_TYPE_TEMPERATURE:
195       type = "temperature";
196       break;
197
198     case IPMI_SENSOR_TYPE_VOLTAGE:
199       type = "voltage";
200       break;
201
202     case IPMI_SENSOR_TYPE_CURRENT:
203       type = "current";
204       break;
205
206     case IPMI_SENSOR_TYPE_FAN:
207       type = "fanspeed";
208       break;
209
210     default:
211       {
212         const char *sensor_type_str;
213
214         sensor_type_str = ipmi_sensor_get_sensor_type_string (sensor);
215         INFO ("ipmi plugin: sensor_list_add: Ignore sensor %s, "
216             "because I don't know how to handle its type (%#x, %s). "
217             "If you need this sensor, please file a bug report.",
218             sensor_name_ptr, sensor_type, sensor_type_str);
219         return (-1);
220       }
221   } /* switch (sensor_type) */
222
223   pthread_mutex_lock (&sensor_list_lock);
224
225   list_prev = NULL;
226   for (list_item = sensor_list;
227       list_item != NULL;
228       list_item = list_item->next)
229   {
230     if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
231       break;
232     list_prev = list_item;
233   } /* for (list_item) */
234
235   if (list_item != NULL)
236   {
237     pthread_mutex_unlock (&sensor_list_lock);
238     return (0);
239   }
240
241   list_item = (c_ipmi_sensor_list_t *) calloc (1, sizeof (c_ipmi_sensor_list_t));
242   if (list_item == NULL)
243   {
244     pthread_mutex_unlock (&sensor_list_lock);
245     return (-1);
246   }
247
248   list_item->sensor_id = ipmi_sensor_convert_to_id (sensor);
249
250   if (list_prev != NULL)
251     list_prev->next = list_item;
252   else
253     sensor_list = list_item;
254
255   sstrncpy (list_item->sensor_name, sensor_name_ptr,
256             sizeof (list_item->sensor_name));
257   sstrncpy (list_item->sensor_type, type, sizeof (list_item->sensor_type));
258
259   pthread_mutex_unlock (&sensor_list_lock);
260
261   return (0);
262 } /* int sensor_list_add */
263
264 static int sensor_list_remove (ipmi_sensor_t *sensor)
265 {
266   ipmi_sensor_id_t sensor_id;
267   c_ipmi_sensor_list_t *list_item;
268   c_ipmi_sensor_list_t *list_prev;
269
270   sensor_id = ipmi_sensor_convert_to_id (sensor);
271
272   pthread_mutex_lock (&sensor_list_lock);
273
274   list_prev = NULL;
275   for (list_item = sensor_list;
276       list_item != NULL;
277       list_item = list_item->next)
278   {
279     if (ipmi_cmp_sensor_id (sensor_id, list_item->sensor_id) == 0)
280       break;
281     list_prev = list_item;
282   } /* for (list_item) */
283
284   if (list_item == NULL)
285   {
286     pthread_mutex_unlock (&sensor_list_lock);
287     return (-1);
288   }
289
290   if (list_prev == NULL)
291     sensor_list = list_item->next;
292   else
293     list_prev->next = list_item->next;
294
295   list_prev = NULL;
296   list_item->next = NULL;
297
298   pthread_mutex_unlock (&sensor_list_lock);
299
300   free (list_item);
301   return (0);
302 } /* int sensor_list_remove */
303
304 static int sensor_list_read_all (void)
305 {
306   c_ipmi_sensor_list_t *list_item;
307
308   pthread_mutex_lock (&sensor_list_lock);
309
310   for (list_item = sensor_list;
311       list_item != NULL;
312       list_item = list_item->next)
313   {
314     ipmi_sensor_id_get_reading (list_item->sensor_id,
315         sensor_read_handler, /* user data = */ list_item);
316   } /* for (list_item) */
317
318   pthread_mutex_unlock (&sensor_list_lock);
319
320   return (0);
321 } /* int sensor_list_read_all */
322
323 static int sensor_list_remove_all (void)
324 {
325   c_ipmi_sensor_list_t *list_item;
326
327   pthread_mutex_lock (&sensor_list_lock);
328
329   list_item = sensor_list;
330   sensor_list = NULL;
331
332   pthread_mutex_unlock (&sensor_list_lock);
333
334   while (list_item != NULL)
335   {
336     c_ipmi_sensor_list_t *list_next = list_item->next;
337
338     free (list_item);
339
340     list_item = list_next;
341   } /* while (list_item) */
342
343   return (0);
344 } /* int sensor_list_remove_all */
345
346 /*
347  * Entity handlers
348  */
349 static void entity_sensor_update_handler (enum ipmi_update_e op,
350     ipmi_entity_t *entity,
351     ipmi_sensor_t *sensor,
352     void *user_data)
353 {
354   /* TODO: Ignore sensors we cannot read */
355
356   if ((op == IPMI_ADDED) || (op == IPMI_CHANGED))
357   {
358     /* Will check for duplicate entries.. */
359     sensor_list_add (sensor);
360   }
361   else if (op == IPMI_DELETED)
362   {
363     sensor_list_remove (sensor);
364   }
365 } /* void entity_sensor_update_handler */
366
367 /*
368  * Domain handlers
369  */
370 static void domain_entity_update_handler (enum ipmi_update_e op,
371     ipmi_domain_t *domain,
372     ipmi_entity_t *entity,
373     void *user_data)
374 {
375   int status;
376
377   if (op == IPMI_ADDED)
378   {
379     status = ipmi_entity_add_sensor_update_handler (entity,
380         entity_sensor_update_handler, /* user data = */ NULL);
381     if (status != 0)
382     {
383       c_ipmi_error ("ipmi_entity_add_sensor_update_handler", status);
384     }
385   }
386   else if (op == IPMI_DELETED)
387   {
388     status = ipmi_entity_remove_sensor_update_handler (entity,
389         entity_sensor_update_handler, /* user data = */ NULL);
390     if (status != 0)
391     {
392       c_ipmi_error ("ipmi_entity_remove_sensor_update_handler", status);
393     }
394   }
395 } /* void domain_entity_update_handler */
396
397 static void domain_connection_change_handler (ipmi_domain_t *domain,
398     int err,
399     unsigned int conn_num,
400     unsigned int port_num,
401     int still_connected,
402     void *user_data)
403 {
404   int status;
405
406   printf ("domain_connection_change_handler (domain = %p, err = %i, "
407       "conn_num = %u, port_num = %u, still_connected = %i, "
408       "user_data = %p);\n",
409       (void *) domain, err, conn_num, port_num, still_connected, user_data);
410
411   status = ipmi_domain_add_entity_update_handler (domain,
412       domain_entity_update_handler, /* user data = */ NULL);
413   if (status != 0)
414   {
415     c_ipmi_error ("ipmi_domain_add_entity_update_handler", status);
416   }
417 } /* void domain_connection_change_handler */
418
419 static int thread_init (os_handler_t **ret_os_handler)
420 {
421   os_handler_t *os_handler;
422   ipmi_open_option_t open_option[1];
423   ipmi_con_t *smi_connection = NULL;
424   ipmi_domain_id_t domain_id;
425   int status;
426
427   os_handler = ipmi_posix_thread_setup_os_handler (SIGUSR2);
428   if (os_handler == NULL)
429   {
430     ERROR ("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
431     return (-1);
432   }
433
434   ipmi_init (os_handler);
435
436   status = ipmi_smi_setup_con (/* if_num = */ 0,
437       os_handler,
438       /* user data = */ NULL,
439       &smi_connection);
440   if (status != 0)
441   {
442     c_ipmi_error ("ipmi_smi_setup_con", status);
443     return (-1);
444   }
445
446   memset (open_option, 0, sizeof (open_option));
447   open_option[0].option = IPMI_OPEN_OPTION_ALL;
448   open_option[0].ival = 1;
449
450   status = ipmi_open_domain ("mydomain", &smi_connection, /* num_con = */ 1,
451       domain_connection_change_handler, /* user data = */ NULL,
452       /* domain_fully_up_handler = */ NULL, /* user data = */ NULL,
453       open_option, sizeof (open_option) / sizeof (open_option[0]),
454       &domain_id);
455   if (status != 0)
456   {
457     c_ipmi_error ("ipmi_open_domain", status);
458     return (-1);
459   }
460
461   *ret_os_handler = os_handler;
462   return (0);
463 } /* int thread_init */
464
465 static void *thread_main (void *user_data)
466 {
467   int status;
468   os_handler_t *os_handler = NULL;
469
470   status = thread_init (&os_handler);
471   if (status != 0)
472   {
473     fprintf (stderr, "ipmi plugin: thread_init failed.\n");
474     return ((void *) -1);
475   }
476
477   while (c_ipmi_active != 0)
478   {
479     struct timeval tv = { 1, 0 };
480     os_handler->perform_one_op (os_handler, &tv);
481   }
482
483   ipmi_posix_thread_free_os_handler (os_handler);
484
485   return ((void *) 0);
486 } /* void *thread_main */
487
488 static int c_ipmi_config (const char *key, const char *value)
489 {
490   if (ignorelist == NULL)
491     ignorelist = ignorelist_create (/* invert = */ 1);
492   if (ignorelist == NULL)
493     return (1);
494
495   if (strcasecmp ("Sensor", key) == 0)
496   {
497     ignorelist_add (ignorelist, value);
498   }
499   else if (strcasecmp ("IgnoreSelected", key) == 0)
500   {
501     int invert = 1;
502     if ((strcasecmp ("True", value) == 0)
503         || (strcasecmp ("Yes", value) == 0)
504         || (strcasecmp ("On", value) == 0))
505       invert = 0;
506     ignorelist_set_invert (ignorelist, invert);
507   }
508   else
509   {
510     return (-1);
511   }
512
513   return (0);
514 } /* int c_ipmi_config */
515
516 static int c_ipmi_init (void)
517 {
518   int status;
519
520   c_ipmi_active = 1;
521
522   status = pthread_create (&thread_id, /* attr = */ NULL, thread_main,
523       /* user data = */ NULL);
524   if (status != 0)
525   {
526     c_ipmi_active = 0;
527     thread_id = (pthread_t) 0;
528     ERROR ("ipmi plugin: pthread_create failed.");
529     return (-1);
530   }
531
532   return (0);
533 } /* int c_ipmi_init */
534
535 static int c_ipmi_read (void)
536 {
537   if ((c_ipmi_active == 0) || (thread_id == (pthread_t) 0))
538   {
539     INFO ("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
540     return (-1);
541   }
542
543   sensor_list_read_all ();
544   
545   return (0);
546 } /* int c_ipmi_read */
547
548 static int c_ipmi_shutdown (void)
549 {
550   c_ipmi_active = 0;
551
552   if (thread_id != (pthread_t) 0)
553   {
554     pthread_join (thread_id, NULL);
555     thread_id = (pthread_t) 0;
556   }
557
558   sensor_list_remove_all ();
559
560   return (0);
561 } /* int c_ipmi_shutdown */
562
563 void module_register (void)
564 {
565   plugin_register_config ("ipmi", c_ipmi_config,
566       config_keys, config_keys_num);
567   plugin_register_init ("ipmi", c_ipmi_init);
568   plugin_register_read ("ipmi", c_ipmi_read);
569   plugin_register_shutdown ("ipmi", c_ipmi_shutdown);
570 } /* void module_register */
571
572 /* vim: set sw=2 sts=2 ts=8 fdm=marker et : */