Merge branch 'collectd-4.5'
[collectd.git] / src / filter_pcre.c
1 /**
2  * collectd - src/filter_pcre.c
3  * Copyright (C) 2008  Sebastian Harl
4  *
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.
8  *
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.
13  *
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
17  *
18  * Author:
19  *   Sebastian Harl <sh at tokkee.org>
20  **/
21
22 /*
23  * This module allows to filter and rewrite value lists based on
24  * Perl-compatible regular expressions.
25  */
26
27 #include "collectd.h"
28 #include "configfile.h"
29 #include "plugin.h"
30 #include "common.h"
31
32 #include "utils_subst.h"
33
34 #include <pcre.h>
35
36 #define log_err(...) ERROR ("filter_pcre: " __VA_ARGS__)
37 #define log_warn(...) WARNING ("filter_pcre: " __VA_ARGS__)
38
39 /*
40  * private data types
41  */
42
43 typedef struct {
44         /* regular expression */
45         pcre       *re;
46         const char *re_str;
47
48         /* extra information from studying the pattern */
49         pcre_extra *extra;
50
51         /* replacment text for string substitution */
52         const char *replacement;
53 } c_pcre_t;
54
55 #define C_PCRE_INIT(regex) do { \
56                 (regex).re     = NULL; \
57                 (regex).re_str = NULL; \
58                 (regex).extra  = NULL; \
59                 (regex).replacement = NULL; \
60         } while (0)
61
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); \
68         } while (0)
69
70 typedef struct {
71         c_pcre_t host;
72         c_pcre_t plugin;
73         c_pcre_t plugin_instance;
74         c_pcre_t type;
75         c_pcre_t type_instance;
76
77         int action;
78 } regex_t;
79
80 typedef struct {
81         int vec[30];
82         int status;
83 } ovec_t;
84
85 typedef struct {
86         ovec_t host;
87         ovec_t plugin;
88         ovec_t plugin_instance;
89         ovec_t type;
90         ovec_t type_instance;
91 } ovectors_t;
92
93 /*
94  * private variables
95  */
96
97 static regex_t *regexes     = NULL;
98 static int      regexes_num = 0;
99
100 /*
101  * internal helper functions
102  */
103
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)
106 {
107         if ((NULL == re) || (NULL == re->re))
108                 return 1;
109
110         if (NULL == string)
111                 string = "";
112
113         ovec->status = pcre_exec (re->re,
114                         /* extra       = */ re->extra,
115                         /* subject     = */ string,
116                         /* length      = */ strlen (string),
117                         /* startoffset = */ 0,
118                         /* options     = */ 0,
119                         /* ovector     = */ ovec->vec,
120                         /* ovecsize    = */ STATIC_ARRAY_SIZE (ovec->vec));
121
122         if (0 <= ovec->status)
123                 return 1;
124
125         if (PCRE_ERROR_NOMATCH != ovec->status)
126                 log_err ("PCRE matching of string \"%s\" failed with status %d",
127                                 string, ovec->status);
128         return 0;
129 } /* c_pcre_match */
130
131 static int c_pcre_subst (c_pcre_t *re, char *string, size_t strlen,
132                 ovec_t *ovec)
133 {
134         char buffer[strlen];
135
136         if ((NULL == re) || (NULL == re->replacement))
137                 return 0;
138
139         assert (0 <= ovec->status);
140
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);
146                 return -1;
147         }
148
149         sstrncpy (string, buffer, strlen);
150         return 0;
151 } /* c_pcre_subst */
152
153 static regex_t *regex_new (void)
154 {
155         regex_t *re;
156
157         ++regexes_num;
158         regexes = (regex_t *)realloc (regexes, regexes_num * sizeof (*regexes));
159         if (NULL == regexes) {
160                 log_err ("Out of memory.");
161                 exit (5);
162         }
163
164         re = regexes + (regexes_num - 1);
165
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);
171
172         re->action = 0;
173         return re;
174 } /* regex_new */
175
176 static void regex_delete (regex_t *re)
177 {
178         if (NULL == re)
179                 return;
180
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);
186
187         re->action = 0;
188 } /* regex_delete */
189
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)
192 {
193         int matches = 0;
194
195         if (NULL == re)
196                 return 1;
197
198         if (c_pcre_match (&re->host, vl->host, &ovectors->host))
199                 ++matches;
200
201         if (c_pcre_match (&re->plugin, vl->plugin, &ovectors->plugin))
202                 ++matches;
203
204         if (c_pcre_match (&re->plugin_instance, vl->plugin_instance,
205                                 &ovectors->plugin_instance))
206                 ++matches;
207
208         if (c_pcre_match (&re->type, vl->type, &ovectors->type))
209                 ++matches;
210
211         if (c_pcre_match (&re->type_instance, vl->type_instance,
212                                 &ovectors->type_instance))
213                 ++matches;
214
215         if (5 == matches)
216                 return 1;
217         return 0;
218 } /* regex_match */
219
220 static int regex_subst (regex_t *re, value_list_t *vl, ovectors_t *ovectors)
221 {
222         if (NULL == re)
223                 return 0;
224
225         c_pcre_subst (&re->host, vl->host, sizeof (vl->host),
226                         &ovectors->host);
227         c_pcre_subst (&re->plugin, vl->plugin, sizeof (vl->plugin),
228                         &ovectors->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),
232                         &ovectors->type);
233         c_pcre_subst (&re->type_instance, vl->type_instance,
234                         sizeof (vl->type_instance), &ovectors->type_instance);
235         return 0;
236 } /* regex_subst */
237
238 /*
239  * interface to collectd
240  */
241
242 static int c_pcre_filter (const data_set_t *ds, value_list_t *vl)
243 {
244         int i;
245
246         ovectors_t ovectors;
247
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;
252                 }
253         return 0;
254 } /* c_pcre_filter */
255
256 static int c_pcre_shutdown (void)
257 {
258         int i;
259
260         plugin_unregister_filter ("filter_pcre");
261         plugin_unregister_shutdown ("filter_pcre");
262
263         for (i = 0; i < regexes_num; ++i)
264                 regex_delete (regexes + i);
265
266         sfree (regexes);
267         regexes_num = 0;
268         return 0;
269 } /* c_pcre_shutdown */
270
271 static int config_set_regex (c_pcre_t *re, oconfig_item_t *ci)
272 {
273         const char *pattern;
274         const char *errptr;
275         int erroffset;
276
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);
280                 return 1;
281         }
282
283         pattern = ci->values[0].value.string;
284
285         re->re = pcre_compile (pattern,
286                         /* options   = */ 0,
287                         /* errptr    = */ &errptr,
288                         /* erroffset = */ &erroffset,
289                         /* tableptr  = */ NULL);
290
291         if (NULL == re->re) {
292                 log_err ("<RegEx>: PCRE compilation of pattern \"%s\" failed "
293                                 "at offset %d: %s", pattern, erroffset, errptr);
294                 return 1;
295         }
296
297         re->re_str = sstrdup (pattern);
298
299         re->extra = pcre_study (re->re,
300                         /* options = */ 0,
301                         /* errptr  = */ &errptr);
302
303         if (NULL != errptr) {
304                 log_err ("<RegEx>: PCRE studying of pattern \"%s\" failed: %s",
305                                 pattern, errptr);
306                 return 1;
307         }
308         return 0;
309 } /* config_set_regex */
310
311 static int config_set_replacement (c_pcre_t *re, oconfig_item_t *ci)
312 {
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);
316                 return 1;
317         }
318
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"));
322                 return 1;
323         }
324
325         re->replacement = sstrdup (ci->values[0].value.string);
326         return 0;
327 } /* config_set_replacement */
328
329 static int config_set_action (int *action, oconfig_item_t *ci)
330 {
331         const char *action_str;
332
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.");
336                 return 1;
337         }
338
339         action_str = ci->values[0].value.string;
340
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;
347         else
348                 log_warn ("<Regex>: Ignoring unknown action \"%s\".", action_str);
349         return 0;
350 } /* config_set_action */
351
352 static int c_pcre_config_regex (oconfig_item_t *ci)
353 {
354         regex_t *re;
355         int i;
356
357         if (0 != ci->values_num) {
358                 log_err ("<RegEx> expects no arguments.");
359                 return 1;
360         }
361
362         re = regex_new ();
363
364         for (i = 0; i < ci->children_num; ++i) {
365                 oconfig_item_t *c = ci->children + i;
366                 int status = 0;
367
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);
390                 else
391                         log_warn ("<RegEx>: Ignoring unknown config key \"%s\".", c->key);
392
393                 if (0 != status) {
394                         log_err ("Ignoring regular expression definition.");
395                         regex_delete (re);
396                         --regexes_num;
397                 }
398         }
399         return 0;
400 } /* c_pcre_config_regex */
401
402 static int c_pcre_config (oconfig_item_t *ci)
403 {
404         int i;
405
406         for (i = 0; i < ci->children_num; ++i) {
407                 oconfig_item_t *c = ci->children + i;
408
409                 if (0 == strcasecmp (c->key, "RegEx"))
410                         c_pcre_config_regex (c);
411                 else
412                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
413         }
414
415         plugin_register_filter ("filter_pcre", c_pcre_filter);
416         plugin_register_shutdown ("filter_pcre", c_pcre_shutdown);
417         return 0;
418 } /* c_pcre_config */
419
420 void module_register (void)
421 {
422         plugin_register_complex_config ("filter_pcre", c_pcre_config);
423 } /* module_register */
424
425 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
426