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