treewide: add blank line below collectd.h
[collectd.git] / src / match_value.c
1 /**
2  * collectd - src/match_value.c
3  * Copyright (C) 2008       Florian Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian Forster <octo at collectd.org>
25  **/
26
27 /*
28  * This module allows to filter and rewrite value lists based on
29  * Perl-compatible regular expressions.
30  */
31
32 #include "collectd.h"
33
34 #include "common.h"
35 #include "utils_cache.h"
36 #include "filter_chain.h"
37
38 #define SATISFY_ALL 0
39 #define SATISFY_ANY 1
40
41 /*
42  * private data types
43  */
44 struct mv_match_s;
45 typedef struct mv_match_s mv_match_t;
46 struct mv_match_s
47 {
48   gauge_t min;
49   gauge_t max;
50   int invert;
51   int satisfy;
52
53   char **data_sources;
54   size_t data_sources_num;
55 };
56
57 /*
58  * internal helper functions
59  */
60 static void mv_free_match (mv_match_t *m) /* {{{ */
61 {
62   size_t i;
63
64   if (m == NULL)
65     return;
66
67   if (m->data_sources != NULL)
68   {
69     for (i = 0; i < m->data_sources_num; ++i)
70       free(m->data_sources[i]);
71     free(m->data_sources);
72   }
73
74   free (m);
75 } /* }}} void mv_free_match */
76
77 static int mv_config_add_satisfy (mv_match_t *m, /* {{{ */
78     oconfig_item_t *ci)
79 {
80   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
81   {
82     ERROR ("`value' match: `%s' needs exactly one string argument.",
83         ci->key);
84     return (-1);
85   }
86
87   if (strcasecmp ("All", ci->values[0].value.string) == 0)
88     m->satisfy = SATISFY_ALL;
89   else if (strcasecmp ("Any", ci->values[0].value.string) == 0)
90     m->satisfy = SATISFY_ANY;
91   else
92   {
93     ERROR ("`value' match: Passing `%s' to the `%s' option is invalid. "
94         "The argument must either be `All' or `Any'.",
95         ci->values[0].value.string, ci->key);
96     return (-1);
97   }
98
99   return (0);
100 } /* }}} int mv_config_add_satisfy */
101
102 static int mv_config_add_data_source (mv_match_t *m, /* {{{ */
103     oconfig_item_t *ci)
104 {
105   size_t new_data_sources_num;
106   char **temp;
107   int i;
108
109   /* Check number of arbuments. */
110   if (ci->values_num < 1)
111   {
112     ERROR ("`value' match: `%s' needs at least one argument.",
113         ci->key);
114     return (-1);
115   }
116
117   /* Check type of arguments */
118   for (i = 0; i < ci->values_num; i++)
119   {
120     if (ci->values[i].type == OCONFIG_TYPE_STRING)
121       continue;
122
123     ERROR ("`value' match: `%s' accepts only string arguments "
124         "(argument %i is a %s).",
125         ci->key, i + 1,
126         (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
127         ? "truth value" : "number");
128     return (-1);
129   }
130
131   /* Allocate space for the char pointers */
132   new_data_sources_num = m->data_sources_num + ((size_t) ci->values_num);
133   temp = realloc (m->data_sources,
134       new_data_sources_num * sizeof (char *));
135   if (temp == NULL)
136   {
137     ERROR ("`value' match: realloc failed.");
138     return (-1);
139   }
140   m->data_sources = temp;
141
142   /* Copy the strings, allocating memory as needed. */
143   for (i = 0; i < ci->values_num; i++)
144   {
145     size_t j;
146
147     /* If we get here, there better be memory for us to write to. */
148     assert (m->data_sources_num < new_data_sources_num);
149
150     j = m->data_sources_num;
151     m->data_sources[j] = sstrdup (ci->values[i].value.string);
152     if (m->data_sources[j] == NULL)
153     {
154       ERROR ("`value' match: sstrdup failed.");
155       continue;
156     }
157     m->data_sources_num++;
158   }
159
160   return (0);
161 } /* }}} int mv_config_add_data_source */
162
163 static int mv_config_add_gauge (gauge_t *ret_value, /* {{{ */
164     oconfig_item_t *ci)
165 {
166
167   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
168   {
169     ERROR ("`value' match: `%s' needs exactly one numeric argument.",
170         ci->key);
171     return (-1);
172   }
173
174   *ret_value = ci->values[0].value.number;
175
176   return (0);
177 } /* }}} int mv_config_add_gauge */
178
179 static int mv_config_add_boolean (int *ret_value, /* {{{ */
180     oconfig_item_t *ci)
181 {
182
183   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
184   {
185     ERROR ("`value' match: `%s' needs exactly one boolean argument.",
186         ci->key);
187     return (-1);
188   }
189
190   if (ci->values[0].value.boolean)
191     *ret_value = 1;
192   else
193     *ret_value = 0;
194
195   return (0);
196 } /* }}} int mv_config_add_boolean */
197
198 static int mv_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
199 {
200   mv_match_t *m;
201   int status;
202   int i;
203
204   m = calloc (1, sizeof (*m));
205   if (m == NULL)
206   {
207     ERROR ("mv_create: calloc failed.");
208     return (-ENOMEM);
209   }
210
211   m->min = NAN;
212   m->max = NAN;
213   m->invert = 0;
214   m->satisfy = SATISFY_ALL;
215   m->data_sources = NULL;
216   m->data_sources_num = 0;
217
218   status = 0;
219   for (i = 0; i < ci->children_num; i++)
220   {
221     oconfig_item_t *child = ci->children + i;
222
223     if (strcasecmp ("Min", child->key) == 0)
224       status = mv_config_add_gauge (&m->min, child);
225     else if (strcasecmp ("Max", child->key) == 0)
226       status = mv_config_add_gauge (&m->max, child);
227     else if (strcasecmp ("Invert", child->key) == 0)
228       status = mv_config_add_boolean (&m->invert, child);
229     else if (strcasecmp ("Satisfy", child->key) == 0)
230       status = mv_config_add_satisfy (m, child);
231     else if (strcasecmp ("DataSource", child->key) == 0)
232       status = mv_config_add_data_source (m, child);
233     else
234     {
235       ERROR ("`value' match: The `%s' configuration option is not "
236           "understood and will be ignored.", child->key);
237       status = 0;
238     }
239
240     if (status != 0)
241       break;
242   }
243
244   /* Additional sanity-checking */
245   while (status == 0)
246   {
247     if (isnan (m->min) && isnan (m->max))
248     {
249       ERROR ("`value' match: Neither minimum nor maximum are defined. "
250           "This match will be ignored.");
251       status = -1;
252     }
253
254     break;
255   }
256
257   if (status != 0)
258   {
259     mv_free_match (m);
260     return (status);
261   }
262
263   *user_data = m;
264   return (0);
265 } /* }}} int mv_create */
266
267 static int mv_destroy (void **user_data) /* {{{ */
268 {
269   if ((user_data != NULL) && (*user_data != NULL))
270     mv_free_match (*user_data);
271   return (0);
272 } /* }}} int mv_destroy */
273
274 static int mv_match (const data_set_t *ds, const value_list_t *vl, /* {{{ */
275     notification_meta_t __attribute__((unused)) **meta, void **user_data)
276 {
277   mv_match_t *m;
278   gauge_t *values;
279   int status;
280   size_t i;
281
282   if ((user_data == NULL) || (*user_data == NULL))
283     return (-1);
284
285   m = *user_data;
286
287   values = uc_get_rate (ds, vl);
288   if (values == NULL)
289   {
290     ERROR ("`value' match: Retrieving the current rate from the cache "
291         "failed.");
292     return (-1);
293   }
294
295   status = FC_MATCH_NO_MATCH;
296
297   for (i = 0; i < ds->ds_num; i++)
298   {
299     int value_matches = 0;
300
301     /* Check if this data source is relevant. */
302     if (m->data_sources != NULL)
303     {
304       size_t j;
305
306       for (j = 0; j < m->data_sources_num; j++)
307         if (strcasecmp (ds->ds[i].name, m->data_sources[j]) == 0)
308           break;
309
310       /* No match, ignore this data source. */
311       if (j >=  m->data_sources_num)
312         continue;
313     }
314
315     DEBUG ("`value' match: current = %g; min = %g; max = %g; invert = %s;",
316         values[i], m->min, m->max,
317         m->invert ? "true" : "false");
318
319     if ((!isnan (m->min) && (values[i] < m->min))
320         || (!isnan (m->max) && (values[i] > m->max)))
321       value_matches = 0;
322     else
323       value_matches = 1;
324
325     if (m->invert)
326     {
327       if (value_matches)
328         value_matches = 0;
329       else
330         value_matches = 1;
331     }
332
333     if (value_matches != 0)
334     {
335       status = FC_MATCH_MATCHES;
336       if (m->satisfy == SATISFY_ANY)
337         break;
338     }
339     else
340     {
341       status = FC_MATCH_NO_MATCH;
342       if (m->satisfy == SATISFY_ALL)
343         break;
344     }
345   } /* for (i = 0; i < ds->ds_num; i++) */
346
347   free (values);
348   return (status);
349 } /* }}} int mv_match */
350
351 void module_register (void)
352 {
353   match_proc_t mproc = { 0 };
354
355   mproc.create  = mv_create;
356   mproc.destroy = mv_destroy;
357   mproc.match   = mv_match;
358   fc_register_match ("value", mproc);
359 } /* module_register */
360
361 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
362