Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / target_replace.c
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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "filter_chain.h"
31 #include "utils_subst.h"
32
33 #include <regex.h>
34
35 struct tr_action_s;
36 typedef struct tr_action_s tr_action_t;
37 struct tr_action_s {
38   regex_t re;
39   char *replacement;
40   _Bool may_be_empty;
41
42   tr_action_t *next;
43 };
44
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 {
48   char *key;
49   regex_t re;
50   char *replacement;
51
52   tr_meta_data_action_t *next;
53 };
54
55 struct tr_data_s {
56   tr_action_t *host;
57   tr_action_t *plugin;
58   tr_action_t *plugin_instance;
59   /* tr_action_t *type; */
60   tr_action_t *type_instance;
61   tr_meta_data_action_t *meta;
62 };
63 typedef struct tr_data_s tr_data_t;
64
65 static char *tr_strdup(const char *orig) /* {{{ */
66 {
67   size_t sz;
68   char *dest;
69
70   if (orig == NULL)
71     return NULL;
72
73   sz = strlen(orig) + 1;
74   dest = malloc(sz);
75   if (dest == NULL)
76     return NULL;
77
78   memcpy(dest, orig, sz);
79
80   return dest;
81 } /* }}} char *tr_strdup */
82
83 static void tr_action_destroy(tr_action_t *act) /* {{{ */
84 {
85   if (act == NULL)
86     return;
87
88   regfree(&act->re);
89   sfree(act->replacement);
90
91   if (act->next != NULL)
92     tr_action_destroy(act->next);
93
94   sfree(act);
95 } /* }}} void tr_action_destroy */
96
97 static void tr_meta_data_action_destroy(tr_meta_data_action_t *act) /* {{{ */
98 {
99   if (act == NULL)
100     return;
101
102   sfree(act->key);
103   regfree(&act->re);
104   sfree(act->replacement);
105
106   if (act->next != NULL)
107     tr_meta_data_action_destroy(act->next);
108
109   sfree(act);
110 } /* }}} void tr_meta_data_action_destroy */
111
112 static int tr_config_add_action(tr_action_t **dest, /* {{{ */
113                                 const oconfig_item_t *ci, _Bool may_be_empty) {
114   tr_action_t *act;
115   int status;
116
117   if (dest == NULL)
118     return -EINVAL;
119
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 "
123           "arguments.",
124           ci->key);
125     return -1;
126   }
127
128   act = calloc(1, sizeof(*act));
129   if (act == NULL) {
130     ERROR("tr_config_add_action: calloc failed.");
131     return -ENOMEM;
132   }
133
134   act->replacement = NULL;
135   act->may_be_empty = may_be_empty;
136
137   status = regcomp(&act->re, ci->values[0].value.string, REG_EXTENDED);
138   if (status != 0) {
139     char errbuf[1024] = "";
140
141     /* regerror assures null termination. */
142     regerror(status, &act->re, errbuf, sizeof(errbuf));
143     ERROR("Target `replace': Compiling the regular expression `%s' "
144           "failed: %s.",
145           ci->values[0].value.string, errbuf);
146     sfree(act);
147     return -EINVAL;
148   }
149
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);
154     return -ENOMEM;
155   }
156
157   /* Insert action at end of list. */
158   if (*dest == NULL)
159     *dest = act;
160   else {
161     tr_action_t *prev;
162
163     prev = *dest;
164     while (prev->next != NULL)
165       prev = prev->next;
166
167     prev->next = act;
168   }
169
170   return 0;
171 } /* }}} int tr_config_add_action */
172
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;
177   int status;
178
179   if (dest == NULL)
180     return -EINVAL;
181
182   if (should_delete) {
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 "
186             "arguments.",
187             ci->key);
188       return -1;
189     }
190   } else {
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 "
195             "arguments.",
196             ci->key);
197       return -1;
198     }
199   }
200
201   if (strlen(ci->values[0].value.string) == 0) {
202     ERROR("Target `replace': The `%s' option does not accept empty string as "
203           "first argument.",
204           ci->key);
205     return -1;
206   }
207
208   act = calloc(1, sizeof(*act));
209   if (act == NULL) {
210     ERROR("tr_config_add_meta_action: calloc failed.");
211     return -ENOMEM;
212   }
213
214   act->key = NULL;
215   act->replacement = NULL;
216
217   status = regcomp(&act->re, ci->values[1].value.string, REG_EXTENDED);
218   if (status != 0) {
219     char errbuf[1024] = "";
220
221     /* regerror assures null termination. */
222     regerror(status, &act->re, errbuf, sizeof(errbuf));
223     ERROR("Target `replace': Compiling the regular expression `%s' "
224           "failed: %s.",
225           ci->values[1].value.string, errbuf);
226     sfree(act->key);
227     sfree(act);
228     return -EINVAL;
229   }
230
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);
235     return -ENOMEM;
236   }
237
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);
243       return -ENOMEM;
244     }
245   }
246
247   /* Insert action at end of list. */
248   if (*dest == NULL)
249     *dest = act;
250   else {
251     tr_meta_data_action_t *prev;
252
253     prev = *dest;
254     while (prev->next != NULL)
255       prev = prev->next;
256
257     prev->next = act;
258   }
259
260   return 0;
261 } /* }}} int tr_config_add_meta_action */
262
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) {
266   int status;
267   char buffer[DATA_MAX_NAME_LEN];
268   regmatch_t matches[8] = {[0] = {0}};
269
270   if (act_head == NULL)
271     return -EINVAL;
272
273   sstrncpy(buffer, buffer_in, sizeof(buffer));
274
275   DEBUG("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
276
277   for (tr_action_t *act = act_head; act != NULL; act = act->next) {
278     char temp[DATA_MAX_NAME_LEN];
279     char *subst_status;
280
281     status = regexec(&act->re, buffer, STATIC_ARRAY_SIZE(matches), matches,
282                      /* flags = */ 0);
283     if (status == REG_NOMATCH)
284       continue;
285     else if (status != 0) {
286       char errbuf[1024] = "";
287
288       regerror(status, &act->re, errbuf, sizeof(errbuf));
289       ERROR("Target `replace': Executing a regular expression failed: %s.",
290             errbuf);
291       continue;
292     }
293
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,
300             act->replacement);
301       continue;
302     }
303     sstrncpy(buffer, temp, sizeof(buffer));
304
305     DEBUG("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
306   } /* for (act = act_head; act != NULL; act = act->next) */
307
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').");
311     return 0;
312   }
313
314   DEBUG("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
315   sstrncpy(buffer_in, buffer, buffer_in_size);
316
317   return 0;
318 } /* }}} int tr_action_invoke */
319
320 static int tr_meta_data_action_invoke(/* {{{ */
321                                       tr_meta_data_action_t *act_head,
322                                       meta_data_t **dest) {
323   int status;
324   regmatch_t matches[8] = {[0] = {0}};
325
326   if (act_head == NULL)
327     return -EINVAL;
328
329   if ((*dest) == NULL) /* nothing to do */
330     return 0;
331
332   for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next) {
333     char temp[DATA_MAX_NAME_LEN];
334     char *subst_status;
335     int value_type;
336     int meta_data_status;
337     char *value;
338     meta_data_t *result;
339
340     value_type = meta_data_type(*dest, act->key);
341     if (value_type == 0) /* not found */
342       continue;
343     if (value_type != MD_TYPE_STRING) {
344       WARNING("Target `replace': Attempting replace on metadata key `%s', "
345               "which isn't a string.",
346               act->key);
347       continue;
348     }
349
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'.",
353             act->key);
354       return meta_data_status;
355     }
356
357     DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
358           "old value = `%s'",
359           act->key, value);
360
361     status = regexec(&act->re, value, STATIC_ARRAY_SIZE(matches), matches,
362                      /* flags = */ 0);
363     if (status == REG_NOMATCH) {
364       sfree(value);
365       continue;
366     } else if (status != 0) {
367       char errbuf[1024] = "";
368
369       regerror(status, &act->re, errbuf, sizeof(errbuf));
370       ERROR("Target `replace': Executing a regular expression failed: %s.",
371             errbuf);
372       sfree(value);
373       continue;
374     }
375
376     if (act->replacement == NULL) {
377       /* no replacement; delete the key */
378       DEBUG("target_replace plugin: tr_meta_data_action_invoke: "
379             "deleting `%s'",
380             act->key);
381       meta_data_delete(*dest, act->key);
382       sfree(value);
383       continue;
384     }
385
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,
392             act->replacement);
393       sfree(value);
394       continue;
395     }
396
397     DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
398           "value `%s' -> `%s'",
399           act->key, value, temp);
400
401     if ((result = meta_data_create()) == NULL) {
402       ERROR("Target `replace': failed to create metadata for `%s'.", act->key);
403       sfree(value);
404       return -ENOMEM;
405     }
406
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'.",
410             act->key);
411       meta_data_destroy(result);
412       sfree(value);
413       return meta_data_status;
414     }
415
416     meta_data_clone_merge(dest, result);
417     meta_data_destroy(result);
418     sfree(value);
419   } /* for (act = act_head; act != NULL; act = act->next) */
420
421   return 0;
422 } /* }}} int tr_meta_data_action_invoke */
423
424 static int tr_destroy(void **user_data) /* {{{ */
425 {
426   tr_data_t *data;
427
428   if (user_data == NULL)
429     return -EINVAL;
430
431   data = *user_data;
432   if (data == NULL)
433     return 0;
434
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);
441   sfree(data);
442
443   return 0;
444 } /* }}} int tr_destroy */
445
446 static int tr_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
447 {
448   tr_data_t *data;
449   int status;
450
451   data = calloc(1, sizeof(*data));
452   if (data == NULL) {
453     ERROR("tr_create: calloc failed.");
454     return -ENOMEM;
455   }
456
457   data->host = NULL;
458   data->plugin = NULL;
459   data->plugin_instance = NULL;
460   /* data->type = NULL; */
461   data->type_instance = NULL;
462   data->meta = NULL;
463
464   status = 0;
465   for (int i = 0; i < ci->children_num; i++) {
466     oconfig_item_t *child = ci->children + i;
467
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);
478 #if 0
479     else if (strcasecmp ("Type", child->key) == 0)
480       status = tr_config_add_action (&data->type, child,
481           /* may be empty = */ 0);
482 #endif
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);
492     else {
493       ERROR("Target `replace': The `%s' configuration option is not understood "
494             "and will be ignored.",
495             child->key);
496       status = 0;
497     }
498
499     if (status != 0)
500       break;
501   }
502
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'.");
511       status = -1;
512     }
513
514     break;
515   }
516
517   if (status != 0) {
518     tr_destroy((void *)&data);
519     return status;
520   }
521
522   *user_data = data;
523   return 0;
524 } /* }}} int tr_create */
525
526 static int tr_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
527                      notification_meta_t __attribute__((unused)) * *meta,
528                      void **user_data) {
529   tr_data_t *data;
530
531   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
532     return -EINVAL;
533
534   data = *user_data;
535   if (data == NULL) {
536     ERROR("Target `replace': Invoke: `data' is NULL.");
537     return -EINVAL;
538   }
539
540   if (data->meta != NULL) {
541     tr_meta_data_action_invoke(data->meta, &(vl->meta));
542   }
543
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);
552
553   return FC_TARGET_CONTINUE;
554 } /* }}} int tr_invoke */
555
556 void module_register(void) {
557   target_proc_t tproc = {0};
558
559   tproc.create = tr_create;
560   tproc.destroy = tr_destroy;
561   tproc.invoke = tr_invoke;
562   fc_register_target("replace", tproc);
563 } /* module_register */