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) /* {{{ */
60 } /* }}} void mv_free_match */
62 static int mv_config_add_satisfy (mv_match_t *m, /* {{{ */
65 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
67 ERROR ("`value' match: `%s' needs exactly one string argument.",
72 if (strcasecmp ("All", ci->values[0].value.string) == 0)
73 m->satisfy = SATISFY_ALL;
74 else if (strcasecmp ("Any", ci->values[0].value.string) == 0)
75 m->satisfy = SATISFY_ANY;
78 ERROR ("`value' match: Passing `%s' to the `%s' option is invalid. "
79 "The argument must either be `All' or `Any'.",
80 ci->values[0].value.string, ci->key);
85 } /* }}} int mv_config_add_satisfy */
87 static int mv_config_add_data_source (mv_match_t *m, /* {{{ */
90 size_t new_data_sources_num;
94 /* Check number of arbuments. */
95 if (ci->values_num < 1)
97 ERROR ("`value' match: `%s' needs at least one argument.",
102 /* Check type of arguments */
103 for (i = 0; i < ci->values_num; i++)
105 if (ci->values[i].type == OCONFIG_TYPE_STRING)
108 ERROR ("`value' match: `%s' accepts only string arguments "
109 "(argument %i is a %s).",
111 (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
112 ? "truth value" : "number");
116 /* Allocate space for the char pointers */
117 new_data_sources_num = m->data_sources_num + ((size_t) ci->values_num);
118 temp = (char **) realloc (m->data_sources,
119 new_data_sources_num * sizeof (char *));
122 ERROR ("`value' match: realloc failed.");
125 m->data_sources = temp;
127 /* Copy the strings, allocating memory as needed. */
128 for (i = 0; i < ci->values_num; i++)
132 /* If we get here, there better be memory for us to write to. */
133 assert (m->data_sources_num < new_data_sources_num);
135 j = m->data_sources_num;
136 m->data_sources[j] = sstrdup (ci->values[i].value.string);
137 if (m->data_sources[j] == NULL)
139 ERROR ("`value' match: sstrdup failed.");
142 m->data_sources_num++;
146 } /* }}} int mv_config_add_data_source */
148 static int mv_config_add_gauge (gauge_t *ret_value, /* {{{ */
152 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
154 ERROR ("`value' match: `%s' needs exactly one numeric argument.",
159 *ret_value = ci->values[0].value.number;
162 } /* }}} int mv_config_add_gauge */
164 static int mv_config_add_boolean (int *ret_value, /* {{{ */
168 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
170 ERROR ("`value' match: `%s' needs exactly one boolean argument.",
175 if (ci->values[0].value.boolean)
181 } /* }}} int mv_config_add_boolean */
183 static int mv_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
189 m = (mv_match_t *) malloc (sizeof (*m));
192 ERROR ("mv_create: malloc failed.");
195 memset (m, 0, sizeof (*m));
200 m->satisfy = SATISFY_ALL;
201 m->data_sources = NULL;
202 m->data_sources_num = 0;
205 for (i = 0; i < ci->children_num; i++)
207 oconfig_item_t *child = ci->children + i;
209 if (strcasecmp ("Min", child->key) == 0)
210 status = mv_config_add_gauge (&m->min, child);
211 else if (strcasecmp ("Max", child->key) == 0)
212 status = mv_config_add_gauge (&m->max, child);
213 else if (strcasecmp ("Invert", child->key) == 0)
214 status = mv_config_add_boolean (&m->invert, child);
215 else if (strcasecmp ("Satisfy", child->key) == 0)
216 status = mv_config_add_satisfy (m, child);
217 else if (strcasecmp ("DataSource", child->key) == 0)
218 status = mv_config_add_data_source (m, child);
221 ERROR ("`value' match: The `%s' configuration option is not "
222 "understood and will be ignored.", child->key);
230 /* Additional sanity-checking */
233 if (isnan (m->min) && isnan (m->max))
235 ERROR ("`value' match: Neither minimum nor maximum are defined. "
236 "This match will be ignored.");
251 } /* }}} int mv_create */
253 static int mv_destroy (void **user_data) /* {{{ */
255 if ((user_data != NULL) && (*user_data != NULL))
256 mv_free_match (*user_data);
258 } /* }}} int mv_destroy */
260 static int mv_match (const data_set_t *ds, const value_list_t *vl, /* {{{ */
261 notification_meta_t __attribute__((unused)) **meta, void **user_data)
268 if ((user_data == NULL) || (*user_data == NULL))
273 values = uc_get_rate (ds, vl);
276 ERROR ("`value' match: Retrieving the current rate from the cache "
281 status = FC_MATCH_NO_MATCH;
283 for (i = 0; i < ds->ds_num; i++)
285 int value_matches = 0;
287 /* Check if this data source is relevant. */
288 if (m->data_sources != NULL)
292 for (j = 0; j < m->data_sources_num; j++)
293 if (strcasecmp (ds->ds[i].name, m->data_sources[j]) == 0)
296 /* No match, ignore this data source. */
297 if (j >= m->data_sources_num)
301 DEBUG ("`value' match: current = %g; min = %g; max = %g; invert = %s;",
302 values[i], m->min, m->max,
303 m->invert ? "true" : "false");
305 if ((!isnan (m->min) && (values[i] < m->min))
306 || (!isnan (m->max) && (values[i] > m->max)))
319 if (value_matches != 0)
321 status = FC_MATCH_MATCHES;
322 if (m->satisfy == SATISFY_ANY)
325 else if (value_matches == 0)
327 status = FC_MATCH_NO_MATCH;
328 if (m->satisfy == SATISFY_ALL)
331 } /* for (i = 0; i < ds->ds_num; i++) */
335 } /* }}} int mv_match */
337 void module_register (void)
341 memset (&mproc, 0, sizeof (mproc));
342 mproc.create = mv_create;
343 mproc.destroy = mv_destroy;
344 mproc.match = mv_match;
345 fc_register_match ("value", mproc);
346 } /* module_register */
348 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */