Add custom message for threshold and missings.
[collectd.git] / src / utils_cache.c
index 956f826..5284dcc 100644 (file)
@@ -25,6 +25,7 @@
 #include "utils_avltree.h"
 #include "utils_cache.h"
 #include "utils_threshold.h"
+#include "utils_subst.h"
 #include "meta_data.h"
 
 #include <assert.h>
@@ -46,6 +47,7 @@ typedef struct cache_entry_s
         * (for purding old entries) */
        int interval;
        int state;
+       int hits;
 
        /*
         * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
@@ -130,9 +132,13 @@ static int uc_send_notification (const char *name)
   char *plugin_instance;
   char *type;
   char *type_instance;
+  threshold_t th;
 
   notification_t n;
 
+  data_set_t ds;
+  value_list_t vl;
+
   name_copy = strdup (name);
   if (name_copy == NULL)
   {
@@ -149,6 +155,18 @@ static int uc_send_notification (const char *name)
     return (-1);
   }
 
+  memset (&ds, '\0', sizeof (ds));
+  memset (&vl, '\0', sizeof (vl));
+  sstrncpy (vl.host, host, sizeof (vl.host));
+  sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
+  if (plugin_instance != NULL)
+    sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+  sstrncpy (ds.type, type, sizeof (ds.type));
+  sstrncpy (vl.type, type, sizeof (vl.type));
+  if (type_instance != NULL)
+    sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+
+
   /* Copy the associative members */
   notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
       host, plugin, plugin_instance, type, type_instance);
@@ -156,6 +174,7 @@ static int uc_send_notification (const char *name)
   sfree (name_copy);
   name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
 
+
   pthread_mutex_lock (&cache_lock);
 
   /*
@@ -182,9 +201,41 @@ static int uc_send_notification (const char *name)
     return (-1);
   }
 
-  ssnprintf (n.message, sizeof (n.message),
-      "%s has not been updated for %i seconds.", name,
-      (int) (n.time - ce->last_update));
+  /* if the associated threshold has a missing message, then use custom
+   * message. FIXME: we do a threshold_search here and in uc_check_timeout
+   * (calling ut_check_interesting, but we really need to do this once */
+  if ( !ut_search_threshold(&vl, &th) &&
+      (th.missing_message != NULL) )
+  {
+    char msg[NOTIF_MAX_MSG_LEN];
+    char temp[NOTIF_MAX_MSG_LEN];
+
+    sstrncpy (msg, th.missing_message, sizeof (msg));
+    (void) ut_build_message (msg, NOTIF_MAX_MSG_LEN, th.missing_message,
+       &ds, 0, &vl, ce->values_gauge,
+       &n, &th);
+
+#define REPLACE_FIELD(t,v) \
+    if (subst_string (temp, sizeof (temp), msg, t, v) != NULL) \
+    sstrncpy (msg, temp, sizeof (msg));
+
+    char itoa_temp[NOTIF_MAX_MSG_LEN];
+#define ITOA(string,i) \
+    memset(string,0x00,sizeof(string)); \
+    snprintf(string, sizeof(string), "%i", i);
+
+    ITOA(itoa_temp, (int)(n.time - ce->last_update))
+      REPLACE_FIELD("%{missing}", itoa_temp)
+
+    (void) ssnprintf (n.message, sizeof (n.message),
+       "%s", msg);
+  }
+  else
+  {
+    ssnprintf (n.message, sizeof (n.message),
+       "%s has not been updated for %i seconds.", name,
+       (int) (n.time - ce->last_update));
+  }
 
   pthread_mutex_unlock (&cache_lock);
 
@@ -318,7 +369,7 @@ int uc_check_timeout (void)
   while (c_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) >= (timeout_g * ce->interval))
     {
       char **tmp;
 
@@ -895,6 +946,83 @@ int uc_get_history (const data_set_t *ds, const value_list_t *vl,
   return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
 } /* int uc_get_history */
 
+int uc_get_hits (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) != 0)
+  {
+    ERROR ("uc_get_state: FORMAT_VL failed.");
+    return (STATE_ERROR);
+  }
+
+  pthread_mutex_lock (&cache_lock);
+
+  if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
+  {
+    assert (ce != NULL);
+    ret = ce->hits;
+  }
+
+  pthread_mutex_unlock (&cache_lock);
+
+  return (ret);
+} /* int uc_get_hits */
+
+int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
+{
+  char name[6 * DATA_MAX_NAME_LEN];
+  cache_entry_t *ce = NULL;
+  int ret = -1;
+
+  if (FORMAT_VL (name, sizeof (name), vl) != 0)
+  {
+    ERROR ("uc_get_state: FORMAT_VL failed.");
+    return (STATE_ERROR);
+  }
+
+  pthread_mutex_lock (&cache_lock);
+
+  if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
+  {
+    assert (ce != NULL);
+    ret = ce->hits;
+    ce->hits = hits;
+  }
+
+  pthread_mutex_unlock (&cache_lock);
+
+  return (ret);
+} /* int uc_set_hits */
+
+int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
+{
+  char name[6 * DATA_MAX_NAME_LEN];
+  cache_entry_t *ce = NULL;
+  int ret = -1;
+
+  if (FORMAT_VL (name, sizeof (name), vl) != 0)
+  {
+    ERROR ("uc_get_state: FORMAT_VL failed.");
+    return (STATE_ERROR);
+  }
+
+  pthread_mutex_lock (&cache_lock);
+
+  if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
+  {
+    assert (ce != NULL);
+    ret = ce->hits;
+    ce->hits = ret + step;
+  }
+
+  pthread_mutex_unlock (&cache_lock);
+
+  return (ret);
+} /* int uc_inc_hits */
+
 /*
  * Meta data interface
  */
@@ -925,6 +1053,9 @@ static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
   if (ce->meta == NULL)
     ce->meta = meta_data_create ();
 
+  if (ce->meta == NULL)
+    pthread_mutex_unlock (&cache_lock);
+
   return (ce->meta);
 } /* }}} meta_data_t *uc_get_meta */