2 * collectd - src/target_replace.c
3 * Copyright (C) 2008 Florian Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <octo at collectd.org>
30 #include "filter_chain.h"
31 #include "utils_subst.h"
36 typedef struct tr_action_s tr_action_t;
50 tr_action_t *plugin_instance;
51 /* tr_action_t *type; */
52 tr_action_t *type_instance;
54 typedef struct tr_data_s tr_data_t;
56 static char *tr_strdup (const char *orig) /* {{{ */
64 sz = strlen (orig) + 1;
69 memcpy (dest, orig, sz);
72 } /* }}} char *tr_strdup */
74 static void tr_action_destroy (tr_action_t *act) /* {{{ */
80 sfree (act->replacement);
82 if (act->next != NULL)
83 tr_action_destroy (act->next);
86 } /* }}} void tr_action_destroy */
88 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
89 const oconfig_item_t *ci, int may_be_empty)
97 if ((ci->values_num != 2)
98 || (ci->values[0].type != OCONFIG_TYPE_STRING)
99 || (ci->values[1].type != OCONFIG_TYPE_STRING))
101 ERROR ("Target `replace': The `%s' option requires exactly two string "
102 "arguments.", ci->key);
106 act = calloc (1, sizeof (*act));
109 ERROR ("tr_config_add_action: calloc failed.");
113 act->replacement = NULL;
114 act->may_be_empty = may_be_empty;
116 status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
119 char errbuf[1024] = "";
121 /* regerror assures null termination. */
122 regerror (status, &act->re, errbuf, sizeof (errbuf));
123 ERROR ("Target `replace': Compiling the regular expression `%s' "
125 ci->values[0].value.string, errbuf);
130 act->replacement = tr_strdup (ci->values[1].value.string);
131 if (act->replacement == NULL)
133 ERROR ("tr_config_add_action: tr_strdup failed.");
139 /* Insert action at end of list. */
147 while (prev->next != NULL)
154 } /* }}} int tr_config_add_action */
156 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
157 char *buffer_in, size_t buffer_in_size, int may_be_empty)
160 char buffer[DATA_MAX_NAME_LEN];
161 regmatch_t matches[8] = { [0] = { 0 } };
163 if (act_head == NULL)
166 sstrncpy (buffer, buffer_in, sizeof (buffer));
168 DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
170 for (tr_action_t *act = act_head; act != NULL; act = act->next)
172 char temp[DATA_MAX_NAME_LEN];
175 status = regexec (&act->re, buffer,
176 STATIC_ARRAY_SIZE (matches), matches,
178 if (status == REG_NOMATCH)
180 else if (status != 0)
182 char errbuf[1024] = "";
184 regerror (status, &act->re, errbuf, sizeof (errbuf));
185 ERROR ("Target `replace': Executing a regular expression failed: %s.",
190 subst_status = subst (temp, sizeof (temp), buffer,
191 (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, act->replacement);
192 if (subst_status == NULL)
194 ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
195 "replacement = %s) failed.",
196 buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
200 sstrncpy (buffer, temp, sizeof (buffer));
202 DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
203 } /* for (act = act_head; act != NULL; act = act->next) */
205 if ((may_be_empty == 0) && (buffer[0] == 0))
207 WARNING ("Target `replace': Replacement resulted in an empty string, "
208 "which is not allowed for this buffer (`host' or `plugin').");
212 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
213 sstrncpy (buffer_in, buffer, buffer_in_size);
216 } /* }}} int tr_action_invoke */
218 static int tr_destroy (void **user_data) /* {{{ */
222 if (user_data == NULL)
229 tr_action_destroy (data->host);
230 tr_action_destroy (data->plugin);
231 tr_action_destroy (data->plugin_instance);
232 /* tr_action_destroy (data->type); */
233 tr_action_destroy (data->type_instance);
237 } /* }}} int tr_destroy */
239 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
244 data = calloc (1, sizeof (*data));
247 ERROR ("tr_create: calloc failed.");
253 data->plugin_instance = NULL;
254 /* data->type = NULL; */
255 data->type_instance = NULL;
258 for (int i = 0; i < ci->children_num; i++)
260 oconfig_item_t *child = ci->children + i;
262 if ((strcasecmp ("Host", child->key) == 0)
263 || (strcasecmp ("Hostname", child->key) == 0))
264 status = tr_config_add_action (&data->host, child,
265 /* may be empty = */ 0);
266 else if (strcasecmp ("Plugin", child->key) == 0)
267 status = tr_config_add_action (&data->plugin, child,
268 /* may be empty = */ 0);
269 else if (strcasecmp ("PluginInstance", child->key) == 0)
270 status = tr_config_add_action (&data->plugin_instance, child,
271 /* may be empty = */ 1);
273 else if (strcasecmp ("Type", child->key) == 0)
274 status = tr_config_add_action (&data->type, child,
275 /* may be empty = */ 0);
277 else if (strcasecmp ("TypeInstance", child->key) == 0)
278 status = tr_config_add_action (&data->type_instance, child,
279 /* may be empty = */ 1);
282 ERROR ("Target `replace': The `%s' configuration option is not understood "
283 "and will be ignored.", child->key);
291 /* Additional sanity-checking */
294 if ((data->host == NULL)
295 && (data->plugin == NULL)
296 && (data->plugin_instance == NULL)
297 /* && (data->type == NULL) */
298 && (data->type_instance == NULL))
300 ERROR ("Target `replace': You need to set at least one of `Host', "
301 "`Plugin', `PluginInstance' or `TypeInstance'.");
310 tr_destroy ((void *) &data);
316 } /* }}} int tr_create */
318 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
319 notification_meta_t __attribute__((unused)) **meta, void **user_data)
323 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
329 ERROR ("Target `replace': Invoke: `data' is NULL.");
333 #define HANDLE_FIELD(f,e) \
334 if (data->f != NULL) \
335 tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
336 HANDLE_FIELD (host, 0);
337 HANDLE_FIELD (plugin, 0);
338 HANDLE_FIELD (plugin_instance, 1);
339 /* HANDLE_FIELD (type); */
340 HANDLE_FIELD (type_instance, 1);
342 return (FC_TARGET_CONTINUE);
343 } /* }}} int tr_invoke */
345 void module_register (void)
347 target_proc_t tproc = { 0 };
349 tproc.create = tr_create;
350 tproc.destroy = tr_destroy;
351 tproc.invoke = tr_invoke;
352 fc_register_target ("replace", tproc);
353 } /* module_register */
355 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */