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