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
42 #define _XOPEN_SOURCE 600
59 #include "filter_chain.h"
60 #include "utils_subst.h"
65 typedef struct tr_action_s tr_action_t;
79 tr_action_t *plugin_instance;
80 /* tr_action_t *type; */
81 tr_action_t *type_instance;
83 typedef struct tr_data_s tr_data_t;
85 static char *tr_strdup (const char *orig) /* {{{ */
93 sz = strlen (orig) + 1;
94 dest = (char *) malloc (sz);
98 memcpy (dest, orig, sz);
101 } /* }}} char *tr_strdup */
103 static void tr_action_destroy (tr_action_t *act) /* {{{ */
109 sfree (act->replacement);
111 if (act->next != NULL)
112 tr_action_destroy (act->next);
115 } /* }}} void tr_action_destroy */
117 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
118 const oconfig_item_t *ci, int may_be_empty)
126 if ((ci->values_num != 2)
127 || (ci->values[0].type != OCONFIG_TYPE_STRING)
128 || (ci->values[1].type != OCONFIG_TYPE_STRING))
130 ERROR ("Target `replace': The `%s' option requires exactly two string "
131 "arguments.", ci->key);
135 act = (tr_action_t *) malloc (sizeof (*act));
138 ERROR ("tr_config_add_action: malloc failed.");
141 memset (act, 0, sizeof (*act));
143 act->replacement = NULL;
144 act->may_be_empty = may_be_empty;
146 status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
149 char errbuf[1024] = "";
151 /* regerror assures null termination. */
152 regerror (status, &act->re, errbuf, sizeof (errbuf));
153 ERROR ("Target `replace': Compiling the regular expression `%s' "
155 ci->values[0].value.string, errbuf);
160 act->replacement = tr_strdup (ci->values[1].value.string);
161 if (act->replacement == NULL)
163 ERROR ("tr_config_add_action: tr_strdup failed.");
169 /* Insert action at end of list. */
177 while (prev->next != NULL)
184 } /* }}} int tr_config_add_action */
186 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
187 char *buffer_in, size_t buffer_in_size, int may_be_empty)
191 char buffer[DATA_MAX_NAME_LEN];
192 regmatch_t matches[8];
194 if (act_head == NULL)
197 sstrncpy (buffer, buffer_in, sizeof (buffer));
198 memset (matches, 0, sizeof (matches));
200 DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
202 for (act = act_head; act != NULL; act = act->next)
204 char temp[DATA_MAX_NAME_LEN];
207 status = regexec (&act->re, buffer,
208 STATIC_ARRAY_SIZE (matches), matches,
210 if (status == REG_NOMATCH)
212 else if (status != 0)
214 char errbuf[1024] = "";
216 regerror (status, &act->re, errbuf, sizeof (errbuf));
217 ERROR ("Target `replace': Executing a regular expression failed: %s.",
222 subst_status = subst (temp, sizeof (temp), buffer,
223 matches[0].rm_so, matches[0].rm_eo, act->replacement);
224 if (subst_status == NULL)
226 ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
227 "replacement = %s) failed.",
228 buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
232 sstrncpy (buffer, temp, sizeof (buffer));
234 DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
235 } /* for (act = act_head; act != NULL; act = act->next) */
237 if ((may_be_empty == 0) && (buffer[0] == 0))
239 WARNING ("Target `replace': Replacement resulted in an empty string, "
240 "which is not allowed for this buffer (`host' or `plugin').");
244 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
245 sstrncpy (buffer_in, buffer, buffer_in_size);
248 } /* }}} int tr_action_invoke */
250 static int tr_destroy (void **user_data) /* {{{ */
254 if (user_data == NULL)
261 tr_action_destroy (data->host);
262 tr_action_destroy (data->plugin);
263 tr_action_destroy (data->plugin_instance);
264 /* tr_action_destroy (data->type); */
265 tr_action_destroy (data->type_instance);
269 } /* }}} int tr_destroy */
271 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
277 data = (tr_data_t *) malloc (sizeof (*data));
280 ERROR ("tr_create: malloc failed.");
283 memset (data, 0, sizeof (*data));
287 data->plugin_instance = NULL;
288 /* data->type = NULL; */
289 data->type_instance = NULL;
292 for (i = 0; i < ci->children_num; i++)
294 oconfig_item_t *child = ci->children + i;
296 if ((strcasecmp ("Host", child->key) == 0)
297 || (strcasecmp ("Hostname", child->key) == 0))
298 status = tr_config_add_action (&data->host, child,
299 /* may be empty = */ 0);
300 else if (strcasecmp ("Plugin", child->key) == 0)
301 status = tr_config_add_action (&data->plugin, child,
302 /* may be empty = */ 0);
303 else if (strcasecmp ("PluginInstance", child->key) == 0)
304 status = tr_config_add_action (&data->plugin_instance, child,
305 /* may be empty = */ 1);
307 else if (strcasecmp ("Type", child->key) == 0)
308 status = tr_config_add_action (&data->type, child,
309 /* may be empty = */ 0);
311 else if (strcasecmp ("TypeInstance", child->key) == 0)
312 status = tr_config_add_action (&data->type_instance, child,
313 /* may be empty = */ 1);
316 ERROR ("Target `replace': The `%s' configuration option is not understood "
317 "and will be ignored.", child->key);
325 /* Additional sanity-checking */
328 if ((data->host == NULL)
329 && (data->plugin == NULL)
330 && (data->plugin_instance == NULL)
331 /* && (data->type == NULL) */
332 && (data->type_instance == NULL))
334 ERROR ("Target `replace': You need to set at lease one of `Host', "
335 "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
344 tr_destroy ((void *) &data);
350 } /* }}} int tr_create */
352 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
353 notification_meta_t __attribute__((unused)) **meta, void **user_data)
357 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
363 ERROR ("Target `replace': Invoke: `data' is NULL.");
367 #define HANDLE_FIELD(f,e) \
368 if (data->f != NULL) \
369 tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
370 HANDLE_FIELD (host, 0);
371 HANDLE_FIELD (plugin, 0);
372 HANDLE_FIELD (plugin_instance, 1);
373 /* HANDLE_FIELD (type); */
374 HANDLE_FIELD (type_instance, 1);
376 return (FC_TARGET_CONTINUE);
377 } /* }}} int tr_invoke */
379 void module_register (void)
383 memset (&tproc, 0, sizeof (tproc));
384 tproc.create = tr_create;
385 tproc.destroy = tr_destroy;
386 tproc.invoke = tr_invoke;
387 fc_register_target ("replace", tproc);
388 } /* module_register */
390 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */