ipmi plugin: Ignore events from ignored sensors
[collectd.git] / src / ipmi.c
1 /**
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
6  *
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.
10  *
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.
15  *
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
19  *
20  * Authors:
21  *   Florian octo Forster <octo at collectd.org>
22  *   Peter Holik <peter at holik.at>
23  *   Bruno PrĂ©mont <bonbons at linux-vserver.org>
24  *   Pavel Rochnyak <pavel2000 ngs.ru>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_ignorelist.h"
32
33 #include <OpenIPMI/ipmi_auth.h>
34 #include <OpenIPMI/ipmi_conn.h>
35 #include <OpenIPMI/ipmi_err.h>
36 #include <OpenIPMI/ipmi_lan.h>
37 #include <OpenIPMI/ipmi_posix.h>
38 #include <OpenIPMI/ipmi_smi.h>
39 #include <OpenIPMI/ipmiif.h>
40
41 #define ERR_BUF_SIZE 1024
42
43 /*
44  * Private data types
45  */
46 struct c_ipmi_sensor_list_s;
47 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
48
49 struct c_ipmi_instance_s {
50   char *name;
51   ignorelist_t *ignorelist;
52   _Bool notify_add;
53   _Bool notify_remove;
54   _Bool notify_notpresent;
55   _Bool notify_conn;
56   _Bool sel_enabled;
57   _Bool sel_clear_event;
58
59   char *host;
60   char *connaddr;
61   char *username;
62   char *password;
63   unsigned int authtype;
64
65   _Bool connected;
66   ipmi_con_t *connection;
67   pthread_mutex_t sensor_list_lock;
68   c_ipmi_sensor_list_t *sensor_list;
69
70   _Bool active;
71   pthread_t thread_id;
72   int init_in_progress;
73
74   struct c_ipmi_instance_s *next;
75 };
76 typedef struct c_ipmi_instance_s c_ipmi_instance_t;
77
78 struct c_ipmi_sensor_list_s {
79   ipmi_sensor_id_t sensor_id;
80   char sensor_name[DATA_MAX_NAME_LEN];
81   char sensor_type[DATA_MAX_NAME_LEN];
82   char type_instance[DATA_MAX_NAME_LEN];
83   int sensor_not_present;
84   c_ipmi_sensor_list_t *next;
85   c_ipmi_instance_t *instance;
86   unsigned int use;
87 };
88
89 struct c_ipmi_db_type_map_s {
90   enum ipmi_unit_type_e type;
91   const char *type_name;
92 };
93 typedef struct c_ipmi_db_type_map_s c_ipmi_db_type_map_t;
94
95 /*
96  * Module global variables
97  */
98 static os_handler_t *os_handler = NULL;
99 static c_ipmi_instance_t *instances = NULL;
100
101 /*
102  * Misc private functions
103  */
104 static void c_ipmi_error(c_ipmi_instance_t *st, const char *func, int status) {
105   char errbuf[ERR_BUF_SIZE] = {0};
106
107   if (IPMI_IS_OS_ERR(status) || IPMI_IS_RMCPP_ERR(status) ||
108       IPMI_IS_IPMI_ERR(status)) {
109     ipmi_get_error_string(status, errbuf, sizeof(errbuf));
110   }
111
112   if (errbuf[0] == 0) {
113     snprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
114   }
115   errbuf[sizeof(errbuf) - 1] = 0;
116
117   ERROR("ipmi plugin: %s failed for `%s`: %s", func, st->name, errbuf);
118 } /* void c_ipmi_error */
119
120 static void c_ipmi_log(os_handler_t *handler, const char *format,
121                        enum ipmi_log_type_e log_type, va_list ap) {
122   char msg[ERR_BUF_SIZE];
123
124   vsnprintf(msg, sizeof(msg), format, ap);
125
126   switch (log_type) {
127   case IPMI_LOG_INFO:
128     INFO("ipmi plugin: %s", msg);
129     break;
130   case IPMI_LOG_WARNING:
131     NOTICE("ipmi plugin: %s", msg);
132     break;
133   case IPMI_LOG_SEVERE:
134     WARNING("ipmi plugin: %s", msg);
135     break;
136   case IPMI_LOG_FATAL:
137     ERROR("ipmi plugin: %s", msg);
138     break;
139   case IPMI_LOG_ERR_INFO:
140     ERROR("ipmi plugin: %s", msg);
141     break;
142 #if COLLECT_DEBUG
143   case IPMI_LOG_DEBUG_START:
144   case IPMI_LOG_DEBUG:
145     DEBUG("ipmi plugin: %s", msg);
146     break;
147   case IPMI_LOG_DEBUG_CONT:
148   case IPMI_LOG_DEBUG_END:
149     DEBUG("%s", msg);
150     break;
151 #else
152   case IPMI_LOG_DEBUG_START:
153   case IPMI_LOG_DEBUG:
154   case IPMI_LOG_DEBUG_CONT:
155   case IPMI_LOG_DEBUG_END:
156     break;
157 #endif
158   }
159 } /* void c_ipmi_log */
160
161 static notification_t c_ipmi_notification_init(c_ipmi_instance_t const *st,
162                                                int severity) {
163   notification_t n = {severity, cdtime(), "", "", "ipmi", "", "", "", NULL};
164
165   sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
166   return n;
167 } /* notification_t c_ipmi_notification_init */
168
169 /*
170  * Sensor handlers
171  */
172 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
173 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor);
174
175 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
176                                 enum ipmi_value_present_e value_present,
177                                 unsigned int __attribute__((unused)) raw_value,
178                                 double value, ipmi_states_t *states,
179                                 void *user_data) {
180   value_list_t vl = VALUE_LIST_INIT;
181
182   c_ipmi_sensor_list_t *list_item = user_data;
183   c_ipmi_instance_t *st = list_item->instance;
184
185   list_item->use--;
186
187   if (err != 0) {
188     if (IPMI_IS_IPMI_ERR(err) &&
189         IPMI_GET_IPMI_ERR(err) == IPMI_NOT_PRESENT_CC) {
190       if (list_item->sensor_not_present == 0) {
191         list_item->sensor_not_present = 1;
192
193         INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` "
194              "not present.",
195              list_item->sensor_name, st->name);
196
197         if (st->notify_notpresent) {
198           notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
199
200           sstrncpy(n.type_instance, list_item->type_instance,
201                    sizeof(n.type_instance));
202           sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
203           snprintf(n.message, sizeof(n.message), "sensor %s not present",
204                    list_item->sensor_name);
205
206           plugin_dispatch_notification(&n);
207         }
208       }
209     } else if (IPMI_IS_IPMI_ERR(err) &&
210                IPMI_GET_IPMI_ERR(err) ==
211                    IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
212       INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` not ready.",
213            list_item->sensor_name, st->name);
214     } else if (IPMI_IS_IPMI_ERR(err) &&
215                IPMI_GET_IPMI_ERR(err) == IPMI_TIMEOUT_CC) {
216       INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` timed out.",
217            list_item->sensor_name, st->name);
218     } else {
219       char errbuf[ERR_BUF_SIZE] = {0};
220       ipmi_get_error_string(err, errbuf, sizeof(errbuf) - 1);
221
222       if (IPMI_IS_IPMI_ERR(err))
223         INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
224              "%s.",
225              list_item->sensor_name, st->name, errbuf);
226       else if (IPMI_IS_OS_ERR(err))
227         INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
228              "%s (%#x).",
229              list_item->sensor_name, st->name, errbuf, IPMI_GET_OS_ERR(err));
230       else if (IPMI_IS_RMCPP_ERR(err))
231         INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
232              "%s.",
233              list_item->sensor_name, st->name, errbuf);
234       else if (IPMI_IS_SOL_ERR(err))
235         INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed: "
236              "%s (%#x).",
237              list_item->sensor_name, st->name, errbuf, IPMI_GET_SOL_ERR(err));
238       else
239         INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` failed "
240              "with error %#x. of class %#x",
241              list_item->sensor_name, st->name, err & 0xff, err & 0xffffff00);
242     }
243     return;
244   } else if (list_item->sensor_not_present == 1) {
245     list_item->sensor_not_present = 0;
246
247     INFO("ipmi plugin: sensor_read_handler: sensor `%s` of `%s` present.",
248          list_item->sensor_name, st->name);
249
250     if (st->notify_notpresent) {
251       notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
252
253       sstrncpy(n.type_instance, list_item->type_instance,
254                sizeof(n.type_instance));
255       sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
256       snprintf(n.message, sizeof(n.message), "sensor %s present",
257                list_item->sensor_name);
258
259       plugin_dispatch_notification(&n);
260     }
261   }
262
263   if (value_present != IPMI_BOTH_VALUES_PRESENT) {
264     INFO("ipmi plugin: sensor_read_handler: Removing sensor `%s` of `%s`, "
265          "because it provides %s. If you need this sensor, "
266          "please file a bug report.",
267          list_item->sensor_name, st->name,
268          (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
269                                                    : "no value");
270     sensor_list_remove(st, sensor);
271     return;
272   }
273
274   if (!ipmi_is_sensor_scanning_enabled(states)) {
275     DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
276           "it is in 'scanning disabled' state.",
277           list_item->sensor_name, st->name);
278     return;
279   }
280
281   if (ipmi_is_initial_update_in_progress(states)) {
282     DEBUG("ipmi plugin: sensor_read_handler: Skipping sensor `%s` of `%s`, "
283           "it is in 'initial update in progress' state.",
284           list_item->sensor_name, st->name);
285     return;
286   }
287
288   vl.values = &(value_t){.gauge = value};
289   vl.values_len = 1;
290
291   if (st->host != NULL)
292     sstrncpy(vl.host, st->host, sizeof(vl.host));
293   sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
294   sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
295   sstrncpy(vl.type_instance, list_item->type_instance,
296            sizeof(vl.type_instance));
297
298   plugin_dispatch_values(&vl);
299 } /* void sensor_read_handler */
300
301 static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) {
302   char temp[DATA_MAX_NAME_LEN] = {0};
303   ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
304   const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
305   char sensor_name[DATA_MAX_NAME_LEN] = "";
306   char *sensor_name_ptr;
307
308   if ((buffer == NULL) || (buf_len == 0))
309     return;
310
311   ipmi_sensor_get_name(sensor, temp, sizeof(temp));
312   temp[sizeof(temp) - 1] = 0;
313
314   if (entity_id_string != NULL && strlen(temp))
315     snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string);
316   else if (entity_id_string != NULL)
317     sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
318   else
319     sstrncpy(sensor_name, temp, sizeof(sensor_name));
320
321   if (strlen(temp)) {
322     sstrncpy(temp, sensor_name, sizeof(temp));
323     sensor_name_ptr = strstr(temp, ").");
324     if (sensor_name_ptr != NULL) {
325       /* If name is something like "foo (123).bar",
326        * change that to "bar (123)".
327        * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
328        * `temp' array, which holds a copy of the current `sensor_name'. */
329       char *sensor_id_ptr;
330
331       /* `sensor_name_ptr' points to ").bar". */
332       sensor_name_ptr[1] = 0;
333       /* `temp' holds "foo (123)\0bar\0". */
334       sensor_name_ptr += 2;
335       /* `sensor_name_ptr' now points to "bar". */
336
337       sensor_id_ptr = strstr(temp, "(");
338       if (sensor_id_ptr != NULL) {
339         /* `sensor_id_ptr' now points to "(123)". */
340         snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
341                  sensor_id_ptr);
342       }
343       /* else: don't touch sensor_name. */
344     }
345   }
346   sstrncpy(buffer, sensor_name, buf_len);
347 }
348
349 static const char *sensor_unit_to_type(ipmi_sensor_t *sensor) {
350   static const c_ipmi_db_type_map_t ipmi_db_type_map[] = {
351       {IPMI_UNIT_TYPE_WATTS, "power"}, {IPMI_UNIT_TYPE_CFM, "flow"}};
352
353   /* check the modifier and rate of the sensor value */
354   if ((ipmi_sensor_get_modifier_unit_use(sensor) != IPMI_MODIFIER_UNIT_NONE) ||
355       (ipmi_sensor_get_rate_unit(sensor) != IPMI_RATE_UNIT_NONE))
356     return NULL;
357
358   /* find the db type by using sensor base unit type */
359   enum ipmi_unit_type_e ipmi_type = ipmi_sensor_get_base_unit(sensor);
360   for (int i = 0; i < STATIC_ARRAY_SIZE(ipmi_db_type_map); i++)
361     if (ipmi_db_type_map[i].type == ipmi_type)
362       return ipmi_db_type_map[i].type_name;
363
364   return NULL;
365 } /* const char* sensor_unit_to_type */
366
367 static int sensor_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
368   ipmi_sensor_id_t sensor_id;
369   c_ipmi_sensor_list_t *list_item;
370   c_ipmi_sensor_list_t *list_prev;
371
372   char buffer[DATA_MAX_NAME_LEN] = {0};
373   char *sensor_name_ptr = buffer;
374   int sensor_type;
375   const char *type;
376
377   sensor_id = ipmi_sensor_convert_to_id(sensor);
378   sensor_get_name(sensor, buffer, sizeof(buffer));
379
380   DEBUG("ipmi plugin: sensor_list_add: Found sensor `%s` of `%s`,"
381         " Type: %#x"
382         " Event reading type: %#x"
383         " Direction: %#x"
384         " Event support: %#x",
385         sensor_name_ptr, st->name, ipmi_sensor_get_sensor_type(sensor),
386         ipmi_sensor_get_event_reading_type(sensor),
387         ipmi_sensor_get_sensor_direction(sensor),
388         ipmi_sensor_get_event_support(sensor));
389
390   /* Both `ignorelist' and `sensor_name_ptr' may be NULL. */
391   if (ignorelist_match(st->ignorelist, sensor_name_ptr) != 0) {
392     INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
393          "because it is on ignore list.",
394          sensor_name_ptr, st->name);
395     return -1;
396   }
397
398   /* FIXME: Use rate unit or base unit to scale the value */
399
400   sensor_type = ipmi_sensor_get_sensor_type(sensor);
401
402   /*
403    * ipmitool/lib/ipmi_sdr.c sdr_sensor_has_analog_reading() has a notice
404    * about 'Threshold sensors' and 'analog readings'. Discrete sensor may
405    * have analog data, but discrete sensors support is not implemented
406    * in Collectd yet.
407    *
408    * ipmi_sensor_id_get_reading() supports only 'Threshold' sensors.
409    * See lib/sensor.c:4842, stand_ipmi_sensor_get_reading() for details.
410    */
411   if (!ipmi_sensor_get_is_readable(sensor)) {
412     INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
413          "because it isn't readable! Its type: (%#x, %s). ",
414          sensor_name_ptr, st->name, sensor_type,
415          ipmi_sensor_get_sensor_type_string(sensor));
416     return -1;
417   }
418
419   if (ipmi_sensor_get_event_reading_type(sensor) !=
420       IPMI_EVENT_READING_TYPE_THRESHOLD) {
421     INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
422          "because it is discrete (%#x)! Its type: (%#x, %s). ",
423          sensor_name_ptr, st->name, sensor_type,
424          ipmi_sensor_get_event_reading_type(sensor),
425          ipmi_sensor_get_sensor_type_string(sensor));
426     return -1;
427   }
428
429   switch (sensor_type) {
430   case IPMI_SENSOR_TYPE_TEMPERATURE:
431     type = "temperature";
432     break;
433
434   case IPMI_SENSOR_TYPE_VOLTAGE:
435     type = "voltage";
436     break;
437
438   case IPMI_SENSOR_TYPE_CURRENT:
439     type = "current";
440     break;
441
442   case IPMI_SENSOR_TYPE_FAN:
443     type = "fanspeed";
444     break;
445
446   case IPMI_SENSOR_TYPE_MEMORY:
447     type = "memory";
448     break;
449
450   default: {
451     /* try to get collectd DB type based on sensor base unit type */
452     if ((type = sensor_unit_to_type(sensor)) != NULL)
453       break;
454
455     INFO("ipmi plugin: sensor_list_add: Ignore sensor `%s` of `%s`, "
456          "because I don't know how to handle its units (%#x, %#x, %#x). "
457          "Sensor type: (%#x, %s). If you need this sensor, please file "
458          "a bug report at http://collectd.org/.",
459          sensor_name_ptr, st->name, ipmi_sensor_get_base_unit(sensor),
460          ipmi_sensor_get_modifier_unit(sensor),
461          ipmi_sensor_get_rate_unit(sensor), sensor_type,
462          ipmi_sensor_get_sensor_type_string(sensor));
463     return -1;
464   }
465   } /* switch (sensor_type) */
466
467   pthread_mutex_lock(&st->sensor_list_lock);
468
469   list_prev = NULL;
470   for (list_item = st->sensor_list; list_item != NULL;
471        list_item = list_item->next) {
472     if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
473       break;
474     list_prev = list_item;
475   } /* for (list_item) */
476
477   if (list_item != NULL) {
478     pthread_mutex_unlock(&st->sensor_list_lock);
479     return 0;
480   }
481
482   list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
483   if (list_item == NULL) {
484     pthread_mutex_unlock(&st->sensor_list_lock);
485     return -1;
486   }
487
488   list_item->instance = st;
489   list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
490
491   if (list_prev != NULL)
492     list_prev->next = list_item;
493   else
494     st->sensor_list = list_item;
495
496   /* if sensor provides the percentage value, use "percent" collectd type
497      and add the `percent` to the type instance of the reported value */
498   if (ipmi_sensor_get_percentage(sensor)) {
499     snprintf(list_item->type_instance, sizeof(list_item->type_instance),
500              "percent-%s", sensor_name_ptr);
501     type = "percent";
502   } else {
503     /* use type instance as a name of the sensor */
504     sstrncpy(list_item->type_instance, sensor_name_ptr,
505              sizeof(list_item->type_instance));
506   }
507
508   sstrncpy(list_item->sensor_name, sensor_name_ptr,
509            sizeof(list_item->sensor_name));
510   sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
511
512   pthread_mutex_unlock(&st->sensor_list_lock);
513
514   if (st->notify_add && (st->init_in_progress == 0)) {
515     notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
516
517     sstrncpy(n.type_instance, list_item->type_instance,
518              sizeof(n.type_instance));
519     sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
520     snprintf(n.message, sizeof(n.message), "sensor %s added",
521              list_item->sensor_name);
522
523     plugin_dispatch_notification(&n);
524   }
525
526   return 0;
527 } /* int sensor_list_add */
528
529 static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
530   ipmi_sensor_id_t sensor_id;
531   c_ipmi_sensor_list_t *list_item;
532   c_ipmi_sensor_list_t *list_prev;
533
534   sensor_id = ipmi_sensor_convert_to_id(sensor);
535
536   pthread_mutex_lock(&st->sensor_list_lock);
537
538   list_prev = NULL;
539   for (list_item = st->sensor_list; list_item != NULL;
540        list_item = list_item->next) {
541     if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
542       break;
543     list_prev = list_item;
544   } /* for (list_item) */
545
546   if (list_item == NULL) {
547     pthread_mutex_unlock(&st->sensor_list_lock);
548     return -1;
549   }
550
551   if (list_prev == NULL)
552     st->sensor_list = list_item->next;
553   else
554     list_prev->next = list_item->next;
555
556   list_prev = NULL;
557   list_item->next = NULL;
558
559   pthread_mutex_unlock(&st->sensor_list_lock);
560
561   if (st->notify_remove && st->active) {
562     notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
563
564     sstrncpy(n.type_instance, list_item->type_instance,
565              sizeof(n.type_instance));
566     sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
567     snprintf(n.message, sizeof(n.message), "sensor %s removed",
568              list_item->sensor_name);
569
570     plugin_dispatch_notification(&n);
571   }
572
573   free(list_item);
574   return 0;
575 } /* int sensor_list_remove */
576
577 static int sensor_list_read_all(c_ipmi_instance_t *st) {
578   pthread_mutex_lock(&st->sensor_list_lock);
579
580   for (c_ipmi_sensor_list_t *list_item = st->sensor_list; list_item != NULL;
581        list_item = list_item->next) {
582     DEBUG("ipmi plugin: try read sensor `%s` of `%s`, use: %d",
583           list_item->sensor_name, st->name, list_item->use);
584
585     /* Reading already initiated */
586     if (list_item->use)
587       continue;
588
589     list_item->use++;
590     ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
591                                /* user data = */ (void *)list_item);
592   } /* for (list_item) */
593
594   pthread_mutex_unlock(&st->sensor_list_lock);
595
596   return 0;
597 } /* int sensor_list_read_all */
598
599 static int sensor_list_remove_all(c_ipmi_instance_t *st) {
600   c_ipmi_sensor_list_t *list_item;
601
602   pthread_mutex_lock(&st->sensor_list_lock);
603
604   list_item = st->sensor_list;
605   st->sensor_list = NULL;
606
607   pthread_mutex_unlock(&st->sensor_list_lock);
608
609   while (list_item != NULL) {
610     c_ipmi_sensor_list_t *list_next = list_item->next;
611
612     free(list_item);
613
614     list_item = list_next;
615   } /* while (list_item) */
616
617   return 0;
618 } /* int sensor_list_remove_all */
619
620 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
621   switch (severity) {
622   case IPMI_LOWER_NON_CRITICAL:
623   case IPMI_UPPER_NON_CRITICAL:
624     return NOTIF_OKAY;
625   case IPMI_LOWER_CRITICAL:
626   case IPMI_UPPER_CRITICAL:
627     return NOTIF_WARNING;
628   case IPMI_LOWER_NON_RECOVERABLE:
629   case IPMI_UPPER_NON_RECOVERABLE:
630     return NOTIF_FAILURE;
631   default:
632     return NOTIF_OKAY;
633   } /* switch (severity) */
634 } /* int sensor_convert_threshold_severity */
635
636 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
637                                   enum ipmi_event_dir_e dir,
638                                   ipmi_event_t *event) {
639   ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
640
641   plugin_notification_meta_add_string(n, "entity_name",
642                                       ipmi_entity_get_entity_id_string(ent));
643   plugin_notification_meta_add_signed_int(n, "entity_id",
644                                           ipmi_entity_get_entity_id(ent));
645   plugin_notification_meta_add_signed_int(n, "entity_instance",
646                                           ipmi_entity_get_entity_instance(ent));
647   plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
648
649   if (event)
650     plugin_notification_meta_add_signed_int(n, "event_type",
651                                             ipmi_event_get_type(event));
652 } /* void add_event_sensor_meta_data */
653
654 static int sensor_threshold_event_handler(
655     ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
656     enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
657     enum ipmi_value_present_e value_present, unsigned int raw_value,
658     double value, void *cb_data, ipmi_event_t *event) {
659
660   c_ipmi_instance_t *st = cb_data;
661
662   /* From the IPMI specification Chapter 2: Events.
663    * If a callback handles the event, then all future callbacks called due to
664    * the event will receive a NULL for the event. So be ready to handle a NULL
665    * event in all your event handlers. A NULL may also be passed to an event
666    * handler if the callback was not due to an event. */
667   if (event == NULL)
668     return IPMI_EVENT_NOT_HANDLED;
669
670   notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
671   /* offset is a table index and it's represented as enum of strings that are
672      organized in the way - high and low for each threshold severity level */
673   unsigned int offset = (2 * threshold) + high_low;
674   unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
675   unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
676   const char *event_state =
677       ipmi_get_reading_name(event_type, sensor_type, offset);
678   sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
679   if (value_present != IPMI_NO_VALUES_PRESENT)
680     snprintf(n.message, sizeof(n.message),
681              "sensor %s received event: %s, value is %f", n.type_instance,
682              event_state, value);
683   else
684     snprintf(n.message, sizeof(n.message),
685              "sensor %s received event: %s, value not provided",
686              n.type_instance, event_state);
687
688   DEBUG("Threshold event received for sensor %s", n.type_instance);
689
690   sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
691   n.severity = sensor_convert_threshold_severity(threshold);
692   n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
693
694   plugin_notification_meta_add_string(&n, "severity",
695                                       ipmi_get_threshold_string(threshold));
696   plugin_notification_meta_add_string(&n, "direction",
697                                       ipmi_get_value_dir_string(high_low));
698
699   switch (value_present) {
700   case IPMI_BOTH_VALUES_PRESENT:
701     plugin_notification_meta_add_double(&n, "val", value);
702   /* both values present, so fall-through to add raw value too */
703   case IPMI_RAW_VALUE_PRESENT: {
704     char buf[DATA_MAX_NAME_LEN] = {0};
705     snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
706     plugin_notification_meta_add_string(&n, "raw", buf);
707   } break;
708   default:
709     break;
710   } /* switch (value_present) */
711
712   add_event_common_data(&n, sensor, dir, event);
713
714   plugin_dispatch_notification(&n);
715   plugin_notification_meta_free(n.meta);
716
717   /* Delete handled ipmi event from the list */
718   if (st->sel_clear_event) {
719     ipmi_event_delete(event, NULL, NULL);
720     return IPMI_EVENT_HANDLED;
721   }
722
723   return IPMI_EVENT_NOT_HANDLED;
724 } /* int sensor_threshold_event_handler */
725
726 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
727                                          enum ipmi_event_dir_e dir, int offset,
728                                          int severity, int prev_severity,
729                                          void *cb_data, ipmi_event_t *event) {
730
731   c_ipmi_instance_t *st = cb_data;
732
733   /* From the IPMI specification Chapter 2: Events.
734    * If a callback handles the event, then all future callbacks called due to
735    * the event will receive a NULL for the event. So be ready to handle a NULL
736    * event in all your event handlers. A NULL may also be passed to an event
737    * handler if the callback was not due to an event. */
738   if (event == NULL)
739     return IPMI_EVENT_NOT_HANDLED;
740
741   notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
742   unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
743   unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
744   const char *event_state =
745       ipmi_get_reading_name(event_type, sensor_type, offset);
746   sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
747   snprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
748            n.type_instance, event_state);
749
750   DEBUG("Discrete event received for sensor %s", n.type_instance);
751
752   sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
753   n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
754
755   plugin_notification_meta_add_signed_int(&n, "offset", offset);
756
757   if (severity != -1)
758     plugin_notification_meta_add_signed_int(&n, "severity", severity);
759
760   if (prev_severity != -1)
761     plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
762
763   add_event_common_data(&n, sensor, dir, event);
764
765   plugin_dispatch_notification(&n);
766   plugin_notification_meta_free(n.meta);
767
768   /* Delete handled ipmi event from the list */
769   if (st->sel_clear_event) {
770     ipmi_event_delete(event, NULL, NULL);
771     return IPMI_EVENT_HANDLED;
772   }
773
774   return IPMI_EVENT_NOT_HANDLED;
775 } /* int sensor_discrete_event_handler */
776
777 /*
778  * Entity handlers
779  */
780 static void
781 entity_sensor_update_handler(enum ipmi_update_e op,
782                              ipmi_entity_t __attribute__((unused)) * entity,
783                              ipmi_sensor_t *sensor, void *user_data) {
784   c_ipmi_instance_t *st = user_data;
785   char sensor_name[DATA_MAX_NAME_LEN] = {0};
786
787   sensor_get_name(sensor, sensor_name, sizeof(sensor_name));
788   if (ignorelist_match(st->ignorelist, sensor_name) != 0)
789     return; /* Ignore - sensor on ignore list */
790
791   if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
792     /* Will check for duplicate entries.. */
793     sensor_list_add(st, sensor);
794
795     if (st->sel_enabled) {
796       int status = 0;
797       /* register threshold event if threshold sensor support events */
798       if ((ipmi_sensor_get_event_reading_type(sensor) ==
799            IPMI_EVENT_READING_TYPE_THRESHOLD) &&
800           (ipmi_sensor_get_threshold_access(sensor) !=
801            IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
802         status = ipmi_sensor_add_threshold_event_handler(
803             sensor, sensor_threshold_event_handler, st);
804       /* register discrete handler if discrete/specific sensor support events */
805       else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
806         status = ipmi_sensor_add_discrete_event_handler(
807             sensor, sensor_discrete_event_handler, st);
808
809       if (status) {
810         ERROR("Unable to add sensor %s event handler, status: %d", sensor_name,
811               status);
812       }
813     }
814   } else if (op == IPMI_DELETED) {
815     sensor_list_remove(st, sensor);
816
817     if (st->sel_enabled) {
818       if (ipmi_sensor_get_event_reading_type(sensor) ==
819           IPMI_EVENT_READING_TYPE_THRESHOLD)
820         ipmi_sensor_remove_threshold_event_handler(
821             sensor, sensor_threshold_event_handler, st);
822       else
823         ipmi_sensor_remove_discrete_event_handler(
824             sensor, sensor_discrete_event_handler, st);
825     }
826   }
827 } /* void entity_sensor_update_handler */
828
829 /*
830  * Domain handlers
831  */
832 static void
833 domain_entity_update_handler(enum ipmi_update_e op,
834                              ipmi_domain_t __attribute__((unused)) * domain,
835                              ipmi_entity_t *entity, void *user_data) {
836   int status;
837   c_ipmi_instance_t *st = user_data;
838
839   if (op == IPMI_ADDED) {
840     status = ipmi_entity_add_sensor_update_handler(
841         entity, entity_sensor_update_handler, /* user data = */ (void *)st);
842     if (status != 0) {
843       c_ipmi_error(st, "ipmi_entity_add_sensor_update_handler", status);
844     }
845   } else if (op == IPMI_DELETED) {
846     status = ipmi_entity_remove_sensor_update_handler(
847         entity, entity_sensor_update_handler, /* user data = */ (void *)st);
848     if (status != 0) {
849       c_ipmi_error(st, "ipmi_entity_remove_sensor_update_handler", status);
850     }
851   }
852 } /* void domain_entity_update_handler */
853
854 static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi,
855                               const ipmi_addr_t __attribute__((unused)) * addr,
856                               unsigned int __attribute__((unused)) addr_len,
857                               ipmi_event_t *event, void *cb_data) {
858   unsigned int type = ipmi_event_get_type(event);
859   ipmi_domain_t *domain = cb_data;
860
861   DEBUG("%s: Event received: type %u", __FUNCTION__, type);
862
863   if (type != 0x02)
864     /* It's not a standard IPMI event. */
865     return;
866
867   /* force domain to reread SELs */
868   ipmi_domain_reread_sels(domain, NULL, NULL);
869 }
870
871 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
872                                              unsigned int conn_num,
873                                              unsigned int port_num,
874                                              int still_connected,
875                                              void *user_data) {
876
877   DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
878         "conn_num = %u, port_num = %u, still_connected = %i, "
879         "user_data = %p);",
880         (void *)domain, err, conn_num, port_num, still_connected, user_data);
881
882   c_ipmi_instance_t *st = user_data;
883
884   if (err != 0)
885     c_ipmi_error(st, "domain_connection_change_handler", err);
886
887   if (!still_connected) {
888
889     if (st->notify_conn && st->connected && st->init_in_progress == 0) {
890       notification_t n = c_ipmi_notification_init(st, NOTIF_FAILURE);
891
892       sstrncpy(n.message, "IPMI connection lost", sizeof(n.plugin));
893
894       plugin_dispatch_notification(&n);
895     }
896
897     st->connected = 0;
898     return;
899   }
900
901   if (st->notify_conn && !st->connected && st->init_in_progress == 0) {
902     notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
903
904     sstrncpy(n.message, "IPMI connection restored", sizeof(n.plugin));
905
906     plugin_dispatch_notification(&n);
907   }
908
909   st->connected = 1;
910
911   int status = ipmi_domain_add_entity_update_handler(
912       domain, domain_entity_update_handler, /* user data = */ st);
913   if (status != 0) {
914     c_ipmi_error(st, "ipmi_domain_add_entity_update_handler", status);
915   }
916
917   status = st->connection->add_event_handler(st->connection, smi_event_handler,
918                                              (void *)domain);
919
920   if (status != 0)
921     c_ipmi_error(st, "Failed to register smi event handler", status);
922 } /* void domain_connection_change_handler */
923
924 static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
925   ipmi_domain_id_t domain_id;
926   int status;
927
928   if (st->connaddr != NULL) {
929     status = ipmi_ip_setup_con(
930         &st->connaddr, &(char *){IPMI_LAN_STD_PORT_STR}, 1, st->authtype,
931         (unsigned int)IPMI_PRIVILEGE_USER, st->username, strlen(st->username),
932         st->password, strlen(st->password), os_handler,
933         /* user data = */ NULL, &st->connection);
934     if (status != 0) {
935       c_ipmi_error(st, "ipmi_ip_setup_con", status);
936       return -1;
937     }
938   } else {
939     status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
940                                 /* user data = */ NULL, &st->connection);
941     if (status != 0) {
942       c_ipmi_error(st, "ipmi_smi_setup_con", status);
943       return -1;
944     }
945   }
946
947   ipmi_open_option_t opts[] = {
948       {.option = IPMI_OPEN_OPTION_ALL, {.ival = 1}},
949 #ifdef IPMI_OPEN_OPTION_USE_CACHE
950       /* OpenIPMI-2.0.17 and later: Disable SDR cache in local file */
951       {.option = IPMI_OPEN_OPTION_USE_CACHE, {.ival = 0}},
952 #endif
953   };
954
955   /*
956    * NOTE: Domain names must be unique. There is static `domains_list` common
957    * to all threads inside lib/domain.c and some ops are done by name.
958    */
959   status = ipmi_open_domain(
960       st->name, &st->connection, /* num_con = */ 1,
961       domain_connection_change_handler, /* user data = */ (void *)st,
962       /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, opts,
963       STATIC_ARRAY_SIZE(opts), &domain_id);
964   if (status != 0) {
965     c_ipmi_error(st, "ipmi_open_domain", status);
966     return -1;
967   }
968
969   return 0;
970 } /* int c_ipmi_thread_init */
971
972 static void *c_ipmi_thread_main(void *user_data) {
973   c_ipmi_instance_t *st = user_data;
974
975   int status = c_ipmi_thread_init(st);
976   if (status != 0) {
977     ERROR("ipmi plugin: c_ipmi_thread_init failed.");
978     st->active = 0;
979     return (void *)-1;
980   }
981
982   while (st->active != 0) {
983     struct timeval tv = {1, 0};
984     os_handler->perform_one_op(os_handler, &tv);
985   }
986   return (void *)0;
987 } /* void *c_ipmi_thread_main */
988
989 static c_ipmi_instance_t *c_ipmi_init_instance() {
990   c_ipmi_instance_t *st;
991
992   st = calloc(1, sizeof(*st));
993   if (st == NULL) {
994     ERROR("ipmi plugin: calloc failed.");
995     return NULL;
996   }
997
998   st->name = strdup("main");
999   if (st->name == NULL) {
1000     sfree(st);
1001     ERROR("ipmi plugin: strdup() failed.");
1002     return NULL;
1003   }
1004
1005   st->ignorelist = ignorelist_create(/* invert = */ 1);
1006   if (st->ignorelist == NULL) {
1007     sfree(st->name);
1008     sfree(st);
1009     ERROR("ipmi plugin: ignorelist_create() failed.");
1010     return NULL;
1011   }
1012
1013   st->sensor_list = NULL;
1014   pthread_mutex_init(&st->sensor_list_lock, /* attr = */ NULL);
1015
1016   st->host = NULL;
1017   st->connaddr = NULL;
1018   st->username = NULL;
1019   st->password = NULL;
1020   st->authtype = IPMI_AUTHTYPE_DEFAULT;
1021
1022   st->next = NULL;
1023
1024   return st;
1025 } /* c_ipmi_instance_t *c_ipmi_init_instance */
1026
1027 static void c_ipmi_free_instance(c_ipmi_instance_t *st) {
1028   if (st == NULL)
1029     return;
1030
1031   assert(st->next == NULL);
1032
1033   sfree(st->name);
1034   sfree(st->host);
1035   sfree(st->connaddr);
1036   sfree(st->username);
1037   sfree(st->password);
1038
1039   ignorelist_free(st->ignorelist);
1040   pthread_mutex_destroy(&st->sensor_list_lock);
1041   sfree(st);
1042 } /* void c_ipmi_free_instance */
1043
1044 static void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
1045   if (instances == NULL) {
1046     instances = instance;
1047     return;
1048   }
1049
1050   c_ipmi_instance_t *last = instances;
1051
1052   while (last->next != NULL)
1053     last = last->next;
1054
1055   last->next = instance;
1056 } /* void c_ipmi_add_instance */
1057
1058 static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
1059   int status = 0;
1060   c_ipmi_instance_t *st = c_ipmi_init_instance();
1061   if (st == NULL)
1062     return ENOMEM;
1063
1064   if (strcasecmp(ci->key, "Instance") == 0)
1065     status = cf_util_get_string(ci, &st->name);
1066
1067   if (status != 0) {
1068     c_ipmi_free_instance(st);
1069     return status;
1070   }
1071
1072   for (int i = 0; i < ci->children_num; i++) {
1073     oconfig_item_t *child = ci->children + i;
1074
1075     if (strcasecmp("Sensor", child->key) == 0)
1076       ignorelist_add(st->ignorelist, child->values[0].value.string);
1077     else if (strcasecmp("IgnoreSelected", child->key) == 0) {
1078       _Bool t;
1079       status = cf_util_get_boolean(child, &t);
1080       if (status != 0)
1081         break;
1082       ignorelist_set_invert(st->ignorelist, /* invert = */ !t);
1083     } else if (strcasecmp("NotifyIPMIConnectionState", child->key) == 0) {
1084       status = cf_util_get_boolean(child, &st->notify_conn);
1085     } else if (strcasecmp("NotifySensorAdd", child->key) == 0) {
1086       status = cf_util_get_boolean(child, &st->notify_add);
1087     } else if (strcasecmp("NotifySensorRemove", child->key) == 0) {
1088       status = cf_util_get_boolean(child, &st->notify_remove);
1089     } else if (strcasecmp("NotifySensorNotPresent", child->key) == 0) {
1090       status = cf_util_get_boolean(child, &st->notify_notpresent);
1091     } else if (strcasecmp("SELEnabled", child->key) == 0) {
1092       status = cf_util_get_boolean(child, &st->sel_enabled);
1093     } else if (strcasecmp("SELClearEvent", child->key) == 0) {
1094       status = cf_util_get_boolean(child, &st->sel_clear_event);
1095     } else if (strcasecmp("Host", child->key) == 0)
1096       status = cf_util_get_string(child, &st->host);
1097     else if (strcasecmp("Address", child->key) == 0)
1098       status = cf_util_get_string(child, &st->connaddr);
1099     else if (strcasecmp("Username", child->key) == 0)
1100       status = cf_util_get_string(child, &st->username);
1101     else if (strcasecmp("Password", child->key) == 0)
1102       status = cf_util_get_string(child, &st->password);
1103     else if (strcasecmp("AuthType", child->key) == 0) {
1104       char tmp[8];
1105       status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
1106       if (status != 0)
1107         break;
1108
1109       if (strcasecmp("MD5", tmp) == 0)
1110         st->authtype = IPMI_AUTHTYPE_MD5;
1111       else if (strcasecmp("rmcp+", tmp) == 0)
1112         st->authtype = IPMI_AUTHTYPE_RMCP_PLUS;
1113       else
1114         WARNING("ipmi plugin: The value \"%s\" is not valid for the "
1115                 "\"AuthType\" option.",
1116                 tmp);
1117     } else {
1118       WARNING("ipmi plugin: Option `%s' not allowed here.", child->key);
1119       status = -1;
1120     }
1121
1122     if (status != 0)
1123       break;
1124   }
1125
1126   if (status != 0) {
1127     c_ipmi_free_instance(st);
1128     return status;
1129   }
1130
1131   c_ipmi_add_instance(st);
1132
1133   return 0;
1134 } /* int c_ipmi_config_add_instance */
1135
1136 static int c_ipmi_config(oconfig_item_t *ci) {
1137   _Bool have_instance_block = 0;
1138
1139   for (int i = 0; i < ci->children_num; i++) {
1140     oconfig_item_t *child = ci->children + i;
1141
1142     if (strcasecmp("Instance", child->key) == 0) {
1143       int status = c_ipmi_config_add_instance(child);
1144       if (status != 0)
1145         return status;
1146
1147       have_instance_block = 1;
1148     } else if (!have_instance_block) {
1149       /* Non-instance option: Assume legacy configuration (without <Instance />
1150        * blocks) and call c_ipmi_config_add_instance with the <Plugin /> block.
1151        */
1152       WARNING("ipmi plugin: Legacy configuration found! Please update your "
1153               "config file.");
1154       return c_ipmi_config_add_instance(ci);
1155     } else {
1156       WARNING("ipmi plugin: The configuration option "
1157               "\"%s\" is not allowed here. Did you "
1158               "forget to add an <Instance /> block "
1159               "around the configuration?",
1160               child->key);
1161       return -1;
1162     }
1163   } /* for (ci->children) */
1164
1165   return 0;
1166 } /* int c_ipmi_config */
1167
1168 static int c_ipmi_read(user_data_t *user_data) {
1169   c_ipmi_instance_t *st = user_data->data;
1170
1171   if (st->active == 0) {
1172     INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
1173     return -1;
1174   }
1175
1176   if (st->connected == 0)
1177     return 0;
1178
1179   sensor_list_read_all(st);
1180
1181   if (st->init_in_progress > 0)
1182     st->init_in_progress--;
1183   else
1184     st->init_in_progress = 0;
1185
1186   return 0;
1187 } /* int c_ipmi_read */
1188
1189 static int c_ipmi_init(void) {
1190   c_ipmi_instance_t *st;
1191   char callback_name[3 * DATA_MAX_NAME_LEN];
1192
1193   if (os_handler != NULL) {
1194     return 0;
1195   }
1196
1197   os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
1198   if (os_handler == NULL) {
1199     ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
1200     return -1;
1201   }
1202
1203   os_handler->set_log_handler(os_handler, c_ipmi_log);
1204
1205   if (ipmi_init(os_handler) != 0) {
1206     ERROR("ipmi plugin: ipmi_init() failed.");
1207     os_handler->free_os_handler(os_handler);
1208     return -1;
1209   };
1210
1211   if (instances == NULL) {
1212     /* No instances were configured, let's start a default instance. */
1213     st = c_ipmi_init_instance();
1214     if (st == NULL)
1215       return ENOMEM;
1216
1217     c_ipmi_add_instance(st);
1218   }
1219
1220   /* Don't send `ADD' notifications during startup (~ 1 minute) */
1221   int cycles = 1 + (int)(TIME_T_TO_CDTIME_T(60) / plugin_get_interval());
1222
1223   st = instances;
1224   while (NULL != st) {
1225     /* The `st->name` is used as "domain name" for ipmi_open_domain().
1226      * That value should be unique, so we do plugin_register_complex_read()
1227      * at first as it checks the uniqueness. */
1228     snprintf(callback_name, sizeof(callback_name), "ipmi/%s", st->name);
1229
1230     user_data_t ud = {
1231         .data = st,
1232     };
1233
1234     int status = plugin_register_complex_read(
1235         /* group     = */ "ipmi",
1236         /* name      = */ callback_name,
1237         /* callback  = */ c_ipmi_read,
1238         /* interval  = */ 0,
1239         /* user_data = */ &ud);
1240
1241     if (status != 0) {
1242       st = st->next;
1243       continue;
1244     }
1245
1246     st->init_in_progress = cycles;
1247     st->active = 1;
1248
1249     status = plugin_thread_create(&st->thread_id, /* attr = */ NULL,
1250                                   c_ipmi_thread_main,
1251                                   /* user data = */ (void *)st, "ipmi");
1252
1253     if (status != 0) {
1254       st->active = 0;
1255       st->thread_id = (pthread_t){0};
1256
1257       plugin_unregister_read(callback_name);
1258
1259       ERROR("ipmi plugin: pthread_create failed for `%s`.", callback_name);
1260     }
1261
1262     st = st->next;
1263   }
1264
1265   return 0;
1266 } /* int c_ipmi_init */
1267
1268 static int c_ipmi_shutdown(void) {
1269   c_ipmi_instance_t *st = instances;
1270   instances = NULL;
1271
1272   while (st != NULL) {
1273     c_ipmi_instance_t *next = st->next;
1274
1275     st->next = NULL;
1276     st->active = 0;
1277
1278     if (!pthread_equal(st->thread_id, (pthread_t){0})) {
1279       pthread_join(st->thread_id, NULL);
1280       st->thread_id = (pthread_t){0};
1281     }
1282
1283     sensor_list_remove_all(st);
1284     c_ipmi_free_instance(st);
1285
1286     st = next;
1287   }
1288
1289   os_handler->free_os_handler(os_handler);
1290   os_handler = NULL;
1291
1292   return 0;
1293 } /* int c_ipmi_shutdown */
1294
1295 void module_register(void) {
1296   plugin_register_complex_config("ipmi", c_ipmi_config);
1297   plugin_register_init("ipmi", c_ipmi_init);
1298   plugin_register_shutdown("ipmi", c_ipmi_shutdown);
1299 } /* void module_register */