1 /**
2 * collectd - src/match_regex.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 * Authors:
19 * Sebastian Harl <sh at tokkee.org>
20 * Florian Forster <octo at verplant.org>
21 **/
23 /*
24 * This module allows to filter and rewrite value lists based on
25 * Perl-compatible regular expressions.
26 */
28 #include "collectd.h"
29 #include "filter_chain.h"
31 #include <sys/types.h>
32 #include <regex.h>
34 #define log_err(...) ERROR ("`regex' match: " __VA_ARGS__)
35 #define log_warn(...) WARNING ("`regex' match: " __VA_ARGS__)
37 /*
38 * private data types
39 */
41 struct mr_regex_s;
42 typedef struct mr_regex_s mr_regex_t;
43 struct mr_regex_s
44 {
45 regex_t re;
46 char *re_str;
48 mr_regex_t *next;
49 };
51 struct mr_match_s;
52 typedef struct mr_match_s mr_match_t;
53 struct mr_match_s
54 {
55 mr_regex_t *host;
56 mr_regex_t *plugin;
57 mr_regex_t *plugin_instance;
58 mr_regex_t *type;
59 mr_regex_t *type_instance;
60 };
62 /*
63 * internal helper functions
64 */
65 static void mr_free_regex (mr_regex_t *r) /* {{{ */
66 {
67 if (r == NULL)
68 return;
70 regfree (&r->re);
71 memset (&r->re, 0, sizeof (r->re));
72 free (r->re_str);
74 if (r->next != NULL)
75 mr_free_regex (r->next);
76 } /* }}} void mr_free_regex */
78 static void mr_free_match (mr_match_t *m) /* {{{ */
79 {
80 if (m == NULL)
81 return;
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);
89 free (m);
90 } /* }}} void mr_free_match */
92 static int mr_match_regexen (mr_regex_t *re_head, /* {{{ */
93 const char *string)
94 {
95 mr_regex_t *re;
97 if (re_head == NULL)
98 return (FC_MATCH_MATCHES);
100 for (re = re_head; re != NULL; re = re->next)
101 {
102 int status;
104 status = regexec (&re->re, string,
105 /* nmatch = */ 0, /* pmatch = */ NULL,
106 /* eflags = */ 0);
107 if (status == 0)
108 return (FC_MATCH_MATCHES);
109 }
111 return (FC_MATCH_NO_MATCH);
112 } /* }}} int mr_match_regexen */
114 static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */
115 oconfig_item_t *ci)
116 {
117 mr_regex_t *re;
118 int status;
120 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
121 {
122 log_warn ("`%s' needs exactly one string argument.", ci->key);
123 return (-1);
124 }
126 re = (mr_regex_t *) malloc (sizeof (*re));
127 if (re == NULL)
128 {
129 log_err ("mr_config_add_regex: malloc failed.");
130 return (-1);
131 }
132 memset (re, 0, sizeof (*re));
133 re->next = NULL;
135 re->re_str = strdup (ci->values[0].value.string);
136 if (re->re_str)
137 {
138 free (re);
139 log_err ("mr_config_add_regex: strdup failed.");
140 return (-1);
141 }
143 status = regcomp (&re->re, re->re_str, REG_EXTENDED | REG_NOSUB);
144 if (status != 0)
145 {
146 char errmsg[1024];
147 regerror (status, &re->re, errmsg, sizeof (errmsg));
148 errmsg[sizeof (errmsg) - 1] = 0;
149 log_err ("Compiling regex `%s' for `%s' failed: %s.",
150 re->re_str, ci->key, errmsg);
151 free (re->re_str);
152 free (re);
153 return (-1);
154 }
156 if (*re_head == NULL)
157 {
158 *re_head = re;
159 }
160 else
161 {
162 mr_regex_t *ptr;
164 ptr = *re_head;
165 while (ptr->next != NULL)
166 ptr = ptr->next;
168 ptr->next = re;
169 }
171 return (0);
172 } /* }}} int mr_config_add_regex */
174 static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
175 {
176 mr_match_t *m;
177 int status;
178 int i;
180 m = (mr_match_t *) malloc (sizeof (*m));
181 if (m == NULL)
182 {
183 log_err ("mr_create: malloc failed.");
184 return (-ENOMEM);
185 }
186 memset (m, 0, sizeof (*m));
188 status = 0;
189 for (i = 0; i < ci->children_num; i++)
190 {
191 oconfig_item_t *child = ci->children + i;
193 if ((strcasecmp ("Host", child->key) == 0)
194 || (strcasecmp ("Hostname", child->key) == 0))
195 status = mr_config_add_regex (&m->host, child);
196 else if (strcasecmp ("Plugin", child->key) == 0)
197 status = mr_config_add_regex (&m->plugin, child);
198 else if (strcasecmp ("PluginInstance", child->key) == 0)
199 status = mr_config_add_regex (&m->plugin_instance, child);
200 else if (strcasecmp ("Type", child->key) == 0)
201 status = mr_config_add_regex (&m->type, child);
202 else if (strcasecmp ("TypeInstance", child->key) == 0)
203 status = mr_config_add_regex (&m->type_instance, child);
204 else
205 {
206 log_err ("The `%s' configuration option is not understood and "
207 "will be ignored.", child->key);
208 status = 0;
209 }
211 if (status != 0)
212 break;
213 }
215 /* Additional sanity-checking */
216 while (status == 0)
217 {
218 if ((m->host == NULL)
219 && (m->plugin == NULL)
220 && (m->plugin_instance == NULL)
221 && (m->type == NULL)
222 && (m->type_instance == NULL))
223 {
224 log_err ("No (valid) regular expressions have been configured. "
225 "This match will be ignored.");
226 status = -1;
227 }
229 break;
230 }
232 if (status != 0)
233 {
234 mr_free_match (m);
235 return (status);
236 }
238 *user_data = m;
239 return (0);
240 } /* }}} int mr_create */
242 static int mr_destroy (void **user_data) /* {{{ */
243 {
244 if ((user_data != NULL) && (*user_data != NULL))
245 mr_free_match (*user_data);
246 return (0);
247 } /* }}} int mr_destroy */
249 static int mr_match (const data_set_t *ds, const value_list_t *vl, /* {{{ */
250 notification_meta_t **meta, void **user_data)
251 {
252 mr_match_t *m;
254 if ((user_data == NULL) || (*user_data == NULL))
255 return (-1);
257 m = *user_data;
259 if (mr_match_regexen (m->host, vl->host) == FC_MATCH_NO_MATCH)
260 return (FC_MATCH_NO_MATCH);
261 if (mr_match_regexen (m->plugin, vl->plugin) == FC_MATCH_NO_MATCH)
262 return (FC_MATCH_NO_MATCH);
263 if (mr_match_regexen (m->plugin_instance,
264 vl->plugin_instance) == FC_MATCH_NO_MATCH)
265 return (FC_MATCH_NO_MATCH);
266 if (mr_match_regexen (m->type, vl->type) == FC_MATCH_NO_MATCH)
267 return (FC_MATCH_NO_MATCH);
268 if (mr_match_regexen (m->type_instance,
269 vl->type_instance) == FC_MATCH_NO_MATCH)
270 return (FC_MATCH_NO_MATCH);
272 return (FC_MATCH_MATCHES);
273 } /* }}} int mr_match */
275 void module_register (void)
276 {
277 match_proc_t mproc;
279 memset (&mproc, 0, sizeof (mproc));
280 mproc.create = mr_create;
281 mproc.destroy = mr_destroy;
282 mproc.match = mr_match;
283 fc_register_match ("regex", mproc);
284 } /* module_register */
286 /* vim: set sw=4 ts=4 tw=78 noexpandtab fdm=marker : */