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;
46 struct tr_meta_data_action_s;
47 typedef struct tr_meta_data_action_s tr_meta_data_action_t;
48 struct tr_meta_data_action_s
54 tr_meta_data_action_t *next;
61 tr_action_t *plugin_instance;
62 /* tr_action_t *type; */
63 tr_action_t *type_instance;
64 tr_meta_data_action_t *meta;
66 typedef struct tr_data_s tr_data_t;
68 static char *tr_strdup (const char *orig) /* {{{ */
76 sz = strlen (orig) + 1;
81 memcpy (dest, orig, sz);
84 } /* }}} char *tr_strdup */
86 static void tr_action_destroy (tr_action_t *act) /* {{{ */
92 sfree (act->replacement);
94 if (act->next != NULL)
95 tr_action_destroy (act->next);
98 } /* }}} void tr_action_destroy */
100 static void tr_meta_data_action_destroy (tr_meta_data_action_t *act) /* {{{ */
107 sfree (act->replacement);
109 if (act->next != NULL)
110 tr_meta_data_action_destroy (act->next);
113 } /* }}} void tr_meta_data_action_destroy */
115 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
116 const oconfig_item_t *ci, _Bool may_be_empty)
124 if ((ci->values_num != 2)
125 || (ci->values[0].type != OCONFIG_TYPE_STRING)
126 || (ci->values[1].type != OCONFIG_TYPE_STRING))
128 ERROR ("Target `replace': The `%s' option requires exactly two string "
129 "arguments.", ci->key);
133 act = calloc (1, sizeof (*act));
136 ERROR ("tr_config_add_action: calloc failed.");
140 act->replacement = NULL;
141 act->may_be_empty = may_be_empty;
143 status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
146 char errbuf[1024] = "";
148 /* regerror assures null termination. */
149 regerror (status, &act->re, errbuf, sizeof (errbuf));
150 ERROR ("Target `replace': Compiling the regular expression `%s' "
152 ci->values[0].value.string, errbuf);
157 act->replacement = tr_strdup (ci->values[1].value.string);
158 if (act->replacement == NULL)
160 ERROR ("tr_config_add_action: tr_strdup failed.");
161 tr_action_destroy (act);
165 /* Insert action at end of list. */
173 while (prev->next != NULL)
180 } /* }}} int tr_config_add_action */
182 static int tr_config_add_meta_action (tr_meta_data_action_t **dest, /* {{{ */
183 const oconfig_item_t *ci, _Bool should_delete)
185 tr_meta_data_action_t *act;
193 if ((ci->values_num != 2)
194 || (ci->values[0].type != OCONFIG_TYPE_STRING)
195 || (ci->values[1].type != OCONFIG_TYPE_STRING))
197 ERROR ("Target `replace': The `%s' option requires exactly two string "
198 "arguments.", ci->key);
204 if ((ci->values_num != 3)
205 || (ci->values[0].type != OCONFIG_TYPE_STRING)
206 || (ci->values[1].type != OCONFIG_TYPE_STRING)
207 || (ci->values[2].type != OCONFIG_TYPE_STRING))
209 ERROR ("Target `replace': The `%s' option requires exactly three string "
210 "arguments.", ci->key);
215 if (strlen (ci->values[0].value.string) == 0)
217 ERROR ("Target `replace': The `%s' option does not accept empty string as "
218 "first argument.", ci->key);
222 act = calloc (1, sizeof (*act));
225 ERROR ("tr_config_add_meta_action: calloc failed.");
230 act->replacement = NULL;
232 status = regcomp (&act->re, ci->values[1].value.string, REG_EXTENDED);
235 char errbuf[1024] = "";
237 /* regerror assures null termination. */
238 regerror (status, &act->re, errbuf, sizeof (errbuf));
239 ERROR ("Target `replace': Compiling the regular expression `%s' "
241 ci->values[1].value.string, errbuf);
247 act->key = tr_strdup (ci->values[0].value.string);
248 if (act->key == NULL)
250 ERROR ("tr_config_add_meta_action: tr_strdup failed.");
251 tr_meta_data_action_destroy (act);
255 if (!should_delete) {
256 act->replacement = tr_strdup (ci->values[2].value.string);
257 if (act->replacement == NULL)
259 ERROR ("tr_config_add_meta_action: tr_strdup failed.");
260 tr_meta_data_action_destroy (act);
265 /* Insert action at end of list. */
270 tr_meta_data_action_t *prev;
273 while (prev->next != NULL)
280 } /* }}} int tr_config_add_meta_action */
282 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
283 char *buffer_in, size_t buffer_in_size, _Bool may_be_empty)
286 char buffer[DATA_MAX_NAME_LEN];
287 regmatch_t matches[8] = { [0] = { 0 } };
289 if (act_head == NULL)
292 sstrncpy (buffer, buffer_in, sizeof (buffer));
294 DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
296 for (tr_action_t *act = act_head; act != NULL; act = act->next)
298 char temp[DATA_MAX_NAME_LEN];
301 status = regexec (&act->re, buffer,
302 STATIC_ARRAY_SIZE (matches), matches,
304 if (status == REG_NOMATCH)
306 else if (status != 0)
308 char errbuf[1024] = "";
310 regerror (status, &act->re, errbuf, sizeof (errbuf));
311 ERROR ("Target `replace': Executing a regular expression failed: %s.",
316 subst_status = subst (temp, sizeof (temp), buffer,
317 (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, act->replacement);
318 if (subst_status == NULL)
320 ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
321 "replacement = %s) failed.",
322 buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
326 sstrncpy (buffer, temp, sizeof (buffer));
328 DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
329 } /* for (act = act_head; act != NULL; act = act->next) */
331 if ((may_be_empty == 0) && (buffer[0] == 0))
333 WARNING ("Target `replace': Replacement resulted in an empty string, "
334 "which is not allowed for this buffer (`host' or `plugin').");
338 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
339 sstrncpy (buffer_in, buffer, buffer_in_size);
342 } /* }}} int tr_action_invoke */
344 static int tr_meta_data_action_invoke ( /* {{{ */
345 tr_meta_data_action_t *act_head, meta_data_t **dest)
348 regmatch_t matches[8] = { [0] = { 0 } };
350 if (act_head == NULL)
353 if ((*dest) == NULL) /* nothing to do */
356 for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next)
358 char temp[DATA_MAX_NAME_LEN];
361 int meta_data_status;
365 value_type = meta_data_type (*dest, act->key);
366 if (value_type == 0) /* not found */
368 if (value_type != MD_TYPE_STRING)
370 WARNING ("Target `replace': Attempting replace on metadata key `%s', "
371 "which isn't a string.",
376 meta_data_status = meta_data_get_string (*dest, act->key, &value);
377 if (meta_data_status != 0)
379 ERROR ("Target `replace': Unable to retrieve metadata value for `%s'.",
381 return (meta_data_status);
384 DEBUG ("target_replace plugin: tr_meta_data_action_invoke: `%s' "
385 "old value = `%s'", act->key, value);
387 status = regexec (&act->re, value,
388 STATIC_ARRAY_SIZE (matches), matches,
390 if (status == REG_NOMATCH)
395 else if (status != 0)
397 char errbuf[1024] = "";
399 regerror (status, &act->re, errbuf, sizeof (errbuf));
400 ERROR ("Target `replace': Executing a regular expression failed: %s.",
406 if (act->replacement == NULL)
408 /* no replacement; delete the key */
409 DEBUG ("target_replace plugin: tr_meta_data_action_invoke: "
410 "deleting `%s'", act->key);
411 meta_data_delete (*dest, act->key);
416 subst_status = subst (temp, sizeof (temp), value,
417 (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, act->replacement);
418 if (subst_status == NULL)
420 ERROR ("Target `replace': subst (value = %s, start = %zu, end = %zu, "
421 "replacement = %s) failed.",
422 value, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
428 DEBUG ("target_replace plugin: tr_meta_data_action_invoke: `%s' "
429 "value `%s' -> `%s'", act->key, value, temp);
431 if ((result = meta_data_create()) == NULL)
433 ERROR ("Target `replace': failed to create metadata for `%s'.",
439 meta_data_status = meta_data_add_string (result, act->key, temp);
440 if (meta_data_status != 0)
442 ERROR ("Target `replace': Unable to set metadata value for `%s'.",
444 meta_data_destroy (result);
446 return (meta_data_status);
449 meta_data_clone_merge (dest, result);
450 meta_data_destroy (result);
452 } /* for (act = act_head; act != NULL; act = act->next) */
455 } /* }}} int tr_meta_data_action_invoke */
457 static int tr_destroy (void **user_data) /* {{{ */
461 if (user_data == NULL)
468 tr_action_destroy (data->host);
469 tr_action_destroy (data->plugin);
470 tr_action_destroy (data->plugin_instance);
471 /* tr_action_destroy (data->type); */
472 tr_action_destroy (data->type_instance);
473 tr_meta_data_action_destroy (data->meta);
477 } /* }}} int tr_destroy */
479 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
484 data = calloc (1, sizeof (*data));
487 ERROR ("tr_create: calloc failed.");
493 data->plugin_instance = NULL;
494 /* data->type = NULL; */
495 data->type_instance = NULL;
499 for (int i = 0; i < ci->children_num; i++)
501 oconfig_item_t *child = ci->children + i;
503 if ((strcasecmp ("Host", child->key) == 0)
504 || (strcasecmp ("Hostname", child->key) == 0))
505 status = tr_config_add_action (&data->host, child,
506 /* may be empty = */ 0);
507 else if (strcasecmp ("Plugin", child->key) == 0)
508 status = tr_config_add_action (&data->plugin, child,
509 /* may be empty = */ 0);
510 else if (strcasecmp ("PluginInstance", child->key) == 0)
511 status = tr_config_add_action (&data->plugin_instance, child,
512 /* may be empty = */ 1);
514 else if (strcasecmp ("Type", child->key) == 0)
515 status = tr_config_add_action (&data->type, child,
516 /* may be empty = */ 0);
518 else if (strcasecmp ("TypeInstance", child->key) == 0)
519 status = tr_config_add_action (&data->type_instance, child,
520 /* may be empty = */ 1);
521 else if (strcasecmp ("MetaData", child->key) == 0)
522 status = tr_config_add_meta_action (&data->meta, child,
523 /* should delete = */ 0);
524 else if (strcasecmp ("DeleteMetaData", child->key) == 0)
525 status = tr_config_add_meta_action (&data->meta, child,
526 /* should delete = */ 1);
529 ERROR ("Target `replace': The `%s' configuration option is not understood "
530 "and will be ignored.", child->key);
538 /* Additional sanity-checking */
541 if ((data->host == NULL)
542 && (data->plugin == NULL)
543 && (data->plugin_instance == NULL)
544 /* && (data->type == NULL) */
545 && (data->type_instance == NULL)
546 && (data->meta == NULL))
548 ERROR ("Target `replace': You need to set at least one of `Host', "
549 "`Plugin', `PluginInstance' or `TypeInstance'.");
558 tr_destroy ((void *) &data);
564 } /* }}} int tr_create */
566 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
567 notification_meta_t __attribute__((unused)) **meta, void **user_data)
571 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
577 ERROR ("Target `replace': Invoke: `data' is NULL.");
581 if (data->meta != NULL)
583 tr_meta_data_action_invoke (data->meta, &(vl->meta));
586 #define HANDLE_FIELD(f,e) \
587 if (data->f != NULL) \
588 tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
589 HANDLE_FIELD (host, 0);
590 HANDLE_FIELD (plugin, 0);
591 HANDLE_FIELD (plugin_instance, 1);
592 /* HANDLE_FIELD (type, 0); */
593 HANDLE_FIELD (type_instance, 1);
595 return (FC_TARGET_CONTINUE);
596 } /* }}} int tr_invoke */
598 void module_register (void)
600 target_proc_t tproc = { 0 };
602 tproc.create = tr_create;
603 tproc.destroy = tr_destroy;
604 tproc.invoke = tr_invoke;
605 fc_register_target ("replace", tproc);
606 } /* module_register */
608 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */