2 * collectd - src/utils_threshold.c
3 * Copyright (C) 2007,2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
32 * Private data structures
34 #define UT_FLAG_INVERT 0x01
35 #define UT_FLAG_PERSIST 0x02
37 typedef struct threshold_s
39 char host[DATA_MAX_NAME_LEN];
40 char plugin[DATA_MAX_NAME_LEN];
41 char plugin_instance[DATA_MAX_NAME_LEN];
42 char type[DATA_MAX_NAME_LEN];
43 char type_instance[DATA_MAX_NAME_LEN];
44 char data_source[DATA_MAX_NAME_LEN];
50 struct threshold_s *next;
55 * Private (static) variables
57 static c_avl_tree_t *threshold_tree = NULL;
58 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
62 * Threshold management
63 * ====================
64 * The following functions add, delete, search, etc. configured thresholds to
65 * the underlying AVL trees.
67 static threshold_t *threshold_get (const char *hostname,
68 const char *plugin, const char *plugin_instance,
69 const char *type, const char *type_instance)
71 char name[6 * DATA_MAX_NAME_LEN];
72 threshold_t *th = NULL;
74 format_name (name, sizeof (name),
75 (hostname == NULL) ? "" : hostname,
76 (plugin == NULL) ? "" : plugin, plugin_instance,
77 (type == NULL) ? "" : type, type_instance);
78 name[sizeof (name) - 1] = '\0';
80 if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
84 } /* threshold_t *threshold_get */
86 static int ut_threshold_add (const threshold_t *th)
88 char name[6 * DATA_MAX_NAME_LEN];
94 if (format_name (name, sizeof (name), th->host,
95 th->plugin, th->plugin_instance,
96 th->type, th->type_instance) != 0)
98 ERROR ("ut_threshold_add: format_name failed.");
102 name_copy = strdup (name);
103 if (name_copy == NULL)
105 ERROR ("ut_threshold_add: strdup failed.");
109 th_copy = (threshold_t *) malloc (sizeof (threshold_t));
113 ERROR ("ut_threshold_add: malloc failed.");
116 memcpy (th_copy, th, sizeof (threshold_t));
119 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
121 pthread_mutex_lock (&threshold_lock);
123 th_ptr = threshold_get (th->host, th->plugin, th->plugin_instance,
124 th->type, th->type_instance);
126 while ((th_ptr != NULL) && (th_ptr->next != NULL))
127 th_ptr = th_ptr->next;
129 if (th_ptr == NULL) /* no such threshold yet */
131 status = c_avl_insert (threshold_tree, name_copy, th_copy);
133 else /* th_ptr points to the last threshold in the list */
135 th_ptr->next = th_copy;
136 /* name_copy isn't needed */
140 pthread_mutex_unlock (&threshold_lock);
144 ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
150 } /* int ut_threshold_add */
152 * End of the threshold management functions
158 * The following approximately two hundred functions are used to handle the
159 * configuration and fill the threshold list.
161 static int ut_config_type_datasource (threshold_t *th, oconfig_item_t *ci)
163 if ((ci->values_num != 1)
164 || (ci->values[0].type != OCONFIG_TYPE_STRING))
166 WARNING ("threshold values: The `DataSource' option needs exactly one "
171 sstrncpy (th->data_source, ci->values[0].value.string,
172 sizeof (th->data_source));
175 } /* int ut_config_type_datasource */
177 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
179 if ((ci->values_num != 1)
180 || (ci->values[0].type != OCONFIG_TYPE_STRING))
182 WARNING ("threshold values: The `Instance' option needs exactly one "
187 sstrncpy (th->type_instance, ci->values[0].value.string,
188 sizeof (th->type_instance));
191 } /* int ut_config_type_instance */
193 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
195 if ((ci->values_num != 1)
196 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
198 WARNING ("threshold values: The `%s' option needs exactly one "
199 "number argument.", ci->key);
203 if (strcasecmp (ci->key, "WarningMax") == 0)
204 th->warning_max = ci->values[0].value.number;
206 th->failure_max = ci->values[0].value.number;
209 } /* int ut_config_type_max */
211 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
213 if ((ci->values_num != 1)
214 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
216 WARNING ("threshold values: The `%s' option needs exactly one "
217 "number argument.", ci->key);
221 if (strcasecmp (ci->key, "WarningMin") == 0)
222 th->warning_min = ci->values[0].value.number;
224 th->failure_min = ci->values[0].value.number;
227 } /* int ut_config_type_min */
229 static int ut_config_type_invert (threshold_t *th, oconfig_item_t *ci)
231 if ((ci->values_num != 1)
232 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
234 WARNING ("threshold values: The `Invert' option needs exactly one "
235 "boolean argument.");
239 if (ci->values[0].value.boolean)
240 th->flags |= UT_FLAG_INVERT;
242 th->flags &= ~UT_FLAG_INVERT;
245 } /* int ut_config_type_invert */
247 static int ut_config_type_persist (threshold_t *th, oconfig_item_t *ci)
249 if ((ci->values_num != 1)
250 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
252 WARNING ("threshold values: The `Persist' option needs exactly one "
253 "boolean argument.");
257 if (ci->values[0].value.boolean)
258 th->flags |= UT_FLAG_PERSIST;
260 th->flags &= ~UT_FLAG_PERSIST;
263 } /* int ut_config_type_persist */
265 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
271 if ((ci->values_num != 1)
272 || (ci->values[0].type != OCONFIG_TYPE_STRING))
274 WARNING ("threshold values: The `Type' block needs exactly one string "
279 if (ci->children_num < 1)
281 WARNING ("threshold values: The `Type' block needs at least one option.");
285 memcpy (&th, th_orig, sizeof (th));
286 sstrncpy (th.type, ci->values[0].value.string, sizeof (th.type));
288 th.warning_min = NAN;
289 th.warning_max = NAN;
290 th.failure_min = NAN;
291 th.failure_max = NAN;
293 for (i = 0; i < ci->children_num; i++)
295 oconfig_item_t *option = ci->children + i;
298 if (strcasecmp ("Instance", option->key) == 0)
299 status = ut_config_type_instance (&th, option);
300 else if (strcasecmp ("DataSource", option->key) == 0)
301 status = ut_config_type_datasource (&th, option);
302 else if ((strcasecmp ("WarningMax", option->key) == 0)
303 || (strcasecmp ("FailureMax", option->key) == 0))
304 status = ut_config_type_max (&th, option);
305 else if ((strcasecmp ("WarningMin", option->key) == 0)
306 || (strcasecmp ("FailureMin", option->key) == 0))
307 status = ut_config_type_min (&th, option);
308 else if (strcasecmp ("Invert", option->key) == 0)
309 status = ut_config_type_invert (&th, option);
310 else if (strcasecmp ("Persist", option->key) == 0)
311 status = ut_config_type_persist (&th, option);
314 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
315 "block.", option->key);
325 status = ut_threshold_add (&th);
329 } /* int ut_config_type */
331 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
333 if ((ci->values_num != 1)
334 || (ci->values[0].type != OCONFIG_TYPE_STRING))
336 WARNING ("threshold values: The `Instance' option needs exactly one "
341 sstrncpy (th->plugin_instance, ci->values[0].value.string,
342 sizeof (th->plugin_instance));
345 } /* int ut_config_plugin_instance */
347 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
353 if ((ci->values_num != 1)
354 || (ci->values[0].type != OCONFIG_TYPE_STRING))
356 WARNING ("threshold values: The `Plugin' block needs exactly one string "
361 if (ci->children_num < 1)
363 WARNING ("threshold values: The `Plugin' block needs at least one nested "
368 memcpy (&th, th_orig, sizeof (th));
369 sstrncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
371 for (i = 0; i < ci->children_num; i++)
373 oconfig_item_t *option = ci->children + i;
376 if (strcasecmp ("Type", option->key) == 0)
377 status = ut_config_type (&th, option);
378 else if (strcasecmp ("Instance", option->key) == 0)
379 status = ut_config_plugin_instance (&th, option);
382 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
383 "block.", option->key);
392 } /* int ut_config_plugin */
394 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
400 if ((ci->values_num != 1)
401 || (ci->values[0].type != OCONFIG_TYPE_STRING))
403 WARNING ("threshold values: The `Host' block needs exactly one string "
408 if (ci->children_num < 1)
410 WARNING ("threshold values: The `Host' block needs at least one nested "
415 memcpy (&th, th_orig, sizeof (th));
416 sstrncpy (th.host, ci->values[0].value.string, sizeof (th.host));
418 for (i = 0; i < ci->children_num; i++)
420 oconfig_item_t *option = ci->children + i;
423 if (strcasecmp ("Type", option->key) == 0)
424 status = ut_config_type (&th, option);
425 else if (strcasecmp ("Plugin", option->key) == 0)
426 status = ut_config_plugin (&th, option);
429 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
430 "block.", option->key);
439 } /* int ut_config_host */
441 int ut_config (const oconfig_item_t *ci)
448 if (ci->values_num != 0)
450 ERROR ("threshold values: The `Threshold' block may not have any "
455 if (threshold_tree == NULL)
457 threshold_tree = c_avl_create ((void *) strcmp);
458 if (threshold_tree == NULL)
460 ERROR ("ut_config: c_avl_create failed.");
465 memset (&th, '\0', sizeof (th));
466 th.warning_min = NAN;
467 th.warning_max = NAN;
468 th.failure_min = NAN;
469 th.failure_max = NAN;
471 for (i = 0; i < ci->children_num; i++)
473 oconfig_item_t *option = ci->children + i;
476 if (strcasecmp ("Type", option->key) == 0)
477 status = ut_config_type (&th, option);
478 else if (strcasecmp ("Plugin", option->key) == 0)
479 status = ut_config_plugin (&th, option);
480 else if (strcasecmp ("Host", option->key) == 0)
481 status = ut_config_host (&th, option);
484 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
493 } /* int um_config */
495 * End of the functions used to configure threshold values.
499 static threshold_t *threshold_search (const value_list_t *vl)
503 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
504 vl->type, vl->type_instance)) != NULL)
506 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
507 vl->type, NULL)) != NULL)
509 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
510 vl->type, vl->type_instance)) != NULL)
512 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
513 vl->type, NULL)) != NULL)
515 else if ((th = threshold_get (vl->host, "", NULL,
516 vl->type, vl->type_instance)) != NULL)
518 else if ((th = threshold_get (vl->host, "", NULL,
519 vl->type, NULL)) != NULL)
521 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
522 vl->type, vl->type_instance)) != NULL)
524 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
525 vl->type, NULL)) != NULL)
527 else if ((th = threshold_get ("", vl->plugin, NULL,
528 vl->type, vl->type_instance)) != NULL)
530 else if ((th = threshold_get ("", vl->plugin, NULL,
531 vl->type, NULL)) != NULL)
533 else if ((th = threshold_get ("", "", NULL,
534 vl->type, vl->type_instance)) != NULL)
536 else if ((th = threshold_get ("", "", NULL,
537 vl->type, NULL)) != NULL)
541 } /* threshold_t *threshold_search */
544 * int ut_report_state
546 * Checks if the `state' differs from the old state and creates a notification
550 static int ut_report_state (const data_set_t *ds,
551 const value_list_t *vl,
552 const threshold_t *th,
553 const gauge_t *values,
565 state_old = uc_get_state (ds, vl);
567 /* If the state didn't change, only report if `persistent' is specified and
568 * the state is not `okay'. */
569 if (state == state_old)
571 if ((th->flags & UT_FLAG_PERSIST) == 0)
573 else if (state == STATE_OKAY)
577 if (state != state_old)
578 uc_set_state (ds, vl, state);
580 NOTIFICATION_INIT_VL (&n, vl, ds);
583 bufsize = sizeof (n.message);
585 if (state == STATE_OKAY)
586 n.severity = NOTIF_OKAY;
587 else if (state == STATE_WARNING)
588 n.severity = NOTIF_WARNING;
590 n.severity = NOTIF_FAILURE;
594 status = ssnprintf (buf, bufsize, "Host %s, plugin %s",
595 vl->host, vl->plugin);
599 if (vl->plugin_instance[0] != '\0')
601 status = ssnprintf (buf, bufsize, " (instance %s)",
602 vl->plugin_instance);
607 status = ssnprintf (buf, bufsize, " type %s", vl->type);
611 if (vl->type_instance[0] != '\0')
613 status = ssnprintf (buf, bufsize, " (instance %s)",
619 plugin_notification_meta_add_string (&n, "DataSource",
620 ds->ds[ds_index].name);
621 plugin_notification_meta_add_double (&n, "CurrentValue", values[ds_index]);
622 plugin_notification_meta_add_double (&n, "WarningMin", th->warning_min);
623 plugin_notification_meta_add_double (&n, "WarningMax", th->warning_max);
624 plugin_notification_meta_add_double (&n, "FailureMin", th->failure_min);
625 plugin_notification_meta_add_double (&n, "FailureMax", th->failure_max);
627 /* Send an okay notification */
628 if (state == STATE_OKAY)
630 status = ssnprintf (buf, bufsize, ": All data sources are within range again.");
639 min = (state == STATE_ERROR) ? th->failure_min : th->warning_min;
640 max = (state == STATE_ERROR) ? th->failure_max : th->warning_max;
642 if (th->flags & UT_FLAG_INVERT)
644 if (!isnan (min) && !isnan (max))
646 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
647 "%f. That is within the %s region of %f and %f.",
648 ds->ds[ds_index].name, values[ds_index],
649 (state == STATE_ERROR) ? "failure" : "warning",
654 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
655 "%f. That is %s the %s threshold of %f.",
656 ds->ds[ds_index].name, values[ds_index],
657 isnan (min) ? "below" : "above",
658 (state == STATE_ERROR) ? "failure" : "warning",
659 isnan (min) ? max : min);
662 else /* is not inverted */
664 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
665 "%f. That is %s the %s threshold of %f.",
666 ds->ds[ds_index].name, values[ds_index],
667 (values[ds_index] < min) ? "below" : "above",
668 (state == STATE_ERROR) ? "failure" : "warning",
669 (values[ds_index] < min) ? min : max);
675 plugin_dispatch_notification (&n);
677 plugin_notification_meta_free (&n);
679 } /* }}} int ut_report_state */
682 * int ut_check_one_data_source
684 * Checks one data source against the given threshold configuration. If the
685 * `DataSource' option is set in the threshold, and the name does NOT match,
686 * `okay' is returned. If the threshold does match, its failure and warning
687 * min and max values are checked and `failure' or `warning' is returned if
691 static int ut_check_one_data_source (const data_set_t *ds,
692 const value_list_t *vl,
693 const threshold_t *th,
694 const gauge_t *values,
701 /* check if this threshold applies to this data source */
702 ds_name = ds->ds[ds_index].name;
703 if ((th->data_source[0] != 0)
704 && (strcmp (ds_name, th->data_source) != 0))
707 if ((th->flags & UT_FLAG_INVERT) != 0)
713 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
714 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
717 return (STATE_ERROR);
719 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
720 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
723 return (STATE_WARNING);
726 } /* }}} int ut_check_one_data_source */
729 * int ut_check_one_threshold
731 * Checks all data sources of a value list against the given threshold, using
732 * the ut_check_one_data_source function above. Returns the worst status,
733 * which is `okay' if nothing has failed.
734 * Returns less than zero if the data set doesn't have any data sources.
736 static int ut_check_one_threshold (const data_set_t *ds,
737 const value_list_t *vl,
738 const threshold_t *th,
739 const gauge_t *values,
746 for (i = 0; i < ds->ds_num; i++)
750 status = ut_check_one_data_source (ds, vl, th, values, i);
756 } /* for (ds->ds_num) */
758 if (ret_ds_index != NULL)
759 *ret_ds_index = ds_index;
762 } /* }}} int ut_check_one_threshold */
765 * int ut_check_threshold (PUBLIC)
767 * Gets a list of matching thresholds and searches for the worst status by one
768 * of the thresholds. Then reports that status using the ut_report_state
770 * Returns zero on success and if no threshold has been configured. Returns
771 * less than zero on failure.
773 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
779 int worst_state = -1;
780 threshold_t *worst_th = NULL;
781 int worst_ds_index = -1;
783 if (threshold_tree == NULL)
786 /* Is this lock really necessary? So far, thresholds are only inserted at
788 pthread_mutex_lock (&threshold_lock);
789 th = threshold_search (vl);
790 pthread_mutex_unlock (&threshold_lock);
794 DEBUG ("ut_check_threshold: Found matching threshold(s)");
796 values = uc_get_rate (ds, vl);
804 status = ut_check_one_threshold (ds, vl, th, values, &ds_index);
807 ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
812 if (worst_state < status)
814 worst_state = status;
816 worst_ds_index = ds_index;
822 status = ut_report_state (ds, vl, worst_th, values,
823 worst_ds_index, worst_state);
826 ERROR ("ut_check_threshold: ut_report_state failed.");
834 } /* }}} int ut_check_threshold */
836 int ut_check_interesting (const char *name)
838 char *name_copy = NULL;
841 char *plugin_instance = NULL;
843 char *type_instance = NULL;
849 /* If there is no tree nothing is interesting. */
850 if (threshold_tree == NULL)
853 name_copy = strdup (name);
854 if (name_copy == NULL)
856 ERROR ("ut_check_interesting: strdup failed.");
860 status = parse_identifier (name_copy, &host,
861 &plugin, &plugin_instance, &type, &type_instance);
864 ERROR ("ut_check_interesting: parse_identifier failed.");
869 memset (&ds, '\0', sizeof (ds));
870 memset (&vl, '\0', sizeof (vl));
872 sstrncpy (vl.host, host, sizeof (vl.host));
873 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
874 if (plugin_instance != NULL)
875 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
876 sstrncpy (ds.type, type, sizeof (ds.type));
877 sstrncpy (vl.type, type, sizeof (vl.type));
878 if (type_instance != NULL)
879 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
882 host = plugin = plugin_instance = type = type_instance = NULL;
884 th = threshold_search (&vl);
887 if ((th->flags & UT_FLAG_PERSIST) == 0)
890 } /* int ut_check_interesting */
892 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */