ipmi plugin: Refactored to use c_ipmi_notification_init_()
[collectd.git] / src / ipmi.c
index 7337dc4..2d859cb 100644 (file)
@@ -38,6 +38,8 @@
 #include <OpenIPMI/ipmi_smi.h>
 #include <OpenIPMI/ipmiif.h>
 
+#define ERR_BUF_SIZE 1024
+
 /*
  * Private data types
  */
@@ -50,6 +52,7 @@ struct c_ipmi_instance_s {
   _Bool notify_add;
   _Bool notify_remove;
   _Bool notify_notpresent;
+  _Bool notify_conn;
   _Bool sel_enabled;
   _Bool sel_clear_event;
 
@@ -59,6 +62,7 @@ struct c_ipmi_instance_s {
   char *password;
   unsigned int authtype;
 
+  _Bool connected;
   ipmi_con_t *connection;
   pthread_mutex_t sensor_list_lock;
   c_ipmi_sensor_list_t *sensor_list;
@@ -84,14 +88,14 @@ struct c_ipmi_sensor_list_s {
 /*
  * Module global variables
  */
-static os_handler_t *os_handler;
+static os_handler_t *os_handler = NULL;
 static c_ipmi_instance_t *instances = NULL;
 
 /*
  * Misc private functions
  */
-static void c_ipmi_error(const char *func, int status) {
-  char errbuf[4096] = {0};
+static void c_ipmi_error(c_ipmi_instance_t *st, const char *func, int status) {
+  char errbuf[ERR_BUF_SIZE] = {0};
 
   if (IPMI_IS_OS_ERR(status) || IPMI_IS_RMCPP_ERR(status) ||
       IPMI_IS_IPMI_ERR(status)) {
@@ -103,12 +107,12 @@ static void c_ipmi_error(const char *func, int status) {
   }
   errbuf[sizeof(errbuf) - 1] = 0;
 
-  ERROR("ipmi plugin: %s failed: %s", func, errbuf);
+  ERROR("ipmi plugin: %s failed for `%s`: %s", func, st->name, errbuf);
 } /* void c_ipmi_error */
 
 static void c_ipmi_log(os_handler_t *handler, const char *format,
                        enum ipmi_log_type_e log_type, va_list ap) {
-  char msg[1024];
+  char msg[ERR_BUF_SIZE];
 
   vsnprintf(msg, sizeof(msg), format, ap);
 
@@ -131,11 +135,11 @@ static void c_ipmi_log(os_handler_t *handler, const char *format,
 #if COLLECT_DEBUG
   case IPMI_LOG_DEBUG_START:
   case IPMI_LOG_DEBUG:
-    fprintf(stderr, "ipmi plugin: %s\n", msg);
+    DEBUG("ipmi plugin: %s", msg);
     break;
   case IPMI_LOG_DEBUG_CONT:
   case IPMI_LOG_DEBUG_END:
-    fprintf(stderr, "%s\n", msg);
+    DEBUG("%s", msg);
     break;
 #else
   case IPMI_LOG_DEBUG_START:
@@ -147,6 +151,14 @@ static void c_ipmi_log(os_handler_t *handler, const char *format,
   }
 } /* void c_ipmi_log */
 
+static notification_t c_ipmi_notification_init(c_ipmi_instance_t const *st,
+                                               int severity) {
+  notification_t n = {severity, cdtime(), "", "", "ipmi", "", "", "", NULL};
+
+  sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
+  return n;
+} /* notification_t c_ipmi_notification_init */
+
 /*
  * Sensor handlers
  */
@@ -176,11 +188,8 @@ static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
              list_item->sensor_name, st->name);
 
         if (st->notify_notpresent) {
-          notification_t n = {
-              NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "", NULL};
+          notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
 
-          sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g,
-                   sizeof(n.host));
           sstrncpy(n.type_instance, list_item->sensor_name,
                    sizeof(n.type_instance));
           sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
@@ -200,7 +209,7 @@ static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
       INFO("ipmi plugin: sensor_read_handler: Sensor `%s` of `%s` timed out.",
            list_item->sensor_name, st->name);
     } else {
-      char errbuf[128] = {0};
+      char errbuf[ERR_BUF_SIZE] = {0};
       ipmi_get_error_string(err, errbuf, sizeof(errbuf) - 1);
 
       if (IPMI_IS_IPMI_ERR(err))
@@ -232,11 +241,8 @@ static void sensor_read_handler(ipmi_sensor_t *sensor, int err,
          list_item->sensor_name, st->name);
 
     if (st->notify_notpresent) {
-      notification_t n = {NOTIF_OKAY, cdtime(), "", "",  "ipmi",
-                          "",         "",       "", NULL};
+      notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
 
-      sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g,
-               sizeof(n.host));
       sstrncpy(n.type_instance, list_item->sensor_name,
                sizeof(n.type_instance));
       sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
@@ -453,10 +459,8 @@ static int sensor_list_add(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
   pthread_mutex_unlock(&st->sensor_list_lock);
 
   if (st->notify_add && (st->init_in_progress == 0)) {
-    notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
+    notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
 
-    sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g,
-             sizeof(n.host));
     sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
     sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
     snprintf(n.message, sizeof(n.message), "sensor %s added",
@@ -501,11 +505,8 @@ static int sensor_list_remove(c_ipmi_instance_t *st, ipmi_sensor_t *sensor) {
   pthread_mutex_unlock(&st->sensor_list_lock);
 
   if (st->notify_remove && st->active) {
-    notification_t n = {NOTIF_WARNING, cdtime(), "", "", "ipmi", "", "", "",
-                        NULL};
+    notification_t n = c_ipmi_notification_init(st, NOTIF_WARNING);
 
-    sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g,
-             sizeof(n.host));
     sstrncpy(n.type_instance, list_item->sensor_name, sizeof(n.type_instance));
     sstrncpy(n.type, list_item->sensor_type, sizeof(n.type));
     snprintf(n.message, sizeof(n.message), "sensor %s removed",
@@ -611,9 +612,9 @@ static int sensor_threshold_event_handler(
   if (event == NULL)
     return IPMI_EVENT_NOT_HANDLED;
 
+  notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
   /* offset is a table index and it's represented as enum of strings that are
      organized in the way - high and low for each threshold severity level */
-  notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
   unsigned int offset = (2 * threshold) + high_low;
   unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
   unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
@@ -631,7 +632,6 @@ static int sensor_threshold_event_handler(
 
   DEBUG("Threshold event received for sensor %s", n.type_instance);
 
-  sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
   sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
   n.severity = sensor_convert_threshold_severity(threshold);
   n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
@@ -683,7 +683,7 @@ static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
   if (event == NULL)
     return IPMI_EVENT_NOT_HANDLED;
 
-  notification_t n = {NOTIF_OKAY, cdtime(), "", "", "ipmi", "", "", "", NULL};
+  notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
   unsigned int event_type = ipmi_sensor_get_event_reading_type(sensor);
   unsigned int sensor_type = ipmi_sensor_get_sensor_type(sensor);
   const char *event_state =
@@ -694,7 +694,6 @@ static int sensor_discrete_event_handler(ipmi_sensor_t *sensor,
 
   DEBUG("Discrete event received for sensor %s", n.type_instance);
 
-  sstrncpy(n.host, (st->host != NULL) ? st->host : hostname_g, sizeof(n.host));
   sstrncpy(n.type, ipmi_sensor_get_sensor_type_string(sensor), sizeof(n.type));
   n.time = NS_TO_CDTIME_T(ipmi_event_get_timestamp(event));
 
@@ -782,13 +781,13 @@ domain_entity_update_handler(enum ipmi_update_e op,
     status = ipmi_entity_add_sensor_update_handler(
         entity, entity_sensor_update_handler, /* user data = */ (void *)st);
     if (status != 0) {
-      c_ipmi_error("ipmi_entity_add_sensor_update_handler", status);
+      c_ipmi_error(st, "ipmi_entity_add_sensor_update_handler", status);
     }
   } else if (op == IPMI_DELETED) {
     status = ipmi_entity_remove_sensor_update_handler(
         entity, entity_sensor_update_handler, /* user data = */ (void *)st);
     if (status != 0) {
-      c_ipmi_error("ipmi_entity_remove_sensor_update_handler", status);
+      c_ipmi_error(st, "ipmi_entity_remove_sensor_update_handler", status);
     }
   }
 } /* void domain_entity_update_handler */
@@ -821,25 +820,46 @@ static void domain_connection_change_handler(ipmi_domain_t *domain, int err,
         "user_data = %p);",
         (void *)domain, err, conn_num, port_num, still_connected, user_data);
 
+  c_ipmi_instance_t *st = (c_ipmi_instance_t *)user_data;
+
   if (err != 0)
-    c_ipmi_error("domain_connection_change_handler", err);
+    c_ipmi_error(st, "domain_connection_change_handler", err);
+
+  if (!still_connected) {
+
+    if (st->notify_conn && st->connected && st->init_in_progress == 0) {
+      notification_t n = c_ipmi_notification_init(st, NOTIF_FAILURE);
+
+      sstrncpy(n.message, "IPMI connection lost", sizeof(n.plugin));
+
+      plugin_dispatch_notification(&n);
+    }
 
-  if (!still_connected)
+    st->connected = 0;
     return;
+  }
 
-  c_ipmi_instance_t *st = (c_ipmi_instance_t *)user_data;
+  if (st->notify_conn && !st->connected && st->init_in_progress == 0) {
+    notification_t n = c_ipmi_notification_init(st, NOTIF_OKAY);
+
+    sstrncpy(n.message, "IPMI connection restored", sizeof(n.plugin));
+
+    plugin_dispatch_notification(&n);
+  }
+
+  st->connected = 1;
 
   int status = ipmi_domain_add_entity_update_handler(
       domain, domain_entity_update_handler, /* user data = */ st);
   if (status != 0) {
-    c_ipmi_error("ipmi_domain_add_entity_update_handler", status);
+    c_ipmi_error(st, "ipmi_domain_add_entity_update_handler", status);
   }
 
   status = st->connection->add_event_handler(st->connection, smi_event_handler,
                                              (void *)domain);
 
   if (status != 0)
-    c_ipmi_error("Failed to register smi event handler", status);
+    c_ipmi_error(st, "Failed to register smi event handler", status);
 } /* void domain_connection_change_handler */
 
 static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
@@ -847,42 +867,31 @@ static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
   int status;
 
   if (st->connaddr != NULL) {
-    char *ip_addrs[1] = {NULL}, *ports[1] = {NULL};
-
-    ip_addrs[0] = strdup(st->connaddr);
-    ports[0] = strdup(IPMI_LAN_STD_PORT_STR);
-
-    status = ipmi_ip_setup_con(ip_addrs, ports, 1, st->authtype,
-                               (unsigned int)IPMI_PRIVILEGE_USER, st->username,
-                               strlen(st->username), st->password,
-                               strlen(st->password), os_handler,
-                               /* user data = */ NULL, &st->connection);
+    status = ipmi_ip_setup_con(
+        &st->connaddr, (char * [1]){IPMI_LAN_STD_PORT_STR}, 1, st->authtype,
+        (unsigned int)IPMI_PRIVILEGE_USER, st->username, strlen(st->username),
+        st->password, strlen(st->password), os_handler,
+        /* user data = */ NULL, &st->connection);
     if (status != 0) {
-      c_ipmi_error("ipmi_ip_setup_con", status);
+      c_ipmi_error(st, "ipmi_ip_setup_con", status);
       return -1;
     }
   } else {
     status = ipmi_smi_setup_con(/* if_num = */ 0, os_handler,
                                 /* user data = */ NULL, &st->connection);
     if (status != 0) {
-      c_ipmi_error("ipmi_smi_setup_con", status);
+      c_ipmi_error(st, "ipmi_smi_setup_con", status);
       return -1;
     }
   }
 
-  size_t open_option_num = 0;
-  ipmi_open_option_t open_option[2];
-
-  open_option[open_option_num].option = IPMI_OPEN_OPTION_ALL;
-  open_option[open_option_num].ival = 1;
-  open_option_num++;
-
+  ipmi_open_option_t opts[] = {
+      {.option = IPMI_OPEN_OPTION_ALL, {.ival = 1}},
 #ifdef IPMI_OPEN_OPTION_USE_CACHE
-  // This option appeared in OpenIPMI-2.0.17
-  open_option[open_option_num].option = IPMI_OPEN_OPTION_USE_CACHE;
-  open_option[open_option_num].ival = 0; /* Disable SDR cache in local file */
-  open_option_num++;
+      /* OpenIPMI-2.0.17 and later: Disable SDR cache in local file */
+      {.option = IPMI_OPEN_OPTION_USE_CACHE, {.ival = 0}},
 #endif
+  };
 
   /*
    * NOTE: Domain names must be unique. There is static `domains_list` common
@@ -891,10 +900,10 @@ static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
   status = ipmi_open_domain(
       st->name, &st->connection, /* num_con = */ 1,
       domain_connection_change_handler, /* user data = */ (void *)st,
-      /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, open_option,
-      open_option_num, &domain_id);
+      /* domain_fully_up_handler = */ NULL, /* user data = */ NULL, opts,
+      STATIC_ARRAY_SIZE(opts), &domain_id);
   if (status != 0) {
-    c_ipmi_error("ipmi_open_domain", status);
+    c_ipmi_error(st, "ipmi_open_domain", status);
     return -1;
   }
 
@@ -902,10 +911,9 @@ static int c_ipmi_thread_init(c_ipmi_instance_t *st) {
 } /* int c_ipmi_thread_init */
 
 static void *c_ipmi_thread_main(void *user_data) {
-  int status;
   c_ipmi_instance_t *st = (c_ipmi_instance_t *)user_data;
 
-  status = c_ipmi_thread_init(st);
+  int status = c_ipmi_thread_init(st);
   if (status != 0) {
     ERROR("ipmi plugin: c_ipmi_thread_init failed.");
     st->active = 0;
@@ -974,7 +982,7 @@ static void c_ipmi_free_instance(c_ipmi_instance_t *st) {
   sfree(st);
 } /* void c_ipmi_free_instance */
 
-void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
+static void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
   if (instances == NULL) {
     instances = instance;
     return;
@@ -986,8 +994,6 @@ void c_ipmi_add_instance(c_ipmi_instance_t *instance) {
     last = last->next;
 
   last->next = instance;
-
-  return;
 } /* void c_ipmi_add_instance */
 
 static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
@@ -1010,25 +1016,23 @@ static int c_ipmi_config_add_instance(oconfig_item_t *ci) {
     if (strcasecmp("Sensor", child->key) == 0)
       ignorelist_add(st->ignorelist, ci->values[0].value.string);
     else if (strcasecmp("IgnoreSelected", child->key) == 0) {
-      if (ci->values[0].value.boolean)
-        ignorelist_set_invert(st->ignorelist, /* invert = */ 0);
-      else
-        ignorelist_set_invert(st->ignorelist, /* invert = */ 1);
+      _Bool t;
+      status = cf_util_get_boolean(child, &t);
+      if (status != 0)
+        break;
+      ignorelist_set_invert(st->ignorelist, /* invert = */ !t);
+    } else if (strcasecmp("NotifyIPMIConnectionState", child->key) == 0) {
+      status = cf_util_get_boolean(child, &st->notify_conn);
     } else if (strcasecmp("NotifySensorAdd", child->key) == 0) {
-      if (ci->values[0].value.boolean)
-        st->notify_add = 1;
+      status = cf_util_get_boolean(child, &st->notify_add);
     } else if (strcasecmp("NotifySensorRemove", child->key) == 0) {
-      if (ci->values[0].value.boolean)
-        st->notify_remove = 1;
+      status = cf_util_get_boolean(child, &st->notify_remove);
     } else if (strcasecmp("NotifySensorNotPresent", child->key) == 0) {
-      if (ci->values[0].value.boolean)
-        st->notify_notpresent = 1;
+      status = cf_util_get_boolean(child, &st->notify_notpresent);
     } else if (strcasecmp("SELEnabled", child->key) == 0) {
-      if (ci->values[0].value.boolean)
-        st->sel_enabled = 1;
+      status = cf_util_get_boolean(child, &st->sel_enabled);
     } else if (strcasecmp("SELClearEvent", child->key) == 0) {
-      if (ci->values[0].value.boolean)
-        st->sel_clear_event = 1;
+      status = cf_util_get_boolean(child, &st->sel_clear_event);
     } else if (strcasecmp("Host", child->key) == 0)
       status = cf_util_get_string(child, &st->host);
     else if (strcasecmp("Address", child->key) == 0)
@@ -1077,33 +1081,42 @@ static int c_ipmi_config(oconfig_item_t *ci) {
     oconfig_item_t *child = ci->children + i;
 
     if (strcasecmp("Instance", child->key) == 0) {
-      c_ipmi_config_add_instance(child);
+      int status = c_ipmi_config_add_instance(child);
+      if (status != 0)
+        return status;
+
       have_instance_block = 1;
     } else if (!have_instance_block) {
       /* Non-instance option: Assume legacy configuration (without <Instance />
        * blocks) and call c_ipmi_config_add_instance with the <Plugin /> block.
        */
+      WARNING("ipmi plugin: Legacy configuration found! Please update your "
+              "config file.");
       return c_ipmi_config_add_instance(ci);
-    } else
+    } else {
       WARNING("ipmi plugin: The configuration option "
               "\"%s\" is not allowed here. Did you "
               "forget to add an <Instance /> block "
               "around the configuration?",
               child->key);
+      return -1;
+    }
   } /* for (ci->children) */
 
   return 0;
 } /* int c_ipmi_config */
 
 static int c_ipmi_read(user_data_t *user_data) {
-  c_ipmi_instance_t *st;
-  st = user_data->data;
+  c_ipmi_instance_t *st = user_data->data;
 
-  if ((st->active == 0) || (st->thread_id == (pthread_t)0)) {
+  if (st->active == 0) {
     INFO("ipmi plugin: c_ipmi_read: I'm not active, returning false.");
     return -1;
   }
 
+  if (st->connected == 0)
+    return 0;
+
   sensor_list_read_all(st);
 
   if (st->init_in_progress > 0)
@@ -1115,10 +1128,13 @@ static int c_ipmi_read(user_data_t *user_data) {
 } /* int c_ipmi_read */
 
 static int c_ipmi_init(void) {
-  int status;
   c_ipmi_instance_t *st;
   char callback_name[3 * DATA_MAX_NAME_LEN];
 
+  if (os_handler != NULL) {
+    return 0;
+  }
+
   os_handler = ipmi_posix_thread_setup_os_handler(SIGIO);
   if (os_handler == NULL) {
     ERROR("ipmi plugin: ipmi_posix_thread_setup_os_handler failed.");
@@ -1133,9 +1149,6 @@ static int c_ipmi_init(void) {
     return -1;
   };
 
-  /* Don't send `ADD' notifications during startup (~ 1 minute) */
-  time_t iv = CDTIME_T_TO_TIME_T(plugin_get_interval());
-
   if (instances == NULL) {
     /* No instances were configured, let's start a default instance. */
     st = c_ipmi_init_instance();
@@ -1145,6 +1158,9 @@ static int c_ipmi_init(void) {
     c_ipmi_add_instance(st);
   }
 
+  /* Don't send `ADD' notifications during startup (~ 1 minute) */
+  int cycles = 1 + (60 / CDTIME_T_TO_TIME_T(plugin_get_interval()));
+
   st = instances;
   while (NULL != st) {
     /* The `st->name` is used as "domain name" for ipmi_open_domain().
@@ -1156,7 +1172,7 @@ static int c_ipmi_init(void) {
         .data = st,
     };
 
-    status = plugin_register_complex_read(
+    int status = plugin_register_complex_read(
         /* group     = */ "ipmi",
         /* name      = */ callback_name,
         /* callback  = */ c_ipmi_read,
@@ -1164,12 +1180,11 @@ static int c_ipmi_init(void) {
         /* user_data = */ &ud);
 
     if (status != 0) {
-      st->active = 0;
       st = st->next;
       continue;
     }
 
-    st->init_in_progress = 1 + (60 / iv);
+    st->init_in_progress = cycles;
     st->active = 1;
 
     status = plugin_thread_create(&st->thread_id, /* attr = */ NULL,
@@ -1178,7 +1193,7 @@ static int c_ipmi_init(void) {
 
     if (status != 0) {
       st->active = 0;
-      st->thread_id = (pthread_t)0;
+      st->thread_id = (pthread_t){0};
 
       plugin_unregister_read(callback_name);
 
@@ -1201,9 +1216,9 @@ static int c_ipmi_shutdown(void) {
     st->next = NULL;
     st->active = 0;
 
-    if (st->thread_id != (pthread_t)0) {
+    if (!pthread_equal(st->thread_id, (pthread_t){0})) {
       pthread_join(st->thread_id, NULL);
-      st->thread_id = (pthread_t)0;
+      st->thread_id = (pthread_t){0};
     }
 
     sensor_list_remove_all(st);
@@ -1213,6 +1228,7 @@ static int c_ipmi_shutdown(void) {
   }
 
   os_handler->free_os_handler(os_handler);
+  os_handler = NULL;
 
   return 0;
 } /* int c_ipmi_shutdown */