X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fthreshold.c;h=855f1360f91dc06b6e378283e2bfd1f51ecf2860;hb=b3a2880887f7accbdf9c661d5d8fea28a050ea73;hp=7d48244c4948a29b85b885e22b2c18c6309ecc01;hpb=83ca15b62cd8477613a8b81d46558041bb63d517;p=collectd.git diff --git a/src/threshold.c b/src/threshold.c index 7d48244c..855f1360 100644 --- a/src/threshold.c +++ b/src/threshold.c @@ -28,74 +28,17 @@ #include "plugin.h" #include "utils_avltree.h" #include "utils_cache.h" +#include "utils_threshold.h" #include #include /* - * Private data structures - * {{{ */ -#define UT_FLAG_INVERT 0x01 -#define UT_FLAG_PERSIST 0x02 -#define UT_FLAG_PERCENTAGE 0x04 -#define UT_FLAG_INTERESTING 0x08 -typedef struct threshold_s -{ - char host[DATA_MAX_NAME_LEN]; - char plugin[DATA_MAX_NAME_LEN]; - char plugin_instance[DATA_MAX_NAME_LEN]; - char type[DATA_MAX_NAME_LEN]; - char type_instance[DATA_MAX_NAME_LEN]; - char data_source[DATA_MAX_NAME_LEN]; - gauge_t warning_min; - gauge_t warning_max; - gauge_t failure_min; - gauge_t failure_max; - gauge_t hysteresis; - unsigned int flags; - int hits; - struct threshold_s *next; -} threshold_t; -/* }}} */ - -/* - * Private (static) variables - * {{{ */ -static c_avl_tree_t *threshold_tree = NULL; -static pthread_mutex_t threshold_lock = PTHREAD_MUTEX_INITIALIZER; -/* }}} */ - -/* * Threshold management * ==================== * The following functions add, delete, search, etc. configured thresholds to * the underlying AVL trees. */ -/* - * threshold_t *threshold_get - * - * Retrieve one specific threshold configuration. For looking up a threshold - * matching a value_list_t, see "threshold_search" below. Returns NULL if the - * specified threshold doesn't exist. - */ -static threshold_t *threshold_get (const char *hostname, - const char *plugin, const char *plugin_instance, - const char *type, const char *type_instance) -{ /* {{{ */ - char name[6 * DATA_MAX_NAME_LEN]; - threshold_t *th = NULL; - - format_name (name, sizeof (name), - (hostname == NULL) ? "" : hostname, - (plugin == NULL) ? "" : plugin, plugin_instance, - (type == NULL) ? "" : type, type_instance); - name[sizeof (name) - 1] = '\0'; - - if (c_avl_get (threshold_tree, name, (void *) &th) == 0) - return (th); - else - return (NULL); -} /* }}} threshold_t *threshold_get */ /* * int ut_threshold_add @@ -170,58 +113,6 @@ static int ut_threshold_add (const threshold_t *th) return (status); } /* }}} int ut_threshold_add */ -/* - * threshold_t *threshold_search - * - * Searches for a threshold configuration using all the possible variations of - * "Host", "Plugin" and "Type" blocks. Returns NULL if no threshold could be - * found. - * XXX: This is likely the least efficient function in collectd. - */ -static threshold_t *threshold_search (const value_list_t *vl) -{ /* {{{ */ - threshold_t *th; - - if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance, - vl->type, vl->type_instance)) != NULL) - return (th); - else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance, - vl->type, NULL)) != NULL) - return (th); - else if ((th = threshold_get (vl->host, vl->plugin, NULL, - vl->type, vl->type_instance)) != NULL) - return (th); - else if ((th = threshold_get (vl->host, vl->plugin, NULL, - vl->type, NULL)) != NULL) - return (th); - else if ((th = threshold_get (vl->host, "", NULL, - vl->type, vl->type_instance)) != NULL) - return (th); - else if ((th = threshold_get (vl->host, "", NULL, - vl->type, NULL)) != NULL) - return (th); - else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance, - vl->type, vl->type_instance)) != NULL) - return (th); - else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance, - vl->type, NULL)) != NULL) - return (th); - else if ((th = threshold_get ("", vl->plugin, NULL, - vl->type, vl->type_instance)) != NULL) - return (th); - else if ((th = threshold_get ("", vl->plugin, NULL, - vl->type, NULL)) != NULL) - return (th); - else if ((th = threshold_get ("", "", NULL, - vl->type, vl->type_instance)) != NULL) - return (th); - else if ((th = threshold_get ("", "", NULL, - vl->type, NULL)) != NULL) - return (th); - - return (NULL); -} /* }}} threshold_t *threshold_search */ - /* * Configuration * ============= @@ -378,6 +269,8 @@ static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci) status = cf_util_get_flag (option, &th.flags, UT_FLAG_INVERT); else if (strcasecmp ("Persist", option->key) == 0) status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST); + else if (strcasecmp ("PersistOK", option->key) == 0) + status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERSIST_OK); else if (strcasecmp ("Percentage", option->key) == 0) status = cf_util_get_flag (option, &th.flags, UT_FLAG_PERCENTAGE); else if (strcasecmp ("Hits", option->key) == 0) @@ -512,64 +405,6 @@ static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci) return (status); } /* int ut_config_host */ - -int ut_config (oconfig_item_t *ci) -{ - int i; - int status = 0; - - threshold_t th; - - if (ci->values_num != 0) - { - ERROR ("threshold values: The `Threshold' block may not have any " - "arguments."); - return (-1); - } - - if (threshold_tree == NULL) - { - threshold_tree = c_avl_create ((void *) strcmp); - if (threshold_tree == NULL) - { - ERROR ("ut_config: c_avl_create failed."); - return (-1); - } - } - - memset (&th, '\0', sizeof (th)); - th.warning_min = NAN; - th.warning_max = NAN; - th.failure_min = NAN; - th.failure_max = NAN; - - th.hits = 0; - th.hysteresis = 0; - th.flags = UT_FLAG_INTERESTING; /* interesting by default */ - - for (i = 0; i < ci->children_num; i++) - { - oconfig_item_t *option = ci->children + i; - status = 0; - - if (strcasecmp ("Type", option->key) == 0) - status = ut_config_type (&th, option); - else if (strcasecmp ("Plugin", option->key) == 0) - status = ut_config_plugin (&th, option); - else if (strcasecmp ("Host", option->key) == 0) - status = ut_config_host (&th, option); - else - { - WARNING ("threshold values: Option `%s' not allowed here.", option->key); - status = -1; - } - - if (status != 0) - break; - } - - return (status); -} /* int um_config */ /* * End of the functions used to configure threshold values. */ @@ -601,8 +436,9 @@ static int ut_report_state (const data_set_t *ds, if ( (th->hits != 0) ) { int hits = uc_get_hits(ds,vl); - /* The STATE_OKAY always reset hits, or if hits reaise the limit */ - if ( (state == STATE_OKAY) || (hits > th->hits) ) + /* STATE_OKAY resets hits unless PERSIST_OK flag is set. Hits resets if + * threshold is hit. */ + if ( ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) ) || (hits > th->hits) ) { DEBUG("ut_report_state: reset uc_get_hits = 0"); uc_set_hits(ds,vl,0); /* reset hit counter and notify */ @@ -615,13 +451,13 @@ static int ut_report_state (const data_set_t *ds, state_old = uc_get_state (ds, vl); - /* If the state didn't change, only report if `persistent' is specified and - * the state is not `okay'. */ + /* If the state didn't change, report if `persistent' is specified. If the + * state is `okay', then only report if `persist_ok` flag is set. */ if (state == state_old) { if ((th->flags & UT_FLAG_PERSIST) == 0) return (0); - else if (state == STATE_OKAY) + else if ( (state == STATE_OKAY) && ((th->flags & UT_FLAG_PERSIST_OK) == 0) ) return (0); } @@ -717,15 +553,41 @@ static int ut_report_state (const data_set_t *ds, ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : ""); } } + else if (th->flags & UT_FLAG_PERCENTAGE) + { + gauge_t value; + gauge_t sum; + int i; + + sum = 0.0; + for (i = 0; i < vl->values_len; i++) + { + if (isnan (values[i])) + continue; + + sum += values[i]; + } + + if (sum == 0.0) + value = NAN; + else + value = 100.0 * values[ds_index] / sum; + + status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently " + "%g (%.2f%%). That is %s the %s threshold of %.2f%%.", + ds->ds[ds_index].name, values[ds_index], value, + (value < min) ? "below" : "above", + (state == STATE_ERROR) ? "failure" : "warning", + (value < min) ? min : max); + } else /* is not inverted */ { status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently " - "%f. That is %s the %s threshold of %f%s.", + "%f. That is %s the %s threshold of %f.", ds->ds[ds_index].name, values[ds_index], (values[ds_index] < min) ? "below" : "above", (state == STATE_ERROR) ? "failure" : "warning", - (values[ds_index] < min) ? min : max, - ((th->flags & UT_FLAG_PERCENTAGE) != 0) ? "%" : ""); + (values[ds_index] < min) ? min : max); } buf += status; bufsize -= status; @@ -886,11 +748,11 @@ static int ut_check_one_threshold (const data_set_t *ds, } /* }}} int ut_check_one_threshold */ /* - * int ut_check_threshold (PUBLIC) + * int ut_check_threshold * * Gets a list of matching thresholds and searches for the worst status by one * of the thresholds. Then reports that status using the ut_report_state - * function above. + * function above. * Returns zero on success and if no threshold has been configured. Returns * less than zero on failure. */ @@ -958,11 +820,101 @@ static int ut_check_threshold (const data_set_t *ds, const value_list_t *vl, return (0); } /* }}} int ut_check_threshold */ +/* + * int ut_missing + * + * This function is called whenever a value goes "missing". + */ +static int ut_missing (const value_list_t *vl, + __attribute__((unused)) user_data_t *ud) +{ /* {{{ */ + threshold_t *th; + cdtime_t missing_time; + char identifier[6 * DATA_MAX_NAME_LEN]; + notification_t n; + + if (threshold_tree == NULL) + return (0); + + th = threshold_search (vl); + /* dispatch notifications for "interesting" values only */ + if ((th == NULL) || ((th->flags & UT_FLAG_INTERESTING) == 0)) + return (0); + + missing_time = cdtime () - vl->time; + FORMAT_VL (identifier, sizeof (identifier), vl); + + NOTIFICATION_INIT_VL (&n, vl); + ssnprintf (n.message, sizeof (n.message), + "%s has not been updated for %.3f seconds.", + identifier, CDTIME_T_TO_DOUBLE (missing_time)); + + plugin_dispatch_notification (&n); + + return (0); +} /* }}} int ut_missing */ + +int ut_config (oconfig_item_t *ci) +{ /* {{{ */ + int i; + int status = 0; + + threshold_t th; + + if (threshold_tree == NULL) + { + threshold_tree = c_avl_create ((void *) strcmp); + if (threshold_tree == NULL) + { + ERROR ("ut_config: c_avl_create failed."); + return (-1); + } + } + + memset (&th, '\0', sizeof (th)); + th.warning_min = NAN; + th.warning_max = NAN; + th.failure_min = NAN; + th.failure_max = NAN; + + th.hits = 0; + th.hysteresis = 0; + th.flags = UT_FLAG_INTERESTING; /* interesting by default */ + + for (i = 0; i < ci->children_num; i++) + { + oconfig_item_t *option = ci->children + i; + status = 0; + + if (strcasecmp ("Type", option->key) == 0) + status = ut_config_type (&th, option); + else if (strcasecmp ("Plugin", option->key) == 0) + status = ut_config_plugin (&th, option); + else if (strcasecmp ("Host", option->key) == 0) + status = ut_config_host (&th, option); + else + { + WARNING ("threshold values: Option `%s' not allowed here.", option->key); + status = -1; + } + + if (status != 0) + break; + } + + if (c_avl_size (threshold_tree) > 0) { + plugin_register_missing ("threshold", ut_missing, + /* user data = */ NULL); + plugin_register_write ("threshold", ut_check_threshold, + /* user data = */ NULL); + } + + return (status); +} /* }}} int um_config */ + void module_register (void) { plugin_register_complex_config ("threshold", ut_config); - plugin_register_write ("threshold", ut_check_threshold, - /* user data = */ NULL); } /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */