2 * collectd - src/target_replace.c
3 * Copyright (C) 2008 Florian Forster
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 * Florian Forster <octo at verplant.org>
23 * First tell the compiler to stick to the C99 and POSIX standards as close as
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
37 #define _POSIX_C_SOURCE 200112L
40 /* Single UNIX needed for strdup. */
44 #define _XOPEN_SOURCE 500
62 #include "filter_chain.h"
63 #include "utils_subst.h"
68 typedef struct tr_action_s tr_action_t;
82 tr_action_t *plugin_instance;
83 /* tr_action_t *type; */
84 tr_action_t *type_instance;
86 typedef struct tr_data_s tr_data_t;
88 static char *tr_strdup (const char *orig) /* {{{ */
96 sz = strlen (orig) + 1;
97 dest = (char *) malloc (sz);
101 memcpy (dest, orig, sz);
104 } /* }}} char *tr_strdup */
106 static void tr_action_destroy (tr_action_t *act) /* {{{ */
112 sfree (act->replacement);
114 if (act->next != NULL)
115 tr_action_destroy (act->next);
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)
129 if ((ci->values_num != 2)
130 || (ci->values[0].type != OCONFIG_TYPE_STRING)
131 || (ci->values[1].type != OCONFIG_TYPE_STRING))
133 ERROR ("Target `replace': The `%s' option requires exactly two string "
134 "arguments.", ci->key);
138 act = (tr_action_t *) malloc (sizeof (*act));
141 ERROR ("tr_config_add_action: malloc failed.");
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);
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' "
158 ci->values[0].value.string, errbuf);
163 act->replacement = tr_strdup (ci->values[1].value.string);
164 if (act->replacement == NULL)
166 ERROR ("tr_config_add_action: tr_strdup failed.");
172 /* Insert action at end of list. */
180 while (prev->next != NULL)
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)
194 char buffer[DATA_MAX_NAME_LEN];
195 regmatch_t matches[8];
197 if (act_head == NULL)
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)
207 char temp[DATA_MAX_NAME_LEN];
210 status = regexec (&act->re, buffer,
211 STATIC_ARRAY_SIZE (matches), matches,
213 if (status == REG_NOMATCH)
215 else if (status != 0)
217 char errbuf[1024] = "";
219 regerror (status, &act->re, errbuf, sizeof (errbuf));
220 ERROR ("Target `replace': Executing a regular expression failed: %s.",
225 subst_status = subst (temp, sizeof (temp), buffer,
226 matches[0].rm_so, matches[0].rm_eo, act->replacement);
227 if (subst_status == NULL)
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,
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))
242 WARNING ("Target `replace': Replacement resulted in an empty string, "
243 "which is not allowed for this buffer (`host' or `plugin').");
247 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
248 sstrncpy (buffer_in, buffer, buffer_in_size);
251 } /* }}} int tr_action_invoke */
253 static int tr_destroy (void **user_data) /* {{{ */
257 if (user_data == NULL)
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);
272 } /* }}} int tr_destroy */
274 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
280 data = (tr_data_t *) malloc (sizeof (*data));
283 ERROR ("tr_create: malloc failed.");
286 memset (data, 0, sizeof (*data));
290 data->plugin_instance = NULL;
291 /* data->type = NULL; */
292 data->type_instance = NULL;
295 for (i = 0; i < ci->children_num; i++)
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);
310 else if (strcasecmp ("Type", child->key) == 0)
311 status = tr_config_add_action (&data->type, child,
312 /* may be empty = */ 0);
314 else if (strcasecmp ("TypeInstance", child->key) == 0)
315 status = tr_config_add_action (&data->type_instance, child,
316 /* may be empty = */ 1);
319 ERROR ("Target `replace': The `%s' configuration option is not understood "
320 "and will be ignored.", child->key);
328 /* Additional sanity-checking */
331 if ((data->host == NULL)
332 && (data->plugin == NULL)
333 && (data->plugin_instance == NULL)
334 /* && (data->type == NULL) */
335 && (data->type_instance == NULL))
337 ERROR ("Target `replace': You need to set at lease one of `Host', "
338 "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
347 tr_destroy ((void *) &data);
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)
360 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
366 ERROR ("Target `replace': Invoke: `data' is NULL.");
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)
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 : */