87f8b4410d86a408c8a2ed06bc0fe2dc226f9ad2
[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  **/
25
26 #include "collectd.h"
27
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_ignorelist.h"
31
32 #include <OpenIPMI/ipmi_conn.h>
33 #include <OpenIPMI/ipmi_err.h>
34 #include <OpenIPMI/ipmi_posix.h>
35 #include <OpenIPMI/ipmi_smi.h>
36 #include <OpenIPMI/ipmiif.h>
37
38 /*
39  * Private data types
40  */
41 struct c_ipmi_sensor_list_s;
42 typedef struct c_ipmi_sensor_list_s c_ipmi_sensor_list_t;
43
44 struct c_ipmi_sensor_list_s {
45   ipmi_sensor_id_t sensor_id;
46   char sensor_name[DATA_MAX_NAME_LEN];
47   char sensor_type[DATA_MAX_NAME_LEN];
48   int sensor_not_present;
49   c_ipmi_sensor_list_t *next;
50 };
51
52 /*
53  * Module global variables
54  */
55 static pthread_mutex_t sensor_list_lock = PTHREAD_MUTEX_INITIALIZER;
56 static c_ipmi_sensor_list_t *sensor_list = NULL;
57
58 static int c_ipmi_init_in_progress = 0;
59 static int c_ipmi_active = 0;
60 static pthread_t thread_id = (pthread_t)0;
61
62 static const char *config_keys[] = {"Sensor",
63                                     "IgnoreSelected",
64                                     "NotifySensorAdd",
65                                     "NotifySensorRemove",
66                                     "NotifySensorNotPresent",
67                                     "SELEnabled",
68                                     "SELClearEvent"};
69 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
70
71 static ignorelist_t *ignorelist = NULL;
72
73 static _Bool c_ipmi_notify_add = 0;
74 static _Bool c_ipmi_notify_remove = 0;
75 static _Bool c_ipmi_notify_notpresent = 0;
76 static _Bool c_ipmi_sel_enabled = 0;
77 static _Bool c_ipmi_sel_clear_event = 0;
78
79 /*
80  * Misc private functions
81  */
82 static void c_ipmi_error(const char *func, int status) {
83   char errbuf[4096] = {0};
84
85   if (IPMI_IS_OS_ERR(status)) {
86     sstrerror(IPMI_GET_OS_ERR(status), errbuf, sizeof(errbuf));
87   } else if (IPMI_IS_IPMI_ERR(status)) {
88     ipmi_get_error_string(IPMI_GET_IPMI_ERR(status), errbuf, sizeof(errbuf));
89   }
90
91   if (errbuf[0] == 0) {
92     snprintf(errbuf, sizeof(errbuf), "Unknown error %#x", status);
93   }
94   errbuf[sizeof(errbuf) - 1] = 0;
95
96   ERROR("ipmi plugin: %s failed: %s", func, errbuf);
97 } /* void c_ipmi_error */
98
99 /*
100  * Sensor handlers
101  */
102 /* Prototype for sensor_list_remove, so sensor_read_handler can call it. */
103 static int sensor_list_remove(ipmi_sensor_t *sensor);
104
105 static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
106                                 enum ipmi_value_present_e value_present,
107                                 unsigned int __attribute__((unused)) raw_value,
108                                 double value,
109                                 ipmi_states_t __attribute__((unused)) * states,
110                                 void *user_data) {
111   value_list_t vl = VALUE_LIST_INIT;
112
113   c_ipmi_sensor_list_t *list_item = (c_ipmi_sensor_list_t *)user_data;
114
115   if (err != 0) {
116     if ((err & 0xff) == IPMI_NOT_PRESENT_CC) {
117       if (list_item->sensor_not_present == 0) {
118         list_item->sensor_not_present = 1;
119
120         INFO("ipmi plugin: sensor_read_handler: sensor %s "
121              "not present.",
122              list_item->sensor_name);
123
124         if (c_ipmi_notify_notpresent) {
125           notification_t n = {
126               NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "", NULL};
127
128           sstrncpy(n.host, hostname_g, sizeof(n.host));
129           sstrncpy(n.type_instance, list_item->sensor_name,
130                    sizeof(n.type_instance));
131           sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
132           snprintf(n.message, sizeof(n.message), "sensor %s not present",
133                    list_item->sensor_name);
134
135           plugin_dispatch_notification(&n);
136         }
137       }
138     } else if (IPMI_IS_IPMI_ERR(err) &&
139                IPMI_GET_IPMI_ERR(err) ==
140                    IPMI_NOT_SUPPORTED_IN_PRESENT_STATE_CC) {
141       INFO("ipmi plugin: sensor_read_handler: Sensor %s not ready",
142            list_item->sensor_name);
143     } else {
144       if (IPMI_IS_IPMI_ERR(err))
145         INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
146              "because it failed with IPMI error %#x.",
147              list_item->sensor_name, IPMI_GET_IPMI_ERR(err));
148       else if (IPMI_IS_OS_ERR(err))
149         INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
150              "because it failed with OS error %#x.",
151              list_item->sensor_name, IPMI_GET_OS_ERR(err));
152       else if (IPMI_IS_RMCPP_ERR(err))
153         INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
154              "because it failed with RMCPP error %#x.",
155              list_item->sensor_name, IPMI_GET_RMCPP_ERR(err));
156       else if (IPMI_IS_SOL_ERR(err))
157         INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
158              "because it failed with RMCPP error %#x.",
159              list_item->sensor_name, IPMI_GET_SOL_ERR(err));
160       else
161         INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
162              "because it failed with error %#x. of class %#x",
163              list_item->sensor_name, err & 0xff, err & 0xffffff00);
164       sensor_list_remove(sensor);
165     }
166     return;
167   } else if (list_item->sensor_not_present == 1) {
168     list_item->sensor_not_present = 0;
169
170     INFO("ipmi plugin: sensor_read_handler: sensor %s present.",
171          list_item->sensor_name);
172
173     if (c_ipmi_notify_notpresent) {
174       notification_t n = {NOTIF_OKAY, cdtime(), "", "",  "ipmi",
175                           "",         "",       "", NULL};
176
177       sstrncpy(n.host, hostname_g, sizeof(n.host));
178       sstrncpy(n.type_instance, list_item->sensor_name,
179                sizeof(n.type_instance));
180       sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
181       snprintf(n.message, sizeof(n.message), "sensor %s present",
182                list_item->sensor_name);
183
184       plugin_dispatch_notification(&n);
185     }
186   }
187
188   if (value_present != IPMI_BOTH_VALUES_PRESENT) {
189     INFO("ipmi plugin: sensor_read_handler: Removing sensor %s, "
190          "because it provides %s. If you need this sensor, "
191          "please file a bug report.",
192          list_item->sensor_name,
193          (value_present == IPMI_RAW_VALUE_PRESENT) ? "only the raw value"
194                                                    : "no value");
195     sensor_list_remove(sensor);
196     return;
197   }
198
199   vl.values = &(value_t){.gauge = value};
200   vl.values_len = 1;
201
202   sstrncpy(vl.plugin, "ipmi", sizeof(vl.plugin));
203   sstrncpy(vl.type, list_item->sensor_type, sizeof(vl.type));
204   sstrncpy(vl.type_instance, list_item->sensor_name, sizeof(vl.type_instance));
205
206   plugin_dispatch_values(&vl);
207 } /* void sensor_read_handler */
208
209 static void sensor_get_name(ipmi_sensor_t *sensor, char *buffer, int buf_len) {
210   char temp[DATA_MAX_NAME_LEN] = {0};
211   ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
212   const char *entity_id_string = ipmi_entity_get_entity_id_string(ent);
213   char sensor_name[DATA_MAX_NAME_LEN] = "";
214   char *sensor_name_ptr;
215
216   if ((buffer == NULL) || (buf_len == 0))
217     return;
218
219   ipmi_sensor_get_name(sensor, temp, sizeof(temp));
220   temp[sizeof(temp) - 1] = 0;
221
222   if (entity_id_string != NULL && strlen(temp))
223     snprintf(sensor_name, sizeof(sensor_name), "%s %s", temp, entity_id_string);
224   else if (entity_id_string != NULL)
225     sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
226   else
227     sstrncpy(sensor_name, temp, sizeof(sensor_name));
228
229   if (strlen(temp)) {
230     sstrncpy(temp, sensor_name, sizeof(temp));
231     sensor_name_ptr = strstr(temp, ").");
232     if (sensor_name_ptr != NULL) {
233       /* If name is something like "foo (123).bar",
234        * change that to "bar (123)".
235        * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
236        * `temp' array, which holds a copy of the current `sensor_name'. */
237       char *sensor_id_ptr;
238
239       /* `sensor_name_ptr' points to ").bar". */
240       sensor_name_ptr[1] = 0;
241       /* `temp' holds "foo (123)\0bar\0". */
242       sensor_name_ptr += 2;
243       /* `sensor_name_ptr' now points to "bar". */
244
245       sensor_id_ptr = strstr(temp, "(");
246       if (sensor_id_ptr != NULL) {
247         /* `sensor_id_ptr' now points to "(123)". */
248         snprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
249                  sensor_id_ptr);
250       }
251       /* else: don't touch sensor_name. */
252     }
253   }
254   sstrncpy(buffer, sensor_name, buf_len);
255 }
256
257 static int sensor_list_add(ipmi_sensor_t *sensor) {
258   ipmi_sensor_id_t sensor_id;
259   c_ipmi_sensor_list_t *list_item;
260   c_ipmi_sensor_list_t *list_prev;
261
262   char buffer[DATA_MAX_NAME_LEN] = {0};
263   char *sensor_name_ptr = buffer;
264   int sensor_type;
265   const char *type;
266
267   sensor_id = ipmi_sensor_convert_to_id(sensor);
268   sensor_get_name(sensor, buffer, sizeof(buffer));
269
270   /* Both `ignorelist' and `plugin_instance' may be NULL. */
271   if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
272     return 0;
273
274   /* FIXME: Use rate unit or base unit to scale the value */
275
276   sensor_type = ipmi_sensor_get_sensor_type(sensor);
277   switch (sensor_type) {
278   case IPMI_SENSOR_TYPE_TEMPERATURE:
279     type = "temperature";
280     break;
281
282   case IPMI_SENSOR_TYPE_VOLTAGE:
283     type = "voltage";
284     break;
285
286   case IPMI_SENSOR_TYPE_CURRENT:
287     type = "current";
288     break;
289
290   case IPMI_SENSOR_TYPE_FAN:
291     type = "fanspeed";
292     break;
293
294   default: {
295     const char *sensor_type_str;
296
297     sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
298     INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
299          "because I don't know how to handle its type (%#x, %s). "
300          "If you need this sensor, please file a bug report.",
301          sensor_name_ptr, sensor_type, sensor_type_str);
302     return -1;
303   }
304   } /* switch (sensor_type) */
305
306   pthread_mutex_lock(&sensor_list_lock);
307
308   list_prev = NULL;
309   for (list_item = sensor_list; list_item != NULL;
310        list_item = list_item->next) {
311     if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
312       break;
313     list_prev = list_item;
314   } /* for (list_item) */
315
316   if (list_item != NULL) {
317     pthread_mutex_unlock(&sensor_list_lock);
318     return 0;
319   }
320
321   list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
322   if (list_item == NULL) {
323     pthread_mutex_unlock(&sensor_list_lock);
324     return -1;
325   }
326
327   list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
328
329   if (list_prev != NULL)
330     list_prev->next = list_item;
331   else
332     sensor_list = list_item;
333
334   sstrncpy(list_item->sensor_name, sensor_name_ptr,
335            sizeof(list_item->sensor_name));
336   sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
337
338   pthread_mutex_unlock(&sensor_list_lock);
339
340   if (c_ipmi_notify_add && (c_ipmi_init_in_progress == 0)) {
341     notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
342
343     sstrncpy(n.host, hostname_g, sizeof(n.host));
344     sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
345     sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
346     snprintf(n.message, sizeof(n.message), "sensor %s added",
347              list_item->sensor_name);
348
349     plugin_dispatch_notification(&n);
350   }
351
352   return 0;
353 } /* int sensor_list_add */
354
355 static int sensor_list_remove(ipmi_sensor_t *sensor) {
356   ipmi_sensor_id_t sensor_id;
357   c_ipmi_sensor_list_t *list_item;
358   c_ipmi_sensor_list_t *list_prev;
359
360   sensor_id = ipmi_sensor_convert_to_id(sensor);
361
362   pthread_mutex_lock(&sensor_list_lock);
363
364   list_prev = NULL;
365   for (list_item = sensor_list; list_item != NULL;
366        list_item = list_item->next) {
367     if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
368       break;
369     list_prev = list_item;
370   } /* for (list_item) */
371
372   if (list_item == NULL) {
373     pthread_mutex_unlock(&sensor_list_lock);
374     return -1;
375   }
376
377   if (list_prev == NULL)
378     sensor_list = list_item->next;
379   else
380     list_prev->next = list_item->next;
381
382   list_prev = NULL;
383   list_item->next = NULL;
384
385   pthread_mutex_unlock(&sensor_list_lock);
386
387   if (c_ipmi_notify_remove && c_ipmi_active) {
388     notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "",
389                         NULL};
390
391     sstrncpy(n.host, hostname_g, sizeof(n.host));
392     sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
393     sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
394     snprintf(n.message, sizeof(n.message), "sensor %s removed",
395              list_item->sensor_name);
396
397     plugin_dispatch_notification(&n);
398   }
399
400   free(list_item);
401   return 0;
402 } /* int sensor_list_remove */
403
404 static int sensor_list_read_all(void) {
405   pthread_mutex_lock(&sensor_list_lock);
406
407   for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
408        list_item = list_item->next) {
409     ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
410                                /* user data = */ list_item);
411   } /* for (list_item) */
412
413   pthread_mutex_unlock(&sensor_list_lock);
414
415   return 0;
416 } /* int sensor_list_read_all */
417
418 static int sensor_list_remove_all(void) {
419   c_ipmi_sensor_list_t *list_item;
420
421   pthread_mutex_lock(&sensor_list_lock);
422
423   list_item = sensor_list;
424   sensor_list = NULL;
425
426   pthread_mutex_unlock(&sensor_list_lock);
427
428   while (list_item != NULL) {
429     c_ipmi_sensor_list_t *list_next = list_item->next;
430
431     free(list_item);
432
433     list_item = list_next;
434   } /* while (list_item) */
435
436   return 0;
437 } /* int sensor_list_remove_all */
438
439 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
440   switch (severity) {
441   case IPMI_LOWER_NON_CRITICAL:
442   case IPMI_UPPER_NON_CRITICAL:
443     return NOTIF_OKAY;
444   case IPMI_LOWER_CRITICAL:
445   case IPMI_UPPER_CRITICAL:
446     return NOTIF_WARNING;
447   case IPMI_LOWER_NON_RECOVERABLE:
448   case IPMI_UPPER_NON_RECOVERABLE:
449     return NOTIF_FAILURE;
450   default:
451     return NOTIF_OKAY;
452   } /* switch (severity) */
453 } /* int sensor_convert_threshold_severity */
454
455 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
456                                   enum ipmi_event_dir_e dir,
457                                   ipmi_event_t *event) {
458   ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
459
460   plugin_notification_meta_add_string(n, "entity_name",
461                                       ipmi_entity_get_entity_id_string(ent));
462   plugin_notification_meta_add_signed_int(n, "entity_id",
463                                           ipmi_entity_get_entity_id(ent));
464   plugin_notification_meta_add_signed_int(n, "entity_instance",
465                                           ipmi_entity_get_entity_instance(ent));
466   plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
467
468   if (event)
469     plugin_notification_meta_add_signed_int(n, "event_type",
470                                             ipmi_event_get_type(event));
471 } /* void add_event_sensor_meta_data */
472
473 static int sensor_threshold_event_handler(
474     ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
475     enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
476     enum ipmi_value_present_e value_present, unsigned int raw_value,
477     double value, void *cb_data, ipmi_event_t *event) {
478
479   /* From the IPMI specification Chapter 2: Events.
480    * If a callback handles the event, then all future callbacks called due to
481    * the event will receive a NULL for the event. So be ready to handle a NULL
482    * event in all your event handlers. A NULL may also be passed to an event
483    * handler if the callback was not due to an event. */
484   if (event == NULL)
485     return IPMI_EVENT_NOT_HANDLED;
486
487   /* offset is a table index and it's represented as enum of strings that are
488      organized in the way - high and low for each threshold severity level */
489   notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
490   unsigned int offset = (2 * threshold) + high_low;
491   unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
492   unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
493   const char *event_state =
494       ipmi_get_reading_name(event_type, sensor_type, offset);
495   sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
496   if (value_present != IPMI_NO_VALUES_PRESENT)
497     snprintf(n.message, sizeof(n.message),
498              "sensor %s received event: %s, value is %f", n.type_instance,
499              event_state, value);
500   else
501     snprintf(n.message, sizeof(n.message),
502              "sensor %s received event: %s, value not provided",
503              n.type_instance, event_state);
504
505   DEBUG("Threshold event received for sensor %s", n.type_instance);
506
507   sstrncpy(n.host, hostname_g, sizeof(n.host));
508   sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
509   n.severity = sensor_convert_threshold_severity(threshold);
510   n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
511
512   plugin_notification_meta_add_string(&n, "severity",
513                                       ipmi_get_threshold_string(threshold));
514   plugin_notification_meta_add_string(&n, "direction",
515                                       ipmi_get_value_dir_string(high_low));
516
517   switch (value_present) {
518   case IPMI_BOTH_VALUES_PRESENT:
519     plugin_notification_meta_add_double(&n, "val", value);
520   /* both values present, so fall-through to add raw value too */
521   case IPMI_RAW_VALUE_PRESENT: {
522     char buf[DATA_MAX_NAME_LEN] = {0};
523     snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
524     plugin_notification_meta_add_string(&n, "raw", buf);
525   } break;
526   default:
527     break;
528   } /* switch (value_present) */
529
530   add_event_common_data(&n, sensor, dir, event);
531
532   plugin_dispatch_notification(&n);
533   plugin_notification_meta_free(n.meta);
534
535   /* Delete handled ipmi event from the list */
536   if (c_ipmi_sel_clear_event) {
537     ipmi_event_delete(event, NULL, NULL);
538     return IPMI_EVENT_HANDLED;
539   }
540
541   return IPMI_EVENT_NOT_HANDLED;
542 } /* int sensor_threshold_event_handler */
543
544 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
545                                          enum ipmi_event_dir_e dir, int offset,
546                                          int severity, int prev_severity,
547                                          void *cb_data, ipmi_event_t *event) {
548   /* From the IPMI specification Chapter 2: Events.
549    * If a callback handles the event, then all future callbacks called due to
550    * the event will receive a NULL for the event. So be ready to handle a NULL
551    * event in all your event handlers. A NULL may also be passed to an event
552    * handler if the callback was not due to an event. */
553   if (event == NULL)
554     return IPMI_EVENT_NOT_HANDLED;
555
556   notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
557   unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
558   unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
559   const char *event_state =
560       ipmi_get_reading_name(event_type, sensor_type, offset);
561   sensor_get_name(sensor, n.type_instance, sizeof(n.type_instance));
562   snprintf(n.message, sizeof(n.message), "sensor %s received event: %s",
563            n.type_instance, event_state);
564
565   DEBUG("Discrete event received for sensor %s", n.type_instance);
566
567   sstrncpy(n.host, hostname_g, sizeof(n.host));
568   sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
569   n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
570
571   plugin_notification_meta_add_signed_int(&n, "offset", offset);
572
573   if (severity != -1)
574     plugin_notification_meta_add_signed_int(&n, "severity", severity);
575
576   if (prev_severity != -1)
577     plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
578
579   add_event_common_data(&n, sensor, dir, event);
580
581   plugin_dispatch_notification(&n);
582   plugin_notification_meta_free(n.meta);
583
584   /* Delete handled ipmi event from the list */
585   if (c_ipmi_sel_clear_event) {
586     ipmi_event_delete(event, NULL, NULL);
587     return IPMI_EVENT_HANDLED;
588   }
589
590   return IPMI_EVENT_NOT_HANDLED;
591 } /* int sensor_discrete_event_handler */
592
593 /*
594  * Entity handlers
595  */
596 static void entity_sensor_update_handler(
597     enum ipmi_update_e op, ipmi_entity_t __attribute__((unused)) * entity,
598     ipmi_sensor_t *sensor, void __attribute__((unused)) * user_data) {
599   /* TODO: Ignore sensors we cannot read */
600
601   if ((op == IPMI_ADDED) || (op == IPMI_CHANGED)) {
602     /* Will check for duplicate entries.. */
603     sensor_list_add(sensor);
604
605     if (c_ipmi_sel_enabled) {
606       int status = 0;
607       /* register threshold event if threshold sensor support events */
608       if ((ipmi_sensor_get_event_reading_type(sensor) ==
609            IPMI_EVENT_READING_TYPE_THRESHOLD) &&
610           (ipmi_sensor_get_threshold_access(sensor) !=
611            IPMI_THRESHOLD_ACCESS_SUPPORT_NONE))
612         status = ipmi_sensor_add_threshold_event_handler(
613             sensor, sensor_threshold_event_handler, NULL);
614       /* register discrete handler if discrete/specific sensor support events */
615       else if (ipmi_sensor_get_event_support(sensor) != IPMI_EVENT_SUPPORT_NONE)
616         status = ipmi_sensor_add_discrete_event_handler(
617             sensor, sensor_discrete_event_handler, NULL);
618
619       if (status) {
620         char buf[DATA_MAX_NAME_LEN] = {0};
621         sensor_get_name(sensor, buf, sizeof(buf));
622         ERROR("Unable to add sensor %s event handler, status: %d", buf, status);
623       }
624     }
625   } else if (op == IPMI_DELETED) {
626     sensor_list_remove(sensor);
627
628     if (c_ipmi_sel_enabled) {
629       if (ipmi_sensor_get_event_reading_type(sensor) ==
630           IPMI_EVENT_READING_TYPE_THRESHOLD)
631         ipmi_sensor_remove_threshold_event_handler(
632             sensor, sensor_threshold_event_handler, NULL);
633       else
634         ipmi_sensor_remove_discrete_event_handler(
635             sensor, sensor_discrete_event_handler, NULL);
636     }
637   }
638 } /* void entity_sensor_update_handler */
639
640 /*
641  * Domain handlers
642  */
643 static void domain_entity_update_handler(
644     enum ipmi_update_e op, ipmi_domain_t __attribute__((unused)) * domain,
645     ipmi_entity_t *entity, void __attribute__((unused)) * user_data) {
646   int status;
647
648   if (op == IPMI_ADDED) {
649     status = ipmi_entity_add_sensor_update_handler(
650         entity, entity_sensor_update_handler, /* user data = */ NULL);
651     if (status != 0) {
652       c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
653     }
654   } else if (op == IPMI_DELETED) {
655     status = ipmi_entity_remove_sensor_update_handler(
656         entity, entity_sensor_update_handler, /* user data = */ NULL);
657     if (status != 0) {
658       c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
659     }
660   }
661 } /* void domain_entity_update_handler */
662
663 static void smi_event_handler(ipmi_con_t __attribute__((unused)) * ipmi,
664                               const ipmi_addr_t __attribute__((unused)) * addr,
665                               unsigned int __attribute__((unused)) addr_len,
666                               ipmi_event_t *event, void *cb_data) {
667   unsigned int type = ipmi_event_get_type(event);
668   ipmi_domain_t *domain = cb_data;
669
670   DEBUG("%s: Event received: type %u", __FUNCTION__, type);
671
672   if (type != 0x02)
673     /* It's not a standard IPMI event. */
674     return;
675
676   /* force domain to reread SELs */
677   ipmi_domain_reread_sels(domain, NULL, NULL);
678 }
679
680 static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
681                                              unsigned int conn_num,
682                                              unsigned int port_num,
683                                              int still_connected,
684                                              void *user_data) {
685   int status;
686
687   DEBUG("domain_connection_change_handler (domain = %p, err = %i, "
688         "conn_num = %u, port_num = %u, still_connected = %i, "
689         "user_data = %p);\n",
690         (void *)domain, err, conn_num, port_num, still_connected, user_data);
691
692   status = ipmi_domain_add_entity_update_handler(
693       domain, domain_entity_update_handler, /* user data = */ NULL);
694   if (status != 0) {
695     c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
696   }
697
698   ipmi_con_t *smi_connection = user_data;
699   status = smi_connection->add_event_handler(smi_connection, smi_event_handler,
700                                              (void *)domain);
701
702   if (status != 0)
703     c_ipmi_error("Failed to register smi event handler", status);
704 } /* void domain_connection_change_handler */
705
706 static int thread_init(os_handler_t **ret_os_handler) {
707   os_handler_t *os_handler;
708   ipmi_con_t *smi_connection = NULL;
709   ipmi_domain_id_t domain_id;
710   int status;
711
712   os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
713   if (os_handler == NULL) {
714     ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
715     return -1;
716   }
717
718   ipmi_init(os_handler);
719
720   status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
721                               /* user data = */ NULL, &smi_connection);
722   if (status != 0) {
723     c_ipmi_error("ipmi_smi_setup_con", status);
724     return -1;
725   }
726
727   ipmi_open_option_t open_option[1] = {[0] = {.option = IPMI_OPEN_OPTION_ALL,
728                                               {.ival = 1}}};
729
730   status = ipmi_open_domain(
731       "mydomain", &smi_connection, /* num_con = */ 1,
732       domain_connection_change_handler,
733       /* user data = */ (void *)smi_connection,
734       /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
735       sizeof(open_option) / sizeof(open_option[0]), &domain_id);
736   if (status != 0) {
737     c_ipmi_error("ipmi_open_domain", status);
738     return -1;
739   }
740
741   *ret_os_handler = os_handler;
742   return 0;
743 } /* int thread_init */
744
745 static void *thread_main(void __attribute__((unused)) * user_data) {
746   int status;
747   os_handler_t *os_handler = NULL;
748
749   status = thread_init(&os_handler);
750   if (status != 0) {
751     ERROR("ipmi plugin: thread_init failed.\n");
752     return (void *)-1;
753   }
754
755   while (c_ipmi_active != 0) {
756     struct timeval tv = {1, 0};
757     os_handler->perform_one_op(os_handler, &tv);
758   }
759
760   ipmi_posix_thread_free_os_handler(os_handler);
761
762   return (void *)0;
763 } /* void *thread_main */
764
765 static int c_ipmi_config(const char *key, const char *value) {
766   if (ignorelist == NULL)
767     ignorelist = ignorelist_create(/* invert = */ 1);
768   if (ignorelist == NULL)
769     return 1;
770
771   if (strcasecmp("Sensor", key) == 0) {
772     ignorelist_add(ignorelist, value);
773   } else if (strcasecmp("IgnoreSelected", key) == 0) {
774     ignorelist_set_invert(ignorelist, !IS_TRUE(value));
775   } else if (strcasecmp("NotifySensorAdd", key) == 0) {
776     c_ipmi_notify_add = IS_TRUE(value);
777   } else if (strcasecmp("NotifySensorRemove", key) == 0) {
778     c_ipmi_notify_remove = IS_TRUE(value);
779   } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
780     c_ipmi_notify_notpresent = IS_TRUE(value);
781   } else if (strcasecmp("SELEnabled", key) == 0) {
782     c_ipmi_sel_enabled = IS_TRUE(value);
783   } else if (strcasecmp("SELClearEvent", key) == 0) {
784     c_ipmi_sel_clear_event = IS_TRUE(value);
785   } else {
786     return -1;
787   }
788
789   return 0;
790 } /* int c_ipmi_config */
791
792 static int c_ipmi_init(void) {
793   int status;
794
795   /* Don't send `ADD' notifications during startup (~ 1 minute) */
796   time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
797   c_ipmi_init_in_progress = 1 + (60 / iv);
798
799   c_ipmi_active = 1;
800
801   status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
802                                 /* user data = */ NULL, "ipmi");
803   if (status != 0) {
804     c_ipmi_active = 0;
805     thread_id = (pthread_t)0;
806     ERROR("ipmi plugin: pthread_create failed.");
807     return -1;
808   }
809
810   return 0;
811 } /* int c_ipmi_init */
812
813 static int c_ipmi_read(void) {
814   if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
815     INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
816     return -1;
817   }
818
819   sensor_list_read_all();
820
821   if (c_ipmi_init_in_progress > 0)
822     c_ipmi_init_in_progress--;
823   else
824     c_ipmi_init_in_progress = 0;
825
826   return 0;
827 } /* int c_ipmi_read */
828
829 static int c_ipmi_shutdown(void) {
830   c_ipmi_active = 0;
831
832   if (thread_id != (pthread_t)0) {
833     pthread_join(thread_id, NULL);
834     thread_id = (pthread_t)0;
835   }
836
837   sensor_list_remove_all();
838
839   return 0;
840 } /* int c_ipmi_shutdown */
841
842 void module_register(void) {
843   plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
844   plugin_register_init("ipmi", c_ipmi_init);
845   plugin_register_read("ipmi", c_ipmi_read);
846   plugin_register_shutdown("ipmi", c_ipmi_shutdown);
847 } /* void module_register */