1 /**
2 * collectd - src/target_replace.c
3 * Copyright (C) 2008 Florian Forster
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 * Florian Forster <octo at verplant.org>
20 **/
22 /*
23 * First tell the compiler to stick to the C99 and POSIX standards as close as
24 * possible.
25 */
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
28 #endif
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
32 #endif
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
36 #endif
37 #define _POSIX_C_SOURCE 200112L
39 #if 0
40 /* Single UNIX needed for strdup. */
41 #ifdef _XOPEN_SOURCE
42 # undef _XOPEN_SOURCE
43 #endif
44 #define _XOPEN_SOURCE 500
45 #endif
47 #ifndef _REENTRANT
48 # define _REENTRANT
49 #endif
51 #ifndef _THREAD_SAFE
52 # define _THREAD_SAFE
53 #endif
55 #ifdef _GNU_SOURCE
56 # undef _GNU_SOURCE
57 #endif
58 /* }}} */
60 #include "collectd.h"
61 #include "common.h"
62 #include "filter_chain.h"
63 #include "utils_subst.h"
65 #include <regex.h>
67 struct tr_action_s;
68 typedef struct tr_action_s tr_action_t;
69 struct tr_action_s
70 {
71 regex_t re;
72 char *replacement;
73 int may_be_empty;
75 tr_action_t *next;
76 };
78 struct tr_data_s
79 {
80 tr_action_t *host;
81 tr_action_t *plugin;
82 tr_action_t *plugin_instance;
83 /* tr_action_t *type; */
84 tr_action_t *type_instance;
85 };
86 typedef struct tr_data_s tr_data_t;
88 static char *tr_strdup (const char *orig) /* {{{ */
89 {
90 size_t sz;
91 char *dest;
93 if (orig == NULL)
94 return (NULL);
96 sz = strlen (orig) + 1;
97 dest = (char *) malloc (sz);
98 if (dest == NULL)
99 return (NULL);
101 memcpy (dest, orig, sz);
103 return (dest);
104 } /* }}} char *tr_strdup */
106 static void tr_action_destroy (tr_action_t *act) /* {{{ */
107 {
108 if (act == NULL)
109 return;
111 regfree (&act->re);
112 sfree (act->replacement);
114 if (act->next != NULL)
115 tr_action_destroy (act->next);
117 sfree (act);
118 } /* }}} void tr_action_destroy */
120 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
121 const oconfig_item_t *ci, int may_be_empty)
122 {
123 tr_action_t *act;
124 int status;
126 if (dest == NULL)
127 return (-EINVAL);
129 if ((ci->values_num != 2)
130 || (ci->values[0].type != OCONFIG_TYPE_STRING)
131 || (ci->values[1].type != OCONFIG_TYPE_STRING))
132 {
133 ERROR ("Target `replace': The `%s' option requires exactly two string "
134 "arguments.", ci->key);
135 return (-1);
136 }
138 act = (tr_action_t *) malloc (sizeof (*act));
139 if (act == NULL)
140 {
141 ERROR ("tr_config_add_action: malloc failed.");
142 return (-ENOMEM);
143 }
144 memset (act, 0, sizeof (*act));
146 act->replacement = NULL;
147 act->may_be_empty = may_be_empty;
149 status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
150 if (status != 0)
151 {
152 char errbuf[1024] = "";
154 /* regerror assures null termination. */
155 regerror (status, &act->re, errbuf, sizeof (errbuf));
156 ERROR ("Target `replace': Compiling the regular expression `%s' "
157 "failed: %s.",
158 ci->values[0].value.string, errbuf);
159 sfree (act);
160 return (-EINVAL);
161 }
163 act->replacement = tr_strdup (ci->values[1].value.string);
164 if (act->replacement == NULL)
165 {
166 ERROR ("tr_config_add_action: tr_strdup failed.");
167 regfree (&act->re);
168 sfree (act);
169 return (-ENOMEM);
170 }
172 /* Insert action at end of list. */
173 if (*dest == NULL)
174 *dest = act;
175 else
176 {
177 tr_action_t *prev;
179 prev = *dest;
180 while (prev->next != NULL)
181 prev = prev->next;
183 prev->next = act;
184 }
186 return (0);
187 } /* }}} int tr_config_add_action */
189 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
190 char *buffer_in, size_t buffer_in_size, int may_be_empty)
191 {
192 tr_action_t *act;
193 int status;
194 char buffer[DATA_MAX_NAME_LEN];
195 regmatch_t matches[8];
197 if (act_head == NULL)
198 return (-EINVAL);
200 sstrncpy (buffer, buffer_in, sizeof (buffer));
201 memset (matches, 0, sizeof (matches));
203 DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
205 for (act = act_head; act != NULL; act = act->next)
206 {
207 char temp[DATA_MAX_NAME_LEN];
208 char *subst_status;
210 status = regexec (&act->re, buffer,
211 STATIC_ARRAY_SIZE (matches), matches,
212 /* flags = */ 0);
213 if (status == REG_NOMATCH)
214 continue;
215 else if (status != 0)
216 {
217 char errbuf[1024] = "";
219 regerror (status, &act->re, errbuf, sizeof (errbuf));
220 ERROR ("Target `replace': Executing a regular expression failed: %s.",
221 errbuf);
222 continue;
223 }
225 subst_status = subst (temp, sizeof (temp), buffer,
226 matches[0].rm_so, matches[0].rm_eo, act->replacement);
227 if (subst_status == NULL)
228 {
229 ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
230 "replacement = %s) failed.",
231 buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
232 act->replacement);
233 continue;
234 }
235 sstrncpy (buffer, temp, sizeof (buffer));
237 DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
238 } /* for (act = act_head; act != NULL; act = act->next) */
240 if ((may_be_empty == 0) && (buffer[0] == 0))
241 {
242 WARNING ("Target `replace': Replacement resulted in an empty string, "
243 "which is not allowed for this buffer (`host' or `plugin').");
244 return (0);
245 }
247 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
248 sstrncpy (buffer_in, buffer, buffer_in_size);
250 return (0);
251 } /* }}} int tr_action_invoke */
253 static int tr_destroy (void **user_data) /* {{{ */
254 {
255 tr_data_t *data;
257 if (user_data == NULL)
258 return (-EINVAL);
260 data = *user_data;
261 if (data == NULL)
262 return (0);
264 tr_action_destroy (data->host);
265 tr_action_destroy (data->plugin);
266 tr_action_destroy (data->plugin_instance);
267 /* tr_action_destroy (data->type); */
268 tr_action_destroy (data->type_instance);
269 sfree (data);
271 return (0);
272 } /* }}} int tr_destroy */
274 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
275 {
276 tr_data_t *data;
277 int status;
278 int i;
280 data = (tr_data_t *) malloc (sizeof (*data));
281 if (data == NULL)
282 {
283 ERROR ("tr_create: malloc failed.");
284 return (-ENOMEM);
285 }
286 memset (data, 0, sizeof (*data));
288 data->host = NULL;
289 data->plugin = NULL;
290 data->plugin_instance = NULL;
291 /* data->type = NULL; */
292 data->type_instance = NULL;
294 status = 0;
295 for (i = 0; i < ci->children_num; i++)
296 {
297 oconfig_item_t *child = ci->children + i;
299 if ((strcasecmp ("Host", child->key) == 0)
300 || (strcasecmp ("Hostname", child->key) == 0))
301 status = tr_config_add_action (&data->host, child,
302 /* may be empty = */ 0);
303 else if (strcasecmp ("Plugin", child->key) == 0)
304 status = tr_config_add_action (&data->plugin, child,
305 /* may be empty = */ 0);
306 else if (strcasecmp ("PluginInstance", child->key) == 0)
307 status = tr_config_add_action (&data->plugin_instance, child,
308 /* may be empty = */ 1);
309 #if 0
310 else if (strcasecmp ("Type", child->key) == 0)
311 status = tr_config_add_action (&data->type, child,
312 /* may be empty = */ 0);
313 #endif
314 else if (strcasecmp ("TypeInstance", child->key) == 0)
315 status = tr_config_add_action (&data->type_instance, child,
316 /* may be empty = */ 1);
317 else
318 {
319 ERROR ("Target `replace': The `%s' configuration option is not understood "
320 "and will be ignored.", child->key);
321 status = 0;
322 }
324 if (status != 0)
325 break;
326 }
328 /* Additional sanity-checking */
329 while (status == 0)
330 {
331 if ((data->host == NULL)
332 && (data->plugin == NULL)
333 && (data->plugin_instance == NULL)
334 /* && (data->type == NULL) */
335 && (data->type_instance == NULL))
336 {
337 ERROR ("Target `replace': You need to set at lease one of `Host', "
338 "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
339 status = -1;
340 }
342 break;
343 }
345 if (status != 0)
346 {
347 tr_destroy ((void *) &data);
348 return (status);
349 }
351 *user_data = data;
352 return (0);
353 } /* }}} int tr_create */
355 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
356 notification_meta_t __attribute__((unused)) **meta, void **user_data)
357 {
358 tr_data_t *data;
360 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
361 return (-EINVAL);
363 data = *user_data;
364 if (data == NULL)
365 {
366 ERROR ("Target `replace': Invoke: `data' is NULL.");
367 return (-EINVAL);
368 }
370 #define HANDLE_FIELD(f,e) \
371 if (data->f != NULL) \
372 tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
373 HANDLE_FIELD (host, 0);
374 HANDLE_FIELD (plugin, 0);
375 HANDLE_FIELD (plugin_instance, 1);
376 /* HANDLE_FIELD (type); */
377 HANDLE_FIELD (type_instance, 1);
379 return (FC_TARGET_CONTINUE);
380 } /* }}} int tr_invoke */
382 void module_register (void)
383 {
384 target_proc_t tproc;
386 memset (&tproc, 0, sizeof (tproc));
387 tproc.create = tr_create;
388 tproc.destroy = tr_destroy;
389 tproc.invoke = tr_invoke;
390 fc_register_target ("replace", tproc);
391 } /* module_register */
393 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */