2 * collectd - src/match_value.c
3 * Copyright (C) 2008 Florian Forster
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.
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.
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
19 * Florian Forster <octo at verplant.org>
23 * This module allows to filter and rewrite value lists based on
24 * Perl-compatible regular expressions.
29 #include "utils_cache.h"
30 #include "filter_chain.h"
39 typedef struct mv_match_s mv_match_t;
48 size_t data_sources_num;
52 * internal helper functions
54 static void mv_free_match (mv_match_t *m) /* {{{ */
61 if (m->data_sources != NULL)
63 for (i = 0; i < m->data_sources_num; ++i)
64 free(m->data_sources[i]);
65 free(m->data_sources);
69 } /* }}} void mv_free_match */
71 static int mv_config_add_satisfy (mv_match_t *m, /* {{{ */
74 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
76 ERROR ("`value' match: `%s' needs exactly one string argument.",
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;
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);
94 } /* }}} int mv_config_add_satisfy */
96 static int mv_config_add_data_source (mv_match_t *m, /* {{{ */
99 size_t new_data_sources_num;
103 /* Check number of arbuments. */
104 if (ci->values_num < 1)
106 ERROR ("`value' match: `%s' needs at least one argument.",
111 /* Check type of arguments */
112 for (i = 0; i < ci->values_num; i++)
114 if (ci->values[i].type == OCONFIG_TYPE_STRING)
117 ERROR ("`value' match: `%s' accepts only string arguments "
118 "(argument %i is a %s).",
120 (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
121 ? "truth value" : "number");
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 *));
131 ERROR ("`value' match: realloc failed.");
134 m->data_sources = temp;
136 /* Copy the strings, allocating memory as needed. */
137 for (i = 0; i < ci->values_num; i++)
141 /* If we get here, there better be memory for us to write to. */
142 assert (m->data_sources_num < new_data_sources_num);
144 j = m->data_sources_num;
145 m->data_sources[j] = sstrdup (ci->values[i].value.string);
146 if (m->data_sources[j] == NULL)
148 ERROR ("`value' match: sstrdup failed.");
151 m->data_sources_num++;
155 } /* }}} int mv_config_add_data_source */
157 static int mv_config_add_gauge (gauge_t *ret_value, /* {{{ */
161 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
163 ERROR ("`value' match: `%s' needs exactly one numeric argument.",
168 *ret_value = ci->values[0].value.number;
171 } /* }}} int mv_config_add_gauge */
173 static int mv_config_add_boolean (int *ret_value, /* {{{ */
177 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
179 ERROR ("`value' match: `%s' needs exactly one boolean argument.",
184 if (ci->values[0].value.boolean)
190 } /* }}} int mv_config_add_boolean */
192 static int mv_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
198 m = (mv_match_t *) malloc (sizeof (*m));
201 ERROR ("mv_create: malloc failed.");
204 memset (m, 0, sizeof (*m));
209 m->satisfy = SATISFY_ALL;
210 m->data_sources = NULL;
211 m->data_sources_num = 0;
214 for (i = 0; i < ci->children_num; i++)
216 oconfig_item_t *child = ci->children + i;
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);
230 ERROR ("`value' match: The `%s' configuration option is not "
231 "understood and will be ignored.", child->key);
239 /* Additional sanity-checking */
242 if (isnan (m->min) && isnan (m->max))
244 ERROR ("`value' match: Neither minimum nor maximum are defined. "
245 "This match will be ignored.");
260 } /* }}} int mv_create */
262 static int mv_destroy (void **user_data) /* {{{ */
264 if ((user_data != NULL) && (*user_data != NULL))
265 mv_free_match (*user_data);
267 } /* }}} int mv_destroy */
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)
277 if ((user_data == NULL) || (*user_data == NULL))
282 values = uc_get_rate (ds, vl);
285 ERROR ("`value' match: Retrieving the current rate from the cache "
290 status = FC_MATCH_NO_MATCH;
292 for (i = 0; i < ds->ds_num; i++)
294 int value_matches = 0;
296 /* Check if this data source is relevant. */
297 if (m->data_sources != NULL)
301 for (j = 0; j < m->data_sources_num; j++)
302 if (strcasecmp (ds->ds[i].name, m->data_sources[j]) == 0)
305 /* No match, ignore this data source. */
306 if (j >= m->data_sources_num)
310 DEBUG ("`value' match: current = %g; min = %g; max = %g; invert = %s;",
311 values[i], m->min, m->max,
312 m->invert ? "true" : "false");
314 if ((!isnan (m->min) && (values[i] < m->min))
315 || (!isnan (m->max) && (values[i] > m->max)))
328 if (value_matches != 0)
330 status = FC_MATCH_MATCHES;
331 if (m->satisfy == SATISFY_ANY)
334 else if (value_matches == 0)
336 status = FC_MATCH_NO_MATCH;
337 if (m->satisfy == SATISFY_ALL)
340 } /* for (i = 0; i < ds->ds_num; i++) */
344 } /* }}} int mv_match */
346 void module_register (void)
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 */
357 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */