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