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>
24 #include "filter_chain.h"
25 #include "utils_subst.h"
30 typedef struct tr_action_s tr_action_t;
44 tr_action_t *plugin_instance;
45 /* tr_action_t *type; */
46 tr_action_t *type_instance;
48 typedef struct tr_data_s tr_data_t;
50 static char *tr_strdup (const char *orig) /* {{{ */
58 sz = strlen (orig) + 1;
59 dest = (char *) malloc (sz);
63 memcpy (dest, orig, sz);
66 } /* }}} char *tr_strdup */
68 static void tr_action_destroy (tr_action_t *act) /* {{{ */
74 sfree (act->replacement);
76 if (act->next != NULL)
77 tr_action_destroy (act->next);
80 } /* }}} void tr_action_destroy */
82 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
83 const oconfig_item_t *ci, int may_be_empty)
91 if ((ci->values_num != 2)
92 || (ci->values[0].type != OCONFIG_TYPE_STRING)
93 || (ci->values[1].type != OCONFIG_TYPE_STRING))
95 ERROR ("Target `replace': The `%s' option requires exactly two string "
96 "arguments.", ci->key);
100 act = (tr_action_t *) malloc (sizeof (*act));
103 ERROR ("tr_config_add_action: malloc failed.");
106 memset (act, 0, sizeof (*act));
108 act->replacement = NULL;
109 act->may_be_empty = may_be_empty;
111 status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
114 char errbuf[1024] = "";
116 /* regerror assures null termination. */
117 regerror (status, &act->re, errbuf, sizeof (errbuf));
118 ERROR ("Target `replace': Compiling the regular expression `%s' "
120 ci->values[0].value.string, errbuf);
125 act->replacement = tr_strdup (ci->values[1].value.string);
126 if (act->replacement == NULL)
128 ERROR ("tr_config_add_action: tr_strdup failed.");
134 /* Insert action at end of list. */
142 while (prev->next != NULL)
149 } /* }}} int tr_config_add_action */
151 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
152 char *buffer_in, size_t buffer_in_size, int may_be_empty)
156 char buffer[DATA_MAX_NAME_LEN];
157 regmatch_t matches[8];
159 if (act_head == NULL)
162 sstrncpy (buffer, buffer_in, sizeof (buffer));
163 memset (matches, 0, sizeof (matches));
165 DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
167 for (act = act_head; act != NULL; act = act->next)
169 char temp[DATA_MAX_NAME_LEN];
172 status = regexec (&act->re, buffer,
173 STATIC_ARRAY_SIZE (matches), matches,
175 if (status == REG_NOMATCH)
177 else if (status != 0)
179 char errbuf[1024] = "";
181 regerror (status, &act->re, errbuf, sizeof (errbuf));
182 ERROR ("Target `replace': Executing a regular expression failed: %s.",
187 subst_status = subst (temp, sizeof (temp), buffer,
188 matches[0].rm_so, matches[0].rm_eo, act->replacement);
189 if (subst_status == NULL)
191 ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
192 "replacement = %s) failed.",
193 buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
197 sstrncpy (buffer, temp, sizeof (buffer));
199 DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
200 } /* for (act = act_head; act != NULL; act = act->next) */
202 if ((may_be_empty == 0) && (buffer[0] == 0))
204 WARNING ("Target `replace': Replacement resulted in an empty string, "
205 "which is not allowed for this buffer (`host' or `plugin').");
209 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
210 sstrncpy (buffer_in, buffer, buffer_in_size);
213 } /* }}} int tr_action_invoke */
215 static int tr_destroy (void **user_data) /* {{{ */
219 if (user_data == NULL)
226 tr_action_destroy (data->host);
227 tr_action_destroy (data->plugin);
228 tr_action_destroy (data->plugin_instance);
229 /* tr_action_destroy (data->type); */
230 tr_action_destroy (data->type_instance);
234 } /* }}} int tr_destroy */
236 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
242 data = (tr_data_t *) malloc (sizeof (*data));
245 ERROR ("tr_create: malloc failed.");
248 memset (data, 0, sizeof (*data));
252 data->plugin_instance = NULL;
253 /* data->type = NULL; */
254 data->type_instance = NULL;
257 for (i = 0; i < ci->children_num; i++)
259 oconfig_item_t *child = ci->children + i;
261 if ((strcasecmp ("Host", child->key) == 0)
262 || (strcasecmp ("Hostname", child->key) == 0))
263 status = tr_config_add_action (&data->host, child,
264 /* may be empty = */ 0);
265 else if (strcasecmp ("Plugin", child->key) == 0)
266 status = tr_config_add_action (&data->plugin, child,
267 /* may be empty = */ 0);
268 else if (strcasecmp ("PluginInstance", child->key) == 0)
269 status = tr_config_add_action (&data->plugin_instance, child,
270 /* may be empty = */ 1);
272 else if (strcasecmp ("Type", child->key) == 0)
273 status = tr_config_add_action (&data->type, child,
274 /* may be empty = */ 0);
276 else if (strcasecmp ("TypeInstance", child->key) == 0)
277 status = tr_config_add_action (&data->type_instance, child,
278 /* may be empty = */ 1);
281 ERROR ("Target `replace': The `%s' configuration option is not understood "
282 "and will be ignored.", child->key);
290 /* Additional sanity-checking */
293 if ((data->host == NULL)
294 && (data->plugin == NULL)
295 && (data->plugin_instance == NULL)
296 /* && (data->type == NULL) */
297 && (data->type_instance == NULL))
299 ERROR ("Target `replace': You need to set at lease one of `Host', "
300 "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
309 tr_destroy ((void *) &data);
315 } /* }}} int tr_create */
317 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
318 notification_meta_t __attribute__((unused)) **meta, void **user_data)
322 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
328 ERROR ("Target `replace': Invoke: `data' is NULL.");
332 #define HANDLE_FIELD(f,e) \
333 if (data->f != NULL) \
334 tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
335 HANDLE_FIELD (host, 0);
336 HANDLE_FIELD (plugin, 0);
337 HANDLE_FIELD (plugin_instance, 1);
338 /* HANDLE_FIELD (type); */
339 HANDLE_FIELD (type_instance, 1);
341 return (FC_TARGET_CONTINUE);
342 } /* }}} int tr_invoke */
344 void module_register (void)
348 memset (&tproc, 0, sizeof (tproc));
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 : */