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;
45 struct tr_meta_data_action_s;
46 typedef struct tr_meta_data_action_s tr_meta_data_action_t;
47 struct tr_meta_data_action_s {
52 tr_meta_data_action_t *next;
58 tr_action_t *plugin_instance;
59 /* tr_action_t *type; */
60 tr_action_t *type_instance;
61 tr_meta_data_action_t *meta;
63 typedef struct tr_data_s tr_data_t;
65 static char *tr_strdup(const char *orig) /* {{{ */
73 sz = strlen(orig) + 1;
78 memcpy(dest, orig, sz);
81 } /* }}} char *tr_strdup */
83 static void tr_action_destroy(tr_action_t *act) /* {{{ */
89 sfree(act->replacement);
91 if (act->next != NULL)
92 tr_action_destroy(act->next);
95 } /* }}} void tr_action_destroy */
97 static void tr_meta_data_action_destroy(tr_meta_data_action_t *act) /* {{{ */
104 sfree(act->replacement);
106 if (act->next != NULL)
107 tr_meta_data_action_destroy(act->next);
110 } /* }}} void tr_meta_data_action_destroy */
112 static int tr_config_add_action(tr_action_t **dest, /* {{{ */
113 const oconfig_item_t *ci, bool may_be_empty) {
120 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
121 (ci->values[1].type != OCONFIG_TYPE_STRING)) {
122 ERROR("Target `replace': The `%s' option requires exactly two string "
128 act = calloc(1, sizeof(*act));
130 ERROR("tr_config_add_action: calloc failed.");
134 act->replacement = NULL;
135 act->may_be_empty = may_be_empty;
137 status = regcomp(&act->re, ci->values[0].value.string, REG_EXTENDED);
139 char errbuf[1024] = "";
141 /* regerror assures null termination. */
142 regerror(status, &act->re, errbuf, sizeof(errbuf));
143 ERROR("Target `replace': Compiling the regular expression `%s' "
145 ci->values[0].value.string, errbuf);
150 act->replacement = tr_strdup(ci->values[1].value.string);
151 if (act->replacement == NULL) {
152 ERROR("tr_config_add_action: tr_strdup failed.");
153 tr_action_destroy(act);
157 /* Insert action at end of list. */
164 while (prev->next != NULL)
171 } /* }}} int tr_config_add_action */
173 static int tr_config_add_meta_action(tr_meta_data_action_t **dest, /* {{{ */
174 const oconfig_item_t *ci,
175 bool should_delete) {
176 tr_meta_data_action_t *act;
183 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
184 (ci->values[1].type != OCONFIG_TYPE_STRING)) {
185 ERROR("Target `replace': The `%s' option requires exactly two string "
191 if ((ci->values_num != 3) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
192 (ci->values[1].type != OCONFIG_TYPE_STRING) ||
193 (ci->values[2].type != OCONFIG_TYPE_STRING)) {
194 ERROR("Target `replace': The `%s' option requires exactly three string "
201 if (strlen(ci->values[0].value.string) == 0) {
202 ERROR("Target `replace': The `%s' option does not accept empty string as "
208 act = calloc(1, sizeof(*act));
210 ERROR("tr_config_add_meta_action: calloc failed.");
215 act->replacement = NULL;
217 status = regcomp(&act->re, ci->values[1].value.string, REG_EXTENDED);
219 char errbuf[1024] = "";
221 /* regerror assures null termination. */
222 regerror(status, &act->re, errbuf, sizeof(errbuf));
223 ERROR("Target `replace': Compiling the regular expression `%s' "
225 ci->values[1].value.string, errbuf);
231 act->key = tr_strdup(ci->values[0].value.string);
232 if (act->key == NULL) {
233 ERROR("tr_config_add_meta_action: tr_strdup failed.");
234 tr_meta_data_action_destroy(act);
238 if (!should_delete) {
239 act->replacement = tr_strdup(ci->values[2].value.string);
240 if (act->replacement == NULL) {
241 ERROR("tr_config_add_meta_action: tr_strdup failed.");
242 tr_meta_data_action_destroy(act);
247 /* Insert action at end of list. */
251 tr_meta_data_action_t *prev;
254 while (prev->next != NULL)
261 } /* }}} int tr_config_add_meta_action */
263 static int tr_action_invoke(tr_action_t *act_head, /* {{{ */
264 char *buffer_in, size_t buffer_in_size,
267 char buffer[DATA_MAX_NAME_LEN];
268 regmatch_t matches[8] = {[0] = {0}};
270 if (act_head == NULL)
273 sstrncpy(buffer, buffer_in, sizeof(buffer));
275 DEBUG("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
277 for (tr_action_t *act = act_head; act != NULL; act = act->next) {
278 char temp[DATA_MAX_NAME_LEN];
281 status = regexec(&act->re, buffer, STATIC_ARRAY_SIZE(matches), matches,
283 if (status == REG_NOMATCH)
285 else if (status != 0) {
286 char errbuf[1024] = "";
288 regerror(status, &act->re, errbuf, sizeof(errbuf));
289 ERROR("Target `replace': Executing a regular expression failed: %s.",
294 subst_status = subst(temp, sizeof(temp), buffer, (size_t)matches[0].rm_so,
295 (size_t)matches[0].rm_eo, act->replacement);
296 if (subst_status == NULL) {
297 ERROR("Target `replace': subst (buffer = %s, start = %" PRIsz
298 ", end = %" PRIsz ", "
299 "replacement = %s) failed.",
300 buffer, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
304 sstrncpy(buffer, temp, sizeof(buffer));
306 DEBUG("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
307 } /* for (act = act_head; act != NULL; act = act->next) */
309 if ((may_be_empty == false) && (buffer[0] == 0)) {
310 WARNING("Target `replace': Replacement resulted in an empty string, "
311 "which is not allowed for this buffer (`host' or `plugin').");
315 DEBUG("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
316 sstrncpy(buffer_in, buffer, buffer_in_size);
319 } /* }}} int tr_action_invoke */
321 static int tr_meta_data_action_invoke(/* {{{ */
322 tr_meta_data_action_t *act_head,
323 meta_data_t **dest) {
325 regmatch_t matches[8] = {[0] = {0}};
327 if (act_head == NULL)
330 if ((*dest) == NULL) /* nothing to do */
333 for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next) {
334 char temp[DATA_MAX_NAME_LEN];
337 int meta_data_status;
341 value_type = meta_data_type(*dest, act->key);
342 if (value_type == 0) /* not found */
344 if (value_type != MD_TYPE_STRING) {
345 WARNING("Target `replace': Attempting replace on metadata key `%s', "
346 "which isn't a string.",
351 meta_data_status = meta_data_get_string(*dest, act->key, &value);
352 if (meta_data_status != 0) {
353 ERROR("Target `replace': Unable to retrieve metadata value for `%s'.",
355 return meta_data_status;
358 DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
362 status = regexec(&act->re, value, STATIC_ARRAY_SIZE(matches), matches,
364 if (status == REG_NOMATCH) {
367 } else if (status != 0) {
368 char errbuf[1024] = "";
370 regerror(status, &act->re, errbuf, sizeof(errbuf));
371 ERROR("Target `replace': Executing a regular expression failed: %s.",
377 if (act->replacement == NULL) {
378 /* no replacement; delete the key */
379 DEBUG("target_replace plugin: tr_meta_data_action_invoke: "
382 meta_data_delete(*dest, act->key);
387 subst_status = subst(temp, sizeof(temp), value, (size_t)matches[0].rm_so,
388 (size_t)matches[0].rm_eo, act->replacement);
389 if (subst_status == NULL) {
390 ERROR("Target `replace': subst (value = %s, start = %" PRIsz
391 ", end = %" PRIsz ", "
392 "replacement = %s) failed.",
393 value, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
399 DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
400 "value `%s' -> `%s'",
401 act->key, value, temp);
403 if ((result = meta_data_create()) == NULL) {
404 ERROR("Target `replace': failed to create metadata for `%s'.", act->key);
409 meta_data_status = meta_data_add_string(result, act->key, temp);
410 if (meta_data_status != 0) {
411 ERROR("Target `replace': Unable to set metadata value for `%s'.",
413 meta_data_destroy(result);
415 return meta_data_status;
418 meta_data_clone_merge(dest, result);
419 meta_data_destroy(result);
421 } /* for (act = act_head; act != NULL; act = act->next) */
424 } /* }}} int tr_meta_data_action_invoke */
426 static int tr_destroy(void **user_data) /* {{{ */
430 if (user_data == NULL)
437 tr_action_destroy(data->host);
438 tr_action_destroy(data->plugin);
439 tr_action_destroy(data->plugin_instance);
440 /* tr_action_destroy (data->type); */
441 tr_action_destroy(data->type_instance);
442 tr_meta_data_action_destroy(data->meta);
446 } /* }}} int tr_destroy */
448 static int tr_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
453 data = calloc(1, sizeof(*data));
455 ERROR("tr_create: calloc failed.");
461 data->plugin_instance = NULL;
462 /* data->type = NULL; */
463 data->type_instance = NULL;
467 for (int i = 0; i < ci->children_num; i++) {
468 oconfig_item_t *child = ci->children + i;
470 if ((strcasecmp("Host", child->key) == 0) ||
471 (strcasecmp("Hostname", child->key) == 0))
472 status = tr_config_add_action(&data->host, child,
473 /* may be empty = */ false);
474 else if (strcasecmp("Plugin", child->key) == 0)
475 status = tr_config_add_action(&data->plugin, child,
476 /* may be empty = */ false);
477 else if (strcasecmp("PluginInstance", child->key) == 0)
478 status = tr_config_add_action(&data->plugin_instance, child,
479 /* may be empty = */ true);
481 else if (strcasecmp ("Type", child->key) == 0)
482 status = tr_config_add_action (&data->type, child,
483 /* may be empty = */ 0);
485 else if (strcasecmp("TypeInstance", child->key) == 0)
486 status = tr_config_add_action(&data->type_instance, child,
487 /* may be empty = */ true);
488 else if (strcasecmp("MetaData", child->key) == 0)
489 status = tr_config_add_meta_action(&data->meta, child,
490 /* should delete = */ false);
491 else if (strcasecmp("DeleteMetaData", child->key) == 0)
492 status = tr_config_add_meta_action(&data->meta, child,
493 /* should delete = */ true);
495 ERROR("Target `replace': The `%s' configuration option is not understood "
496 "and will be ignored.",
505 /* Additional sanity-checking */
506 while (status == 0) {
507 if ((data->host == NULL) && (data->plugin == NULL) &&
508 (data->plugin_instance == NULL)
509 /* && (data->type == NULL) */
510 && (data->type_instance == NULL) && (data->meta == NULL)) {
511 ERROR("Target `replace': You need to set at least one of `Host', "
512 "`Plugin', `PluginInstance' or `TypeInstance'.");
520 tr_destroy((void *)&data);
526 } /* }}} int tr_create */
528 static int tr_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
529 notification_meta_t __attribute__((unused)) * *meta,
533 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
538 ERROR("Target `replace': Invoke: `data' is NULL.");
542 if (data->meta != NULL) {
543 tr_meta_data_action_invoke(data->meta, &(vl->meta));
546 #define HANDLE_FIELD(f, e) \
547 if (data->f != NULL) \
548 tr_action_invoke(data->f, vl->f, sizeof(vl->f), e)
549 HANDLE_FIELD(host, false);
550 HANDLE_FIELD(plugin, false);
551 HANDLE_FIELD(plugin_instance, true);
552 /* HANDLE_FIELD (type, false); */
553 HANDLE_FIELD(type_instance, true);
555 return FC_TARGET_CONTINUE;
556 } /* }}} int tr_invoke */
558 void module_register(void) {
559 target_proc_t tproc = {0};
561 tproc.create = tr_create;
562 tproc.destroy = tr_destroy;
563 tproc.invoke = tr_invoke;
564 fc_register_target("replace", tproc);
565 } /* module_register */