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