2 * collectd - src/threshold.c
3 * Copyright (C) 2007-2010 Florian Forster
4 * Copyright (C) 2008-2009 Sebastian Harl
5 * Copyright (C) 2009 Andrés J. Díaz
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at collectd.org>
22 * Sebastian Harl <sh at tokkee.org>
23 * Andrés J. Díaz <ajdiaz at connectical.com>
29 #include "utils_avltree.h"
30 #include "utils_cache.h"
31 #include "utils_threshold.h"
37 * Threshold management
38 * ====================
39 * The following functions add, delete, search, etc. configured thresholds to
40 * the underlying AVL trees.
44 * int ut_threshold_add
46 * Adds a threshold configuration to the list of thresholds. The threshold_t
47 * structure is copied and may be destroyed after this call. Returns zero on
48 * success, non-zero otherwise.
50 static int ut_threshold_add (const threshold_t *th)
52 char name[6 * DATA_MAX_NAME_LEN];
58 if (format_name (name, sizeof (name), th->host,
59 th->plugin, th->plugin_instance,
60 th->type, th->type_instance) != 0)
62 ERROR ("ut_threshold_add: format_name failed.");
66 name_copy = strdup (name);
67 if (name_copy == NULL)
69 ERROR ("ut_threshold_add: strdup failed.");
73 th_copy = (threshold_t *) malloc (sizeof (threshold_t));
77 ERROR ("ut_threshold_add: malloc failed.");
80 memcpy (th_copy, th, sizeof (threshold_t));
83 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
85 pthread_mutex_lock (&threshold_lock);
87 th_ptr = threshold_get (th->host, th->plugin, th->plugin_instance,
88 th->type, th->type_instance);
90 while ((th_ptr != NULL) && (th_ptr->next != NULL))
91 th_ptr = th_ptr->next;
93 if (th_ptr == NULL) /* no such threshold yet */
95 status = c_avl_insert (threshold_tree, name_copy, th_copy);
97 else /* th_ptr points to the last threshold in the list */
99 th_ptr->next = th_copy;
100 /* name_copy isn't needed */
104 pthread_mutex_unlock (&threshold_lock);
108 ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
114 } /* }}} int ut_threshold_add */
119 * The following approximately two hundred functions are used to handle the
120 * configuration and fill the threshold list.
122 static int ut_config_type_datasource (threshold_t *th, oconfig_item_t *ci)
124 if ((ci->values_num != 1)
125 || (ci->values[0].type != OCONFIG_TYPE_STRING))
127 WARNING ("threshold values: The `DataSource' option needs exactly one "
132 sstrncpy (th->data_source, ci->values[0].value.string,
133 sizeof (th->data_source));
136 } /* int ut_config_type_datasource */
138 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
140 if ((ci->values_num != 1)
141 || (ci->values[0].type != OCONFIG_TYPE_STRING))
143 WARNING ("threshold values: The `Instance' option needs exactly one "
148 sstrncpy (th->type_instance, ci->values[0].value.string,
149 sizeof (th->type_instance));
152 } /* int ut_config_type_instance */
154 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
156 if ((ci->values_num != 1)
157 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
159 WARNING ("threshold values: The `%s' option needs exactly one "
160 "number argument.", ci->key);
164 if (strcasecmp (ci->key, "WarningMax") == 0)
165 th->warning_max = ci->values[0].value.number;
167 th->failure_max = ci->values[0].value.number;
170 } /* int ut_config_type_max */
172 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
174 if ((ci->values_num != 1)
175 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
177 WARNING ("threshold values: The `%s' option needs exactly one "
178 "number argument.", ci->key);
182 if (strcasecmp (ci->key, "WarningMin") == 0)
183 th->warning_min = ci->values[0].value.number;
185 th->failure_min = ci->values[0].value.number;
188 } /* int ut_config_type_min */
190 static int ut_config_type_hits (threshold_t *th, oconfig_item_t *ci)
192 if ((ci->values_num != 1)
193 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
195 WARNING ("threshold values: The `%s' option needs exactly one "
196 "number argument.", ci->key);
200 th->hits = ci->values[0].value.number;
203 } /* int ut_config_type_hits */
205 static int ut_config_type_hysteresis (threshold_t *th, oconfig_item_t *ci)
207 if ((ci->values_num != 1)
208 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
210 WARNING ("threshold values: The `%s' option needs exactly one "
211 "number argument.", ci->key);
215 th->hysteresis = ci->values[0].value.number;
218 } /* int ut_config_type_hysteresis */
220 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
226 if ((ci->values_num != 1)
227 || (ci->values[0].type != OCONFIG_TYPE_STRING))
229 WARNING ("threshold values: The `Type' block needs exactly one string "
234 if (ci->children_num < 1)
236 WARNING ("threshold values: The `Type' block needs at least one option.");
240 memcpy (&th, th_orig, sizeof (th));
241 sstrncpy (th.type, ci->values[0].value.string, sizeof (th.type));
243 th.warning_min = NAN;
244 th.warning_max = NAN;
245 th.failure_min = NAN;
246 th.failure_max = NAN;
249 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
251 for (i = 0; i < ci->children_num; i++)
253 oconfig_item_t *option = ci->children + i;
256 if (strcasecmp ("Instance", option->key) == 0)
257 status = ut_config_type_instance (&th, option);
258 else if (strcasecmp ("DataSource", option->key) == 0)
259 status = ut_config_type_datasource (&th, option);
260 else if ((strcasecmp ("WarningMax", option->key) == 0)
261 || (strcasecmp ("FailureMax", option->key) == 0))
262 status = ut_config_type_max (&th, option);
263 else if ((strcasecmp ("WarningMin", option->key) == 0)
264 || (strcasecmp ("FailureMin", option->key) == 0))
265 status = ut_config_type_min (&th, option);
266 else if (strcasecmp ("Interesting", option->key) == 0)
267 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INTERESTING);
268 else if (strcasecmp ("Invert", option->key) == 0)
269 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INVERT);
270 else if (strcasecmp ("Persist", option->key) == 0)
271 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST);
272 else if (strcasecmp ("PersistOK", option->key) == 0)
273 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST_OK);
274 else if (strcasecmp ("Percentage", option->key) == 0)
275 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERCENTAGE);
276 else if (strcasecmp ("Hits", option->key) == 0)
277 status = ut_config_type_hits (&th, option);
278 else if (strcasecmp ("Hysteresis", option->key) == 0)
279 status = ut_config_type_hysteresis (&th, option);
282 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
283 "block.", option->key);
293 status = ut_threshold_add (&th);
297 } /* int ut_config_type */
299 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
301 if ((ci->values_num != 1)
302 || (ci->values[0].type != OCONFIG_TYPE_STRING))
304 WARNING ("threshold values: The `Instance' option needs exactly one "
309 sstrncpy (th->plugin_instance, ci->values[0].value.string,
310 sizeof (th->plugin_instance));
313 } /* int ut_config_plugin_instance */
315 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
321 if ((ci->values_num != 1)
322 || (ci->values[0].type != OCONFIG_TYPE_STRING))
324 WARNING ("threshold values: The `Plugin' block needs exactly one string "
329 if (ci->children_num < 1)
331 WARNING ("threshold values: The `Plugin' block needs at least one nested "
336 memcpy (&th, th_orig, sizeof (th));
337 sstrncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
339 for (i = 0; i < ci->children_num; i++)
341 oconfig_item_t *option = ci->children + i;
344 if (strcasecmp ("Type", option->key) == 0)
345 status = ut_config_type (&th, option);
346 else if (strcasecmp ("Instance", option->key) == 0)
347 status = ut_config_plugin_instance (&th, option);
350 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
351 "block.", option->key);
360 } /* int ut_config_plugin */
362 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
368 if ((ci->values_num != 1)
369 || (ci->values[0].type != OCONFIG_TYPE_STRING))
371 WARNING ("threshold values: The `Host' block needs exactly one string "
376 if (ci->children_num < 1)
378 WARNING ("threshold values: The `Host' block needs at least one nested "
383 memcpy (&th, th_orig, sizeof (th));
384 sstrncpy (th.host, ci->values[0].value.string, sizeof (th.host));
386 for (i = 0; i < ci->children_num; i++)
388 oconfig_item_t *option = ci->children + i;
391 if (strcasecmp ("Type", option->key) == 0)
392 status = ut_config_type (&th, option);
393 else if (strcasecmp ("Plugin", option->key) == 0)
394 status = ut_config_plugin (&th, option);
397 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
398 "block.", option->key);
407 } /* int ut_config_host */
409 * End of the functions used to configure threshold values.
414 * int ut_report_state
416 * Checks if the `state' differs from the old state and creates a notification
420 static int ut_report_state (const data_set_t *ds,
421 const value_list_t *vl,
422 const threshold_t *th,
423 const gauge_t *values,
435 /* Check if hits matched */
436 if ( (th->hits != 0) )
438 int hits = uc_get_hits(ds,vl);
439 /* STATE_OKAY resets hits unless PERSIST_OK flag is set. Hits resets if
440 * threshold is hit. */
441 if ( ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) ) || (hits > th->hits) )
443 DEBUG("ut_report_state: reset uc_get_hits = 0");
444 uc_set_hits(ds,vl,0); /* reset hit counter and notify */
446 DEBUG("ut_report_state: th->hits = %d, uc_get_hits = %d",th->hits,uc_get_hits(ds,vl));
447 (void) uc_inc_hits(ds,vl,1); /* increase hit counter */
450 } /* end check hits */
452 state_old = uc_get_state (ds, vl);
454 /* If the state didn't change, report if `persistent' is specified. If the
455 * state is `okay', then only report if `persist_ok` flag is set. */
456 if (state == state_old)
458 if ((th->flags & UT_FLAG_PERSIST) == 0)
460 else if ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) )
464 if (state != state_old)
465 uc_set_state (ds, vl, state);
467 NOTIFICATION_INIT_VL (&n, vl);
470 bufsize = sizeof (n.message);
472 if (state == STATE_OKAY)
473 n.severity = NOTIF_OKAY;
474 else if (state == STATE_WARNING)
475 n.severity = NOTIF_WARNING;
477 n.severity = NOTIF_FAILURE;
481 status = ssnprintf (buf, bufsize, "Host %s, plugin %s",
482 vl->host, vl->plugin);
486 if (vl->plugin_instance[0] != '\0')
488 status = ssnprintf (buf, bufsize, " (instance %s)",
489 vl->plugin_instance);
494 status = ssnprintf (buf, bufsize, " type %s", vl->type);
498 if (vl->type_instance[0] != '\0')
500 status = ssnprintf (buf, bufsize, " (instance %s)",
506 plugin_notification_meta_add_string (&n, "DataSource",
507 ds->ds[ds_index].name);
508 plugin_notification_meta_add_double (&n, "CurrentValue", values[ds_index]);
509 plugin_notification_meta_add_double (&n, "WarningMin", th->warning_min);
510 plugin_notification_meta_add_double (&n, "WarningMax", th->warning_max);
511 plugin_notification_meta_add_double (&n, "FailureMin", th->failure_min);
512 plugin_notification_meta_add_double (&n, "FailureMax", th->failure_max);
514 /* Send an okay notification */
515 if (state == STATE_OKAY)
517 if (state_old == STATE_MISSING)
518 status = ssnprintf (buf, bufsize,
519 ": Value is no longer missing.");
521 status = ssnprintf (buf, bufsize,
522 ": All data sources are within range again. "
523 "Current value of \"%s\" is %f.",
524 ds->ds[ds_index].name, values[ds_index]);
533 min = (state == STATE_ERROR) ? th->failure_min : th->warning_min;
534 max = (state == STATE_ERROR) ? th->failure_max : th->warning_max;
536 if (th->flags & UT_FLAG_INVERT)
538 if (!isnan (min) && !isnan (max))
540 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
541 "%f. That is within the %s region of %f%s and %f%s.",
542 ds->ds[ds_index].name, values[ds_index],
543 (state == STATE_ERROR) ? "failure" : "warning",
544 min, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "",
545 max, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
549 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
550 "%f. That is %s the %s threshold of %f%s.",
551 ds->ds[ds_index].name, values[ds_index],
552 isnan (min) ? "below" : "above",
553 (state == STATE_ERROR) ? "failure" : "warning",
554 isnan (min) ? max : min,
555 ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
558 else if (th->flags & UT_FLAG_PERCENTAGE)
565 for (i = 0; i < vl->values_len; i++)
567 if (isnan (values[i]))
576 value = 100.0 * values[ds_index] / sum;
578 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
579 "%g (%.2f%%). That is %s the %s threshold of %.2f%%.",
580 ds->ds[ds_index].name, values[ds_index], value,
581 (value < min) ? "below" : "above",
582 (state == STATE_ERROR) ? "failure" : "warning",
583 (value < min) ? min : max);
585 else /* is not inverted */
587 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
588 "%f. That is %s the %s threshold of %f.",
589 ds->ds[ds_index].name, values[ds_index],
590 (values[ds_index] < min) ? "below" : "above",
591 (state == STATE_ERROR) ? "failure" : "warning",
592 (values[ds_index] < min) ? min : max);
598 plugin_dispatch_notification (&n);
600 plugin_notification_meta_free (n.meta);
602 } /* }}} int ut_report_state */
605 * int ut_check_one_data_source
607 * Checks one data source against the given threshold configuration. If the
608 * `DataSource' option is set in the threshold, and the name does NOT match,
609 * `okay' is returned. If the threshold does match, its failure and warning
610 * min and max values are checked and `failure' or `warning' is returned if
614 static int ut_check_one_data_source (const data_set_t *ds,
615 const value_list_t __attribute__((unused)) *vl,
616 const threshold_t *th,
617 const gauge_t *values,
623 int prev_state = STATE_OKAY;
625 /* check if this threshold applies to this data source */
628 ds_name = ds->ds[ds_index].name;
629 if ((th->data_source[0] != 0)
630 && (strcmp (ds_name, th->data_source) != 0))
634 if ((th->flags & UT_FLAG_INVERT) != 0)
640 /* XXX: This is an experimental code, not optimized, not fast, not reliable,
641 * and probably, do not work as you expect. Enjoy! :D */
642 if (th->hysteresis > 0)
644 prev_state = uc_get_state(ds,vl);
645 /* The purpose of hysteresis is elliminating flapping state when the value
646 * oscilates around the thresholds. In other words, what is important is
647 * the previous state; if the new value would trigger a transition, make
648 * sure that we artificially widen the range which is considered to apply
649 * for the previous state, and only trigger the notification if the value
650 * is outside of this expanded range.
652 * There is no hysteresis for the OKAY state.
654 gauge_t hysteresis_for_warning = 0, hysteresis_for_failure = 0;
658 hysteresis_for_failure = th->hysteresis;
661 hysteresis_for_warning = th->hysteresis;
664 /* do nothing -- the hysteresis only applies to the non-normal states */
668 if ((!isnan (th->failure_min) && (th->failure_min + hysteresis_for_failure > values[ds_index]))
669 || (!isnan (th->failure_max) && (th->failure_max - hysteresis_for_failure < values[ds_index])))
672 if ((!isnan (th->warning_min) && (th->warning_min + hysteresis_for_warning > values[ds_index]))
673 || (!isnan (th->warning_max) && (th->warning_max - hysteresis_for_warning < values[ds_index])))
677 else { /* no hysteresis */
678 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
679 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
682 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
683 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
688 return (STATE_ERROR);
691 return (STATE_WARNING);
694 } /* }}} int ut_check_one_data_source */
697 * int ut_check_one_threshold
699 * Checks all data sources of a value list against the given threshold, using
700 * the ut_check_one_data_source function above. Returns the worst status,
701 * which is `okay' if nothing has failed.
702 * Returns less than zero if the data set doesn't have any data sources.
704 static int ut_check_one_threshold (const data_set_t *ds,
705 const value_list_t *vl,
706 const threshold_t *th,
707 const gauge_t *values,
713 gauge_t values_copy[ds->ds_num];
715 memcpy (values_copy, values, sizeof (values_copy));
717 if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
724 WARNING ("ut_check_one_threshold: The %s type has only one data "
725 "source, but you have configured to check this as a percentage. "
726 "That doesn't make much sense, because the percentage will always "
727 "be 100%%!", ds->type);
730 /* Prepare `sum' and `num'. */
731 for (i = 0; i < ds->ds_num; i++)
732 if (!isnan (values[i]))
738 if ((num == 0) /* All data sources are undefined. */
739 || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
741 for (i = 0; i < ds->ds_num; i++)
742 values_copy[i] = NAN;
744 else /* We can actually calculate the percentage. */
746 for (i = 0; i < ds->ds_num; i++)
747 values_copy[i] = 100.0 * values[i] / sum;
749 } /* if (UT_FLAG_PERCENTAGE) */
751 for (i = 0; i < ds->ds_num; i++)
755 status = ut_check_one_data_source (ds, vl, th, values_copy, i);
761 } /* for (ds->ds_num) */
763 if (ret_ds_index != NULL)
764 *ret_ds_index = ds_index;
767 } /* }}} int ut_check_one_threshold */
770 * int ut_check_threshold
772 * Gets a list of matching thresholds and searches for the worst status by one
773 * of the thresholds. Then reports that status using the ut_report_state
775 * Returns zero on success and if no threshold has been configured. Returns
776 * less than zero on failure.
778 static int ut_check_threshold (const data_set_t *ds, const value_list_t *vl,
779 __attribute__((unused)) user_data_t *ud)
785 int worst_state = -1;
786 threshold_t *worst_th = NULL;
787 int worst_ds_index = -1;
789 if (threshold_tree == NULL)
792 /* Is this lock really necessary? So far, thresholds are only inserted at
794 pthread_mutex_lock (&threshold_lock);
795 th = threshold_search (vl);
796 pthread_mutex_unlock (&threshold_lock);
800 DEBUG ("ut_check_threshold: Found matching threshold(s)");
802 values = uc_get_rate (ds, vl);
810 status = ut_check_one_threshold (ds, vl, th, values, &ds_index);
813 ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
818 if (worst_state < status)
820 worst_state = status;
822 worst_ds_index = ds_index;
828 status = ut_report_state (ds, vl, worst_th, values,
829 worst_ds_index, worst_state);
832 ERROR ("ut_check_threshold: ut_report_state failed.");
840 } /* }}} int ut_check_threshold */
845 * This function is called whenever a value goes "missing".
847 static int ut_missing (const value_list_t *vl,
848 __attribute__((unused)) user_data_t *ud)
851 cdtime_t missing_time;
852 char identifier[6 * DATA_MAX_NAME_LEN];
856 if (threshold_tree == NULL)
859 th = threshold_search (vl);
860 /* dispatch notifications for "interesting" values only */
861 if ((th == NULL) || ((th->flags & UT_FLAG_INTERESTING) == 0))
865 missing_time = now - vl->time;
866 FORMAT_VL (identifier, sizeof (identifier), vl);
868 NOTIFICATION_INIT_VL (&n, vl);
869 ssnprintf (n.message, sizeof (n.message),
870 "%s has not been updated for %.3f seconds.",
871 identifier, CDTIME_T_TO_DOUBLE (missing_time));
874 plugin_dispatch_notification (&n);
877 } /* }}} int ut_missing */
879 int ut_config (oconfig_item_t *ci)
886 if (threshold_tree == NULL)
888 threshold_tree = c_avl_create ((void *) strcmp);
889 if (threshold_tree == NULL)
891 ERROR ("ut_config: c_avl_create failed.");
896 memset (&th, '\0', sizeof (th));
897 th.warning_min = NAN;
898 th.warning_max = NAN;
899 th.failure_min = NAN;
900 th.failure_max = NAN;
904 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
906 for (i = 0; i < ci->children_num; i++)
908 oconfig_item_t *option = ci->children + i;
911 if (strcasecmp ("Type", option->key) == 0)
912 status = ut_config_type (&th, option);
913 else if (strcasecmp ("Plugin", option->key) == 0)
914 status = ut_config_plugin (&th, option);
915 else if (strcasecmp ("Host", option->key) == 0)
916 status = ut_config_host (&th, option);
919 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
927 if (c_avl_size (threshold_tree) > 0) {
928 plugin_register_missing ("threshold", ut_missing,
929 /* user data = */ NULL);
930 plugin_register_write ("threshold", ut_check_threshold,
931 /* user data = */ NULL);
935 } /* }}} int um_config */
937 void module_register (void)
939 plugin_register_complex_config ("threshold", ut_config);
942 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */