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