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