Code

src/utils_cache.c: Update GETVAL output when missing state.
[collectd.git] / src / utils_cache.c
index a63c7d1c9b1cba770447a6a9080d0c6a6c7ea235..4d2c554990acc6e2927b784510c796dcd3cc60ef 100644 (file)
@@ -260,8 +260,9 @@ int uc_check_timeout (void)
          (keys_len + 1) * sizeof (char *));
       if (tmp == NULL)
       {
-       ERROR ("uc_purge: realloc failed.");
+       ERROR ("uc_check_timeout: realloc failed.");
        c_avl_iterator_destroy (iter);
+       sfree (keys);
        pthread_mutex_unlock (&cache_lock);
        return (-1);
       }
@@ -277,6 +278,8 @@ int uc_check_timeout (void)
     }
   } /* while (c_avl_iterator_next) */
 
+  ce = NULL;
+
   for (i = 0; i < keys_len; i++)
   {
     int status;
@@ -287,10 +290,10 @@ int uc_check_timeout (void)
     {
       ERROR ("uc_check_timeout: ut_check_interesting failed.");
       sfree (keys[i]);
+      continue;
     }
     else if (status == 0) /* ``service'' is uninteresting */
     {
-      ce = NULL;
       DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
          keys[i]);
       status = c_avl_remove (cache_tree, keys[i],
@@ -302,20 +305,34 @@ int uc_check_timeout (void)
       sfree (keys[i]);
       sfree (key);
       cache_free (ce);
+      continue;
     }
-    else if (status == 1) /* persist */
+
+    /* If we get here, the value is ``interesting''. Query the record from the
+     * cache and update the state field. */
+    if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
+    {
+      ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
+      /* Do not free `keys[i]' so a notification is sent further down. */
+      continue;
+    }
+    assert (ce != NULL);
+
+    if (status == 2) /* persist */
     {
       DEBUG ("uc_check_timeout: %s is missing, sending notification.",
          keys[i]);
       ce->state = STATE_MISSING;
+      /* Do not free `keys[i]' so a notification is sent further down. */
     }
-    else if (status == 2) /* do not persist */
+    else if (status == 1) /* do not persist */
     {
       if (ce->state == STATE_MISSING)
       {
        DEBUG ("uc_check_timeout: %s is missing but "
            "notification has already been sent.",
            keys[i]);
+       /* Set `keys[i]' to NULL to no notification is sent. */
        sfree (keys[i]);
       }
       else /* (ce->state != STATE_MISSING) */
@@ -323,6 +340,7 @@ int uc_check_timeout (void)
        DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
            keys[i]);
        ce->state = STATE_MISSING;
+       /* Do not free `keys[i]' so a notification is sent further down. */
       }
     }
     else
@@ -330,7 +348,12 @@ int uc_check_timeout (void)
       WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
          "invalid status %i.",
          keys[i], status);
+      sfree (keys[i]);
     }
+
+    /* Make really sure the next iteration doesn't work with this pointer.
+     * There have been too many bugs in the past.. :/  -- octo */
+    ce = NULL;
   } /* for (keys[i]) */
 
   c_avl_iterator_destroy (iter);
@@ -474,16 +497,24 @@ int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_val
   {
     assert (ce != NULL);
 
-    ret_num = ce->values_num;
-    ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
-    if (ret == NULL)
+    /* remove missing values from getval */
+    if (ce->state == STATE_MISSING)
     {
-      ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
       status = -1;
     }
     else
     {
-      memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
+      ret_num = ce->values_num;
+      ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
+      if (ret == NULL)
+      {
+        ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
+        status = -1;
+      }
+      else
+      {
+        memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
+      }
     }
   }
   else
@@ -556,6 +587,10 @@ int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
   {
     char **temp;
 
+    /* remove missing values when list values */
+    if (value->state == STATE_MISSING)
+      continue;
+
     if (ret_times != NULL)
     {
       time_t *tmp_times;