src/utils_cache.[ch]: Implemented uc_[gs]et_state to receive and set the state of...
[collectd.git] / src / utils_cache.c
index 20d4deb..560365b 100644 (file)
@@ -23,6 +23,8 @@
 #include "common.h"
 #include "plugin.h"
 #include "utils_avltree.h"
+#include "utils_cache.h"
+#include "utils_threshold.h"
 
 #include <assert.h>
 #include <pthread.h>
@@ -42,6 +44,7 @@ typedef struct cache_entry_s
        /* Interval in which the data is collected
         * (for purding old entries) */
        int interval;
+       int state;
 } cache_entry_t;
 
 static avl_tree_t     *cache_tree = NULL;
@@ -105,8 +108,9 @@ static int uc_send_notification (const char *name)
   }
     
   /* Check if the entry has been updated in the meantime */
-  if ((n.time - ce->last_update) <= (2 * ce->interval))
+  if ((n.time - ce->last_update) < (2 * ce->interval))
   {
+    ce->state = STATE_OKAY;
     pthread_mutex_unlock (&cache_lock);
     sfree (name_copy);
     return (-1);
@@ -114,7 +118,7 @@ static int uc_send_notification (const char *name)
 
   snprintf (n.message, sizeof (n.message),
       "%s has not been updated for %i seconds.", name,
-      (int) (ce->last_update - n.time));
+      (int) (n.time - ce->last_update));
 
   pthread_mutex_unlock (&cache_lock);
 
@@ -154,7 +158,7 @@ int uc_check_timeout (void)
   while (avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
   {
     /* If entry has not been updated, add to `keys' array */
-    if ((now - ce->last_update) > (2 * ce->interval))
+    if ((now - ce->last_update) >= (2 * ce->interval))
     {
       char **tmp;
 
@@ -182,21 +186,35 @@ int uc_check_timeout (void)
   {
     int status;
 
-    /* TODO: Check if value interesting:
-     * - Not interesting: Remove value from cache and shut up
-     * - Interesting:     Don't remove value from cache but send a
-     *                    notification.
-     */
-    status = avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
-    if (status != 0)
+    status = ut_check_interesting (keys[i]);
+
+    if (status < 0)
     {
-      ERROR ("uc_check_timeout: avl_remove (%s) failed.", keys[i]);
-      continue;
+      ERROR ("uc_check_timeout: ut_check_interesting failed.");
+      sfree (keys[i]);
     }
-
-    sfree (key);
-    sfree (ce);
-  }
+    else if (status == 0) /* ``service'' is uninteresting */
+    {
+      ce = NULL;
+      DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''", keys[i]);
+      status = avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
+      if (status != 0)
+      {
+       ERROR ("uc_check_timeout: avl_remove (%s) failed.", keys[i]);
+      }
+      sfree (keys[i]);
+      sfree (ce);
+    }
+    else /* (status > 0); ``service'' is interesting */
+    {
+      /*
+       * `keys[i]' is not freed and set to NULL, so that the for-loop below
+       * will send out notifications. There's nothing else to do here.
+       */
+      DEBUG ("uc_check_timeout: %s is missing and ``interesting''", keys[i]);
+      ce->state = STATE_ERROR;
+    }
+  } /* for (keys[i]) */
 
   avl_iterator_destroy (iter);
 
@@ -204,6 +222,9 @@ int uc_check_timeout (void)
 
   for (i = 0; i < keys_len; i++)
   {
+    if (keys[i] == NULL)
+      continue;
+
     uc_send_notification (keys[i]);
     sfree (keys[i]);
   }
@@ -247,6 +268,7 @@ int uc_update (const data_set_t *ds, const value_list_t *vl)
       /* TODO: Implement a `real' okay notification. Watch out for locking
        * issues, though! */
       NOTICE ("uc_insert: Okay notification for %s", name);
+      ce->state = STATE_OKAY;
     }
 
     for (i = 0; i < ds->ds_num; i++)
@@ -331,6 +353,9 @@ int uc_update (const data_set_t *ds, const value_list_t *vl)
     } /* for (i) */
 
     ce->last_time = vl->time;
+    ce->last_update = time (NULL);
+    ce->interval = vl->interval;
+    ce->state = STATE_OKAY;
 
     if (avl_insert (cache_tree, key, ce) != 0)
     {
@@ -382,4 +407,59 @@ gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
   return (ret);
 } /* gauge_t *uc_get_rate */
 
+int uc_get_state (const data_set_t *ds, const value_list_t *vl)
+{
+  char name[6 * DATA_MAX_NAME_LEN];
+  cache_entry_t *ce = NULL;
+  int ret = STATE_ERROR;
+
+  if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
+  {
+    ERROR ("uc_get_state: FORMAT_VL failed.");
+    return (STATE_ERROR);
+  }
+
+  pthread_mutex_lock (&cache_lock);
+
+  if (avl_get (cache_tree, name, (void *) &ce) == 0)
+  {
+    assert (ce != NULL);
+    ret = ce->state;
+  }
+
+  pthread_mutex_unlock (&cache_lock);
+
+  return (ret);
+} /* int uc_get_state */
+
+int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
+{
+  char name[6 * DATA_MAX_NAME_LEN];
+  cache_entry_t *ce = NULL;
+  int ret = -1;
+
+  if (state < STATE_OKAY)
+    state = STATE_OKAY;
+  if (state > STATE_ERROR)
+    state = STATE_ERROR;
+
+  if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
+  {
+    ERROR ("uc_get_state: FORMAT_VL failed.");
+    return (STATE_ERROR);
+  }
+
+  pthread_mutex_lock (&cache_lock);
+
+  if (avl_get (cache_tree, name, (void *) &ce) == 0)
+  {
+    assert (ce != NULL);
+    ret = ce->state;
+    ce->state = state;
+  }
+
+  pthread_mutex_unlock (&cache_lock);
+
+  return (ret);
+} /* int uc_set_state */
 /* vim: set sw=2 ts=8 sts=2 tw=78 : */