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, int 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.");
166 /* Insert action at end of list. */
174 while (prev->next != NULL)
181 } /* }}} int tr_config_add_action */
183 static int tr_config_add_meta_action (tr_meta_data_action_t **dest, /* {{{ */
184 const oconfig_item_t *ci, int should_delete)
186 tr_meta_data_action_t *act;
194 if ((ci->values_num != 2)
195 || (ci->values[0].type != OCONFIG_TYPE_STRING)
196 || (ci->values[1].type != OCONFIG_TYPE_STRING))
198 ERROR ("Target `replace': The `%s' option requires exactly two string "
199 "arguments.", ci->key);
205 if ((ci->values_num != 3)
206 || (ci->values[0].type != OCONFIG_TYPE_STRING)
207 || (ci->values[1].type != OCONFIG_TYPE_STRING)
208 || (ci->values[2].type != OCONFIG_TYPE_STRING))
210 ERROR ("Target `replace': The `%s' option requires exactly three string "
211 "arguments.", ci->key);
216 if (strlen (ci->values[0].value.string) == 0)
218 ERROR ("Target `replace': The `%s' option does not accept empty string as "
219 "first argument.", ci->key);
223 act = calloc (1, sizeof (*act));
226 ERROR ("tr_config_add_meta_action: calloc failed.");
231 act->replacement = NULL;
233 act->key = tr_strdup (ci->values[0].value.string);
234 if (act->key == NULL)
236 ERROR ("tr_config_add_meta_action: tr_strdup failed.");
241 status = regcomp (&act->re, ci->values[1].value.string, REG_EXTENDED);
244 char errbuf[1024] = "";
246 /* regerror assures null termination. */
247 regerror (status, &act->re, errbuf, sizeof (errbuf));
248 ERROR ("Target `replace': Compiling the regular expression `%s' "
250 ci->values[1].value.string, errbuf);
256 if (!should_delete) {
257 act->replacement = tr_strdup (ci->values[2].value.string);
258 if (act->replacement == NULL)
260 ERROR ("tr_config_add_meta_action: tr_strdup failed.");
268 /* Insert action at end of list. */
273 tr_meta_data_action_t *prev;
276 while (prev->next != NULL)
283 } /* }}} int tr_config_add_meta_action */
285 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
286 char *buffer_in, size_t buffer_in_size, int may_be_empty)
289 char buffer[DATA_MAX_NAME_LEN];
290 regmatch_t matches[8] = { [0] = { 0 } };
292 if (act_head == NULL)
295 sstrncpy (buffer, buffer_in, sizeof (buffer));
297 DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
299 for (tr_action_t *act = act_head; act != NULL; act = act->next)
301 char temp[DATA_MAX_NAME_LEN];
304 status = regexec (&act->re, buffer,
305 STATIC_ARRAY_SIZE (matches), matches,
307 if (status == REG_NOMATCH)
309 else if (status != 0)
311 char errbuf[1024] = "";
313 regerror (status, &act->re, errbuf, sizeof (errbuf));
314 ERROR ("Target `replace': Executing a regular expression failed: %s.",
319 subst_status = subst (temp, sizeof (temp), buffer,
320 (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, act->replacement);
321 if (subst_status == NULL)
323 ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
324 "replacement = %s) failed.",
325 buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
329 sstrncpy (buffer, temp, sizeof (buffer));
331 DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
332 } /* for (act = act_head; act != NULL; act = act->next) */
334 if ((may_be_empty == 0) && (buffer[0] == 0))
336 WARNING ("Target `replace': Replacement resulted in an empty string, "
337 "which is not allowed for this buffer (`host' or `plugin').");
341 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
342 sstrncpy (buffer_in, buffer, buffer_in_size);
345 } /* }}} int tr_action_invoke */
347 static int tr_meta_data_action_invoke ( /* {{{ */
348 tr_meta_data_action_t *act_head, meta_data_t **dest)
351 regmatch_t matches[8] = { [0] = { 0 } };
353 if (act_head == NULL)
356 if ((*dest) == NULL) /* nothing to do */
359 for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next)
361 char temp[DATA_MAX_NAME_LEN];
364 int meta_data_status;
367 value_type = meta_data_type (*dest, act->key);
368 if (value_type == 0) /* not found */
370 if (value_type != MD_TYPE_STRING)
372 ERROR ("Target `replace': Attempting replace on metadata key `%s', "
373 "which isn't a string.",
378 meta_data_status = meta_data_get_string (*dest, act->key, &value);
379 if (meta_data_status != 0)
381 ERROR ("Target `replace': Unable to retrieve metadata value for `%s'.",
383 return (meta_data_status);
386 DEBUG ("target_replace plugin: tr_meta_data_action_invoke: `%s' "
387 "old value = `%s'", act->key, value);
389 status = regexec (&act->re, value,
390 STATIC_ARRAY_SIZE (matches), matches,
392 if (status == REG_NOMATCH)
394 else if (status != 0)
396 char errbuf[1024] = "";
398 regerror (status, &act->re, errbuf, sizeof (errbuf));
399 ERROR ("Target `replace': Executing a regular expression failed: %s.",
404 if (act->replacement != NULL)
408 subst_status = subst (temp, sizeof (temp), value,
409 (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
411 if (subst_status == NULL)
413 ERROR ("Target `replace': subst (value = %s, start = %zu, end = %zu, "
414 "replacement = %s) failed.",
415 value, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
420 DEBUG ("target_replace plugin: tr_meta_data_action_invoke: `%s' "
421 "value `%s' -> `%s'", act->key, value, temp);
423 if ((result = meta_data_create()) == NULL)
425 ERROR ("Target `replace': failed to create metadata for `%s'.",
430 meta_data_status = meta_data_add_string (result, act->key, temp);
432 if (meta_data_status != 0)
434 ERROR ("Target `replace': Unable to set metadata value for `%s'.",
436 return (meta_data_status);
439 meta_data_clone_merge (dest, result);
440 meta_data_destroy (result);
442 else /* no replacement; delete the key */
444 DEBUG ("target_replace plugin: tr_meta_data_action_invoke: "
445 "deleting `%s'", act->key);
446 meta_data_delete (*dest, act->key);
448 } /* for (act = act_head; act != NULL; act = act->next) */
451 } /* }}} int tr_meta_data_action_invoke */
453 static int tr_destroy (void **user_data) /* {{{ */
457 if (user_data == NULL)
464 tr_action_destroy (data->host);
465 tr_action_destroy (data->plugin);
466 tr_action_destroy (data->plugin_instance);
467 /* tr_action_destroy (data->type); */
468 tr_action_destroy (data->type_instance);
469 tr_meta_data_action_destroy (data->meta);
473 } /* }}} int tr_destroy */
475 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
480 data = calloc (1, sizeof (*data));
483 ERROR ("tr_create: calloc failed.");
489 data->plugin_instance = NULL;
490 /* data->type = NULL; */
491 data->type_instance = NULL;
495 for (int i = 0; i < ci->children_num; i++)
497 oconfig_item_t *child = ci->children + i;
499 if ((strcasecmp ("Host", child->key) == 0)
500 || (strcasecmp ("Hostname", child->key) == 0))
501 status = tr_config_add_action (&data->host, child,
502 /* may be empty = */ 0);
503 else if (strcasecmp ("Plugin", child->key) == 0)
504 status = tr_config_add_action (&data->plugin, child,
505 /* may be empty = */ 0);
506 else if (strcasecmp ("PluginInstance", child->key) == 0)
507 status = tr_config_add_action (&data->plugin_instance, child,
508 /* may be empty = */ 1);
510 else if (strcasecmp ("Type", child->key) == 0)
511 status = tr_config_add_action (&data->type, child,
512 /* may be empty = */ 0);
514 else if (strcasecmp ("TypeInstance", child->key) == 0)
515 status = tr_config_add_action (&data->type_instance, child,
516 /* may be empty = */ 1);
517 else if (strcasecmp ("MetaData", child->key) == 0)
518 status = tr_config_add_meta_action (&data->meta, child,
519 /* should delete = */ 0);
520 else if (strcasecmp ("DeleteMetaData", child->key) == 0)
521 status = tr_config_add_meta_action (&data->meta, child,
522 /* should delete = */ 1);
525 ERROR ("Target `replace': The `%s' configuration option is not understood "
526 "and will be ignored.", child->key);
534 /* Additional sanity-checking */
537 if ((data->host == NULL)
538 && (data->plugin == NULL)
539 && (data->plugin_instance == NULL)
540 /* && (data->type == NULL) */
541 && (data->type_instance == NULL)
542 && (data->meta == NULL))
544 ERROR ("Target `replace': You need to set at least one of `Host', "
545 "`Plugin', `PluginInstance' or `TypeInstance'.");
554 tr_destroy ((void *) &data);
560 } /* }}} int tr_create */
562 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
563 notification_meta_t __attribute__((unused)) **meta, void **user_data)
567 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
573 ERROR ("Target `replace': Invoke: `data' is NULL.");
577 if (data->meta != NULL)
579 tr_meta_data_action_invoke (data->meta, &(vl->meta));
582 #define HANDLE_FIELD(f,e) \
583 if (data->f != NULL) \
584 tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
585 HANDLE_FIELD (host, 0);
586 HANDLE_FIELD (plugin, 0);
587 HANDLE_FIELD (plugin_instance, 1);
588 /* HANDLE_FIELD (type, 0); */
589 HANDLE_FIELD (type_instance, 1);
591 return (FC_TARGET_CONTINUE);
592 } /* }}} int tr_invoke */
594 void module_register (void)
596 target_proc_t tproc = { 0 };
598 tproc.create = tr_create;
599 tproc.destroy = tr_destroy;
600 tproc.invoke = tr_invoke;
601 fc_register_target ("replace", tproc);
602 } /* module_register */
604 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */