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