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.
28 #include "utils_cache.h"
29 #include "filter_chain.h"
35 typedef struct mv_match_s mv_match_t;
44 * internal helper functions
46 static void mv_free_match (mv_match_t *m) /* {{{ */
52 } /* }}} void mv_free_match */
54 static int mv_config_add_gauge (gauge_t *ret_value, /* {{{ */
58 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
60 ERROR ("`value' match: `%s' needs exactly one numeric argument.",
65 *ret_value = ci->values[0].value.number;
68 } /* }}} int mv_config_add_gauge */
70 static int mv_config_add_boolean (int *ret_value, /* {{{ */
74 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
76 ERROR ("`value' match: `%s' needs exactly one boolean argument.",
81 if (ci->values[0].value.boolean)
87 } /* }}} int mv_config_add_boolean */
89 static int mv_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
95 m = (mv_match_t *) malloc (sizeof (*m));
98 ERROR ("mv_create: malloc failed.");
101 memset (m, 0, sizeof (*m));
108 for (i = 0; i < ci->children_num; i++)
110 oconfig_item_t *child = ci->children + i;
112 if (strcasecmp ("Min", child->key) == 0)
113 status = mv_config_add_gauge (&m->min, child);
114 else if (strcasecmp ("Max", child->key) == 0)
115 status = mv_config_add_gauge (&m->max, child);
116 else if (strcasecmp ("Invert", child->key) == 0)
117 status = mv_config_add_boolean (&m->invert, child);
120 ERROR ("`value' match: The `%s' configuration option is not "
121 "understood and will be ignored.", child->key);
129 /* Additional sanity-checking */
132 if (isnan (m->min) && isnan (m->max))
134 ERROR ("`value' match: Neither minimum nor maximum are defined. "
135 "This match will be ignored.");
150 } /* }}} int mv_create */
152 static int mv_destroy (void **user_data) /* {{{ */
154 if ((user_data != NULL) && (*user_data != NULL))
155 mv_free_match (*user_data);
157 } /* }}} int mv_destroy */
159 static int mv_match (const data_set_t *ds, const value_list_t *vl, /* {{{ */
160 notification_meta_t **meta, void **user_data)
167 if ((user_data == NULL) || (*user_data == NULL))
172 values = uc_get_rate (ds, vl);
175 ERROR ("`value' match: Retrieving the current rate from the cache "
180 status = FC_MATCH_MATCHES;
181 for (i = 0; i < ds->ds_num; i++)
183 DEBUG ("`value' match: current = %g; min = %g; max = %g; invert = %s;",
184 values[i], m->min, m->max,
185 m->invert ? "true" : "false");
187 if ((!isnan (m->min) && (values[i] < m->min))
188 || (!isnan (m->max) && (values[i] > m->max)))
190 status = FC_MATCH_NO_MATCH;
197 if (status == FC_MATCH_MATCHES)
198 status = FC_MATCH_NO_MATCH;
200 status = FC_MATCH_MATCHES;
205 } /* }}} int mv_match */
207 void module_register (void)
211 memset (&mproc, 0, sizeof (mproc));
212 mproc.create = mv_create;
213 mproc.destroy = mv_destroy;
214 mproc.match = mv_match;
215 fc_register_match ("value", mproc);
216 } /* module_register */
218 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */