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
6 * Copyright (C) 2014 Pierre-Yves Ritschard
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 * Pierre-Yves Ritschard <pyr at spootnik.org>
23 * Florian octo Forster <octo at collectd.org>
24 * Sebastian Harl <sh at tokkee.org>
25 * Andrés J. Díaz <ajdiaz at connectical.com>
31 #include "utils_avltree.h"
32 #include "utils_cache.h"
33 #include "utils_threshold.h"
40 * Threshold management
41 * ====================
42 * The following functions add, delete, search, etc. configured thresholds to
43 * the underlying AVL trees.
46 * threshold_t *threshold_get
48 * Retrieve one specific threshold configuration. For looking up a threshold
49 * matching a value_list_t, see "threshold_search" below. Returns NULL if the
50 * specified threshold doesn't exist.
52 static threshold_t *threshold_get (const char *hostname,
53 const char *plugin, const char *plugin_instance,
54 const char *type, const char *type_instance)
56 char name[6 * DATA_MAX_NAME_LEN];
57 threshold_t *th = NULL;
59 format_name (name, sizeof (name),
60 (hostname == NULL) ? "" : hostname,
61 (plugin == NULL) ? "" : plugin, plugin_instance,
62 (type == NULL) ? "" : type, type_instance);
63 name[sizeof (name) - 1] = '\0';
65 if (c_avl_get (threshold_tree, name, (void *) &th) == 0)
69 } /* }}} threshold_t *threshold_get */
72 * threshold_t *threshold_search
74 * Searches for a threshold configuration using all the possible variations of
75 * "Host", "Plugin" and "Type" blocks. Returns NULL if no threshold could be
77 * XXX: This is likely the least efficient function in collectd.
79 static threshold_t *threshold_search (const value_list_t *vl)
83 if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
84 vl->type, vl->type_instance)) != NULL)
86 else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
87 vl->type, NULL)) != NULL)
89 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
90 vl->type, vl->type_instance)) != NULL)
92 else if ((th = threshold_get (vl->host, vl->plugin, NULL,
93 vl->type, NULL)) != NULL)
95 else if ((th = threshold_get (vl->host, "", NULL,
96 vl->type, vl->type_instance)) != NULL)
98 else if ((th = threshold_get (vl->host, "", NULL,
99 vl->type, NULL)) != NULL)
101 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
102 vl->type, vl->type_instance)) != NULL)
104 else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
105 vl->type, NULL)) != NULL)
107 else if ((th = threshold_get ("", vl->plugin, NULL,
108 vl->type, vl->type_instance)) != NULL)
110 else if ((th = threshold_get ("", vl->plugin, NULL,
111 vl->type, NULL)) != NULL)
113 else if ((th = threshold_get ("", "", NULL,
114 vl->type, vl->type_instance)) != NULL)
116 else if ((th = threshold_get ("", "", NULL,
117 vl->type, NULL)) != NULL)
121 } /* }}} threshold_t *threshold_search */
124 * int ut_check_one_data_source
126 * Checks one data source against the given threshold configuration. If the
127 * `DataSource' option is set in the threshold, and the name does NOT match,
128 * `okay' is returned. If the threshold does match, its failure and warning
129 * min and max values are checked and `failure' or `warning' is returned if
133 static int ut_check_one_data_source (const data_set_t *ds,
134 const value_list_t __attribute__((unused)) *vl,
135 const threshold_t *th,
136 const gauge_t *values,
142 int prev_state = STATE_OKAY;
144 /* check if this threshold applies to this data source */
147 ds_name = ds->ds[ds_index].name;
148 if ((th->data_source[0] != 0)
149 && (strcmp (ds_name, th->data_source) != 0))
153 if ((th->flags & UT_FLAG_INVERT) != 0)
159 /* XXX: This is an experimental code, not optimized, not fast, not reliable,
160 * and probably, do not work as you expect. Enjoy! :D */
161 if ( (th->hysteresis > 0) && ((prev_state = uc_get_state(ds,vl)) != STATE_OKAY) )
166 if ( (!isnan (th->failure_min) && ((th->failure_min + th->hysteresis) < values[ds_index])) ||
167 (!isnan (th->failure_max) && ((th->failure_max - th->hysteresis) > values[ds_index])) )
172 if ( (!isnan (th->warning_min) && ((th->warning_min + th->hysteresis) < values[ds_index])) ||
173 (!isnan (th->warning_max) && ((th->warning_max - th->hysteresis) > values[ds_index])) )
179 else { /* no hysteresis */
180 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
181 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
184 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
185 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
190 return (STATE_ERROR);
193 return (STATE_WARNING);
196 } /* }}} int ut_check_one_data_source */
199 * int ut_check_one_threshold
201 * Checks all data sources of a value list against the given threshold, using
202 * the ut_check_one_data_source function above. Returns the worst status,
203 * which is `okay' if nothing has failed.
204 * Returns less than zero if the data set doesn't have any data sources.
206 static int ut_check_one_threshold (const data_set_t *ds,
207 const value_list_t *vl,
208 const threshold_t *th,
209 const gauge_t *values,
215 gauge_t values_copy[ds->ds_num];
217 memcpy (values_copy, values, sizeof (values_copy));
219 if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
226 WARNING ("ut_check_one_threshold: The %s type has only one data "
227 "source, but you have configured to check this as a percentage. "
228 "That doesn't make much sense, because the percentage will always "
229 "be 100%%!", ds->type);
232 /* Prepare `sum' and `num'. */
233 for (i = 0; i < ds->ds_num; i++)
234 if (!isnan (values[i]))
240 if ((num == 0) /* All data sources are undefined. */
241 || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
243 for (i = 0; i < ds->ds_num; i++)
244 values_copy[i] = NAN;
246 else /* We can actually calculate the percentage. */
248 for (i = 0; i < ds->ds_num; i++)
249 values_copy[i] = 100.0 * values[i] / sum;
251 } /* if (UT_FLAG_PERCENTAGE) */
253 for (i = 0; i < ds->ds_num; i++)
255 status = ut_check_one_data_source (ds, vl, th, values_copy, i);
258 if (statuses[i] < status)
259 statuses[i] = status;
261 } /* for (ds->ds_num) */
264 } /* }}} int ut_check_one_threshold */
267 * int ut_check_threshold
269 * Gets a list of matching thresholds and searches for the worst status by one
270 * of the thresholds. Then reports that status using the ut_report_state
272 * Returns zero on success and if no threshold has been configured. Returns
273 * less than zero on failure.
275 int write_riemann_threshold_check (const data_set_t *ds, const value_list_t *vl,
282 memset(statuses, 0, vl->values_len * sizeof(*statuses));
283 if (threshold_tree == NULL)
286 /* Is this lock really necessary? So far, thresholds are only inserted at
288 pthread_mutex_lock (&threshold_lock);
289 th = threshold_search (vl);
290 pthread_mutex_unlock (&threshold_lock);
294 DEBUG ("ut_check_threshold: Found matching threshold(s)");
296 values = uc_get_rate (ds, vl);
302 status = ut_check_one_threshold (ds, vl, th, values, statuses);
305 ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
316 } /* }}} int ut_check_threshold */
319 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */