write_kafka plugin: Use 32bit random number when formatting a random key.
[collectd.git] / src / target_set.c
index fee5ffd..1b26a55 100644 (file)
@@ -29,8 +29,7 @@
 #include "common.h"
 #include "filter_chain.h"
 
-struct ts_data_s
-{
+struct ts_data_s {
   char *host;
   char *plugin;
   char *plugin_instance;
@@ -40,14 +39,15 @@ struct ts_data_s
 };
 typedef struct ts_data_s ts_data_t;
 
-static int ts_util_get_key_and_string_wo_strdup (const oconfig_item_t *ci, char **ret_key, char **ret_string) /* {{{ */
+static int ts_util_get_key_and_string_wo_strdup(const oconfig_item_t *ci,
+                                                char **ret_key,
+                                                char **ret_string) /* {{{ */
 {
-  if ((ci->values_num != 2)
-      || (ci->values[0].type != OCONFIG_TYPE_STRING)
-      || (ci->values[1].type != OCONFIG_TYPE_STRING))
-  {
-    ERROR ("ts_util_get_key_and_string_wo_strdup: The %s option requires "
-        "exactly two string argument.", ci->key);
+  if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
+      (ci->values[1].type != OCONFIG_TYPE_STRING)) {
+    ERROR("ts_util_get_key_and_string_wo_strdup: The %s option requires "
+          "exactly two string argument.",
+          ci->key);
     return (-1);
   }
 
@@ -57,21 +57,19 @@ static int ts_util_get_key_and_string_wo_strdup (const oconfig_item_t *ci, char
   return (0);
 } /* }}} int ts_util_get_key_and_string_wo_strdup */
 
-static int ts_config_add_string (char **dest, /* {{{ */
-    const oconfig_item_t *ci, int may_be_empty)
-{
+static int ts_config_add_string(char **dest, /* {{{ */
+                                const oconfig_item_t *ci, int may_be_empty) {
   char *tmp = NULL;
   int status;
 
-  status = cf_util_get_string (ci, &tmp);
+  status = cf_util_get_string(ci, &tmp);
   if (status != 0)
     return (status);
 
-  if (!may_be_empty && (strlen (tmp) == 0))
-  {
-    ERROR ("Target `set': The `%s' option does not accept empty strings.",
-        ci->key);
-    sfree (tmp);
+  if (!may_be_empty && (strlen(tmp) == 0)) {
+    ERROR("Target `set': The `%s' option does not accept empty strings.",
+          ci->key);
+    sfree(tmp);
     return (-1);
   }
 
@@ -79,45 +77,42 @@ static int ts_config_add_string (char **dest, /* {{{ */
   return (0);
 } /* }}} int ts_config_add_string */
 
-static int ts_config_add_meta (meta_data_t **dest, /* {{{ */
-    const oconfig_item_t *ci, int may_be_empty)
-{
+static int ts_config_add_meta(meta_data_t **dest, /* {{{ */
+                              const oconfig_item_t *ci, int may_be_empty) {
   char *key = NULL;
   char *string = NULL;
   int status;
 
-  status = ts_util_get_key_and_string_wo_strdup (ci, &key, &string);
+  status = ts_util_get_key_and_string_wo_strdup(ci, &key, &string);
   if (status != 0)
     return (status);
 
-  if (strlen (key) == 0)
-  {
-    ERROR ("Target `set': The `%s' option does not accept empty string as first argument.",
-        ci->key);
+  if (strlen(key) == 0) {
+    ERROR("Target `set': The `%s' option does not accept empty string as first "
+          "argument.",
+          ci->key);
     return (-1);
   }
 
-  if (!may_be_empty && (strlen (string) == 0))
-  {
-    ERROR ("Target `set': The `%s' option does not accept empty string as second argument.",
-        ci->key);
+  if (!may_be_empty && (strlen(string) == 0)) {
+    ERROR("Target `set': The `%s' option does not accept empty string as "
+          "second argument.",
+          ci->key);
     return (-1);
   }
 
-  if ((*dest) == NULL)
-  {
+  if ((*dest) == NULL) {
     // Create a new meta_data_t
-    if ((*dest = meta_data_create()) == NULL)
-    {
-      ERROR ("Target `set': failed to create a meta data for `%s'.", ci->key);
+    if ((*dest = meta_data_create()) == NULL) {
+      ERROR("Target `set': failed to create a meta data for `%s'.", ci->key);
       return (-1);
     }
   }
 
-  return (meta_data_add_string (*dest, key, string));
+  return (meta_data_add_string(*dest, key, string));
 } /* }}} int ts_config_add_meta */
 
-static int ts_destroy (void **user_data) /* {{{ */
+static int ts_destroy(void **user_data) /* {{{ */
 {
   ts_data_t *data;
 
@@ -128,26 +123,25 @@ static int ts_destroy (void **user_data) /* {{{ */
   if (data == NULL)
     return (0);
 
-  free (data->host);
-  free (data->plugin);
-  free (data->plugin_instance);
+  free(data->host);
+  free(data->plugin);
+  free(data->plugin_instance);
   /* free (data->type); */
-  free (data->type_instance);
+  free(data->type_instance);
   meta_data_destroy(data->meta);
-  free (data);
+  free(data);
 
   return (0);
 } /* }}} int ts_destroy */
 
-static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
+static int ts_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
 {
   ts_data_t *data;
   int status;
 
-  data = calloc (1, sizeof (*data));
-  if (data == NULL)
-  {
-    ERROR ("ts_create: calloc failed.");
+  data = calloc(1, sizeof(*data));
+  if (data == NULL) {
+    ERROR("ts_create: calloc failed.");
     return (-ENOMEM);
   }
 
@@ -159,35 +153,34 @@ static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
   data->meta = NULL;
 
   status = 0;
-  for (int i = 0; i < ci->children_num; i++)
-  {
+  for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *child = ci->children + i;
 
-    if ((strcasecmp ("Host", child->key) == 0)
-        || (strcasecmp ("Hostname", child->key) == 0))
-      status = ts_config_add_string (&data->host, child,
-          /* may be empty = */ 0);
-    else if (strcasecmp ("Plugin", child->key) == 0)
-      status = ts_config_add_string (&data->plugin, child,
-          /* may be empty = */ 0);
-    else if (strcasecmp ("PluginInstance", child->key) == 0)
-      status = ts_config_add_string (&data->plugin_instance, child,
-          /* may be empty = */ 1);
+    if ((strcasecmp("Host", child->key) == 0) ||
+        (strcasecmp("Hostname", child->key) == 0))
+      status = ts_config_add_string(&data->host, child,
+                                    /* may be empty = */ 0);
+    else if (strcasecmp("Plugin", child->key) == 0)
+      status = ts_config_add_string(&data->plugin, child,
+                                    /* may be empty = */ 0);
+    else if (strcasecmp("PluginInstance", child->key) == 0)
+      status = ts_config_add_string(&data->plugin_instance, child,
+                                    /* may be empty = */ 1);
 #if 0
     else if (strcasecmp ("Type", child->key) == 0)
       status = ts_config_add_string (&data->type, child,
           /* may be empty = */ 0);
 #endif
-    else if (strcasecmp ("TypeInstance", child->key) == 0)
-      status = ts_config_add_string (&data->type_instance, child,
-          /* may be empty = */ 1);
-    else if (strcasecmp ("MetaDataSet", child->key) == 0)
-      status = ts_config_add_meta (&data->meta, child,
-          /* may be empty = */ 1);
-    else
-    {
-      ERROR ("Target `set': The `%s' configuration option is not understood "
-          "and will be ignored.", child->key);
+    else if (strcasecmp("TypeInstance", child->key) == 0)
+      status = ts_config_add_string(&data->type_instance, child,
+                                    /* may be empty = */ 1);
+    else if (strcasecmp("MetaData", child->key) == 0)
+      status = ts_config_add_meta(&data->meta, child,
+                                  /* may be empty = */ 1);
+    else {
+      ERROR("Target `set': The `%s' configuration option is not understood "
+            "and will be ignored.",
+            child->key);
       status = 0;
     }
 
@@ -196,27 +189,21 @@ static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
   }
 
   /* Additional sanity-checking */
-  while (status == 0)
-  {
-    if ((data->host == NULL)
-        && (data->plugin == NULL)
-        && (data->plugin_instance == NULL)
+  while (status == 0) {
+    if ((data->host == NULL) && (data->plugin == NULL) &&
+        (data->plugin_instance == NULL)
         /* && (data->type == NULL) */
-        && (data->type_instance == NULL)
-        && (data->meta == NULL))
-    {
-      ERROR ("Target `set': You need to set at least one of `Host', "
-          "`Plugin', `PluginInstance', `TypeInstance', "
-          "`MetaDataSet' or `MetaDataEval'.");
+        && (data->type_instance == NULL) && (data->meta == NULL)) {
+      ERROR("Target `set': You need to set at least one of `Host', "
+            "`Plugin', `PluginInstance', `TypeInstance', `MetaData'.");
       status = -1;
     }
 
     break;
   }
 
-  if (status != 0)
-  {
-    ts_destroy ((void *) &data);
+  if (status != 0) {
+    ts_destroy((void *)&data);
     return (status);
   }
 
@@ -224,45 +211,44 @@ static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
   return (0);
 } /* }}} int ts_create */
 
-static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
-    notification_meta_t __attribute__((unused)) **meta, void **user_data)
-{
+static int ts_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
+                     notification_meta_t __attribute__((unused)) * *meta,
+                     void **user_data) {
   ts_data_t *data;
 
   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
     return (-EINVAL);
 
   data = *user_data;
-  if (data == NULL)
-  {
-    ERROR ("Target `set': Invoke: `data' is NULL.");
+  if (data == NULL) {
+    ERROR("Target `set': Invoke: `data' is NULL.");
     return (-EINVAL);
   }
 
-  if (data->meta != NULL)
-  {
+  if (data->meta != NULL) {
     meta_data_clone_merge(&(vl->meta), data->meta);
   }
 
-#define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
-  SET_FIELD (host);
-  SET_FIELD (plugin);
-  SET_FIELD (plugin_instance);
+#define SET_FIELD(f)                                                           \
+  if (data->f != NULL) {                                                       \
+    sstrncpy(vl->f, data->f, sizeof(vl->f));                                   \
+  }
+  SET_FIELD(host);
+  SET_FIELD(plugin);
+  SET_FIELD(plugin_instance);
   /* SET_FIELD (type); */
-  SET_FIELD (type_instance);
+  SET_FIELD(type_instance);
 
   return (FC_TARGET_CONTINUE);
 } /* }}} int ts_invoke */
 
-void module_register (void)
-{
-       target_proc_t tproc = { 0 };
+void module_register(void) {
+  target_proc_t tproc = {0};
 
-       tproc.create  = ts_create;
-       tproc.destroy = ts_destroy;
-       tproc.invoke  = ts_invoke;
-       fc_register_target ("set", tproc);
+  tproc.create = ts_create;
+  tproc.destroy = ts_destroy;
+  tproc.invoke = ts_invoke;
+  fc_register_target("set", tproc);
 } /* module_register */
 
 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
-