2 * collectd - src/match_regex.c
3 * Copyright (C) 2008 Sebastian Harl
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 * Sebastian Harl <sh at tokkee.org>
20 * Florian Forster <octo at verplant.org>
24 * This module allows to filter and rewrite value lists based on
25 * Perl-compatible regular expressions.
29 #include "filter_chain.h"
31 #include <sys/types.h>
34 #define log_err(...) ERROR ("`regex' match: " __VA_ARGS__)
35 #define log_warn(...) WARNING ("`regex' match: " __VA_ARGS__)
42 typedef struct mr_regex_s mr_regex_t;
52 typedef struct mr_match_s mr_match_t;
57 mr_regex_t *plugin_instance;
59 mr_regex_t *type_instance;
63 * internal helper functions
65 static void mr_free_regex (mr_regex_t *r) /* {{{ */
71 memset (&r->re, 0, sizeof (r->re));
75 mr_free_regex (r->next);
76 } /* }}} void mr_free_regex */
78 static void mr_free_match (mr_match_t *m) /* {{{ */
83 mr_free_regex (m->host);
84 mr_free_regex (m->plugin);
85 mr_free_regex (m->plugin_instance);
86 mr_free_regex (m->type);
87 mr_free_regex (m->type_instance);
90 } /* }}} void mr_free_match */
92 static int mr_match_regexen (mr_regex_t *re_head, /* {{{ */
98 return (FC_MATCH_MATCHES);
100 for (re = re_head; re != NULL; re = re->next)
104 status = regexec (&re->re, string,
105 /* nmatch = */ 0, /* pmatch = */ NULL,
108 return (FC_MATCH_MATCHES);
111 return (FC_MATCH_NO_MATCH);
112 } /* }}} int mr_match_regexen */
114 static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */
120 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
122 log_warn ("`%s' needs exactly one string argument.", ci->key);
126 re = (mr_regex_t *) malloc (sizeof (*re));
129 log_err ("mr_config_add_regex: malloc failed.");
132 memset (re, 0, sizeof (*re));
135 re->re_str = strdup (ci->values[0].value.string);
139 log_err ("mr_config_add_regex: strdup failed.");
143 status = regcomp (&re->re, re->re_str, REG_EXTENDED | REG_NOSUB);
147 regerror (status, &re->re, errmsg, sizeof (errmsg));
148 errmsg[sizeof (errmsg) - 1] = 0;
149 log_err ("Compiling regex `%s' for `%s' failed: %s.",
150 re->re_str, ci->key, errmsg);
156 if (*re_head == NULL)
165 while (ptr->next != NULL)
172 } /* }}} int mr_config_add_regex */
174 static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
180 m = (mr_match_t *) malloc (sizeof (*m));
183 log_err ("mr_create: malloc failed.");
186 memset (m, 0, sizeof (*m));
189 for (i = 0; i < ci->children_num; i++)
191 oconfig_item_t *child = ci->children + i;
193 if ((strcasecmp ("Host", child->key) == 0)
194 || (strcasecmp ("Hostname", child->key) == 0))
195 status = mr_config_add_regex (&m->host, child);
196 else if (strcasecmp ("Plugin", child->key) == 0)
197 status = mr_config_add_regex (&m->plugin, child);
198 else if (strcasecmp ("PluginInstance", child->key) == 0)
199 status = mr_config_add_regex (&m->plugin_instance, child);
200 else if (strcasecmp ("Type", child->key) == 0)
201 status = mr_config_add_regex (&m->type, child);
202 else if (strcasecmp ("TypeInstance", child->key) == 0)
203 status = mr_config_add_regex (&m->type_instance, child);
206 log_err ("The `%s' configuration option is not understood and "
207 "will be ignored.", child->key);
215 /* Additional sanity-checking */
218 if ((m->host == NULL)
219 && (m->plugin == NULL)
220 && (m->plugin_instance == NULL)
222 && (m->type_instance == NULL))
224 log_err ("No (valid) regular expressions have been configured. "
225 "This match will be ignored.");
240 } /* }}} int mr_create */
242 static int mr_destroy (void **user_data) /* {{{ */
244 if ((user_data != NULL) && (*user_data != NULL))
245 mr_free_match (*user_data);
247 } /* }}} int mr_destroy */
249 static int mr_match (const data_set_t *ds, const value_list_t *vl, /* {{{ */
250 notification_meta_t **meta, void **user_data)
254 if ((user_data == NULL) || (*user_data == NULL))
259 if (mr_match_regexen (m->host, vl->host) == FC_MATCH_NO_MATCH)
260 return (FC_MATCH_NO_MATCH);
261 if (mr_match_regexen (m->plugin, vl->plugin) == FC_MATCH_NO_MATCH)
262 return (FC_MATCH_NO_MATCH);
263 if (mr_match_regexen (m->plugin_instance,
264 vl->plugin_instance) == FC_MATCH_NO_MATCH)
265 return (FC_MATCH_NO_MATCH);
266 if (mr_match_regexen (m->type, vl->type) == FC_MATCH_NO_MATCH)
267 return (FC_MATCH_NO_MATCH);
268 if (mr_match_regexen (m->type_instance,
269 vl->type_instance) == FC_MATCH_NO_MATCH)
270 return (FC_MATCH_NO_MATCH);
272 return (FC_MATCH_MATCHES);
273 } /* }}} int mr_match */
275 void module_register (void)
279 memset (&mproc, 0, sizeof (mproc));
280 mproc.create = mr_create;
281 mproc.destroy = mr_destroy;
282 mproc.match = mr_match;
283 fc_register_match ("regex", mproc);
284 } /* module_register */
286 /* vim: set sw=4 ts=4 tw=78 noexpandtab fdm=marker : */