Merge branch 'collectd-4.2'
[collectd.git] / src / utils_threshold.c
index 1c9ccdd..ecdbfbe 100644 (file)
@@ -31,6 +31,9 @@
 /*
  * Private data structures
  * {{{ */
+#define UT_FLAG_INVERT  0x01
+#define UT_FLAG_PERSIST 0x02
+
 typedef struct threshold_s
 {
   char host[DATA_MAX_NAME_LEN];
@@ -40,14 +43,14 @@ typedef struct threshold_s
   char type_instance[DATA_MAX_NAME_LEN];
   gauge_t min;
   gauge_t max;
-  int invert;
+  int flags;
 } threshold_t;
 /* }}} */
 
 /*
  * Private (static) variables
  * {{{ */
-static avl_tree_t     *threshold_tree = NULL;
+static c_avl_tree_t   *threshold_tree = NULL;
 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
 /* }}} */
 
@@ -91,12 +94,12 @@ static int ut_threshold_add (const threshold_t *th)
   DEBUG ("ut_threshold_add: Adding entry `%s'", name);
 
   pthread_mutex_lock (&threshold_lock);
-  status = avl_insert (threshold_tree, name_copy, th_copy);
+  status = c_avl_insert (threshold_tree, name_copy, th_copy);
   pthread_mutex_unlock (&threshold_lock);
 
   if (status != 0)
   {
-    ERROR ("ut_threshold_add: avl_insert (%s) failed.", name);
+    ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
     sfree (name_copy);
     sfree (th_copy);
   }
@@ -170,11 +173,32 @@ static int ut_config_type_invert (threshold_t *th, oconfig_item_t *ci)
     return (-1);
   }
 
-  th->invert = (ci->values[0].value.boolean) ? 1 : 0;
+  if (ci->values[0].value.boolean)
+    th->flags |= UT_FLAG_INVERT;
+  else
+    th->flags &= ~UT_FLAG_INVERT;
 
   return (0);
 } /* int ut_config_type_invert */
 
+static int ut_config_type_persist (threshold_t *th, oconfig_item_t *ci)
+{
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+  {
+    WARNING ("threshold values: The `Persist' option needs exactly one "
+       "boolean argument.");
+    return (-1);
+  }
+
+  if (ci->values[0].value.boolean)
+    th->flags |= UT_FLAG_PERSIST;
+  else
+    th->flags &= ~UT_FLAG_PERSIST;
+
+  return (0);
+} /* int ut_config_type_persist */
+
 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
 {
   int i;
@@ -199,6 +223,9 @@ static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
   strncpy (th.type, ci->values[0].value.string, sizeof (th.type));
   th.type[sizeof (th.type) - 1] = '\0';
 
+  th.min = NAN;
+  th.max = NAN;
+
   for (i = 0; i < ci->children_num; i++)
   {
     oconfig_item_t *option = ci->children + i;
@@ -212,6 +239,8 @@ static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
       status = ut_config_type_min (&th, option);
     else if (strcasecmp ("Invert", option->key) == 0)
       status = ut_config_type_invert (&th, option);
+    else if (strcasecmp ("Persist", option->key) == 0)
+      status = ut_config_type_persist (&th, option);
     else
     {
       WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
@@ -353,10 +382,10 @@ int ut_config (const oconfig_item_t *ci)
 
   if (threshold_tree == NULL)
   {
-    threshold_tree = avl_create ((void *) strcmp);
+    threshold_tree = c_avl_create ((void *) strcmp);
     if (threshold_tree == NULL)
     {
-      ERROR ("ut_config: avl_create failed.");
+      ERROR ("ut_config: c_avl_create failed.");
       return (-1);
     }
   }
@@ -406,7 +435,7 @@ static threshold_t *threshold_get (const char *hostname,
       (type == NULL) ? "" : type, type_instance);
   name[sizeof (name) - 1] = '\0';
 
-  if (avl_get (threshold_tree, name, (void *) &th) == 0)
+  if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
     return (th);
   else
     return (NULL);
@@ -471,7 +500,7 @@ int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
   if (th == NULL)
     return (0);
 
-  DEBUG ("Found matching threshold");
+  DEBUG ("ut_check_threshold: Found matching threshold");
 
   values = uc_get_rate (ds, vl);
   if (values == NULL)
@@ -479,15 +508,26 @@ int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
 
   for (i = 0; i < ds->ds_num; i++)
   {
-    if ((th->min > values[i]) || (th->max < values[i]))
+    int out_of_range = 0;
+    int is_inverted = 0;
+
+    if ((th->flags & UT_FLAG_INVERT) != 0)
+      is_inverted = 1;
+    if ((!isnan (th->min) && (th->min > values[i]))
+       || (!isnan (th->max) && (th->max < values[i])))
+      out_of_range = 1;
+
+    /* If only one of these conditions is true, there is a problem */
+    if ((out_of_range + is_inverted) == 1)
     {
       notification_t n;
       char *buf;
       size_t bufsize;
       int status;
 
-      WARNING ("ut_check_threshold: ds[%s]: %lf <= !%lf <= %lf",
-         ds->ds[i].name, th->min, values[i], th->max);
+      WARNING ("ut_check_threshold: ds[%s]: %lf <= !%lf <= %lf (invert: %s)",
+         ds->ds[i].name, th->min, values[i], th->max,
+         is_inverted ? "true" : "false");
 
       buf = n.message;
       bufsize = sizeof (n.message);
@@ -517,11 +557,32 @@ int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
        bufsize -= status;
       }
 
-      status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
-         "%lf. That is %s the configured threshold of %lf.",
-         ds->ds[i].name, values[i],
-         (values[i] < th->min) ? "below" : "above",
-         (values[i] < th->min) ? th->min : th->max);
+      if (is_inverted)
+      {
+       if (!isnan (th->min) && !isnan (th->max))
+       {
+         status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
+             "%lf. That is within the critical region of %lf and %lf.",
+             ds->ds[i].name, values[i],
+             th->min, th->min);
+       }
+       else
+       {
+         status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
+             "%lf. That is %s the configured threshold of %lf.",
+             ds->ds[i].name, values[i],
+             isnan (th->min) ? "below" : "above",
+             isnan (th->min) ? th->max : th->min);
+       }
+      }
+      else /* (!is_inverted) */
+      {
+       status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
+           "%lf. That is %s the configured threshold of %lf.",
+           ds->ds[i].name, values[i],
+           (values[i] < th->min) ? "below" : "above",
+           (values[i] < th->min) ? th->min : th->max);
+      }
       buf += status;
       bufsize -= status;
 
