ipmi: force domain to reread SELs when event is received on SMI.
[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 int c_ipmi_notify_add = 0;
71 static int c_ipmi_notify_remove = 0;
72 static int c_ipmi_notify_notpresent = 0;
73 static int c_ipmi_sel_enabled = 0;
74 static int 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 *buf, int buf_len) {
206   char buffer[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   ipmi_sensor_get_name(sensor, buffer, sizeof(buffer));
213   buffer[sizeof(buffer) - 1] = 0;
214
215   if (entity_id_string != NULL && strlen(buffer))
216     ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", buffer,
217               entity_id_string);
218   else if (entity_id_string != NULL)
219     sstrncpy(sensor_name, entity_id_string, sizeof(sensor_name));
220   else
221     sstrncpy(sensor_name, buffer, sizeof(sensor_name));
222
223   if (strlen(buffer)) {
224     sstrncpy(buffer, sensor_name, sizeof(buffer));
225     sensor_name_ptr = strstr(buffer, ").");
226     if (sensor_name_ptr != NULL) {
227       /* If name is something like "foo (123).bar",
228        * change that to "bar (123)".
229        * Both, sensor_name_ptr and sensor_id_ptr point to memory within the
230        * `buffer' array, which holds a copy of the current `sensor_name'. */
231       char *sensor_id_ptr;
232
233       /* `sensor_name_ptr' points to ").bar". */
234       sensor_name_ptr[1] = 0;
235       /* `buffer' holds "foo (123)\0bar\0". */
236       sensor_name_ptr += 2;
237       /* `sensor_name_ptr' now points to "bar". */
238
239       sensor_id_ptr = strstr(buffer, "(");
240       if (sensor_id_ptr != NULL) {
241         /* `sensor_id_ptr' now points to "(123)". */
242         ssnprintf(sensor_name, sizeof(sensor_name), "%s %s", sensor_name_ptr,
243                   sensor_id_ptr);
244       }
245       /* else: don't touch sensor_name. */
246     }
247   }
248
249   assert(buf != NULL);
250   sstrncpy(buf, sensor_name, buf_len);
251 }
252
253 static int sensor_list_add(ipmi_sensor_t *sensor) {
254   ipmi_sensor_id_t sensor_id;
255   c_ipmi_sensor_list_t *list_item;
256   c_ipmi_sensor_list_t *list_prev;
257
258   char buffer[DATA_MAX_NAME_LEN] = {0};
259   char *sensor_name_ptr = buffer;
260   int sensor_type;
261   const char *type;
262
263   sensor_id = ipmi_sensor_convert_to_id(sensor);
264   sensor_get_name(sensor, buffer, sizeof(buffer));
265
266   /* Both `ignorelist' and `plugin_instance' may be NULL. */
267   if (ignorelist_match(ignorelist, sensor_name_ptr) != 0)
268     return (0);
269
270   /* FIXME: Use rate unit or base unit to scale the value */
271
272   sensor_type = ipmi_sensor_get_sensor_type(sensor);
273   switch (sensor_type) {
274   case IPMI_SENSOR_TYPE_TEMPERATURE:
275     type = "temperature";
276     break;
277
278   case IPMI_SENSOR_TYPE_VOLTAGE:
279     type = "voltage";
280     break;
281
282   case IPMI_SENSOR_TYPE_CURRENT:
283     type = "current";
284     break;
285
286   case IPMI_SENSOR_TYPE_FAN:
287     type = "fanspeed";
288     break;
289
290   default: {
291     const char *sensor_type_str;
292
293     sensor_type_str = ipmi_sensor_get_sensor_type_string(sensor);
294     INFO("ipmi plugin: sensor_list_add: Ignore sensor %s, "
295          "because I don't know how to handle its type (%#x, %s). "
296          "If you need this sensor, please file a bug report.",
297          sensor_name_ptr, sensor_type, sensor_type_str);
298     return (-1);
299   }
300   } /* switch (sensor_type) */
301
302   pthread_mutex_lock(&sensor_list_lock);
303
304   list_prev = NULL;
305   for (list_item = sensor_list; list_item != NULL;
306        list_item = list_item->next) {
307     if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
308       break;
309     list_prev = list_item;
310   } /* for (list_item) */
311
312   if (list_item != NULL) {
313     pthread_mutex_unlock(&sensor_list_lock);
314     return (0);
315   }
316
317   list_item = (c_ipmi_sensor_list_t *)calloc(1, sizeof(c_ipmi_sensor_list_t));
318   if (list_item == NULL) {
319     pthread_mutex_unlock(&sensor_list_lock);
320     return (-1);
321   }
322
323   list_item->sensor_id = ipmi_sensor_convert_to_id(sensor);
324
325   if (list_prev != NULL)
326     list_prev->next = list_item;
327   else
328     sensor_list = list_item;
329
330   sstrncpy(list_item->sensor_name, sensor_name_ptr,
331            sizeof(list_item->sensor_name));
332   sstrncpy(list_item->sensor_type, type, sizeof(list_item->sensor_type));
333
334   pthread_mutex_unlock(&sensor_list_lock);
335
336   if (c_ipmi_notify_add && (c_ipmi_init_in_progress == 0)) {
337     notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
338
339     sstrncpy(n.host, hostname_g, sizeof(n.host));
340     sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
341     sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
342     ssnprintf(n.message, sizeof(n.message), "sensor %s added",
343               list_item->sensor_name);
344
345     plugin_dispatch_notification(&n);
346   }
347
348   return (0);
349 } /* int sensor_list_add */
350
351 static int sensor_list_remove(ipmi_sensor_t *sensor) {
352   ipmi_sensor_id_t sensor_id;
353   c_ipmi_sensor_list_t *list_item;
354   c_ipmi_sensor_list_t *list_prev;
355
356   sensor_id = ipmi_sensor_convert_to_id(sensor);
357
358   pthread_mutex_lock(&sensor_list_lock);
359
360   list_prev = NULL;
361   for (list_item = sensor_list; list_item != NULL;
362        list_item = list_item->next) {
363     if (ipmi_cmp_sensor_id(sensor_id, list_item->sensor_id) == 0)
364       break;
365     list_prev = list_item;
366   } /* for (list_item) */
367
368   if (list_item == NULL) {
369     pthread_mutex_unlock(&sensor_list_lock);
370     return (-1);
371   }
372
373   if (list_prev == NULL)
374     sensor_list = list_item->next;
375   else
376     list_prev->next = list_item->next;
377
378   list_prev = NULL;
379   list_item->next = NULL;
380
381   pthread_mutex_unlock(&sensor_list_lock);
382
383   if (c_ipmi_notify_remove && c_ipmi_active) {
384     notification_t n = {NOTIF_WARNING, cdtime(), "", "",  "ipmi",
385                         "",            "",       "", NULL};
386
387     sstrncpy(n.host, hostname_g, sizeof(n.host));
388     sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
389     sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
390     ssnprintf(n.message, sizeof(n.message), "sensor %s removed",
391               list_item->sensor_name);
392
393     plugin_dispatch_notification(&n);
394   }
395
396   free(list_item);
397   return (0);
398 } /* int sensor_list_remove */
399
400 static int sensor_list_read_all(void) {
401   pthread_mutex_lock(&sensor_list_lock);
402
403   for (c_ipmi_sensor_list_t *list_item = sensor_list; list_item != NULL;
404        list_item = list_item->next) {
405     ipmi_sensor_id_get_reading(list_item->sensor_id, sensor_read_handler,
406                                /* user data = */ list_item);
407   } /* for (list_item) */
408
409   pthread_mutex_unlock(&sensor_list_lock);
410
411   return (0);
412 } /* int sensor_list_read_all */
413
414 static int sensor_list_remove_all(void) {
415   c_ipmi_sensor_list_t *list_item;
416
417   pthread_mutex_lock(&sensor_list_lock);
418
419   list_item = sensor_list;
420   sensor_list = NULL;
421
422   pthread_mutex_unlock(&sensor_list_lock);
423
424   while (list_item != NULL) {
425     c_ipmi_sensor_list_t *list_next = list_item->next;
426
427     free(list_item);
428
429     list_item = list_next;
430   } /* while (list_item) */
431
432   return (0);
433 } /* int sensor_list_remove_all */
434
435 static int sensor_convert_threshold_severity(enum ipmi_thresh_e severity) {
436   int _severity = NOTIF_OKAY;
437
438   switch (severity) {
439   case IPMI_LOWER_NON_CRITICAL:
440   case IPMI_UPPER_NON_CRITICAL:
441     _severity = NOTIF_OKAY;
442     break;
443   case IPMI_LOWER_CRITICAL:
444   case IPMI_UPPER_CRITICAL:
445     _severity = NOTIF_WARNING;
446     break;
447   case IPMI_LOWER_NON_RECOVERABLE:
448   case IPMI_UPPER_NON_RECOVERABLE:
449     _severity = NOTIF_FAILURE;
450     break;
451   default:
452     break;
453   } /* switch (severity) */
454
455   return (_severity);
456 } /* int sensor_convert_threshold_severity */
457
458 static void add_event_common_data(notification_t *n, ipmi_sensor_t *sensor,
459                                   enum ipmi_event_dir_e dir,
460                                   ipmi_event_t *event) {
461   ipmi_entity_t *ent = ipmi_sensor_get_entity(sensor);
462
463   plugin_notification_meta_add_string(n, "entity_name",
464                                       ipmi_entity_get_entity_id_string(ent));
465   plugin_notification_meta_add_signed_int(n, "entity_id",
466                                           ipmi_entity_get_entity_id(ent));
467   plugin_notification_meta_add_signed_int(n, "entity_instance",
468                                           ipmi_entity_get_entity_instance(ent));
469   plugin_notification_meta_add_boolean(n, "assert", dir == IPMI_ASSERTION);
470
471   if (event)
472     plugin_notification_meta_add_signed_int(n, "event_type",
473                                             ipmi_event_get_type(event));
474 } /* void add_event_sensor_meta_data */
475
476 static int sensor_threshold_event_handler(
477     ipmi_sensor_t *sensor, enum ipmi_event_dir_e dir,
478     enum ipmi_thresh_e threshold, enum ipmi_event_value_dir_e high_low,
479     enum ipmi_value_present_e value_present, unsigned int raw_value,
480     double value, void *cb_data, ipmi_event_t *event) {
481   notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
482   /* offset is a table index and it's represented as enum of strings that are
483      organized in the way - high and low for each threshold severity level */
484   unsigned int offset = (2 * threshold) + high_low;
485   unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
486   unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
487   const char *event_state =
488       ipmi_get_reading_name(event_type, sensor_type, offset);
489   char buf[DATA_MAX_NAME_LEN] = {0};
490
491   /* From the IPMI specification Chapter 2: Events.
492    * If a callback handles the event, then all future callbacks called due to
493    * the event will receive a NULL for the event. So be ready to handle a NULL
494    * event in all your event handlers. A NULL may also be passed to an event
495    * handler if the callback was not due to an event. */
496   if (event == NULL)
497     return (IPMI_EVENT_NOT_HANDLED);
498
499   sensor_get_name(sensor, buf, sizeof(buf));
500   sstrncpy(n.type_instance, buf, sizeof(n.type_instance));
501   if (value_present != IPMI_NO_VALUES_PRESENT)
502     ssnprintf(n.message, sizeof(n.message),
503               "sensor %s received event: %s, value is %f", buf, event_state,
504               value);
505   else
506     ssnprintf(n.message, sizeof(n.message),
507               "sensor %s received event: %s, value not provided", buf,
508               event_state);
509
510   DEBUG("Threshold event received for sensor %s", buf);
511
512   sstrncpy(n.host, hostname_g, sizeof(n.host));
513   sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
514   n.severity = sensor_convert_threshold_severity(threshold);
515   n.time = ipmi_event_get_timestamp(event);
516
517   plugin_notification_meta_add_string(&n, "severity",
518                                       ipmi_get_threshold_string(threshold));
519   plugin_notification_meta_add_string(&n, "direction",
520                                       ipmi_get_value_dir_string(high_low));
521
522   switch (value_present) {
523   case IPMI_BOTH_VALUES_PRESENT:
524     plugin_notification_meta_add_double(&n, "val", value);
525   case IPMI_RAW_VALUE_PRESENT:
526     snprintf(buf, sizeof(buf), "0x%2.2x", raw_value);
527     plugin_notification_meta_add_string(&n, "raw", buf);
528     break;
529   default:
530     break;
531   } /* switch (value_present) */
532
533   add_event_common_data(&n, sensor, dir, event);
534
535   plugin_dispatch_notification(&n);
536
537   /* Delete handled ipmi event from the list */
538   if (c_ipmi_sel_clear_event) {
539     ipmi_event_delete(event, NULL, NULL);
540     return (IPMI_EVENT_HANDLED);
541   }
542
543   return (IPMI_EVENT_NOT_HANDLED);
544 } /* int sensor_threshold_event_handler */
545
546 static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
547                                          enum ipmi_event_dir_e dir, int offset,
548                                          int severity, int prev_severity,
549                                          void *cb_data, ipmi_event_t *event) {
550   notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
551   unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
552   unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
553   const char *event_state =
554       ipmi_get_reading_name(event_type, sensor_type, offset);
555   char buf[DATA_MAX_NAME_LEN] = {0};
556
557   /* From the IPMI specification Chapter 2: Events.
558    * If a callback handles the event, then all future callbacks called due to
559    * the event will receive a NULL for the event. So be ready to handle a NULL
560    * event in all your event handlers. A NULL may also be passed to an event 
561    * handler if the callback was not due to an event. */
562   if (event == NULL)
563     return (IPMI_EVENT_NOT_HANDLED);
564
565   sensor_get_name(sensor, buf, sizeof(buf));
566   sstrncpy(n.type_instance, buf, sizeof(n.type_instance));
567   ssnprintf(n.message, sizeof(n.message), "sensor %s received event: %s", buf,
568             event_state);
569
570   DEBUG("Discrete event received for sensor %s", buf);
571
572   sstrncpy(n.host, hostname_g, sizeof(n.host));
573   sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
574   n.time = ipmi_event_get_timestamp(event);
575
576   plugin_notification_meta_add_signed_int(&n, "offset", offset);
577
578   if (severity != -1)
579     plugin_notification_meta_add_signed_int(&n, "severity", severity);
580
581   if (prev_severity != -1)
582     plugin_notification_meta_add_signed_int(&n, "prevseverity", prev_severity);
583
584   add_event_common_data(&n, sensor, dir, event);
585
586   plugin_dispatch_notification(&n);
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     int invert = 1;
781     if (IS_TRUE(value))
782       invert = 0;
783     ignorelist_set_invert(ignorelist, invert);
784   } else if (strcasecmp("NotifySensorAdd", key) == 0) {
785     if (IS_TRUE(value))
786       c_ipmi_notify_add = 1;
787   } else if (strcasecmp("NotifySensorRemove", key) == 0) {
788     if (IS_TRUE(value))
789       c_ipmi_notify_remove = 1;
790   } else if (strcasecmp("NotifySensorNotPresent", key) == 0) {
791     if (IS_TRUE(value))
792       c_ipmi_notify_notpresent = 1;
793   } else if (strcasecmp("SELEnabled", key) == 0) {
794     if (IS_TRUE(value))
795       c_ipmi_sel_enabled = 1;
796   } else if (strcasecmp("SELClearEvent", key) == 0) {
797     if (IS_TRUE(value))
798       c_ipmi_sel_clear_event = 1;
799   } else {
800     return (-1);
801   }
802
803   return (0);
804 } /* int c_ipmi_config */
805
806 static int c_ipmi_init(void) {
807   int status;
808
809   /* Don't send `ADD' notifications during startup (~ 1 minute) */
810   time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
811   c_ipmi_init_in_progress = 1 + (60 / iv);
812
813   c_ipmi_active = 1;
814
815   status = plugin_thread_create(&thread_id, /* attr = */ NULL, thread_main,
816                                 /* user data = */ NULL, "ipmi");
817   if (status != 0) {
818     c_ipmi_active = 0;
819     thread_id = (pthread_t)0;
820     ERROR("ipmi plugin: pthread_create failed.");
821     return (-1);
822   }
823
824   return (0);
825 } /* int c_ipmi_init */
826
827 static int c_ipmi_read(void) {
828   if ((c_ipmi_active == 0) || (thread_id == (pthread_t)0)) {
829     INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
830     return (-1);
831   }
832
833   sensor_list_read_all();
834
835   if (c_ipmi_init_in_progress > 0)
836     c_ipmi_init_in_progress--;
837   else
838     c_ipmi_init_in_progress = 0;
839
840   return (0);
841 } /* int c_ipmi_read */
842
843 static int c_ipmi_shutdown(void) {
844   c_ipmi_active = 0;
845
846   if (thread_id != (pthread_t)0) {
847     pthread_join(thread_id, NULL);
848     thread_id = (pthread_t)0;
849   }
850
851   sensor_list_remove_all();
852
853   return (0);
854 } /* int c_ipmi_shutdown */
855
856 void module_register(void) {
857   plugin_register_config("ipmi", c_ipmi_config, config_keys, config_keys_num);
858   plugin_register_init("ipmi", c_ipmi_init);
859   plugin_register_read("ipmi", c_ipmi_read);
860   plugin_register_shutdown("ipmi", c_ipmi_shutdown);
861 } /* void module_register */