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,
265 _Bool may_be_empty) {
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 = %zu, end = %zu, "
298 "replacement = %s) failed.",
299 buffer, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
303 sstrncpy(buffer, temp, sizeof(buffer));
305 DEBUG("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
306 } /* for (act = act_head; act != NULL; act = act->next) */
308 if ((may_be_empty == 0) && (buffer[0] == 0)) {
309 WARNING("Target `replace': Replacement resulted in an empty string, "
310 "which is not allowed for this buffer (`host' or `plugin').");
314 DEBUG("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
315 sstrncpy(buffer_in, buffer, buffer_in_size);
318 } /* }}} int tr_action_invoke */
320 static int tr_meta_data_action_invoke(/* {{{ */
321 tr_meta_data_action_t *act_head,
322 meta_data_t **dest) {
324 regmatch_t matches[8] = {[0] = {0}};
326 if (act_head == NULL)
329 if ((*dest) == NULL) /* nothing to do */
332 for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next) {
333 char temp[DATA_MAX_NAME_LEN];
336 int meta_data_status;
340 value_type = meta_data_type(*dest, act->key);
341 if (value_type == 0) /* not found */
343 if (value_type != MD_TYPE_STRING) {
344 WARNING("Target `replace': Attempting replace on metadata key `%s', "
345 "which isn't a string.",
350 meta_data_status = meta_data_get_string(*dest, act->key, &value);
351 if (meta_data_status != 0) {
352 ERROR("Target `replace': Unable to retrieve metadata value for `%s'.",
354 return (meta_data_status);
357 DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
361 status = regexec(&act->re, value, STATIC_ARRAY_SIZE(matches), matches,
363 if (status == REG_NOMATCH) {
366 } else if (status != 0) {
367 char errbuf[1024] = "";
369 regerror(status, &act->re, errbuf, sizeof(errbuf));
370 ERROR("Target `replace': Executing a regular expression failed: %s.",
376 if (act->replacement == NULL) {
377 /* no replacement; delete the key */
378 DEBUG("target_replace plugin: tr_meta_data_action_invoke: "
381 meta_data_delete(*dest, act->key);
386 subst_status = subst(temp, sizeof(temp), value, (size_t)matches[0].rm_so,
387 (size_t)matches[0].rm_eo, act->replacement);
388 if (subst_status == NULL) {
389 ERROR("Target `replace': subst (value = %s, start = %zu, end = %zu, "
390 "replacement = %s) failed.",
391 value, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
397 DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
398 "value `%s' -> `%s'",
399 act->key, value, temp);
401 if ((result = meta_data_create()) == NULL) {
402 ERROR("Target `replace': failed to create metadata for `%s'.", act->key);
407 meta_data_status = meta_data_add_string(result, act->key, temp);
408 if (meta_data_status != 0) {
409 ERROR("Target `replace': Unable to set metadata value for `%s'.",
411 meta_data_destroy(result);
413 return (meta_data_status);
416 meta_data_clone_merge(dest, result);
417 meta_data_destroy(result);
419 } /* for (act = act_head; act != NULL; act = act->next) */
422 } /* }}} int tr_meta_data_action_invoke */
424 static int tr_destroy(void **user_data) /* {{{ */
428 if (user_data == NULL)
435 tr_action_destroy(data->host);
436 tr_action_destroy(data->plugin);
437 tr_action_destroy(data->plugin_instance);
438 /* tr_action_destroy (data->type); */
439 tr_action_destroy(data->type_instance);
440 tr_meta_data_action_destroy(data->meta);
444 } /* }}} int tr_destroy */
446 static int tr_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
451 data = calloc(1, sizeof(*data));
453 ERROR("tr_create: calloc failed.");
459 data->plugin_instance = NULL;
460 /* data->type = NULL; */
461 data->type_instance = NULL;
465 for (int i = 0; i < ci->children_num; i++) {
466 oconfig_item_t *child = ci->children + i;
468 if ((strcasecmp("Host", child->key) == 0) ||
469 (strcasecmp("Hostname", child->key) == 0))
470 status = tr_config_add_action(&data->host, child,
471 /* may be empty = */ 0);
472 else if (strcasecmp("Plugin", child->key) == 0)
473 status = tr_config_add_action(&data->plugin, child,
474 /* may be empty = */ 0);
475 else if (strcasecmp("PluginInstance", child->key) == 0)
476 status = tr_config_add_action(&data->plugin_instance, child,
477 /* may be empty = */ 1);
479 else if (strcasecmp ("Type", child->key) == 0)
480 status = tr_config_add_action (&data->type, child,
481 /* may be empty = */ 0);
483 else if (strcasecmp("TypeInstance", child->key) == 0)
484 status = tr_config_add_action(&data->type_instance, child,
485 /* may be empty = */ 1);
486 else if (strcasecmp("MetaData", child->key) == 0)
487 status = tr_config_add_meta_action(&data->meta, child,
488 /* should delete = */ 0);
489 else if (strcasecmp("DeleteMetaData", child->key) == 0)
490 status = tr_config_add_meta_action(&data->meta, child,
491 /* should delete = */ 1);
493 ERROR("Target `replace': The `%s' configuration option is not understood "
494 "and will be ignored.",
503 /* Additional sanity-checking */
504 while (status == 0) {
505 if ((data->host == NULL) && (data->plugin == NULL) &&
506 (data->plugin_instance == NULL)
507 /* && (data->type == NULL) */
508 && (data->type_instance == NULL) && (data->meta == NULL)) {
509 ERROR("Target `replace': You need to set at least one of `Host', "
510 "`Plugin', `PluginInstance' or `TypeInstance'.");
518 tr_destroy((void *)&data);
524 } /* }}} int tr_create */
526 static int tr_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
527 notification_meta_t __attribute__((unused)) * *meta,
531 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
536 ERROR("Target `replace': Invoke: `data' is NULL.");
540 if (data->meta != NULL) {
541 tr_meta_data_action_invoke(data->meta, &(vl->meta));
544 #define HANDLE_FIELD(f, e) \
545 if (data->f != NULL) \
546 tr_action_invoke(data->f, vl->f, sizeof(vl->f), e)
547 HANDLE_FIELD(host, 0);
548 HANDLE_FIELD(plugin, 0);
549 HANDLE_FIELD(plugin_instance, 1);
550 /* HANDLE_FIELD (type, 0); */
551 HANDLE_FIELD(type_instance, 1);
553 return (FC_TARGET_CONTINUE);
554 } /* }}} int tr_invoke */
556 void module_register(void) {
557 target_proc_t tproc = {0};
559 tproc.create = tr_create;
560 tproc.destroy = tr_destroy;
561 tproc.invoke = tr_invoke;
562 fc_register_target("replace", tproc);
563 } /* module_register */