1 /**
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
7 *
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.
11 *
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.
16 *
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
20 *
21 * Author:
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>
26 **/
28 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_avltree.h"
32 #include "utils_cache.h"
33 #include "utils_threshold.h"
35 #include <assert.h>
36 #include <ltdl.h>
37 #include <pthread.h>
39 /*
40 * Threshold management
41 * ====================
42 * The following functions add, delete, etc. configured thresholds to
43 * the underlying AVL trees.
44 */
46 /*
47 * int ut_check_one_data_source
48 *
49 * Checks one data source against the given threshold configuration. If the
50 * `DataSource' option is set in the threshold, and the name does NOT match,
51 * `okay' is returned. If the threshold does match, its failure and warning
52 * min and max values are checked and `failure' or `warning' is returned if
53 * appropriate.
54 * Does not fail.
55 */
56 static int ut_check_one_data_source (const data_set_t *ds,
57 const value_list_t __attribute__((unused)) *vl,
58 const threshold_t *th,
59 const gauge_t *values,
60 int ds_index)
61 { /* {{{ */
62 const char *ds_name;
63 int is_warning = 0;
64 int is_failure = 0;
65 int prev_state = STATE_OKAY;
67 /* check if this threshold applies to this data source */
68 if (ds != NULL)
69 {
70 ds_name = ds->ds[ds_index].name;
71 if ((th->data_source[0] != 0)
72 && (strcmp (ds_name, th->data_source) != 0))
73 return (STATE_OKAY);
74 }
76 if ((th->flags & UT_FLAG_INVERT) != 0)
77 {
78 is_warning--;
79 is_failure--;
80 }
82 /* XXX: This is an experimental code, not optimized, not fast, not reliable,
83 * and probably, do not work as you expect. Enjoy! :D */
84 if ( (th->hysteresis > 0) && ((prev_state = uc_get_state(ds,vl)) != STATE_OKAY) )
85 {
86 switch(prev_state)
87 {
88 case STATE_ERROR:
89 if ( (!isnan (th->failure_min) && ((th->failure_min + th->hysteresis) < values[ds_index])) ||
90 (!isnan (th->failure_max) && ((th->failure_max - th->hysteresis) > values[ds_index])) )
91 return (STATE_OKAY);
92 else
93 is_failure++;
94 case STATE_WARNING:
95 if ( (!isnan (th->warning_min) && ((th->warning_min + th->hysteresis) < values[ds_index])) ||
96 (!isnan (th->warning_max) && ((th->warning_max - th->hysteresis) > values[ds_index])) )
97 return (STATE_OKAY);
98 else
99 is_warning++;
100 }
101 }
102 else { /* no hysteresis */
103 if ((!isnan (th->failure_min) && (th->failure_min > values[ds_index]))
104 || (!isnan (th->failure_max) && (th->failure_max < values[ds_index])))
105 is_failure++;
107 if ((!isnan (th->warning_min) && (th->warning_min > values[ds_index]))
108 || (!isnan (th->warning_max) && (th->warning_max < values[ds_index])))
109 is_warning++;
110 }
112 if (is_failure != 0)
113 return (STATE_ERROR);
115 if (is_warning != 0)
116 return (STATE_WARNING);
118 return (STATE_OKAY);
119 } /* }}} int ut_check_one_data_source */
121 /*
122 * int ut_check_one_threshold
123 *
124 * Checks all data sources of a value list against the given threshold, using
125 * the ut_check_one_data_source function above. Returns the worst status,
126 * which is `okay' if nothing has failed.
127 * Returns less than zero if the data set doesn't have any data sources.
128 */
129 static int ut_check_one_threshold (const data_set_t *ds,
130 const value_list_t *vl,
131 const threshold_t *th,
132 const gauge_t *values,
133 int *statuses)
134 { /* {{{ */
135 int ret = -1;
136 int i;
137 int status;
138 gauge_t values_copy[ds->ds_num];
140 memcpy (values_copy, values, sizeof (values_copy));
142 if ((th->flags & UT_FLAG_PERCENTAGE) != 0)
143 {
144 int num = 0;
145 gauge_t sum=0.0;
147 if (ds->ds_num == 1)
148 {
149 WARNING ("ut_check_one_threshold: The %s type has only one data "
150 "source, but you have configured to check this as a percentage. "
151 "That doesn't make much sense, because the percentage will always "
152 "be 100%%!", ds->type);
153 }
155 /* Prepare `sum' and `num'. */
156 for (i = 0; i < ds->ds_num; i++)
157 if (!isnan (values[i]))
158 {
159 num++;
160 sum += values[i];
161 }
163 if ((num == 0) /* All data sources are undefined. */
164 || (sum == 0.0)) /* Sum is zero, cannot calculate percentage. */
165 {
166 for (i = 0; i < ds->ds_num; i++)
167 values_copy[i] = NAN;
168 }
169 else /* We can actually calculate the percentage. */
170 {
171 for (i = 0; i < ds->ds_num; i++)
172 values_copy[i] = 100.0 * values[i] / sum;
173 }
174 } /* if (UT_FLAG_PERCENTAGE) */
176 for (i = 0; i < ds->ds_num; i++)
177 {
178 status = ut_check_one_data_source (ds, vl, th, values_copy, i);
179 if (status != -1) {
180 ret = 0;
181 if (statuses[i] < status)
182 statuses[i] = status;
183 }
184 } /* for (ds->ds_num) */
186 return (ret);
187 } /* }}} int ut_check_one_threshold */
189 /*
190 * int ut_check_threshold
191 *
192 * Gets a list of matching thresholds and searches for the worst status by one
193 * of the thresholds. Then reports that status using the ut_report_state
194 * function above.
195 * Returns zero on success and if no threshold has been configured. Returns
196 * less than zero on failure.
197 */
198 int write_riemann_threshold_check (const data_set_t *ds, const value_list_t *vl,
199 int *statuses)
200 { /* {{{ */
201 threshold_t *th;
202 gauge_t *values;
203 int status;
205 memset(statuses, 0, vl->values_len * sizeof(*statuses));
206 if (threshold_tree == NULL)
207 return 0;
209 /* Is this lock really necessary? So far, thresholds are only inserted at
210 * startup. -octo */
211 pthread_mutex_lock (&threshold_lock);
212 th = threshold_search (vl);
213 pthread_mutex_unlock (&threshold_lock);
214 if (th == NULL)
215 return (0);
217 DEBUG ("ut_check_threshold: Found matching threshold(s)");
219 values = uc_get_rate (ds, vl);
220 if (values == NULL)
221 return (0);
223 while (th != NULL)
224 {
225 status = ut_check_one_threshold (ds, vl, th, values, statuses);
226 if (status < 0)
227 {
228 ERROR ("ut_check_threshold: ut_check_one_threshold failed.");
229 sfree (values);
230 return (-1);
231 }
233 th = th->next;
234 } /* while (th) */
236 sfree (values);
238 return (0);
239 } /* }}} int ut_check_threshold */
242 /* vim: set sw=2 ts=8 sts=2 tw=78 et fdm=marker : */