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 strncpy (th->type_instance, ci->values[0].value.string,
188 sizeof (th->type_instance));
189 th->type_instance[sizeof (th->type_instance) - 1] = '\0';
192 } /* int ut_config_type_instance */
194 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
196 if ((ci->values_num != 1)
197 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
199 WARNING ("threshold values: The `%s' option needs exactly one "
200 "number argument.", ci->key);
204 if (strcasecmp (ci->key, "WarningMax") == 0)
205 th->warning_max = ci->values[0].value.number;
207 th->failure_max = ci->values[0].value.number;
210 } /* int ut_config_type_max */
212 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
214 if ((ci->values_num != 1)
215 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
217 WARNING ("threshold values: The `%s' option needs exactly one "
218 "number argument.", ci->key);
222 if (strcasecmp (ci->key, "WarningMin") == 0)
223 th->warning_min = ci->values[0].value.number;
225 th->failure_min = ci->values[0].value.number;
228 } /* int ut_config_type_min */
230 static int ut_config_type_invert (threshold_t *th, oconfig_item_t *ci)
232 if ((ci->values_num != 1)
233 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
235 WARNING ("threshold values: The `Invert' option needs exactly one "
236 "boolean argument.");
240 if (ci->values[0].value.boolean)
241 th->flags |= UT_FLAG_INVERT;
243 th->flags &= ~UT_FLAG_INVERT;
246 } /* int ut_config_type_invert */
248 static int ut_config_type_persist (threshold_t *th, oconfig_item_t *ci)
250 if ((ci->values_num != 1)
251 || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
253 WARNING ("threshold values: The `Persist' option needs exactly one "
254 "boolean argument.");
258 if (ci->values[0].value.boolean)
259 th->flags |= UT_FLAG_PERSIST;
261 th->flags &= ~UT_FLAG_PERSIST;
264 } /* int ut_config_type_persist */
266 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
272 if ((ci->values_num != 1)
273 || (ci->values[0].type != OCONFIG_TYPE_STRING))
275 WARNING ("threshold values: The `Type' block needs exactly one string "
280 if (ci->children_num < 1)
282 WARNING ("threshold values: The `Type' block needs at least one option.");
286 memcpy (&th, th_orig, sizeof (th));
287 strncpy (th.type, ci->values[0].value.string, sizeof (th.type));
288 th.type[sizeof (th.type) - 1] = '\0';
290 th.warning_min = NAN;
291 th.warning_max = NAN;
292 th.failure_min = NAN;
293 th.failure_max = NAN;
295 for (i = 0; i < ci->children_num; i++)
297 oconfig_item_t *option = ci->children + i;
300 if (strcasecmp ("Instance", option->key) == 0)
301 status = ut_config_type_instance (&th, option);
302 if (strcasecmp ("DataSource", option->key) == 0)
303 status = ut_config_type_datasource (&th, option);
304 else if ((strcasecmp ("WarningMax", option->key) == 0)
305 || (strcasecmp ("FailureMax", option->key) == 0))
306 status = ut_config_type_max (&th, option);
307 else if ((strcasecmp ("WarningMin", option->key) == 0)
308 || (strcasecmp ("FailureMin", option->key) == 0))
309 status = ut_config_type_min (&th, option);
310 else if (strcasecmp ("Invert", option->key) == 0)
311 status = ut_config_type_invert (&th, option);
312 else if (strcasecmp ("Persist", option->key) == 0)
313 status = ut_config_type_persist (&th, option);
316 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
317 "block.", option->key);
327 status = ut_threshold_add (&th);
331 } /* int ut_config_type */
333 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
335 if ((ci->values_num != 1)
336 || (ci->values[0].type != OCONFIG_TYPE_STRING))
338 WARNING ("threshold values: The `Instance' option needs exactly one "
343 strncpy (th->plugin_instance, ci->values[0].value.string,
344 sizeof (th->plugin_instance));
345 th->plugin_instance[sizeof (th->plugin_instance) - 1] = '\0';
348 } /* int ut_config_plugin_instance */
350 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
356 if ((ci->values_num != 1)
357 || (ci->values[0].type != OCONFIG_TYPE_STRING))
359 WARNING ("threshold values: The `Plugin' block needs exactly one string "
364 if (ci->children_num < 1)
366 WARNING ("threshold values: The `Plugin' block needs at least one nested "
371 memcpy (&th, th_orig, sizeof (th));
372 strncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
373 th.plugin[sizeof (th.plugin) - 1] = '\0';
375 for (i = 0; i < ci->children_num; i++)
377 oconfig_item_t *option = ci->children + i;
380 if (strcasecmp ("Type", option->key) == 0)
381 status = ut_config_type (&th, option);
382 else if (strcasecmp ("Instance", option->key) == 0)
383 status = ut_config_plugin_instance (&th, option);
386 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
387 "block.", option->key);
396 } /* int ut_config_plugin */
398 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
404 if ((ci->values_num != 1)
405 || (ci->values[0].type != OCONFIG_TYPE_STRING))
407 WARNING ("threshold values: The `Host' block needs exactly one string "
412 if (ci->children_num < 1)
414 WARNING ("threshold values: The `Host' block needs at least one nested "
419 memcpy (&th, th_orig, sizeof (th));
420 strncpy (th.host, ci->values[0].value.string, sizeof (th.host));
421 th.host[sizeof (th.host) - 1] = '\0';
423 for (i = 0; i < ci->children_num; i++)
425 oconfig_item_t *option = ci->children + i;
428 if (strcasecmp ("Type", option->key) == 0)
429 status = ut_config_type (&th, option);
430 else if (strcasecmp ("Plugin", option->key) == 0)
431 status = ut_config_plugin (&th, option);
434 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
435 "block.", option->key);
444 } /* int ut_config_host */
446 int ut_config (const oconfig_item_t *ci)
453 if (ci->values_num != 0)
455 ERROR ("threshold values: The `Threshold' block may not have any "
460 if (threshold_tree == NULL)
462 threshold_tree = c_avl_create ((void *) strcmp);
463 if (threshold_tree == NULL)
465 ERROR ("ut_config: c_avl_create failed.");
470 memset (&th, '\0', sizeof (th));
471 th.warning_min = NAN;
472 th.warning_max = NAN;
473 th.failure_min = NAN;
474 th.failure_max = NAN;
476 for (i = 0; i < ci->children_num; i++)
478 oconfig_item_t *option = ci->children + i;
481 if (strcasecmp ("Type", option->key) == 0)
482 status = ut_config_type (&th, option);
483 else if (strcasecmp ("Plugin", option->key) == 0)
484 status = ut_config_plugin (&th, option);
485 else if (strcasecmp ("Host", option->key) == 0)
486 status = ut_config_host (&th, option);
489 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
498 } /* int um_config */
500 * End of the functions used to configure threshold values.
504 static threshold_t *threshold_search (const value_list_t *vl)
508 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
509 vl->type, vl->type_instance)) != NULL)
511 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
512 vl->type, NULL)) != NULL)
514 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
515 vl->type, vl->type_instance)) != NULL)
517 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
518 vl->type, NULL)) != NULL)
520 else if ((th = threshold_get (vl->host, "", NULL,
521 vl->type, vl->type_instance)) != NULL)
523 else if ((th = threshold_get (vl->host, "", NULL,
524 vl->type, NULL)) != NULL)
526 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
527 vl->type, vl->type_instance)) != NULL)
529 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
530 vl->type, NULL)) != NULL)
532 else if ((th = threshold_get ("", vl->plugin, NULL,
533 vl->type, vl->type_instance)) != NULL)
535 else if ((th = threshold_get ("", vl->plugin, NULL,
536 vl->type, NULL)) != NULL)
538 else if ((th = threshold_get ("", "", NULL,
539 vl->type, vl->type_instance)) != NULL)
541 else if ((th = threshold_get ("", "", NULL,
542 vl->type, NULL)) != NULL)
546 } /* threshold_t *threshold_search */
549 * int ut_report_state
551 * Checks if the `state' differs from the old state and creates a notification
555 static int ut_report_state (const data_set_t *ds,
556 const value_list_t *vl,
557 const threshold_t *th,
558 const gauge_t *values,
570 state_old = uc_get_state (ds, vl);
572 /* If the state didn't change, only report if `persistent' is specified and
573 * the state is not `okay'. */
574 if (state == state_old)
576 if ((th->flags & UT_FLAG_PERSIST) == 0)
578 else if (state == STATE_OKAY)
582 if (state != state_old)
583 uc_set_state (ds, vl, state);
585 NOTIFICATION_INIT_VL (&n, vl, ds);
588 bufsize = sizeof (n.message);
590 if (state == STATE_OKAY)
591 n.severity = NOTIF_OKAY;
592 else if (state == STATE_WARNING)
593 n.severity = NOTIF_WARNING;
595 n.severity = NOTIF_FAILURE;
599 status = snprintf (buf, bufsize, "Host %s, plugin %s",
600 vl->host, vl->plugin);
604 if (vl->plugin_instance[0] != '\0')
606 status = snprintf (buf, bufsize, " (instance %s)",
607 vl->plugin_instance);
612 status = snprintf (buf, bufsize, " type %s", vl->type);
616 if (vl->type_instance[0] != '\0')
618 status = snprintf (buf, bufsize, " (instance %s)",
624 /* Send an okay notification */
625 if (state == STATE_OKAY)
627 status = snprintf (buf, bufsize, ": All data sources are within range again.");
636 min = (state == STATE_ERROR) ? th->failure_min : th->warning_min;
637 max = (state == STATE_ERROR) ? th->failure_max : th->warning_max;
639 if (th->flags & UT_FLAG_INVERT)
641 if (!isnan (min) && !isnan (max))
643 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
644 "%f. That is within the %s region of %f and %f.",
645 ds->ds[ds_index].name, values[ds_index],
646 (state == STATE_ERROR) ? "failure" : "warning",
651 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
652 "%f. That is %s the %s threshold of %f.",
653 ds->ds[ds_index].name, values[ds_index],
654 isnan (min) ? "below" : "above",
655 (state == STATE_ERROR) ? "failure" : "warning",
656 isnan (min) ? max : min);
659 else /* is not inverted */
661 status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
662 "%f. That is %s the %s threshold of %f.",
663 ds->ds[ds_index].name, values[ds_index],
664 (values[ds_index] < min) ? "below" : "above",
665 (state == STATE_ERROR) ? "failure" : "warning",
666 (values[ds_index] < min) ? min : max);
672 plugin_dispatch_notification (&n);
675 } /* }}} int ut_report_state */
678 * int ut_check_one_data_source
680 * Checks one data source against the given threshold configuration. If the
681 * `DataSource' option is set in the threshold, and the name does NOT match,
682 * `okay' is returned. If the threshold does match, its failure and warning
683 * min and max values are checked and `failure' or `warning' is returned if
687 static int ut_check_one_data_source (const data_set_t *ds,
688 const value_list_t *vl,
689 const threshold_t *th,
690 const gauge_t *values,
697 /* check if this threshold applies to this data source */
698 ds_name = ds->ds[ds_index].name;
699 if ((th->data_source[0] != 0)
700 && (strcmp (ds_name, th->data_source) != 0))
703 if ((th->flags & UT_FLAG_INVERT) != 0)
709 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
710 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
713 return (STATE_ERROR);
715 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
716 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
719 return (STATE_WARNING);
722 } /* }}} int ut_check_one_data_source */
725 * int ut_check_one_threshold
727 * Checks all data sources of a value list against the given threshold, using
728 * the ut_check_one_data_source function above. Returns the worst status,
729 * which is `okay' if nothing has failed.
730 * Returns less than zero if the data set doesn't have any data sources.
732 static int ut_check_one_threshold (const data_set_t *ds,
733 const value_list_t *vl,
734 const threshold_t *th,
735 const gauge_t *values,
742 for (i = 0; i < ds->ds_num; i++)
746 status = ut_check_one_data_source (ds, vl, th, values, i);
752 } /* for (ds->ds_num) */
754 if (ret_ds_index != NULL)
755 *ret_ds_index = ds_index;
758 } /* }}} int ut_check_one_threshold */
761 * int ut_check_threshold (PUBLIC)
763 * Gets a list of matching thresholds and searches for the worst status by one
764 * of the thresholds. Then reports that status using the ut_report_state
766 * Returns zero on success and if no threshold has been configured. Returns
767 * less than zero on failure.
769 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
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 */
832 int ut_check_interesting (const char *name)
834 char *name_copy = NULL;
837 char *plugin_instance = NULL;
839 char *type_instance = NULL;
845 /* If there is no tree nothing is interesting. */
846 if (threshold_tree == NULL)
849 name_copy = strdup (name);
850 if (name_copy == NULL)
852 ERROR ("ut_check_interesting: strdup failed.");
856 status = parse_identifier (name_copy, &host,
857 &plugin, &plugin_instance, &type, &type_instance);
860 ERROR ("ut_check_interesting: parse_identifier failed.");
864 memset (&ds, '\0', sizeof (ds));
865 memset (&vl, '\0', sizeof (vl));
867 strncpy (vl.host, host, sizeof (vl.host));
868 vl.host[sizeof (vl.host) - 1] = '\0';
869 strncpy (vl.plugin, plugin, sizeof (vl.plugin));
870 vl.plugin[sizeof (vl.plugin) - 1] = '\0';
871 if (plugin_instance != NULL)
873 strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
874 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
876 strncpy (ds.type, type, sizeof (ds.type));
877 ds.type[sizeof (ds.type) - 1] = '\0';
878 strncpy (vl.type, type, sizeof (vl.type));
879 vl.type[sizeof (vl.type) - 1] = '\0';
880 if (type_instance != NULL)
882 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
883 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
887 host = plugin = plugin_instance = type = type_instance = NULL;
889 th = threshold_search (&vl);
892 if ((th->flags & UT_FLAG_PERSIST) == 0)
895 } /* int ut_check_interesting */
897 /* vim: set sw=2 ts=8 sts=2 tw=78 fdm=marker : */