2 * collectd - src/match_regex.c
3 * Copyright (C) 2008 Sebastian Harl
4 * Copyright (C) 2008 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Sebastian Harl <sh at tokkee.org>
21 * Florian Forster <octo at verplant.org>
25 * This module allows to filter and rewrite value lists based on
26 * Perl-compatible regular expressions.
30 #include "filter_chain.h"
32 #include <sys/types.h>
35 #define log_err(...) ERROR ("`regex' match: " __VA_ARGS__)
36 #define log_warn(...) WARNING ("`regex' match: " __VA_ARGS__)
43 typedef struct mr_regex_s mr_regex_t;
53 typedef struct mr_match_s mr_match_t;
58 mr_regex_t *plugin_instance;
60 mr_regex_t *type_instance;
64 * internal helper functions
66 static void mr_free_regex (mr_regex_t *r) /* {{{ */
72 memset (&r->re, 0, sizeof (r->re));
76 mr_free_regex (r->next);
77 } /* }}} void mr_free_regex */
79 static void mr_free_match (mr_match_t *m) /* {{{ */
84 mr_free_regex (m->host);
85 mr_free_regex (m->plugin);
86 mr_free_regex (m->plugin_instance);
87 mr_free_regex (m->type);
88 mr_free_regex (m->type_instance);
91 } /* }}} void mr_free_match */
93 static int mr_match_regexen (mr_regex_t *re_head, /* {{{ */
99 return (FC_MATCH_MATCHES);
101 for (re = re_head; re != NULL; re = re->next)
105 status = regexec (&re->re, string,
106 /* nmatch = */ 0, /* pmatch = */ NULL,
110 DEBUG ("regex match: Regular expression `%s' matches `%s'.",
115 DEBUG ("regex match: Regular expression `%s' does not match `%s'.",
117 return (FC_MATCH_NO_MATCH);
122 return (FC_MATCH_MATCHES);
123 } /* }}} int mr_match_regexen */
125 static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */
131 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
133 log_warn ("`%s' needs exactly one string argument.", ci->key);
137 re = (mr_regex_t *) malloc (sizeof (*re));
140 log_err ("mr_config_add_regex: malloc failed.");
143 memset (re, 0, sizeof (*re));
146 re->re_str = strdup (ci->values[0].value.string);
147 if (re->re_str == NULL)
150 log_err ("mr_config_add_regex: strdup failed.");
154 status = regcomp (&re->re, re->re_str, REG_EXTENDED | REG_NOSUB);
158 regerror (status, &re->re, errmsg, sizeof (errmsg));
159 errmsg[sizeof (errmsg) - 1] = 0;
160 log_err ("Compiling regex `%s' for `%s' failed: %s.",
161 re->re_str, ci->key, errmsg);
167 if (*re_head == NULL)
176 while (ptr->next != NULL)
183 } /* }}} int mr_config_add_regex */
185 static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
191 m = (mr_match_t *) malloc (sizeof (*m));
194 log_err ("mr_create: malloc failed.");
197 memset (m, 0, sizeof (*m));
200 for (i = 0; i < ci->children_num; i++)
202 oconfig_item_t *child = ci->children + i;
204 if ((strcasecmp ("Host", child->key) == 0)
205 || (strcasecmp ("Hostname", child->key) == 0))
206 status = mr_config_add_regex (&m->host, child);
207 else if (strcasecmp ("Plugin", child->key) == 0)
208 status = mr_config_add_regex (&m->plugin, child);
209 else if (strcasecmp ("PluginInstance", child->key) == 0)
210 status = mr_config_add_regex (&m->plugin_instance, child);
211 else if (strcasecmp ("Type", child->key) == 0)
212 status = mr_config_add_regex (&m->type, child);
213 else if (strcasecmp ("TypeInstance", child->key) == 0)
214 status = mr_config_add_regex (&m->type_instance, child);
217 log_err ("The `%s' configuration option is not understood and "
218 "will be ignored.", child->key);
226 /* Additional sanity-checking */
229 if ((m->host == NULL)
230 && (m->plugin == NULL)
231 && (m->plugin_instance == NULL)
233 && (m->type_instance == NULL))
235 log_err ("No (valid) regular expressions have been configured. "
236 "This match will be ignored.");
251 } /* }}} int mr_create */
253 static int mr_destroy (void **user_data) /* {{{ */
255 if ((user_data != NULL) && (*user_data != NULL))
256 mr_free_match (*user_data);
258 } /* }}} int mr_destroy */
260 static int mr_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
261 const value_list_t *vl,
262 notification_meta_t __attribute__((unused)) **meta,
267 if ((user_data == NULL) || (*user_data == NULL))
272 if (mr_match_regexen (m->host, vl->host) == FC_MATCH_NO_MATCH)
273 return (FC_MATCH_NO_MATCH);
274 if (mr_match_regexen (m->plugin, vl->plugin) == FC_MATCH_NO_MATCH)
275 return (FC_MATCH_NO_MATCH);
276 if (mr_match_regexen (m->plugin_instance,
277 vl->plugin_instance) == FC_MATCH_NO_MATCH)
278 return (FC_MATCH_NO_MATCH);
279 if (mr_match_regexen (m->type, vl->type) == FC_MATCH_NO_MATCH)
280 return (FC_MATCH_NO_MATCH);
281 if (mr_match_regexen (m->type_instance,
282 vl->type_instance) == FC_MATCH_NO_MATCH)
283 return (FC_MATCH_NO_MATCH);
285 return (FC_MATCH_MATCHES);
286 } /* }}} int mr_match */
288 void module_register (void)
292 memset (&mproc, 0, sizeof (mproc));
293 mproc.create = mr_create;
294 mproc.destroy = mr_destroy;
295 mproc.match = mr_match;
296 fc_register_match ("regex", mproc);
297 } /* module_register */
299 /* vim: set sw=4 ts=4 tw=78 noexpandtab fdm=marker : */