@@ -540,4 +601,67 @@ int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
   return (0);
 } /* int ut_check_threshold */
 
+int ut_check_interesting (const char *name)
+{
+  char *name_copy = NULL;
+  char *host = NULL;
+  char *plugin = NULL;
+  char *plugin_instance = NULL;
+  char *type = NULL;
+  char *type_instance = NULL;
+  int status;
+  data_set_t ds;
+  value_list_t vl;
+  threshold_t *th;
+
+  /* If there is no tree nothing is interesting. */
+  if (threshold_tree == NULL)
+    return (0);
+
+  name_copy = strdup (name);
+  if (name_copy == NULL)
+  {
+    ERROR ("ut_check_interesting: strdup failed.");
+    return (-1);
+  }
+
+  status = parse_identifier (name_copy, &host,
+      &plugin, &plugin_instance, &type, &type_instance);
+  if (status != 0)
+  {
+    ERROR ("ut_check_interesting: parse_identifier failed.");
+    return (-1);
+  }
+
+  memset (&ds, '\0', sizeof (ds));
+  memset (&vl, '\0', sizeof (vl));
+
+  strncpy (vl.host, host, sizeof (vl.host));
+  vl.host[sizeof (vl.host) - 1] = '\0';
+  strncpy (vl.plugin, plugin, sizeof (vl.plugin));
+  vl.plugin[sizeof (vl.plugin) - 1] = '\0';
+  if (plugin_instance != NULL)
+  {
+    strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+    vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
+  }
+  strncpy (ds.type, type, sizeof (ds.type));
+  ds.type[sizeof (ds.type) - 1] = '\0';
+  if (type_instance != NULL)
+  {
+    strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+  }
+
+  sfree (name_copy);
+  host = plugin = plugin_instance = type = type_instance = NULL;
+
+  th = threshold_search (&ds, &vl);
+  if (th == NULL)
+    return (0);
+  if ((th->flags & UT_FLAG_PERSIST) == 0)
+    return (1);
+  return (2);
+} /* int ut_check_interesting */
+
 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */