treewide: replace memset to 0 with initializers
[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 #include "common.h"
34 #include "utils_cache.h"
35 #include "filter_chain.h"
36
37 #define SATISFY_ALL 0
38 #define SATISFY_ANY 1
39
40 /*
41  * private data types
42  */
43 struct mv_match_s;
44 typedef struct mv_match_s mv_match_t;
45 struct mv_match_s
46 {
47   gauge_t min;
48   gauge_t max;
49   int invert;
50   int satisfy;
51
52   char **data_sources;
53   size_t data_sources_num;
54 };
55
56 /*
57  * internal helper functions
58  */
59 static void mv_free_match (mv_match_t *m) /* {{{ */
60 {
61   size_t i;
62
63   if (m == NULL)
64     return;
65
66   if (m->data_sources != NULL)
67   {
68     for (i = 0; i < m->data_sources_num; ++i)
69       free(m->data_sources[i]);
70     free(m->data_sources);
71   }
72
73   free (m);
74 } /* }}} void mv_free_match */
75
76 static int mv_config_add_satisfy (mv_match_t *m, /* {{{ */
77     oconfig_item_t *ci)
78 {
79   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
80   {
81     ERROR ("`value' match: `%s' needs exactly one string argument.",
82         ci->key);
83     return (-1);
84   }
85
86   if (strcasecmp ("All", ci->values[0].value.string) == 0)
87     m->satisfy = SATISFY_ALL;
88   else if (strcasecmp ("Any", ci->values[0].value.string) == 0)
89     m->satisfy = SATISFY_ANY;
90   else
91   {
92     ERROR ("`value' match: Passing `%s' to the `%s' option is invalid. "
93         "The argument must either be `All' or `Any'.",
94         ci->values[0].value.string, ci->key);
95     return (-1);
96   }
97
98   return (0);
99 } /* }}} int mv_config_add_satisfy */
100
101 static int mv_config_add_data_source (mv_match_t *m, /* {{{ */
102     oconfig_item_t *ci)
103 {
104   size_t new_data_sources_num;
105   char **temp;
106   int i;
107
108   /* Check number of arbuments. */
109   if (ci->values_num < 1)
110   {
111     ERROR ("`value' match: `%s' needs at least one argument.",
112         ci->key);
113     return (-1);
114   }
115
116   /* Check type of arguments */
117   for (i = 0; i < ci->values_num; i++)
118   {
119     if (ci->values[i].type == OCONFIG_TYPE_STRING)
120       continue;
121
122     ERROR ("`value' match: `%s' accepts only string arguments "
123         "(argument %i is a %s).",
124         ci->key, i + 1,
125         (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
126         ? "truth value" : "number");
127     return (-1);
128   }
129
130   /* Allocate space for the char pointers */
131   new_data_sources_num = m->data_sources_num + ((size_t) ci->values_num);
132   temp = realloc (m->data_sources,
133       new_data_sources_num * sizeof (char *));
134   if (temp == NULL)
135   {
136     ERROR ("`value' match: realloc failed.");
137     return (-1);
138   }
139   m->data_sources = temp;
140
141   /* Copy the strings, allocating memory as needed. */
142   for (i = 0; i < ci->values_num; i++)
143   {
144     size_t j;
145
146     /* If we get here, there better be memory for us to write to. */
147     assert (m->data_sources_num < new_data_sources_num);
148
149     j = m->data_sources_num;
150     m->data_sources[j] = sstrdup (ci->values[i].value.string);
151     if (m->data_sources[j] == NULL)
152     {
153       ERROR ("`value' match: sstrdup failed.");
154       continue;
155     }
156     m->data_sources_num++;
157   }
158
159   return (0);
160 } /* }}} int mv_config_add_data_source */
161
162 static int mv_config_add_gauge (gauge_t *ret_value, /* {{{ */
163     oconfig_item_t *ci)
164 {
165
166   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
167   {
168     ERROR ("`value' match: `%s' needs exactly one numeric argument.",
169         ci->key);
170     return (-1);
171   }
172
173   *ret_value = ci->values[0].value.number;
174
175   return (0);
176 } /* }}} int mv_config_add_gauge */
177
178 static int mv_config_add_boolean (int *ret_value, /* {{{ */
179     oconfig_item_t *ci)
180 {
181
182   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
183   {
184     ERROR ("`value' match: `%s' needs exactly one boolean argument.",
185         ci->key);
186     return (-1);
187   }
188
189   if (ci->values[0].value.boolean)
190     *ret_value = 1;
191   else
192     *ret_value = 0;
193
194   return (0);
195 } /* }}} int mv_config_add_boolean */
196
197 static int mv_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
198 {
199   mv_match_t *m;
200   int status;
201   int i;
202
203   m = calloc (1, sizeof (*m));
204   if (m == NULL)
205   {
206     ERROR ("mv_create: calloc failed.");
207     return (-ENOMEM);
208   }
209
210   m->min = NAN;
211   m->max = NAN;
212   m->invert = 0;
213   m->satisfy = SATISFY_ALL;
214   m->data_sources = NULL;
215   m->data_sources_num = 0;
216
217   status = 0;
218   for (i = 0; i < ci->children_num; i++)
219   {
220     oconfig_item_t *child = ci->children + i;
221
222     if (strcasecmp ("Min", child->key) == 0)
223       status = mv_config_add_gauge (&m->min, child);
224     else if (strcasecmp ("Max", child->key) == 0)
225       status = mv_config_add_gauge (&m->max, child);
226     else if (strcasecmp ("Invert", child->key) == 0)
227       status = mv_config_add_boolean (&m->invert, child);
228     else if (strcasecmp ("Satisfy", child->key) == 0)
229       status = mv_config_add_satisfy (m, child);
230     else if (strcasecmp ("DataSource", child->key) == 0)
231       status = mv_config_add_data_source (m, child);
232     else
233     {
234       ERROR ("`value' match: The `%s' configuration option is not "
235           "understood and will be ignored.", child->key);
236       status = 0;
237     }
238
239     if (status != 0)
240       break;
241   }
242
243   /* Additional sanity-checking */
244   while (status == 0)
245   {
246     if (isnan (m->min) && isnan (m->max))
247     {
248       ERROR ("`value' match: Neither minimum nor maximum are defined. "
249           "This match will be ignored.");
250       status = -1;
251     }
252
253     break;
254   }
255
256   if (status != 0)
257   {
258     mv_free_match (m);
259     return (status);
260   }
261
262   *user_data = m;
263   return (0);
264 } /* }}} int mv_create */
265
266 static int mv_destroy (void **user_data) /* {{{ */
267 {
268   if ((user_data != NULL) && (*user_data != NULL))
269     mv_free_match (*user_data);
270   return (0);
271 } /* }}} int mv_destroy */
272
273 static int mv_match (const data_set_t *ds, const value_list_t *vl, /* {{{ */
274     notification_meta_t __attribute__((unused)) **meta, void **user_data)
275 {
276   mv_match_t *m;
277   gauge_t *values;
278   int status;
279   size_t i;
280
281   if ((user_data == NULL) || (*user_data == NULL))
282     return (-1);
283
284   m = *user_data;
285
286   values = uc_get_rate (ds, vl);
287   if (values == NULL)
288   {
289     ERROR ("`value' match: Retrieving the current rate from the cache "
290         "failed.");
291     return (-1);
292   }
293
294   status = FC_MATCH_NO_MATCH;
295
296   for (i = 0; i < ds->ds_num; i++)
297   {
298     int value_matches = 0;
299
300     /* Check if this data source is relevant. */
301     if (m->data_sources != NULL)
302     {
303       size_t j;
304
305       for (j = 0; j < m->data_sources_num; j++)
306         if (strcasecmp (ds->ds[i].name, m->data_sources[j]) == 0)
307           break;
308
309       /* No match, ignore this data source. */
310       if (j >=  m->data_sources_num)
311         continue;
312     }
313
314     DEBUG ("`value' match: current = %g; min = %g; max = %g; invert = %s;",
315         values[i], m->min, m->max,
316         m->invert ? "true" : "false");
317
318     if ((!isnan (m->min) && (values[i] < m->min))
319         || (!isnan (m->max) && (values[i] > m->max)))
320       value_matches = 0;
321     else
322       value_matches = 1;
323
324     if (m->invert)
325     {
326       if (value_matches)
327         value_matches = 0;
328       else
329         value_matches = 1;
330     }
331
332     if (value_matches != 0)
333     {
334       status = FC_MATCH_MATCHES;
335       if (m->satisfy == SATISFY_ANY)
336         break;
337     }
338     else
339     {
340       status = FC_MATCH_NO_MATCH;
341       if (m->satisfy == SATISFY_ALL)
342         break;
343     }
344   } /* for (i = 0; i < ds->ds_num; i++) */
345
346   free (values);
347   return (status);
348 } /* }}} int mv_match */
349
350 void module_register (void)
351 {
352   match_proc_t mproc = { 0 };
353
354   mproc.create  = mv_create;
355   mproc.destroy = mv_destroy;
356   mproc.match   = mv_match;
357   fc_register_match ("value", mproc);
358 } /* module_register */
359
360 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
361