Merge branch 'pr/2056'
[collectd.git] / src / daemon / utils_match.c
index 5083b05..d1be244 100644 (file)
@@ -25,6 +25,7 @@
  **/
 
 #include "collectd.h"
+
 #include "common.h"
 #include "plugin.h"
 
@@ -32,7 +33,6 @@
 
 #include <regex.h>
 
-#define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
 #define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
 
 struct cu_match_s
@@ -44,6 +44,7 @@ struct cu_match_s
   int (*callback) (const char *str, char * const *matches, size_t matches_num,
       void *user_data);
   void *user_data;
+  void (*free) (void *user_data);
 };
 
 /*
@@ -63,7 +64,7 @@ static char *match_substr (const char *str, int begin, int end)
   }
 
   ret_len = end - begin;
-  ret = (char *) malloc (sizeof (char) * (ret_len + 1));
+  ret = malloc (ret_len + 1);
   if (ret == NULL)
   {
     ERROR ("utils_match: match_substr: malloc failed.");
@@ -98,8 +99,16 @@ static int default_callback (const char __attribute__((unused)) *str,
     if (matches[1] == endptr)
       return (-1);
 
+    if (data->ds_type & UTILS_MATCH_CF_GAUGE_DIST)
+    {
+      latency_counter_add(data->latency, DOUBLE_TO_CDTIME_T(value));
+      data->values_num++;
+      return (0);
+    }
+
     if ((data->values_num == 0)
-       || (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST))
+       || (data->ds_type & UTILS_MATCH_CF_GAUGE_LAST)
+       || (data->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST))
     {
       data->value.gauge = value;
     }
@@ -169,7 +178,7 @@ static int default_callback (const char __attribute__((unused)) *str,
 
     if (data->ds_type & UTILS_MATCH_CF_DERIVE_INC)
     {
-      data->value.counter++;
+      data->value.derive++;
       data->values_num++;
       return (0);
     }
@@ -224,13 +233,22 @@ static int default_callback (const char __attribute__((unused)) *str,
   return (0);
 } /* int default_callback */
 
+static void match_simple_free (void *data)
+{
+  cu_match_value_t *user_data = (cu_match_value_t *) data;
+  if (user_data->latency)
+    latency_counter_destroy(user_data->latency);
+
+  free (data);
+} /* void match_simple_free */
+
 /*
  * Public functions
  */
 cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
                int (*callback) (const char *str,
                  char * const *matches, size_t matches_num, void *user_data),
-               void *user_data)
+               void *user_data, void (*free_user_data) (void *user_data))
 {
   cu_match_t *obj;
   int status;
@@ -238,10 +256,9 @@ cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
   DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
         regex, excluderegex);
 
-  obj = (cu_match_t *) malloc (sizeof (cu_match_t));
+  obj = calloc (1, sizeof (*obj));
   if (obj == NULL)
     return (NULL);
-  memset (obj, '\0', sizeof (cu_match_t));
 
   status = regcomp (&obj->regex, regex, REG_EXTENDED | REG_NEWLINE);
   if (status != 0)
@@ -265,6 +282,7 @@ cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
 
   obj->callback = callback;
   obj->user_data = user_data;
+  obj->free = free_user_data;
 
   return (obj);
 } /* cu_match_t *match_create_callback */
@@ -275,22 +293,33 @@ cu_match_t *match_create_simple (const char *regex,
   cu_match_value_t *user_data;
   cu_match_t *obj;
 
-  user_data = (cu_match_value_t *) malloc (sizeof (cu_match_value_t));
+  user_data = calloc (1, sizeof (*user_data));
   if (user_data == NULL)
     return (NULL);
-  memset (user_data, '\0', sizeof (cu_match_value_t));
   user_data->ds_type = match_ds_type;
 
+  if ((match_ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
+      && (match_ds_type & UTILS_MATCH_CF_GAUGE_DIST))
+  {
+    user_data->latency = latency_counter_create();
+    if (user_data->latency == NULL)
+    {
+      ERROR ("match_create_simple(): latency_counter_create() failed.");
+      free (user_data);
+      return (NULL);
+    }
+  }
+
   obj = match_create_callback (regex, excluderegex,
-                              default_callback, user_data);
+                              default_callback, user_data, match_simple_free);
   if (obj == NULL)
   {
+    if (user_data->latency)
+      latency_counter_destroy(user_data->latency);
+
     sfree (user_data);
     return (NULL);
   }
-
-  obj->flags |= UTILS_MATCH_FLAGS_FREE_USER_DATA;
-
   return (obj);
 } /* cu_match_t *match_create_simple */
 
@@ -299,7 +328,9 @@ void match_value_reset (cu_match_value_t *mv)
   if (mv == NULL)
     return;
 
-  if (mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
+  /* Reset GAUGE metrics only and except GAUGE_PERSIST. */
+  if ((mv->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
+      && !(mv->ds_type & UTILS_MATCH_CF_GAUGE_PERSIST))
   {
     mv->value.gauge = NAN;
     mv->values_num = 0;
@@ -311,10 +342,8 @@ void match_destroy (cu_match_t *obj)
   if (obj == NULL)
     return;
 
-  if (obj->flags & UTILS_MATCH_FLAGS_FREE_USER_DATA)
-  {
-    sfree (obj->user_data);
-  }
+  if ((obj->user_data != NULL) && (obj->free != NULL))
+      (*obj->free) (obj->user_data);
 
   sfree (obj);
 } /* void match_destroy */
@@ -323,9 +352,8 @@ int match_apply (cu_match_t *obj, const char *str)
 {
   int status;
   regmatch_t re_match[32];
-  char *matches[32];
+  char *matches[32] = { 0 };
   size_t matches_num;
-  size_t i;
 
   if ((obj == NULL) || (str == NULL))
     return (-1);
@@ -349,7 +377,6 @@ int match_apply (cu_match_t *obj, const char *str)
   if (status != 0)
     return (0);
 
-  memset (matches, '\0', sizeof (matches));
   for (matches_num = 0; matches_num < STATIC_ARRAY_SIZE (matches); matches_num++)
   {
     if ((re_match[matches_num].rm_so < 0)
@@ -378,7 +405,7 @@ int match_apply (cu_match_t *obj, const char *str)
     }
   }
 
-  for (i = 0; i < matches_num; i++)
+  for (size_t i = 0; i < matches_num; i++)
   {
     sfree (matches[i]);
   }