2 * collectd - src/filter_pcre.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>
23 * This module allows to filter and rewrite value lists based on
24 * Perl-compatible regular expressions.
28 #include "configfile.h"
32 #include "utils_subst.h"
36 #define log_err(...) ERROR ("filter_pcre: " __VA_ARGS__)
37 #define log_warn(...) WARNING ("filter_pcre: " __VA_ARGS__)
44 /* regular expression */
48 /* extra information from studying the pattern */
51 /* replacment text for string substitution */
52 const char *replacement;
55 #define C_PCRE_INIT(regex) do { \
57 (regex).re_str = NULL; \
58 (regex).extra = NULL; \
59 (regex).replacement = NULL; \
62 #define C_PCRE_FREE(regex) do { \
63 pcre_free ((regex).re); \
64 free ((void *)(regex).re_str); \
65 pcre_free ((regex).extra); \
66 free ((void *)(regex).replacement); \
67 C_PCRE_INIT (regex); \
73 c_pcre_t plugin_instance;
75 c_pcre_t type_instance;
88 ovec_t plugin_instance;
97 static regex_t *regexes = NULL;
98 static int regexes_num = 0;
101 * internal helper functions
104 /* returns true if string matches the regular expression */
105 static int c_pcre_match (c_pcre_t *re, const char *string, ovec_t *ovec)
107 if ((NULL == re) || (NULL == re->re))
113 ovec->status = pcre_exec (re->re,
114 /* extra = */ re->extra,
115 /* subject = */ string,
116 /* length = */ strlen (string),
117 /* startoffset = */ 0,
119 /* ovector = */ ovec->vec,
120 /* ovecsize = */ STATIC_ARRAY_SIZE (ovec->vec));
122 if (0 <= ovec->status)
125 if (PCRE_ERROR_NOMATCH != ovec->status)
126 log_err ("PCRE matching of string \"%s\" failed with status %d",
127 string, ovec->status);
131 static int c_pcre_subst (c_pcre_t *re, char *string, size_t strlen,
136 if ((NULL == re) || (NULL == re->replacement))
139 assert (0 <= ovec->status);
141 if (NULL == subst (buffer, sizeof (buffer), string,
142 ovec->vec[0], ovec->vec[1], re->replacement)) {
143 log_err ("Substitution in string \"%s\" (using regex \"%s\" and "
144 "replacement string \"%s\") failed.",
145 string, re->re_str, re->replacement);
149 sstrncpy (string, buffer, strlen);
153 static regex_t *regex_new (void)
158 regexes = (regex_t *)realloc (regexes, regexes_num * sizeof (*regexes));
159 if (NULL == regexes) {
160 log_err ("Out of memory.");
164 re = regexes + (regexes_num - 1);
166 C_PCRE_INIT (re->host);
167 C_PCRE_INIT (re->plugin);
168 C_PCRE_INIT (re->plugin_instance);
169 C_PCRE_INIT (re->type);
170 C_PCRE_INIT (re->type_instance);
176 static void regex_delete (regex_t *re)
181 C_PCRE_FREE (re->host);
182 C_PCRE_FREE (re->plugin);
183 C_PCRE_FREE (re->plugin_instance);
184 C_PCRE_FREE (re->type);
185 C_PCRE_FREE (re->type_instance);
190 /* returns true if the value list matches the regular expression */
191 static int regex_match (regex_t *re, value_list_t *vl, ovectors_t *ovectors)
198 if (c_pcre_match (&re->host, vl->host, &ovectors->host))
201 if (c_pcre_match (&re->plugin, vl->plugin, &ovectors->plugin))
204 if (c_pcre_match (&re->plugin_instance, vl->plugin_instance,
205 &ovectors->plugin_instance))
208 if (c_pcre_match (&re->type, vl->type, &ovectors->type))
211 if (c_pcre_match (&re->type_instance, vl->type_instance,
212 &ovectors->type_instance))
220 static int regex_subst (regex_t *re, value_list_t *vl, ovectors_t *ovectors)
225 c_pcre_subst (&re->host, vl->host, sizeof (vl->host),
227 c_pcre_subst (&re->plugin, vl->plugin, sizeof (vl->plugin),
229 c_pcre_subst (&re->plugin_instance, vl->plugin_instance,
230 sizeof (vl->plugin_instance), &ovectors->plugin_instance);
231 c_pcre_subst (&re->type, vl->type, sizeof (vl->type),
233 c_pcre_subst (&re->type_instance, vl->type_instance,
234 sizeof (vl->type_instance), &ovectors->type_instance);
239 * interface to collectd
242 static int c_pcre_filter (const data_set_t *ds, value_list_t *vl)
248 for (i = 0; i < regexes_num; ++i)
249 if (regex_match (regexes + i, vl, &ovectors)) {
250 regex_subst (regexes + i, vl, &ovectors);
251 return regexes[i].action;
254 } /* c_pcre_filter */
256 static int c_pcre_shutdown (void)
260 plugin_unregister_filter ("filter_pcre");
261 plugin_unregister_shutdown ("filter_pcre");
263 for (i = 0; i < regexes_num; ++i)
264 regex_delete (regexes + i);
269 } /* c_pcre_shutdown */
271 static int config_set_regex (c_pcre_t *re, oconfig_item_t *ci)
277 if ((0 != ci->children_num) || (1 != ci->values_num)
278 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
279 log_err ("<RegEx>: %s expects a single string argument.", ci->key);
283 pattern = ci->values[0].value.string;
285 re->re = pcre_compile (pattern,
287 /* errptr = */ &errptr,
288 /* erroffset = */ &erroffset,
289 /* tableptr = */ NULL);
291 if (NULL == re->re) {
292 log_err ("<RegEx>: PCRE compilation of pattern \"%s\" failed "
293 "at offset %d: %s", pattern, erroffset, errptr);
297 re->re_str = sstrdup (pattern);
299 re->extra = pcre_study (re->re,
301 /* errptr = */ &errptr);
303 if (NULL != errptr) {
304 log_err ("<RegEx>: PCRE studying of pattern \"%s\" failed: %s",
309 } /* config_set_regex */
311 static int config_set_replacement (c_pcre_t *re, oconfig_item_t *ci)
313 if ((0 != ci->children_num) || (1 != ci->values_num)
314 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
315 log_err ("<RegEx>: %s expects a single string argument.", ci->key);
319 if (NULL == re->re) {
320 log_err ("<RegEx>: %s without an appropriate regex (%s) "
321 "is not allowed.", ci->key, ci->key + strlen ("Substitute"));
325 re->replacement = sstrdup (ci->values[0].value.string);
327 } /* config_set_replacement */
329 static int config_set_action (int *action, oconfig_item_t *ci)
331 const char *action_str;
333 if ((0 != ci->children_num) || (1 != ci->values_num)
334 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
335 log_err ("<RegEx>: Action expects a single string argument.");
339 action_str = ci->values[0].value.string;
341 if (0 == strcasecmp (action_str, "NoWrite"))
342 *action |= FILTER_NOWRITE;
343 else if (0 == strcasecmp (action_str, "NoThresholdCheck"))
344 *action |= FILTER_NOTHRESHOLD_CHECK;
345 else if (0 == strcasecmp (action_str, "Ignore"))
346 *action |= FILTER_IGNORE;
348 log_warn ("<Regex>: Ignoring unknown action \"%s\".", action_str);
350 } /* config_set_action */
352 static int c_pcre_config_regex (oconfig_item_t *ci)
357 if (0 != ci->values_num) {
358 log_err ("<RegEx> expects no arguments.");
364 for (i = 0; i < ci->children_num; ++i) {
365 oconfig_item_t *c = ci->children + i;
368 if (0 == strcasecmp (c->key, "Host"))
369 status = config_set_regex (&re->host, c);
370 else if (0 == strcasecmp (c->key, "Plugin"))
371 status = config_set_regex (&re->plugin, c);
372 else if (0 == strcasecmp (c->key, "PluginInstance"))
373 status = config_set_regex (&re->plugin_instance, c);
374 else if (0 == strcasecmp (c->key, "Type"))
375 status = config_set_regex (&re->type, c);
376 else if (0 == strcasecmp (c->key, "TypeInstance"))
377 status = config_set_regex (&re->type_instance, c);
378 else if (0 == strcasecmp (c->key, "Action"))
379 status = config_set_action (&re->action, c);
380 else if (0 == strcasecmp (c->key, "SubstituteHost"))
381 status = config_set_replacement (&re->host, c);
382 else if (0 == strcasecmp (c->key, "SubstitutePlugin"))
383 status = config_set_replacement (&re->plugin, c);
384 else if (0 == strcasecmp (c->key, "SubstitutePluginInstance"))
385 status = config_set_replacement (&re->plugin_instance, c);
386 else if (0 == strcasecmp (c->key, "SubstituteType"))
387 status = config_set_replacement (&re->type, c);
388 else if (0 == strcasecmp (c->key, "SubstituteTypeInstance"))
389 status = config_set_replacement (&re->type_instance, c);
391 log_warn ("<RegEx>: Ignoring unknown config key \"%s\".", c->key);
394 log_err ("Ignoring regular expression definition.");
400 } /* c_pcre_config_regex */
402 static int c_pcre_config (oconfig_item_t *ci)
406 for (i = 0; i < ci->children_num; ++i) {
407 oconfig_item_t *c = ci->children + i;
409 if (0 == strcasecmp (c->key, "RegEx"))
410 c_pcre_config_regex (c);
412 log_warn ("Ignoring unknown config key \"%s\".", c->key);
415 plugin_register_filter ("filter_pcre", c_pcre_filter);
416 plugin_register_shutdown ("filter_pcre", c_pcre_shutdown);
418 } /* c_pcre_config */
420 void module_register (void)
422 plugin_register_complex_config ("filter_pcre", c_pcre_config);
423 } /* module_register */
425 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */