1 /**
2 * collectd - src/target_replace.c
3 * Copyright (C) 2008 Florian Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "filter_chain.h"
31 #include "utils_subst.h"
33 #include <regex.h>
35 struct tr_action_s;
36 typedef struct tr_action_s tr_action_t;
37 struct tr_action_s
38 {
39 regex_t re;
40 char *replacement;
41 _Bool may_be_empty;
43 tr_action_t *next;
44 };
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
49 {
50 char *key;
51 regex_t re;
52 char *replacement;
54 tr_meta_data_action_t *next;
55 };
57 struct tr_data_s
58 {
59 tr_action_t *host;
60 tr_action_t *plugin;
61 tr_action_t *plugin_instance;
62 /* tr_action_t *type; */
63 tr_action_t *type_instance;
64 tr_meta_data_action_t *meta;
65 };
66 typedef struct tr_data_s tr_data_t;
68 static char *tr_strdup (const char *orig) /* {{{ */
69 {
70 size_t sz;
71 char *dest;
73 if (orig == NULL)
74 return (NULL);
76 sz = strlen (orig) + 1;
77 dest = malloc (sz);
78 if (dest == NULL)
79 return (NULL);
81 memcpy (dest, orig, sz);
83 return (dest);
84 } /* }}} char *tr_strdup */
86 static void tr_action_destroy (tr_action_t *act) /* {{{ */
87 {
88 if (act == NULL)
89 return;
91 regfree (&act->re);
92 sfree (act->replacement);
94 if (act->next != NULL)
95 tr_action_destroy (act->next);
97 sfree (act);
98 } /* }}} void tr_action_destroy */
100 static void tr_meta_data_action_destroy (tr_meta_data_action_t *act) /* {{{ */
101 {
102 if (act == NULL)
103 return;
105 sfree (act->key);
106 regfree (&act->re);
107 sfree (act->replacement);
109 if (act->next != NULL)
110 tr_meta_data_action_destroy (act->next);
112 sfree (act);
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)
117 {
118 tr_action_t *act;
119 int status;
121 if (dest == NULL)
122 return (-EINVAL);
124 if ((ci->values_num != 2)
125 || (ci->values[0].type != OCONFIG_TYPE_STRING)
126 || (ci->values[1].type != OCONFIG_TYPE_STRING))
127 {
128 ERROR ("Target `replace': The `%s' option requires exactly two string "
129 "arguments.", ci->key);
130 return (-1);
131 }
133 act = calloc (1, sizeof (*act));
134 if (act == NULL)
135 {
136 ERROR ("tr_config_add_action: calloc failed.");
137 return (-ENOMEM);
138 }
140 act->replacement = NULL;
141 act->may_be_empty = may_be_empty;
143 status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
144 if (status != 0)
145 {
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' "
151 "failed: %s.",
152 ci->values[0].value.string, errbuf);
153 sfree (act);
154 return (-EINVAL);
155 }
157 act->replacement = tr_strdup (ci->values[1].value.string);
158 if (act->replacement == NULL)
159 {
160 ERROR ("tr_config_add_action: tr_strdup failed.");
161 tr_action_destroy (act);
162 return (-ENOMEM);
163 }
165 /* Insert action at end of list. */
166 if (*dest == NULL)
167 *dest = act;
168 else
169 {
170 tr_action_t *prev;
172 prev = *dest;
173 while (prev->next != NULL)
174 prev = prev->next;
176 prev->next = act;
177 }
179 return (0);
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)
184 {
185 tr_meta_data_action_t *act;
186 int status;
188 if (dest == NULL)
189 return (-EINVAL);
191 if (should_delete)
192 {
193 if ((ci->values_num != 2)
194 || (ci->values[0].type != OCONFIG_TYPE_STRING)
195 || (ci->values[1].type != OCONFIG_TYPE_STRING))
196 {
197 ERROR ("Target `replace': The `%s' option requires exactly two string "
198 "arguments.", ci->key);
199 return (-1);
200 }
201 }
202 else
203 {
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))
208 {
209 ERROR ("Target `replace': The `%s' option requires exactly three string "
210 "arguments.", ci->key);
211 return (-1);
212 }
213 }
215 if (strlen (ci->values[0].value.string) == 0)
216 {
217 ERROR ("Target `replace': The `%s' option does not accept empty string as "
218 "first argument.", ci->key);
219 return (-1);
220 }
222 act = calloc (1, sizeof (*act));
223 if (act == NULL)
224 {
225 ERROR ("tr_config_add_meta_action: calloc failed.");
226 return (-ENOMEM);
227 }
229 act->key = NULL;
230 act->replacement = NULL;
232 status = regcomp (&act->re, ci->values[1].value.string, REG_EXTENDED);
233 if (status != 0)
234 {
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' "
240 "failed: %s.",
241 ci->values[1].value.string, errbuf);
242 sfree (act->key);
243 sfree (act);
244 return (-EINVAL);
245 }
247 act->key = tr_strdup (ci->values[0].value.string);
248 if (act->key == NULL)
249 {
250 ERROR ("tr_config_add_meta_action: tr_strdup failed.");
251 tr_meta_data_action_destroy (act);
252 return (-ENOMEM);
253 }
255 if (!should_delete) {
256 act->replacement = tr_strdup (ci->values[2].value.string);
257 if (act->replacement == NULL)
258 {
259 ERROR ("tr_config_add_meta_action: tr_strdup failed.");
260 tr_meta_data_action_destroy (act);
261 return (-ENOMEM);
262 }
263 }
265 /* Insert action at end of list. */
266 if (*dest == NULL)
267 *dest = act;
268 else
269 {
270 tr_meta_data_action_t *prev;
272 prev = *dest;
273 while (prev->next != NULL)
274 prev = prev->next;
276 prev->next = act;
277 }
279 return (0);
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)
284 {
285 int status;
286 char buffer[DATA_MAX_NAME_LEN];
287 regmatch_t matches[8] = { [0] = { 0 } };
289 if (act_head == NULL)
290 return (-EINVAL);
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)
297 {
298 char temp[DATA_MAX_NAME_LEN];
299 char *subst_status;
301 status = regexec (&act->re, buffer,
302 STATIC_ARRAY_SIZE (matches), matches,
303 /* flags = */ 0);
304 if (status == REG_NOMATCH)
305 continue;
306 else if (status != 0)
307 {
308 char errbuf[1024] = "";
310 regerror (status, &act->re, errbuf, sizeof (errbuf));
311 ERROR ("Target `replace': Executing a regular expression failed: %s.",
312 errbuf);
313 continue;
314 }
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)
319 {
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,
323 act->replacement);
324 continue;
325 }
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))
332 {
333 WARNING ("Target `replace': Replacement resulted in an empty string, "
334 "which is not allowed for this buffer (`host' or `plugin').");
335 return (0);
336 }
338 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
339 sstrncpy (buffer_in, buffer, buffer_in_size);
341 return (0);
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)
346 {
347 int status;
348 regmatch_t matches[8] = { [0] = { 0 } };
350 if (act_head == NULL)
351 return (-EINVAL);
353 if ((*dest) == NULL) /* nothing to do */
354 return (0);
356 for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next)
357 {
358 char temp[DATA_MAX_NAME_LEN];
359 char *subst_status;
360 int value_type;
361 int meta_data_status;
362 char *value;
363 meta_data_t *result;
365 value_type = meta_data_type (*dest, act->key);
366 if (value_type == 0) /* not found */
367 continue;
368 if (value_type != MD_TYPE_STRING)
369 {
370 WARNING ("Target `replace': Attempting replace on metadata key `%s', "
371 "which isn't a string.",
372 act->key);
373 continue;
374 }
376 meta_data_status = meta_data_get_string (*dest, act->key, &value);
377 if (meta_data_status != 0)
378 {
379 ERROR ("Target `replace': Unable to retrieve metadata value for `%s'.",
380 act->key);
381 return (meta_data_status);
382 }
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,
389 /* flags = */ 0);
390 if (status == REG_NOMATCH)
391 {
392 sfree (value);
393 continue;
394 }
395 else if (status != 0)
396 {
397 char errbuf[1024] = "";
399 regerror (status, &act->re, errbuf, sizeof (errbuf));
400 ERROR ("Target `replace': Executing a regular expression failed: %s.",
401 errbuf);
402 sfree (value);
403 continue;
404 }
406 if (act->replacement == NULL)
407 {
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);
412 sfree (value);
413 continue;
414 }
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)
419 {
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,
423 act->replacement);
424 sfree (value);
425 continue;
426 }
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)
432 {
433 ERROR ("Target `replace': failed to create metadata for `%s'.",
434 act->key);
435 sfree (value);
436 return (-ENOMEM);
437 }
439 meta_data_status = meta_data_add_string (result, act->key, temp);
440 if (meta_data_status != 0)
441 {
442 ERROR ("Target `replace': Unable to set metadata value for `%s'.",
443 act->key);
444 meta_data_destroy (result);
445 sfree (value);
446 return (meta_data_status);
447 }
449 meta_data_clone_merge (dest, result);
450 meta_data_destroy (result);
451 sfree (value);
452 } /* for (act = act_head; act != NULL; act = act->next) */
454 return (0);
455 } /* }}} int tr_meta_data_action_invoke */
457 static int tr_destroy (void **user_data) /* {{{ */
458 {
459 tr_data_t *data;
461 if (user_data == NULL)
462 return (-EINVAL);
464 data = *user_data;
465 if (data == NULL)
466 return (0);
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);
474 sfree (data);
476 return (0);
477 } /* }}} int tr_destroy */
479 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
480 {
481 tr_data_t *data;
482 int status;
484 data = calloc (1, sizeof (*data));
485 if (data == NULL)
486 {
487 ERROR ("tr_create: calloc failed.");
488 return (-ENOMEM);
489 }
491 data->host = NULL;
492 data->plugin = NULL;
493 data->plugin_instance = NULL;
494 /* data->type = NULL; */
495 data->type_instance = NULL;
496 data->meta = NULL;
498 status = 0;
499 for (int i = 0; i < ci->children_num; i++)
500 {
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);
513 #if 0
514 else if (strcasecmp ("Type", child->key) == 0)
515 status = tr_config_add_action (&data->type, child,
516 /* may be empty = */ 0);
517 #endif
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);
527 else
528 {
529 ERROR ("Target `replace': The `%s' configuration option is not understood "
530 "and will be ignored.", child->key);
531 status = 0;
532 }
534 if (status != 0)
535 break;
536 }
538 /* Additional sanity-checking */
539 while (status == 0)
540 {
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))
547 {
548 ERROR ("Target `replace': You need to set at least one of `Host', "
549 "`Plugin', `PluginInstance' or `TypeInstance'.");
550 status = -1;
551 }
553 break;
554 }
556 if (status != 0)
557 {
558 tr_destroy ((void *) &data);
559 return (status);
560 }
562 *user_data = data;
563 return (0);
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)
568 {
569 tr_data_t *data;
571 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
572 return (-EINVAL);
574 data = *user_data;
575 if (data == NULL)
576 {
577 ERROR ("Target `replace': Invoke: `data' is NULL.");
578 return (-EINVAL);
579 }
581 if (data->meta != NULL)
582 {
583 tr_meta_data_action_invoke (data->meta, &(vl->meta));
584 }
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)
599 {
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 : */