2 * collectd - src/utils_threshold.c
3 * Copyright (C) 2007-2010 Florian octo Forster
4 * Copyright (C) 2008-2009 Sebastian Harl
5 * Copyright (C) 2009,2010 Andrés J. Díaz
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Florian octo Forster <octo at verplant.org>
23 * Sebastian Harl <sh at tokkee.org>
24 * Andrés J. Díaz <ajdiaz at connectical.com>
30 #include "utils_avltree.h"
31 #include "utils_cache.h"
32 #include "utils_threshold.h"
33 #include "utils_subst.h"
39 * Private data structures
41 #define UT_FLAG_INVERT 0x01
42 #define UT_FLAG_PERSIST 0x02
43 #define UT_FLAG_PERCENTAGE 0x04
44 #define UT_FLAG_INTERESTING 0x08
48 * Private (static) variables
50 static c_avl_tree_t *threshold_tree = NULL;
51 static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER;
55 * Threshold management
56 * ====================
57 * The following functions add, delete, search, etc. configured thresholds to
58 * the underlying AVL trees.
60 static threshold_t *threshold_get (const char *hostname,
61 const char *plugin, const char *plugin_instance,
62 const char *type, const char *type_instance)
64 char name[6 * DATA_MAX_NAME_LEN];
65 threshold_t *th = NULL;
67 format_name (name, sizeof (name),
68 (hostname == NULL) ? "" : hostname,
69 (plugin == NULL) ? "" : plugin, plugin_instance,
70 (type == NULL) ? "" : type, type_instance);
71 name[sizeof (name) - 1] = '\0';
73 if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
77 } /* threshold_t *threshold_get */
79 static int ut_threshold_add (const threshold_t *th)
81 char name[6 * DATA_MAX_NAME_LEN];
87 if (format_name (name, sizeof (name), th->host,
88 th->plugin, th->plugin_instance,
89 th->type, th->type_instance) != 0)
91 ERROR ("ut_threshold_add: format_name failed.");
95 name_copy = strdup (name);
96 if (name_copy == NULL)
98 ERROR ("ut_threshold_add: strdup failed.");
102 th_copy = (threshold_t *) malloc (sizeof (threshold_t));
106 ERROR ("ut_threshold_add: malloc failed.");
109 memcpy (th_copy, th, sizeof (threshold_t));
112 DEBUG ("ut_threshold_add: Adding entry `%s'", name);
114 pthread_mutex_lock (&threshold_lock);
116 th_ptr = threshold_get (th->host, th->plugin, th->plugin_instance,
117 th->type, th->type_instance);
119 while ((th_ptr != NULL) && (th_ptr->next != NULL))
120 th_ptr = th_ptr->next;
122 if (th_ptr == NULL) /* no such threshold yet */
124 status = c_avl_insert (threshold_tree, name_copy, th_copy);
126 else /* th_ptr points to the last threshold in the list */
128 th_ptr->next = th_copy;
129 /* name_copy isn't needed */
133 pthread_mutex_unlock (&threshold_lock);
137 ERROR ("ut_threshold_add: c_avl_insert (%s) failed.", name);
143 } /* int ut_threshold_add */
145 * End of the threshold management functions
151 * The following approximately two hundred functions are used to handle the
152 * configuration and fill the threshold list.
154 static int ut_config_type_datasource (threshold_t *th, oconfig_item_t *ci)
156 if ((ci->values_num != 1)
157 || (ci->values[0].type != OCONFIG_TYPE_STRING))
159 WARNING ("threshold values: The `DataSource' option needs exactly one "
164 sstrncpy (th->data_source, ci->values[0].value.string,
165 sizeof (th->data_source));
168 } /* int ut_config_type_datasource */
170 static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
172 if ((ci->values_num != 1)
173 || (ci->values[0].type != OCONFIG_TYPE_STRING))
175 WARNING ("threshold values: The `Instance' option needs exactly one "
180 sstrncpy (th->type_instance, ci->values[0].value.string,
181 sizeof (th->type_instance));
184 } /* int ut_config_type_instance */
186 static int ut_config_type_max (threshold_t *th, oconfig_item_t *ci)
188 if ((ci->values_num != 1)
189 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
191 WARNING ("threshold values: The `%s' option needs exactly one "
192 "number argument.", ci->key);
196 if (strcasecmp (ci->key, "WarningMax") == 0)
197 th->warning_max = ci->values[0].value.number;
199 th->failure_max = ci->values[0].value.number;
202 } /* int ut_config_type_max */
204 static int ut_config_type_min (threshold_t *th, oconfig_item_t *ci)
206 if ((ci->values_num != 1)
207 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
209 WARNING ("threshold values: The `%s' option needs exactly one "
210 "number argument.", ci->key);
214 if (strcasecmp (ci->key, "WarningMin") == 0)
215 th->warning_min = ci->values[0].value.number;
217 th->failure_min = ci->values[0].value.number;
220 } /* int ut_config_type_min */
222 static int ut_config_type_hits (threshold_t *th, oconfig_item_t *ci)
224 if ((ci->values_num != 1)
225 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
227 WARNING ("threshold values: The `%s' option needs exactly one "
228 "number argument.", ci->key);
232 th->hits = ci->values[0].value.number;
235 } /* int ut_config_type_hits */
237 static int ut_config_type_hysteresis (threshold_t *th, oconfig_item_t *ci)
239 if ((ci->values_num != 1)
240 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
242 WARNING ("threshold values: The `%s' option needs exactly one "
243 "number argument.", ci->key);
247 th->hysteresis = ci->values[0].value.number;
250 } /* int ut_config_type_hysteresis */
252 static int ut_config_type_message (threshold_t *th, oconfig_item_t *ci)
255 if ((ci->values_num != 1)
256 || (ci->values[0].type != OCONFIG_TYPE_STRING))
258 WARNING ("threshold values: The `%s' option needs exactly one "
259 "string argument.", ci->key);
263 if (ci->values[0].value.string[0] == 0)
265 WARNING ("threshold values: The `%s' option does not accept empty strings.",
270 th->message = strdup (ci->values[0].value.string);
271 if (th->message == NULL)
273 ERROR ("ut_config_type_message: sstrdup failed.");
278 } /* int ut_config_type_message */
280 static int ut_config_type_missingmessage (threshold_t *th, oconfig_item_t *ci)
283 if ((ci->values_num != 1)
284 || (ci->values[0].type != OCONFIG_TYPE_STRING))
286 WARNING ("threshold values: The `%s' option needs exactly one "
287 "string argument.", ci->key);
291 if (ci->values[0].value.string[0] == 0)
293 WARNING ("threshold values: The `%s' option does not accept empty strings.",
298 th->missing_message = strdup (ci->values[0].value.string);
299 if (th->missing_message == NULL)
301 ERROR ("ut_config_type_missingmessage: sstrdup failed.");
306 } /* int ut_config_type_missingmessage */
308 static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
314 if ((ci->values_num != 1)
315 || (ci->values[0].type != OCONFIG_TYPE_STRING))
317 WARNING ("threshold values: The `Type' block needs exactly one string "
322 if (ci->children_num < 1)
324 WARNING ("threshold values: The `Type' block needs at least one option.");
328 memcpy (&th, th_orig, sizeof (th));
329 sstrncpy (th.type, ci->values[0].value.string, sizeof (th.type));
331 th.warning_min = NAN;
332 th.warning_max = NAN;
333 th.failure_min = NAN;
334 th.failure_max = NAN;
338 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
340 for (i = 0; i < ci->children_num; i++)
342 oconfig_item_t *option = ci->children + i;
345 if (strcasecmp ("Instance", option->key) == 0)
346 status = ut_config_type_instance (&th, option);
347 else if (strcasecmp ("DataSource", option->key) == 0)
348 status = ut_config_type_datasource (&th, option);
349 else if ((strcasecmp ("WarningMax", option->key) == 0)
350 || (strcasecmp ("FailureMax", option->key) == 0))
351 status = ut_config_type_max (&th, option);
352 else if ((strcasecmp ("WarningMin", option->key) == 0)
353 || (strcasecmp ("FailureMin", option->key) == 0))
354 status = ut_config_type_min (&th, option);
355 else if (strcasecmp ("Interesting", option->key) == 0)
356 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INTERESTING);
357 else if (strcasecmp ("Invert", option->key) == 0)
358 status = cf_util_get_flag (option, &th.flags, UT_FLAG_INVERT);
359 else if (strcasecmp ("Persist", option->key) == 0)
360 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST);
361 else if (strcasecmp ("Percentage", option->key) == 0)
362 status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERCENTAGE);
363 else if (strcasecmp ("Hits", option->key) == 0)
364 status = ut_config_type_hits (&th, option);
365 else if (strcasecmp ("Hysteresis", option->key) == 0)
366 status = ut_config_type_hysteresis (&th, option);
367 else if (strcasecmp ("Message", option->key) == 0)
368 status = ut_config_type_message (&th, option);
369 else if (strcasecmp ("MissingMessage", option->key) == 0)
370 status = ut_config_type_missingmessage (&th, option);
373 WARNING ("threshold values: Option `%s' not allowed inside a `Type' "
374 "block.", option->key);
384 status = ut_threshold_add (&th);
388 } /* int ut_config_type */
390 static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
392 if ((ci->values_num != 1)
393 || (ci->values[0].type != OCONFIG_TYPE_STRING))
395 WARNING ("threshold values: The `Instance' option needs exactly one "
400 sstrncpy (th->plugin_instance, ci->values[0].value.string,
401 sizeof (th->plugin_instance));
404 } /* int ut_config_plugin_instance */
406 static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
412 if ((ci->values_num != 1)
413 || (ci->values[0].type != OCONFIG_TYPE_STRING))
415 WARNING ("threshold values: The `Plugin' block needs exactly one string "
420 if (ci->children_num < 1)
422 WARNING ("threshold values: The `Plugin' block needs at least one nested "
427 memcpy (&th, th_orig, sizeof (th));
428 sstrncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
430 for (i = 0; i < ci->children_num; i++)
432 oconfig_item_t *option = ci->children + i;
435 if (strcasecmp ("Type", option->key) == 0)
436 status = ut_config_type (&th, option);
437 else if (strcasecmp ("Instance", option->key) == 0)
438 status = ut_config_plugin_instance (&th, option);
441 WARNING ("threshold values: Option `%s' not allowed inside a `Plugin' "
442 "block.", option->key);
451 } /* int ut_config_plugin */
453 static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
459 if ((ci->values_num != 1)
460 || (ci->values[0].type != OCONFIG_TYPE_STRING))
462 WARNING ("threshold values: The `Host' block needs exactly one string "
467 if (ci->children_num < 1)
469 WARNING ("threshold values: The `Host' block needs at least one nested "
474 memcpy (&th, th_orig, sizeof (th));
475 sstrncpy (th.host, ci->values[0].value.string, sizeof (th.host));
477 for (i = 0; i < ci->children_num; i++)
479 oconfig_item_t *option = ci->children + i;
482 if (strcasecmp ("Type", option->key) == 0)
483 status = ut_config_type (&th, option);
484 else if (strcasecmp ("Plugin", option->key) == 0)
485 status = ut_config_plugin (&th, option);
488 WARNING ("threshold values: Option `%s' not allowed inside a `Host' "
489 "block.", option->key);
498 } /* int ut_config_host */
500 int ut_config (const oconfig_item_t *ci)
507 if (ci->values_num != 0)
509 ERROR ("threshold values: The `Threshold' block may not have any "
514 if (threshold_tree == NULL)
516 threshold_tree = c_avl_create ((void *) strcmp);
517 if (threshold_tree == NULL)
519 ERROR ("ut_config: c_avl_create failed.");
524 memset (&th, '\0', sizeof (th));
525 th.warning_min = NAN;
526 th.warning_max = NAN;
527 th.failure_min = NAN;
528 th.failure_max = NAN;
532 th.flags = UT_FLAG_INTERESTING; /* interesting by default */
534 for (i = 0; i < ci->children_num; i++)
536 oconfig_item_t *option = ci->children + i;
539 if (strcasecmp ("Type", option->key) == 0)
540 status = ut_config_type (&th, option);
541 else if (strcasecmp ("Plugin", option->key) == 0)
542 status = ut_config_plugin (&th, option);
543 else if (strcasecmp ("Host", option->key) == 0)
544 status = ut_config_host (&th, option);
547 WARNING ("threshold values: Option `%s' not allowed here.", option->key);
556 } /* int um_config */
558 * End of the functions used to configure threshold values.
562 static threshold_t *threshold_search (const value_list_t *vl)
566 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
567 vl->type, vl->type_instance)) != NULL)
569 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
570 vl->type, NULL)) != NULL)
572 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
573 vl->type, vl->type_instance)) != NULL)
575 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
576 vl->type, NULL)) != NULL)
578 else if ((th = threshold_get (vl->host, "", NULL,
579 vl->type, vl->type_instance)) != NULL)
581 else if ((th = threshold_get (vl->host, "", NULL,
582 vl->type, NULL)) != NULL)
584 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
585 vl->type, vl->type_instance)) != NULL)
587 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
588 vl->type, NULL)) != NULL)
590 else if ((th = threshold_get ("", vl->plugin, NULL,
591 vl->type, vl->type_instance)) != NULL)
593 else if ((th = threshold_get ("", vl->plugin, NULL,
594 vl->type, NULL)) != NULL)
596 else if ((th = threshold_get ("", "", NULL,
597 vl->type, vl->type_instance)) != NULL)
599 else if ((th = threshold_get ("", "", NULL,
600 vl->type, NULL)) != NULL)
604 } /* }}} threshold_t *threshold_search */
606 /* char *ut_build_message
608 * Return a custom formated message for dataset, values and previously created
609 * notification (which must include time and other fields), if th is present,
610 * templates for threshold will be interpreted, if th is NULL these will be
613 int ut_build_message(char *out, size_t bufsize, const char *fmt,
614 const data_set_t *ds, int ds_index, const value_list_t *vl, const gauge_t *values,
615 const notification_t *n, const threshold_t *th)
618 /* TODO: We could provide here a way to use meta information on thresholds
619 * directly in the future. */
620 char msg[NOTIF_MAX_MSG_LEN];
625 sstrncpy (msg, fmt, sizeof (msg));
627 #define REPLACE_FIELD(t,v) do { \
628 char temp[sizeof (msg)]; \
629 if (subst_string (temp, sizeof (temp), msg, (t), (v)) != NULL) \
630 sstrncpy (msg, temp, sizeof (msg)); \
633 #define REPLACE_FIELD_DBL(t,v) do { \
634 char value_str[NOTIF_MAX_MSG_LEN]; \
635 ssnprintf (value_str, sizeof (value_str), "%g", (v)); \
636 REPLACE_FIELD(t, value_str); \
639 #define REPLACE_FIELD_INT(t,v) do { \
640 char value_str[NOTIF_MAX_MSG_LEN]; \
641 ssnprintf (value_str, sizeof (value_str), "%i", (v)); \
642 REPLACE_FIELD(t, value_str); \
645 REPLACE_FIELD ("%{host}", n->host);
646 REPLACE_FIELD ("%{plugin}", n->plugin);
647 REPLACE_FIELD ("%{plugin_instance}", n->plugin_instance);
648 REPLACE_FIELD ("%{type}", n->type);
649 REPLACE_FIELD ("%{type_instance}", n->type_instance);
650 REPLACE_FIELD ("%{data_source}", ds->ds[ds_index].name);
652 /* This is the offending value, its equivalent to %{ds:value}, if
653 * value is the data_source name. */
654 REPLACE_FIELD_DBL ("%{value}", values[ds_index]);
656 /* Now replace all %{ds:<template>} like target_notification does */
659 for (i = 0; i < ds->ds_num; i++)
661 char template[DATA_MAX_NAME_LEN];
662 char value_str[DATA_MAX_NAME_LEN];
664 ssnprintf (template, sizeof (template), "%%{ds:%s}", ds->ds[i].name);
666 if (ds->ds[i].type != DS_TYPE_GAUGE)
668 if ((rates == NULL) && (rates_failed == 0))
670 rates = uc_get_rate (ds, vl);
676 /* If this is a gauge value, use the current value. */
677 if (ds->ds[i].type == DS_TYPE_GAUGE)
678 ssnprintf (value_str, sizeof (value_str),
679 "%g", (double) vl->values[i].gauge);
680 /* If it's a counter, try to use the current rate. This may fail, if the
681 * value has been renamed. */
682 else if (rates != NULL)
683 ssnprintf (value_str, sizeof (value_str),
684 "%g", (double) rates[i]);
685 /* Since we don't know any better, use the string `unknown'. */
687 sstrncpy (value_str, "unknown", sizeof (value_str));
689 REPLACE_FIELD (template, value_str);
695 if ( !isnan(th->warning_min)) {
696 REPLACE_FIELD_DBL ("%{warning_min}", th->warning_min);
698 if ( !isnan(th->warning_max)) {
699 REPLACE_FIELD_DBL ("%{warning_max}", th->warning_max);
701 if ( !isnan(th->failure_min)) {
702 REPLACE_FIELD_DBL ("%{failure_min}", th->failure_min);
704 if ( !isnan(th->failure_max)) {
705 REPLACE_FIELD_DBL ("%{failure_max}", th->failure_max);
708 REPLACE_FIELD_DBL ("%{hysteresis}", th->hysteresis);
709 REPLACE_FIELD_INT ("%{hits}", th->hits);
712 return ssnprintf (out, bufsize, "%s", msg);
713 } /* }}} int ut_build_message */
716 * int ut_report_state
718 * Checks if the `state' differs from the old state and creates a notification
722 static int ut_report_state (const data_set_t *ds,
723 const value_list_t *vl,
724 const threshold_t *th,
725 const gauge_t *values,
737 /* Check if hits matched */
740 int hits = uc_get_hits(ds,vl);
741 /* The STATE_OKAY always reset hits, or if hits reaise the limit */
742 if ((state == STATE_OKAY) || (hits > th->hits))
744 DEBUG("ut_report_state: reset uc_get_hits = 0");
745 uc_set_hits (ds, vl, 0); /* reset hit counter and notify */
749 DEBUG("ut_report_state: th->hits = %d, uc_get_hits = %d",th->hits,uc_get_hits(ds,vl));
750 (void) uc_inc_hits (ds, vl, 1); /* increase hit counter */
753 } /* end check hits */
755 state_old = uc_get_state (ds, vl);
757 /* If the state didn't change, only report if `persistent' is specified and
758 * the state is not `okay'. */
759 if (state == state_old)
761 if ((th->flags & UT_FLAG_PERSIST) == 0)
763 else if (state == STATE_OKAY)
767 if (state != state_old)
768 uc_set_state (ds, vl, state);
770 NOTIFICATION_INIT_VL (&n, vl, ds);
773 bufsize = sizeof (n.message);
775 if (state == STATE_OKAY)
776 n.severity = NOTIF_OKAY;
777 else if (state == STATE_WARNING)
778 n.severity = NOTIF_WARNING;
780 n.severity = NOTIF_FAILURE;
784 /* Format custom message if present */
785 if (th->message != NULL)
787 status = ut_build_message (buf, bufsize, th->message,
788 ds, ds_index, vl, values,
793 else /* No custom message. Using default message for threshold */
795 status = ssnprintf (buf, bufsize, "Host %s, plugin %s",
796 vl->host, vl->plugin);
800 if (vl->plugin_instance[0] != '\0')
802 status = ssnprintf (buf, bufsize, " (instance %s)",
803 vl->plugin_instance);
808 status = ssnprintf (buf, bufsize, " type %s", vl->type);
812 if (vl->type_instance[0] != '\0')
814 status = ssnprintf (buf, bufsize, " (instance %s)",
820 /* Build okay notification message */
821 if (state == STATE_OKAY)
823 status = ssnprintf (buf, bufsize, ": All data sources are within range again.");
827 else /* build non-okay notification message */
832 min = (state == STATE_ERROR) ? th->failure_min : th->warning_min;
833 max = (state == STATE_ERROR) ? th->failure_max : th->warning_max;
835 if (th->flags & UT_FLAG_INVERT)
837 if (!isnan (min) && !isnan (max))
839 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
840 "%f. That is within the %s region of %f%s and %f%s.",
841 ds->ds[ds_index].name, values[ds_index],
842 (state == STATE_ERROR) ? "failure" : "warning",
843 min, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "",
844 max, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
848 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
849 "%f. That is %s the %s threshold of %f%s.",
850 ds->ds[ds_index].name, values[ds_index],
851 isnan (min) ? "below" : "above",
852 (state == STATE_ERROR) ? "failure" : "warning",
853 isnan (min) ? max : min,
854 ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
857 else /* is not inverted */
859 status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
860 "%f. That is %s the %s threshold of %f%s.",
861 ds->ds[ds_index].name, values[ds_index],
862 (values[ds_index] < min) ? "below" : "above",
863 (state == STATE_ERROR) ? "failure" : "warning",
864 (values[ds_index] < min) ? min : max,
865 ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : "");
872 /* adds meta to notification */
873 plugin_notification_meta_add_string (&n, "DataSource",
874 ds->ds[ds_index].name);
875 plugin_notification_meta_add_double (&n, "CurrentValue", values[ds_index]);
876 plugin_notification_meta_add_double (&n, "WarningMin", th->warning_min);
877 plugin_notification_meta_add_double (&n, "WarningMax", th->warning_max);
878 plugin_notification_meta_add_double (&n, "FailureMin", th->failure_min);
879 plugin_notification_meta_add_double (&n, "FailureMax", th->failure_max);
881 plugin_dispatch_notification (&n);
883 plugin_notification_meta_free (n.meta);
885 } /* }}} int ut_report_state */
888 * int ut_check_one_data_source
890 * Checks one data source against the given threshold configuration. If the
891 * `DataSource' option is set in the threshold, and the name does NOT match,
892 * `okay' is returned. If the threshold does match, its failure and warning
893 * min and max values are checked and `failure' or `warning' is returned if
897 static int ut_check_one_data_source (const data_set_t *ds,
898 const value_list_t __attribute__((unused)) *vl,
899 const threshold_t *th,
900 const gauge_t *values,
906 int prev_state = STATE_OKAY;
908 /* check if this threshold applies to this data source */
911 ds_name = ds->ds[ds_index].name;
912 if ((th->data_source[0] != 0)
913 && (strcmp (ds_name, th->data_source) != 0))
917 if ((th->flags & UT_FLAG_INVERT) != 0)
923 /* XXX: This is an experimental code, not optimized, not fast, not reliable,
924 * and probably, do not work as you expect. Enjoy! :D */
925 if ( (th->hysteresis > 0) && ((prev_state = uc_get_state(ds,vl)) != STATE_OKAY) )
930 if ( (!isnan (th->failure_min) && ((th->failure_min + th->hysteresis) < values[ds_index])) ||
931 (!isnan (th->failure_max) && ((th->failure_max - th->hysteresis) > values[ds_index])) )
936 if ( (!isnan (th->warning_min) && ((th->warning_min + th->hysteresis) < values[ds_index])) ||
937 (!isnan (th->warning_max) && ((th->warning_max - th->hysteresis) > values[ds_index])) )
943 else { /* no hysteresis */
944 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
945 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
948 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
949 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
954 return (STATE_ERROR);
957 return (STATE_WARNING);
960 } /* }}} int ut_check_one_data_source */
963 * int ut_check_one_threshold
965 * Checks all data sources of a value list against the given threshold, using
966 * the ut_check_one_data_source function above. Returns the worst status,
967 * which is `okay' if nothing has failed.
968 * Returns less than zero if the data set doesn't have any data sources.
970 static int ut_check_one_threshold (const data_set_t *ds,
971 const value_list_t *vl,
972 const threshold_t *th,
973 const gauge_t *values,
979 gauge_t values_copy[ds->ds_num];
981 memcpy (values_copy, values, sizeof (values_copy));
983 if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
990 WARNING ("ut_check_one_threshold: The %s type has only one data "
991 "source, but you have configured to check this as a percentage. "
992 "That doesn't make much sense, because the percentage will always "
993 "be 100%%!", ds->type);
996 /* Prepare `sum' and `num'. */
997 for (i = 0; i < ds->ds_num; i++)
998 if (!isnan (values[i]))
1004 if ((num == 0) /* All data sources are undefined. */
1005 || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
1007 for (i = 0; i < ds->ds_num; i++)
1008 values_copy[i] = NAN;
1010 else /* We can actually calculate the percentage. */
1012 for (i = 0; i < ds->ds_num; i++)
1013 values_copy[i] = 100.0 * values[i] / sum;
1015 } /* if (UT_FLAG_PERCENTAGE) */
1017 for (i = 0; i < ds->ds_num; i++)
1021 status = ut_check_one_data_source (ds, vl, th, values_copy, i);
1027 } /* for (ds->ds_num) */
1029 if (ret_ds_index != NULL)
1030 *ret_ds_index = ds_index;
1033 } /* }}} int ut_check_one_threshold */
1036 * int ut_check_threshold (PUBLIC)
1038 * Gets a list of matching thresholds and searches for the worst status by one
1039 * of the thresholds. Then reports that status using the ut_report_state
1041 * Returns zero on success and if no threshold has been configured. Returns
1042 * less than zero on failure.
1044 int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
1050 int worst_state = -1;
1051 threshold_t *worst_th = NULL;
1052 int worst_ds_index = -1;
1054 if (threshold_tree == NULL)
1057 /* Is this lock really necessary? So far, thresholds are only inserted at
1059 pthread_mutex_lock (&threshold_lock);
1060 th = threshold_search (vl);
1061 pthread_mutex_unlock (&threshold_lock);
1065 DEBUG ("ut_check_threshold: Found matching threshold(s)");
1067 values = uc_get_rate (ds, vl);
1075 status = ut_check_one_threshold (ds, vl, th, values, &ds_index);
1078 ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
1083 if (worst_state < status)
1085 worst_state = status;
1087 worst_ds_index = ds_index;
1093 status = ut_report_state (ds, vl, worst_th, values,
1094 worst_ds_index, worst_state);
1097 ERROR ("ut_check_threshold: ut_report_state failed.");
1105 } /* }}} int ut_check_threshold */
1108 * int ut_check_interesting (PUBLIC)
1110 * Given an identification returns
1111 * 0: No threshold is defined.
1112 * 1: A threshold has been found. The flag `persist' is off.
1113 * 2: A threshold has been found. The flag `persist' is on.
1114 * (That is, it is expected that many notifications are sent until the
1115 * problem disappears.)
1117 int ut_check_interesting (const char *name)
1119 char *name_copy = NULL;
1121 char *plugin = NULL;
1122 char *plugin_instance = NULL;
1124 char *type_instance = NULL;
1130 /* If there is no tree nothing is interesting. */
1131 if (threshold_tree == NULL)
1134 name_copy = strdup (name);
1135 if (name_copy == NULL)
1137 ERROR ("ut_check_interesting: strdup failed.");
1141 status = parse_identifier (name_copy, &host,
1142 &plugin, &plugin_instance, &type, &type_instance);
1145 ERROR ("ut_check_interesting: parse_identifier failed.");
1150 memset (&ds, '\0', sizeof (ds));
1151 memset (&vl, '\0', sizeof (vl));
1153 sstrncpy (vl.host, host, sizeof (vl.host));
1154 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
1155 if (plugin_instance != NULL)
1156 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
1157 sstrncpy (ds.type, type, sizeof (ds.type));
1158 sstrncpy (vl.type, type, sizeof (vl.type));
1159 if (type_instance != NULL)
1160 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
1163 host = plugin = plugin_instance = type = type_instance = NULL;
1165 th = threshold_search (&vl);
1169 if ((th->flags & UT_FLAG_INTERESTING) == 0)
1172 if ((th->flags & UT_FLAG_PERSIST) == 0)
1175 } /* }}} int ut_check_interesting */
1177 int ut_search_threshold (const value_list_t *vl, /* {{{ */
1178 threshold_t *ret_threshold)
1185 t = threshold_search (vl);
1189 memcpy (ret_threshold, t, sizeof (*ret_threshold));
1190 ret_threshold->next = NULL;
1193 } /* }}} int ut_search_threshold */
1195 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */