Merge branch 'collectd-4.10' into collectd-5.0
[collectd.git] / src / threshold.c
index 8939750..277b453 100644 (file)
@@ -628,7 +628,7 @@ static int ut_report_state (const data_set_t *ds,
   if (state != state_old)
     uc_set_state (ds, vl, state);
 
-  NOTIFICATION_INIT_VL (&n, vl, ds);
+  NOTIFICATION_INIT_VL (&n, vl);
 
   buf = n.message;
   bufsize = sizeof (n.message);
@@ -717,15 +717,41 @@ static int ut_report_state (const data_set_t *ds,
            ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
       }
     }
+    else if (th->flags & UT_FLAG_PERCENTAGE)
+    {
+      gauge_t value;
+      gauge_t sum;
+      int i;
+
+      sum = 0.0;
+      for (i = 0; i < vl->values_len; i++)
+      {
+        if (isnan (values[i]))
+          continue;
+
+        sum += values[i];
+      }
+
+      if (sum == 0.0)
+        value = NAN;
+      else
+        value = 100.0 * values[ds_index] / sum;
+
+      status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
+          "%g (%.2f%%). That is %s the %s threshold of %.2f%%.",
+          ds->ds[ds_index].name, values[ds_index], value,
+          (value < min) ? "below" : "above",
+          (state == STATE_ERROR) ? "failure" : "warning",
+          (value < min) ? min : max);
+    }
     else /* is not inverted */
     {
       status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
-         "%f. That is %s the %s threshold of %f%s.",
+         "%f. That is %s the %s threshold of %f.",
          ds->ds[ds_index].name, values[ds_index],
          (values[ds_index] < min) ? "below" : "above",
          (state == STATE_ERROR) ? "failure" : "warning",
-         (values[ds_index] < min) ? min : max,
-         ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
+         (values[ds_index] < min) ? min : max);
     }
     buf += status;
     bufsize -= status;
@@ -886,7 +912,7 @@ static int ut_check_one_threshold (const data_set_t *ds,
 } /* }}} int ut_check_one_threshold */
 
 /*
- * int ut_check_threshold (PUBLIC)
+ * int ut_check_threshold
  *
  * Gets a list of matching thresholds and searches for the worst status by one
  * of the thresholds. Then reports that status using the ut_report_state
@@ -958,9 +984,41 @@ static int ut_check_threshold (const data_set_t *ds, const value_list_t *vl,
   return (0);
 } /* }}} int ut_check_threshold */
 
+/*
+ * int ut_missing
+ *
+ * This function is called whenever a value goes "missing".
+ */
+static int ut_missing (const value_list_t *vl,
+    __attribute__((unused)) user_data_t *ud)
+{ /* {{{ */
+  threshold_t *th;
+  cdtime_t missing_time;
+  char identifier[6 * DATA_MAX_NAME_LEN];
+  notification_t n;
+
+  th = threshold_search (vl);
+  if (th == NULL)
+    return (0);
+
+  missing_time = cdtime () - vl->time;
+  FORMAT_VL (identifier, sizeof (identifier), vl);
+
+  NOTIFICATION_INIT_VL (&n, vl);
+  ssnprintf (n.message, sizeof (n.message),
+      "%s has not been updated for %.3f seconds.",
+      identifier, CDTIME_T_TO_DOUBLE (missing_time));
+
+  plugin_dispatch_notification (&n);
+
+  return (0);
+} /* }}} int ut_missing */
+
 void module_register (void)
 {
   plugin_register_complex_config ("threshold", ut_config);
+  plugin_register_missing ("threshold", ut_missing,
+      /* user data = */ NULL);
   plugin_register_write ("threshold", ut_check_threshold,
       /* user data = */ NULL);
 }