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