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,
109 DEBUG ("regex match: Regular expression `%s' matches `%s'.",
111 return (FC_MATCH_MATCHES);
115 DEBUG ("regex match: Regular expression `%s' does not match `%s'.",
121 return (FC_MATCH_NO_MATCH);
122 } /* }}} int mr_match_regexen */
124 static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */
130 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
132 log_warn ("`%s' needs exactly one string argument.", ci->key);
136 re = (mr_regex_t *) malloc (sizeof (*re));
139 log_err ("mr_config_add_regex: malloc failed.");
142 memset (re, 0, sizeof (*re));
145 re->re_str = strdup (ci->values[0].value.string);
146 if (re->re_str == NULL)
149 log_err ("mr_config_add_regex: strdup failed.");
153 status = regcomp (&re->re, re->re_str, REG_EXTENDED | REG_NOSUB);
157 regerror (status, &re->re, errmsg, sizeof (errmsg));
158 errmsg[sizeof (errmsg) - 1] = 0;
159 log_err ("Compiling regex `%s' for `%s' failed: %s.",
160 re->re_str, ci->key, errmsg);
166 if (*re_head == NULL)
175 while (ptr->next != NULL)
182 } /* }}} int mr_config_add_regex */
184 static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
190 m = (mr_match_t *) malloc (sizeof (*m));
193 log_err ("mr_create: malloc failed.");
196 memset (m, 0, sizeof (*m));
199 for (i = 0; i < ci->children_num; i++)
201 oconfig_item_t *child = ci->children + i;
203 if ((strcasecmp ("Host", child->key) == 0)
204 || (strcasecmp ("Hostname", child->key) == 0))
205 status = mr_config_add_regex (&m->host, child);
206 else if (strcasecmp ("Plugin", child->key) == 0)
207 status = mr_config_add_regex (&m->plugin, child);
208 else if (strcasecmp ("PluginInstance", child->key) == 0)
209 status = mr_config_add_regex (&m->plugin_instance, child);
210 else if (strcasecmp ("Type", child->key) == 0)
211 status = mr_config_add_regex (&m->type, child);
212 else if (strcasecmp ("TypeInstance", child->key) == 0)
213 status = mr_config_add_regex (&m->type_instance, child);
216 log_err ("The `%s' configuration option is not understood and "
217 "will be ignored.", child->key);
225 /* Additional sanity-checking */
228 if ((m->host == NULL)
229 && (m->plugin == NULL)
230 && (m->plugin_instance == NULL)
232 && (m->type_instance == NULL))
234 log_err ("No (valid) regular expressions have been configured. "
235 "This match will be ignored.");
250 } /* }}} int mr_create */
252 static int mr_destroy (void **user_data) /* {{{ */
254 if ((user_data != NULL) && (*user_data != NULL))
255 mr_free_match (*user_data);
257 } /* }}} int mr_destroy */
259 static int mr_match (const data_set_t *ds, const value_list_t *vl, /* {{{ */
260 notification_meta_t **meta, void **user_data)
264 if ((user_data == NULL) || (*user_data == NULL))
269 if (mr_match_regexen (m->host, vl->host) == FC_MATCH_NO_MATCH)
270 return (FC_MATCH_NO_MATCH);
271 if (mr_match_regexen (m->plugin, vl->plugin) == FC_MATCH_NO_MATCH)
272 return (FC_MATCH_NO_MATCH);
273 if (mr_match_regexen (m->plugin_instance,
274 vl->plugin_instance) == FC_MATCH_NO_MATCH)
275 return (FC_MATCH_NO_MATCH);
276 if (mr_match_regexen (m->type, vl->type) == FC_MATCH_NO_MATCH)
277 return (FC_MATCH_NO_MATCH);
278 if (mr_match_regexen (m->type_instance,
279 vl->type_instance) == FC_MATCH_NO_MATCH)
280 return (FC_MATCH_NO_MATCH);
282 return (FC_MATCH_MATCHES);
283 } /* }}} int mr_match */
285 void module_register (void)
289 memset (&mproc, 0, sizeof (mproc));
290 mproc.create = mr_create;
291 mproc.destroy = mr_destroy;
292 mproc.match = mr_match;
293 fc_register_match ("regex", mproc);
294 } /* module_register */
296 /* vim: set sw=4 ts=4 tw=78 noexpandtab fdm=marker : */