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"
36 * Threshold management
37 * ====================
38 * The following functions add, delete, search, etc. configured thresholds to
39 * the underlying AVL trees.
43 * int ut_threshold_add
45 * Adds a threshold configuration to the list of thresholds. The threshold_t
46 * structure is copied and may be destroyed after this call. Returns zero on
47 * success, non-zero otherwise.
49 static int ut_threshold_add (const threshold_t *th)
51 char name[6 * DATA_MAX_NAME_LEN];
57 if (format_name (name, sizeof (name), th->host,
58 th->plugin, th->plugin_instance,
59 th->type, th->type_instance) != 0)
61 ERROR ("ut_threshold_add: format_name failed.");
65 name_copy = strdup (name);
66 if (name_copy == NULL)
68 ERROR ("ut_threshold_add: strdup failed.");
72 th_copy = malloc (sizeof (*th_copy));
76 ERROR ("ut_threshold_add: malloc failed.");
79 memcpy (th_copy, th, sizeof (threshold_t));
81 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
83 pthread_mutex_lock (&threshold_lock);
85 th_ptr = threshold_get (th->host, th->plugin, th->plugin_instance,
86 th->type, th->type_instance);
88 while ((th_ptr != NULL) && (th_ptr->next != NULL))
89 th_ptr = th_ptr->next;
91 if (th_ptr == NULL) /* no such threshold yet */
93 status = c_avl_insert (threshold_tree, name_copy, th_copy);
95 else /* th_ptr points to the last threshold in the list */
97 th_ptr->next = th_copy;
98 /* name_copy isn't needed */
102 pthread_mutex_unlock (&threshold_lock);
106 ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
112 } /* }}} int ut_threshold_add */
117 * The following approximately two hundred functions are used to handle the
118 * configuration and fill the threshold list.
120 static int ut_config_type_datasource (threshold_t *th, oconfig_item_t *ci)
122 if ((ci->values_num != 1)
123 || (ci->values[0].type != OCONFIG_TYPE_STRING))
125 WARNING ("threshold values: The `DataSource' option needs exactly one "
130 sstrncpy (th->data_source, ci->values[0].value.string,
131 sizeof (th->data_source));
134 } /* int ut_config_type_datasource */
136 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
138 if ((ci->values_num != 1)
139 || (ci->values[0].type != OCONFIG_TYPE_STRING))
141 WARNING ("threshold values: The `Instance' option needs exactly one "
146 sstrncpy (th->type_instance, ci->values[0].value.string,
147 sizeof (th->type_instance));
150 } /* int ut_config_type_instance */
152 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
154 if ((ci->values_num != 1)
155 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
157 WARNING ("threshold values: The `%s' option needs exactly one "
158 "number argument.", ci->key);
162 if (strcasecmp (ci->key, "WarningMax") == 0)
163 th->warning_max = ci->values[0].value.number;
165 th->failure_max = ci->values[0].value.number;
168 } /* int ut_config_type_max */
170 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
172 if ((ci->values_num != 1)
173 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
175 WARNING ("threshold values: The `%s' option needs exactly one "
176 "number argument.", ci->key);
180 if (strcasecmp (ci->key, "WarningMin") == 0)
181 th->warning_min = ci->values[0].value.number;
183 th->failure_min = ci->values[0].value.number;
186 } /* int ut_config_type_min */
188 static int ut_config_type_hits (threshold_t *th, oconfig_item_t *ci)
190 if ((ci->values_num != 1)
191 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
193 WARNING ("threshold values: The `%s' option needs exactly one "
194 "number argument.", ci->key);
198 th->hits = ci->values[0].value.number;
201 } /* int ut_config_type_hits */
203 static int ut_config_type_hysteresis (threshold_t *th, oconfig_item_t *ci)
205 if ((ci->values_num != 1)
206 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
208 WARNING ("threshold values: The `%s' option needs exactly one "
209 "number argument.", ci->key);
213 th->hysteresis = ci->values[0].value.number;
216 } /* int ut_config_type_hysteresis */
218 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
224 if ((ci->values_num != 1)
225 || (ci->values[0].type != OCONFIG_TYPE_STRING))
227 WARNING ("threshold values: The `Type' block needs exactly one string "
232 if (ci->children_num < 1)
234 WARNING ("threshold values: The `Type' block needs at least one option.");
238 memcpy (&th, th_orig, sizeof (th));
239 sstrncpy (th.type, ci->values[0].value.string, sizeof (th.type));
241 th.warning_min = NAN;
242 th.warning_max = NAN;
243 th.failure_min = NAN;
244 th.failure_max = NAN;
247 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
249 for (i = 0; i < ci->children_num; i++)
251 oconfig_item_t *option = ci->children + i;
253 if (strcasecmp ("Instance", option->key) == 0)
254 status = ut_config_type_instance (&th, option);
255 else if (strcasecmp ("DataSource", option->key) == 0)
256 status = ut_config_type_datasource (&th, option);
257 else if ((strcasecmp ("WarningMax", option->key) == 0)
258 || (strcasecmp ("FailureMax", option->key) == 0))
259 status = ut_config_type_max (&th, option);
260 else if ((strcasecmp ("WarningMin", option->key) == 0)
261 || (strcasecmp ("FailureMin", option->key) == 0))
262 status = ut_config_type_min (&th, option);
263 else if (strcasecmp ("Interesting", option->key) == 0)
264 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INTERESTING);
265 else if (strcasecmp ("Invert", option->key) == 0)
266 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INVERT);
267 else if (strcasecmp ("Persist", option->key) == 0)
268 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST);
269 else if (strcasecmp ("PersistOK", option->key) == 0)
270 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST_OK);
271 else if (strcasecmp ("Percentage", option->key) == 0)
272 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERCENTAGE);
273 else if (strcasecmp ("Hits", option->key) == 0)
274 status = ut_config_type_hits (&th, option);
275 else if (strcasecmp ("Hysteresis", option->key) == 0)
276 status = ut_config_type_hysteresis (&th, option);
279 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
280 "block.", option->key);
290 status = ut_threshold_add (&th);
294 } /* int ut_config_type */
296 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
298 if ((ci->values_num != 1)
299 || (ci->values[0].type != OCONFIG_TYPE_STRING))
301 WARNING ("threshold values: The `Instance' option needs exactly one "
306 sstrncpy (th->plugin_instance, ci->values[0].value.string,
307 sizeof (th->plugin_instance));
310 } /* int ut_config_plugin_instance */
312 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
318 if ((ci->values_num != 1)
319 || (ci->values[0].type != OCONFIG_TYPE_STRING))
321 WARNING ("threshold values: The `Plugin' block needs exactly one string "
326 if (ci->children_num < 1)
328 WARNING ("threshold values: The `Plugin' block needs at least one nested "
333 memcpy (&th, th_orig, sizeof (th));
334 sstrncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
336 for (i = 0; i < ci->children_num; i++)
338 oconfig_item_t *option = ci->children + i;
340 if (strcasecmp ("Type", option->key) == 0)
341 status = ut_config_type (&th, option);
342 else if (strcasecmp ("Instance", option->key) == 0)
343 status = ut_config_plugin_instance (&th, option);
346 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
347 "block.", option->key);
356 } /* int ut_config_plugin */
358 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
364 if ((ci->values_num != 1)
365 || (ci->values[0].type != OCONFIG_TYPE_STRING))
367 WARNING ("threshold values: The `Host' block needs exactly one string "
372 if (ci->children_num < 1)
374 WARNING ("threshold values: The `Host' block needs at least one nested "
379 memcpy (&th, th_orig, sizeof (th));
380 sstrncpy (th.host, ci->values[0].value.string, sizeof (th.host));
382 for (i = 0; i < ci->children_num; i++)
384 oconfig_item_t *option = ci->children + i;
386 if (strcasecmp ("Type", option->key) == 0)
387 status = ut_config_type (&th, option);
388 else if (strcasecmp ("Plugin", option->key) == 0)
389 status = ut_config_plugin (&th, option);
392 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
393 "block.", option->key);
402 } /* int ut_config_host */
404 * End of the functions used to configure threshold values.
409 * int ut_report_state
411 * Checks if the `state' differs from the old state and creates a notification
415 static int ut_report_state (const data_set_t *ds,
416 const value_list_t *vl,
417 const threshold_t *th,
418 const gauge_t *values,
430 /* Check if hits matched */
431 if ( (th->hits != 0) )
433 int hits = uc_get_hits(ds,vl);
434 /* STATE_OKAY resets hits unless PERSIST_OK flag is set. Hits resets if
435 * threshold is hit. */
436 if ( ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) ) || (hits > th->hits) )
438 DEBUG("ut_report_state: reset uc_get_hits = 0");
439 uc_set_hits(ds,vl,0); /* reset hit counter and notify */
441 DEBUG("ut_report_state: th->hits = %d, uc_get_hits = %d",th->hits,uc_get_hits(ds,vl));
442 (void) uc_inc_hits(ds,vl,1); /* increase hit counter */
445 } /* end check hits */
447 state_old = uc_get_state (ds, vl);
449 /* If the state didn't change, report if `persistent' is specified. If the
450 * state is `okay', then only report if `persist_ok` flag is set. */
451 if (state == state_old)
453 if ((th->flags & UT_FLAG_PERSIST) == 0)
455 else if ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) )
459 if (state != state_old)
460 uc_set_state (ds, vl, state);
462 NOTIFICATION_INIT_VL (&n, vl);
465 bufsize = sizeof (n.message);
467 if (state == STATE_OKAY)
468 n.severity = NOTIF_OKAY;
469 else if (state == STATE_WARNING)
470 n.severity = NOTIF_WARNING;
472 n.severity = NOTIF_FAILURE;
476 status = ssnprintf (buf, bufsize, "Host %s, plugin %s",
477 vl->host, vl->plugin);
481 if (vl->plugin_instance[0] != '\0')
483 status = ssnprintf (buf, bufsize, " (instance %s)",
484 vl->plugin_instance);
489 status = ssnprintf (buf, bufsize, " type %s", vl->type);
493 if (vl->type_instance[0] != '\0')
495 status = ssnprintf (buf, bufsize, " (instance %s)",
501 plugin_notification_meta_add_string (&n, "DataSource",
502 ds->ds[ds_index].name);
503 plugin_notification_meta_add_double (&n, "CurrentValue", values[ds_index]);
504 plugin_notification_meta_add_double (&n, "WarningMin", th->warning_min);
505 plugin_notification_meta_add_double (&n, "WarningMax", th->warning_max);
506 plugin_notification_meta_add_double (&n, "FailureMin", th->failure_min);
507 plugin_notification_meta_add_double (&n, "FailureMax", th->failure_max);
509 /* Send an okay notification */
510 if (state == STATE_OKAY)
512 if (state_old == STATE_MISSING)
513 ssnprintf (buf, bufsize, ": Value is no longer missing.");
515 ssnprintf (buf, bufsize,
516 ": All data sources are within range again. "
517 "Current value of \"%s\" is %f.",
518 ds->ds[ds_index].name, values[ds_index]);
525 min = (state == STATE_ERROR) ? th->failure_min : th->warning_min;
526 max = (state == STATE_ERROR) ? th->failure_max : th->warning_max;
528 if (th->flags & UT_FLAG_INVERT)
530 if (!isnan (min) && !isnan (max))
532 ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
533 "%f. That is within the %s region of %f%s and %f%s.",
534 ds->ds[ds_index].name, values[ds_index],
535 (state == STATE_ERROR) ? "failure" : "warning",
536 min, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "",
537 max, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
541 ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
542 "%f. That is %s the %s threshold of %f%s.",
543 ds->ds[ds_index].name, values[ds_index],
544 isnan (min) ? "below" : "above",
545 (state == STATE_ERROR) ? "failure" : "warning",
546 isnan (min) ? max : min,
547 ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
550 else if (th->flags & UT_FLAG_PERCENTAGE)
557 for (i = 0; i < vl->values_len; i++)
559 if (isnan (values[i]))
568 value = 100.0 * values[ds_index] / sum;
570 ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
571 "%g (%.2f%%). That is %s the %s threshold of %.2f%%.",
572 ds->ds[ds_index].name, values[ds_index], value,
573 (value < min) ? "below" : "above",
574 (state == STATE_ERROR) ? "failure" : "warning",
575 (value < min) ? min : max);
577 else /* is not inverted */
579 ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
580 "%f. That is %s the %s threshold of %f.",
581 ds->ds[ds_index].name, values[ds_index],
582 (values[ds_index] < min) ? "below" : "above",
583 (state == STATE_ERROR) ? "failure" : "warning",
584 (values[ds_index] < min) ? min : max);
588 plugin_dispatch_notification (&n);
590 plugin_notification_meta_free (n.meta);
592 } /* }}} int ut_report_state */
595 * int ut_check_one_data_source
597 * Checks one data source against the given threshold configuration. If the
598 * `DataSource' option is set in the threshold, and the name does NOT match,
599 * `okay' is returned. If the threshold does match, its failure and warning
600 * min and max values are checked and `failure' or `warning' is returned if
604 static int ut_check_one_data_source (const data_set_t *ds,
605 const value_list_t __attribute__((unused)) *vl,
606 const threshold_t *th,
607 const gauge_t *values,
613 int prev_state = STATE_OKAY;
615 /* check if this threshold applies to this data source */
618 ds_name = ds->ds[ds_index].name;
619 if ((th->data_source[0] != 0)
620 && (strcmp (ds_name, th->data_source) != 0))
624 if ((th->flags & UT_FLAG_INVERT) != 0)
630 /* XXX: This is an experimental code, not optimized, not fast, not reliable,
631 * and probably, do not work as you expect. Enjoy! :D */
632 if (th->hysteresis > 0)
634 prev_state = uc_get_state(ds,vl);
635 /* The purpose of hysteresis is elliminating flapping state when the value
636 * oscilates around the thresholds. In other words, what is important is
637 * the previous state; if the new value would trigger a transition, make
638 * sure that we artificially widen the range which is considered to apply
639 * for the previous state, and only trigger the notification if the value
640 * is outside of this expanded range.
642 * There is no hysteresis for the OKAY state.
644 gauge_t hysteresis_for_warning = 0, hysteresis_for_failure = 0;
648 hysteresis_for_failure = th->hysteresis;
651 hysteresis_for_warning = th->hysteresis;
654 /* do nothing -- the hysteresis only applies to the non-normal states */
658 if ((!isnan (th->failure_min) && (th->failure_min + hysteresis_for_failure > values[ds_index]))
659 || (!isnan (th->failure_max) && (th->failure_max - hysteresis_for_failure < values[ds_index])))
662 if ((!isnan (th->warning_min) && (th->warning_min + hysteresis_for_warning > values[ds_index]))
663 || (!isnan (th->warning_max) && (th->warning_max - hysteresis_for_warning < values[ds_index])))
667 else { /* no hysteresis */
668 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
669 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
672 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
673 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
678 return (STATE_ERROR);
681 return (STATE_WARNING);
684 } /* }}} int ut_check_one_data_source */
687 * int ut_check_one_threshold
689 * Checks all data sources of a value list against the given threshold, using
690 * the ut_check_one_data_source function above. Returns the worst status,
691 * which is `okay' if nothing has failed.
692 * Returns less than zero if the data set doesn't have any data sources.
694 static int ut_check_one_threshold (const data_set_t *ds,
695 const value_list_t *vl,
696 const threshold_t *th,
697 const gauge_t *values,
703 gauge_t values_copy[ds->ds_num];
705 memcpy (values_copy, values, sizeof (values_copy));
707 if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
714 WARNING ("ut_check_one_threshold: The %s type has only one data "
715 "source, but you have configured to check this as a percentage. "
716 "That doesn't make much sense, because the percentage will always "
717 "be 100%%!", ds->type);
720 /* Prepare `sum' and `num'. */
721 for (i = 0; i < ds->ds_num; i++)
722 if (!isnan (values[i]))
728 if ((num == 0) /* All data sources are undefined. */
729 || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
731 for (i = 0; i < ds->ds_num; i++)
732 values_copy[i] = NAN;
734 else /* We can actually calculate the percentage. */
736 for (i = 0; i < ds->ds_num; i++)
737 values_copy[i] = 100.0 * values[i] / sum;
739 } /* if (UT_FLAG_PERCENTAGE) */
741 for (i = 0; i < ds->ds_num; i++)
745 status = ut_check_one_data_source (ds, vl, th, values_copy, i);
751 } /* for (ds->ds_num) */
753 if (ret_ds_index != NULL)
754 *ret_ds_index = ds_index;
757 } /* }}} int ut_check_one_threshold */
760 * int ut_check_threshold
762 * Gets a list of matching thresholds and searches for the worst status by one
763 * of the thresholds. Then reports that status using the ut_report_state
765 * Returns zero on success and if no threshold has been configured. Returns
766 * less than zero on failure.
768 static int ut_check_threshold (const data_set_t *ds, const value_list_t *vl,
769 __attribute__((unused)) user_data_t *ud)
775 int worst_state = -1;
776 threshold_t *worst_th = NULL;
777 int worst_ds_index = -1;
779 if (threshold_tree == NULL)
782 /* Is this lock really necessary? So far, thresholds are only inserted at
784 pthread_mutex_lock (&threshold_lock);
785 th = threshold_search (vl);
786 pthread_mutex_unlock (&threshold_lock);
790 DEBUG ("ut_check_threshold: Found matching threshold(s)");
792 values = uc_get_rate (ds, vl);
800 status = ut_check_one_threshold (ds, vl, th, values, &ds_index);
803 ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
808 if (worst_state < status)
810 worst_state = status;
812 worst_ds_index = ds_index;
818 status = ut_report_state (ds, vl, worst_th, values,
819 worst_ds_index, worst_state);
822 ERROR ("ut_check_threshold: ut_report_state failed.");
830 } /* }}} int ut_check_threshold */
835 * This function is called whenever a value goes "missing".
837 static int ut_missing (const value_list_t *vl,
838 __attribute__((unused)) user_data_t *ud)
841 cdtime_t missing_time;
842 char identifier[6 * DATA_MAX_NAME_LEN];
846 if (threshold_tree == NULL)
849 th = threshold_search (vl);
850 /* dispatch notifications for "interesting" values only */
851 if ((th == NULL) || ((th->flags & UT_FLAG_INTERESTING) == 0))
855 missing_time = now - vl->time;
856 FORMAT_VL (identifier, sizeof (identifier), vl);
858 NOTIFICATION_INIT_VL (&n, vl);
859 ssnprintf (n.message, sizeof (n.message),
860 "%s has not been updated for %.3f seconds.",
861 identifier, CDTIME_T_TO_DOUBLE (missing_time));
864 plugin_dispatch_notification (&n);
867 } /* }}} int ut_missing */
869 static int ut_config (oconfig_item_t *ci)
873 int old_size = c_avl_size (threshold_tree);
877 if (threshold_tree == NULL)
879 threshold_tree = c_avl_create ((void *) strcmp);
880 if (threshold_tree == NULL)
882 ERROR ("ut_config: c_avl_create failed.");
887 memset (&th, '\0', sizeof (th));
888 th.warning_min = NAN;
889 th.warning_max = NAN;
890 th.failure_min = NAN;
891 th.failure_max = NAN;
895 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
897 for (i = 0; i < ci->children_num; i++)
899 oconfig_item_t *option = ci->children + i;
901 if (strcasecmp ("Type", option->key) == 0)
902 status = ut_config_type (&th, option);
903 else if (strcasecmp ("Plugin", option->key) == 0)
904 status = ut_config_plugin (&th, option);
905 else if (strcasecmp ("Host", option->key) == 0)
906 status = ut_config_host (&th, option);
909 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
917 /* register callbacks if this is the first time we see a valid config */
918 if ((old_size == 0) && (c_avl_size (threshold_tree) > 0))
920 plugin_register_missing ("threshold", ut_missing,
921 /* user data = */ NULL);
922 plugin_register_write ("threshold", ut_check_threshold,
923 /* user data = */ NULL);
927 } /* }}} int um_config */
929 void module_register (void)
931 plugin_register_complex_config ("threshold", ut_config);
934 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */