Merge branch 'collectd-5.7'
[collectd.git] / src / ovs_events.c
index 85a9fa0..d372b87 100644 (file)
 #define OVS_EVENTS_PLUGIN "ovs_events"
 #define OVS_EVENTS_CTX_LOCK                                                    \
   for (int __i = ovs_events_ctx_lock(); __i != 0; __i = ovs_events_ctx_unlock())
-#define OVS_EVENTS_CONFIG_ERROR(option)                                        \
-  do {                                                                         \
-    ERROR(OVS_EVENTS_PLUGIN ": read '%s' config option failed", option);       \
-    goto failure;                                                              \
-  } while (0)
 
 /* Link status type */
 enum ovs_events_link_status_e { DOWN, UP };
@@ -91,10 +86,14 @@ typedef struct ovs_events_ctx_s ovs_events_ctx_t;
  */
 static ovs_events_ctx_t ovs_events_ctx = {
     .mutex = PTHREAD_MUTEX_INITIALIZER,
-    .config = {.ovs_db_node = "localhost", /* use default OVS DB node */
+    .config = {.send_notification = 1,     /* send notification by default */
+               .ovs_db_node = "localhost", /* use default OVS DB node */
                .ovs_db_serv = "6640"}      /* use default OVS DB service */
 };
 
+/* Forward declaration */
+static int ovs_events_plugin_read(user_data_t *u);
+
 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
  * It always returns 1 when context is locked.
  */
@@ -171,7 +170,7 @@ static char *ovs_events_get_select_params() {
 
   /* allocate memory for OVS DB select params */
   size_t params_size = sizeof(params_fmt) + strlen(opt_buff);
-  char *params_buff = malloc(params_size);
+  char *params_buff = calloc(1, params_size);
   if (params_buff == NULL) {
     sfree(opt_buff);
     return NULL;
@@ -196,66 +195,99 @@ static void ovs_events_config_free() {
   }
 }
 
+/* Parse/process "Interfaces" configuration option. Returns 0 if success
+ * otherwise -1 (error)
+ */
+static int ovs_events_config_get_interfaces(const oconfig_item_t *ci) {
+  for (int j = 0; j < ci->values_num; j++) {
+    /* check interface name type */
+    if (ci->values[j].type != OCONFIG_TYPE_STRING) {
+      ERROR(OVS_EVENTS_PLUGIN
+            ": given interface name is not a string [idx=%d]", j);
+      return (-1);
+    }
+    /* allocate memory for configured interface */
+    ovs_events_iface_list_t *new_iface = calloc(1, sizeof(*new_iface));
+    if (new_iface == NULL) {
+      ERROR(OVS_EVENTS_PLUGIN ": calloc () copy interface name fail");
+      return (-1);
+    } else {
+      /* store interface name */
+      sstrncpy(new_iface->name, ci->values[j].value.string,
+               sizeof(new_iface->name));
+      new_iface->next = ovs_events_ctx.config.ifaces;
+      ovs_events_ctx.config.ifaces = new_iface;
+      DEBUG(OVS_EVENTS_PLUGIN ": found monitored interface \"%s\"",
+            new_iface->name);
+    }
+  }
+  return (0);
+}
+
 /* Parse plugin configuration file and store the config
  * in allocated memory. Returns negative value in case of error.
  */
 static int ovs_events_plugin_config(oconfig_item_t *ci) {
-  ovs_events_iface_list_t *new_iface;
-
+  _Bool dispatch_values = 0;
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
     if (strcasecmp("SendNotification", child->key) == 0) {
       if (cf_util_get_boolean(child,
-                              &ovs_events_ctx.config.send_notification) != 0)
-        OVS_EVENTS_CONFIG_ERROR(child->key);
+                              &ovs_events_ctx.config.send_notification) != 0) {
+        ovs_events_config_free();
+        return (-1);
+      }
     } else if (strcasecmp("Address", child->key) == 0) {
       if (cf_util_get_string_buffer(
               child, ovs_events_ctx.config.ovs_db_node,
-              sizeof(ovs_events_ctx.config.ovs_db_node)) != 0)
-        OVS_EVENTS_CONFIG_ERROR(child->key);
+              sizeof(ovs_events_ctx.config.ovs_db_node)) != 0) {
+        ovs_events_config_free();
+        return (-1);
+      }
     } else if (strcasecmp("Port", child->key) == 0) {
-      if (cf_util_get_string_buffer(
-              child, ovs_events_ctx.config.ovs_db_serv,
-              sizeof(ovs_events_ctx.config.ovs_db_serv)) != 0)
-        OVS_EVENTS_CONFIG_ERROR(child->key);
+      char *service = NULL;
+      if (cf_util_get_service(child, &service) != 0) {
+        ovs_events_config_free();
+        return (-1);
+      }
+      strncpy(ovs_events_ctx.config.ovs_db_serv, service,
+              sizeof(ovs_events_ctx.config.ovs_db_serv));
+      sfree(service);
     } else if (strcasecmp("Socket", child->key) == 0) {
       if (cf_util_get_string_buffer(
               child, ovs_events_ctx.config.ovs_db_unix,
-              sizeof(ovs_events_ctx.config.ovs_db_unix)) != 0)
-        OVS_EVENTS_CONFIG_ERROR(child->key);
+              sizeof(ovs_events_ctx.config.ovs_db_unix)) != 0) {
+        ovs_events_config_free();
+        return (-1);
+      }
     } else if (strcasecmp("Interfaces", child->key) == 0) {
-      for (int j = 0; j < child->values_num; j++) {
-        /* check value type */
-        if (child->values[j].type != OCONFIG_TYPE_STRING) {
-          ERROR(OVS_EVENTS_PLUGIN
-                ": given interface name is not a string [idx=%d]",
-                j);
-          goto failure;
-        }
-        /* allocate memory for configured interface */
-        if ((new_iface = malloc(sizeof(*new_iface))) == NULL) {
-          ERROR(OVS_EVENTS_PLUGIN ": malloc () copy interface name fail");
-          goto failure;
-        } else {
-          /* store interface name */
-          sstrncpy(new_iface->name, child->values[j].value.string,
-                   sizeof(new_iface->name));
-          new_iface->next = ovs_events_ctx.config.ifaces;
-          ovs_events_ctx.config.ifaces = new_iface;
-          DEBUG(OVS_EVENTS_PLUGIN ": found monitored interface \"%s\"",
-                new_iface->name);
-        }
+      if (ovs_events_config_get_interfaces(child) != 0) {
+        ovs_events_config_free();
+        return (-1);
+      }
+    } else if (strcasecmp("DispatchValues", child->key) == 0) {
+      if (cf_util_get_boolean(child, &dispatch_values) != 0) {
+        ovs_events_config_free();
+        return (-1);
       }
     } else {
       ERROR(OVS_EVENTS_PLUGIN ": option '%s' is not allowed here", child->key);
-      goto failure;
+      ovs_events_config_free();
+      return (-1);
     }
   }
-  return (0);
+  /* Check and warn about invalid configuration */
+  if (!ovs_events_ctx.config.send_notification && !dispatch_values) {
+      WARNING(OVS_EVENTS_PLUGIN ": send notification and dispatch values "
+              "options are disabled. No information will be dispatched by the "
+              "plugin. Please check your configuration");
+  }
+  /* Dispatch link status values if configured */
+  if (dispatch_values)
+    return plugin_register_complex_read(NULL, OVS_EVENTS_PLUGIN,
+                                        ovs_events_plugin_read, 0, NULL);
 
-failure:
-  ovs_events_config_free();
-  return (-1);
+  return (0);
 }
 
 /* Dispatch OVS interface link status event to collectd */
@@ -285,19 +317,21 @@ static void ovs_events_dispatch_notification(const ovs_events_iface_info_t *ifin
     return;
   }
 
-  if (strlen(ifinfo->ext_vm_uuid) > 0)
+  if (strlen(ifinfo->ext_vm_uuid) > 0) {
     if (plugin_notification_meta_add_string(&n, "vm-uuid",
                                             ifinfo->ext_vm_uuid) < 0) {
       ERROR(OVS_EVENTS_PLUGIN ": add interface vm-uuid meta data failed");
       return;
     }
+  }
 
-  if (strlen(ifinfo->ext_iface_id) > 0)
+  if (strlen(ifinfo->ext_iface_id) > 0) {
     if (plugin_notification_meta_add_string(&n, "iface-id",
                                             ifinfo->ext_iface_id) < 0) {
       ERROR(OVS_EVENTS_PLUGIN ": add interface iface-id meta data failed");
       return;
     }
+  }
 
   /* fill the notification data */
   ssnprintf(n.message, sizeof(n.message),
@@ -452,7 +486,7 @@ static void ovs_events_table_update_cb(yajl_val jupdates) {
     return;
   }
   /* go through all row updates  */
-  for (int row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len;
+  for (size_t row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len;
        ++row_index) {
     jrow_update = YAJL_GET_OBJECT(jupdate)->values[row_index];
 
@@ -499,14 +533,14 @@ static void ovs_events_poll_result_cb(yajl_val jresult, yajl_val jerror) {
 
   /* go through all rows and get interface info */
   jvalues = YAJL_GET_ARRAY(jresult)->values;
-  for (int i = 0; i < YAJL_GET_ARRAY(jresult)->len; i++) {
+  for (size_t i = 0; i < YAJL_GET_ARRAY(jresult)->len; i++) {
     jvalue = ovs_utils_get_value_by_key(jvalues[i], "rows");
     if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue)) {
       ERROR(OVS_EVENTS_PLUGIN "invalid data (array of rows is expected)");
       return;
     }
     /* get interfaces info */
-    for (int j = 0; j < YAJL_GET_ARRAY(jvalue)->len; j++) {
+    for (size_t j = 0; j < YAJL_GET_ARRAY(jvalue)->len; j++) {
       if (ovs_events_get_iface_info(YAJL_GET_ARRAY(jvalue)->values[j],
                                     &ifinfo) < 0) {
         ERROR(OVS_EVENTS_PLUGIN
@@ -620,7 +654,5 @@ static int ovs_events_plugin_shutdown(void) {
 void module_register(void) {
   plugin_register_complex_config(OVS_EVENTS_PLUGIN, ovs_events_plugin_config);
   plugin_register_init(OVS_EVENTS_PLUGIN, ovs_events_plugin_init);
-  plugin_register_complex_read(NULL, OVS_EVENTS_PLUGIN, ovs_events_plugin_read,
-                               0, NULL);
   plugin_register_shutdown(OVS_EVENTS_PLUGIN, ovs_events_plugin_shutdown);
 }