1 /**
2 * collectd - src/match_regex.c
3 * Copyright (C) 2008 Sebastian Harl
4 * Copyright (C) 2008 Florian Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Sebastian Harl <sh at tokkee.org>
21 * Florian Forster <octo at verplant.org>
22 **/
24 /*
25 * This module allows to filter and rewrite value lists based on
26 * Perl-compatible regular expressions.
27 */
29 #include "collectd.h"
30 #include "filter_chain.h"
32 #include <sys/types.h>
33 #include <regex.h>
35 #define log_err(...) ERROR ("`regex' match: " __VA_ARGS__)
36 #define log_warn(...) WARNING ("`regex' match: " __VA_ARGS__)
38 /*
39 * private data types
40 */
42 struct mr_regex_s;
43 typedef struct mr_regex_s mr_regex_t;
44 struct mr_regex_s
45 {
46 regex_t re;
47 char *re_str;
49 mr_regex_t *next;
50 };
52 struct mr_match_s;
53 typedef struct mr_match_s mr_match_t;
54 struct mr_match_s
55 {
56 mr_regex_t *host;
57 mr_regex_t *plugin;
58 mr_regex_t *plugin_instance;
59 mr_regex_t *type;
60 mr_regex_t *type_instance;
61 };
63 /*
64 * internal helper functions
65 */
66 static void mr_free_regex (mr_regex_t *r) /* {{{ */
67 {
68 if (r == NULL)
69 return;
71 regfree (&r->re);
72 memset (&r->re, 0, sizeof (r->re));
73 free (r->re_str);
75 if (r->next != NULL)
76 mr_free_regex (r->next);
77 } /* }}} void mr_free_regex */
79 static void mr_free_match (mr_match_t *m) /* {{{ */
80 {
81 if (m == NULL)
82 return;
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);
90 free (m);
91 } /* }}} void mr_free_match */
93 static int mr_match_regexen (mr_regex_t *re_head, /* {{{ */
94 const char *string)
95 {
96 mr_regex_t *re;
98 if (re_head == NULL)
99 return (FC_MATCH_MATCHES);
101 for (re = re_head; re != NULL; re = re->next)
102 {
103 int status;
105 status = regexec (&re->re, string,
106 /* nmatch = */ 0, /* pmatch = */ NULL,
107 /* eflags = */ 0);
108 if (status == 0)
109 {
110 DEBUG ("regex match: Regular expression `%s' matches `%s'.",
111 re->re_str, string);
112 }
113 else
114 {
115 DEBUG ("regex match: Regular expression `%s' does not match `%s'.",
116 re->re_str, string);
117 return (FC_MATCH_NO_MATCH);
118 }
120 }
122 return (FC_MATCH_MATCHES);
123 } /* }}} int mr_match_regexen */
125 static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */
126 oconfig_item_t *ci)
127 {
128 mr_regex_t *re;
129 int status;
131 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
132 {
133 log_warn ("`%s' needs exactly one string argument.", ci->key);
134 return (-1);
135 }
137 re = (mr_regex_t *) malloc (sizeof (*re));
138 if (re == NULL)
139 {
140 log_err ("mr_config_add_regex: malloc failed.");
141 return (-1);
142 }
143 memset (re, 0, sizeof (*re));
144 re->next = NULL;
146 re->re_str = strdup (ci->values[0].value.string);
147 if (re->re_str == NULL)
148 {
149 free (re);
150 log_err ("mr_config_add_regex: strdup failed.");
151 return (-1);
152 }
154 status = regcomp (&re->re, re->re_str, REG_EXTENDED | REG_NOSUB);
155 if (status != 0)
156 {
157 char errmsg[1024];
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);
162 free (re->re_str);
163 free (re);
164 return (-1);
165 }
167 if (*re_head == NULL)
168 {
169 *re_head = re;
170 }
171 else
172 {
173 mr_regex_t *ptr;
175 ptr = *re_head;
176 while (ptr->next != NULL)
177 ptr = ptr->next;
179 ptr->next = re;
180 }
182 return (0);
183 } /* }}} int mr_config_add_regex */
185 static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
186 {
187 mr_match_t *m;
188 int status;
189 int i;
191 m = (mr_match_t *) malloc (sizeof (*m));
192 if (m == NULL)
193 {
194 log_err ("mr_create: malloc failed.");
195 return (-ENOMEM);
196 }
197 memset (m, 0, sizeof (*m));
199 status = 0;
200 for (i = 0; i < ci->children_num; i++)
201 {
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);
215 else
216 {
217 log_err ("The `%s' configuration option is not understood and "
218 "will be ignored.", child->key);
219 status = 0;
220 }
222 if (status != 0)
223 break;
224 }
226 /* Additional sanity-checking */
227 while (status == 0)
228 {
229 if ((m->host == NULL)
230 && (m->plugin == NULL)
231 && (m->plugin_instance == NULL)
232 && (m->type == NULL)
233 && (m->type_instance == NULL))
234 {
235 log_err ("No (valid) regular expressions have been configured. "
236 "This match will be ignored.");
237 status = -1;
238 }
240 break;
241 }
243 if (status != 0)
244 {
245 mr_free_match (m);
246 return (status);
247 }
249 *user_data = m;
250 return (0);
251 } /* }}} int mr_create */
253 static int mr_destroy (void **user_data) /* {{{ */
254 {
255 if ((user_data != NULL) && (*user_data != NULL))
256 mr_free_match (*user_data);
257 return (0);
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,
263 void **user_data)
264 {
265 mr_match_t *m;
267 if ((user_data == NULL) || (*user_data == NULL))
268 return (-1);
270 m = *user_data;
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)
289 {
290 match_proc_t mproc;
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 : */