Allow replacing within and deleting metadata keys.
[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 {
39   regex_t re;
40   char *replacement;
41   int may_be_empty;
42
43   tr_action_t *next;
44 };
45
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;
53
54   tr_meta_data_action_t *next;
55 };
56
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;
67
68 static char *tr_strdup (const char *orig) /* {{{ */
69 {
70   size_t sz;
71   char *dest;
72
73   if (orig == NULL)
74     return (NULL);
75
76   sz = strlen (orig) + 1;
77   dest = malloc (sz);
78   if (dest == NULL)
79     return (NULL);
80
81   memcpy (dest, orig, sz);
82
83   return (dest);
84 } /* }}} char *tr_strdup */
85
86 static void tr_action_destroy (tr_action_t *act) /* {{{ */
87 {
88   if (act == NULL)
89     return;
90
91   regfree (&act->re);
92   sfree (act->replacement);
93
94   if (act->next != NULL)
95     tr_action_destroy (act->next);
96
97   sfree (act);
98 } /* }}} void tr_action_destroy */
99
100 static void tr_meta_data_action_destroy (tr_meta_data_action_t *act) /* {{{ */
101 {
102   if (act == NULL)
103     return;
104
105   sfree (act->key);
106   regfree (&act->re);
107   sfree (act->replacement);
108
109   if (act->next != NULL)
110     tr_meta_data_action_destroy (act->next);
111
112   sfree (act);
113 } /* }}} void tr_meta_data_action_destroy */
114
115 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
116     const oconfig_item_t *ci, int may_be_empty)
117 {
118   tr_action_t *act;
119   int status;
120
121   if (dest == NULL)
122     return (-EINVAL);
123
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   }
132
133   act = calloc (1, sizeof (*act));
134   if (act == NULL)
135   {
136     ERROR ("tr_config_add_action: calloc failed.");
137     return (-ENOMEM);
138   }
139
140   act->replacement = NULL;
141   act->may_be_empty = may_be_empty;
142
143   status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
144   if (status != 0)
145   {
146     char errbuf[1024] = "";
147
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   }
156
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     regfree (&act->re);
162     sfree (act);
163     return (-ENOMEM);
164   }
165
166   /* Insert action at end of list. */
167   if (*dest == NULL)
168     *dest = act;
169   else
170   {
171     tr_action_t *prev;
172
173     prev = *dest;
174     while (prev->next != NULL)
175       prev = prev->next;
176
177     prev->next = act;
178   }
179
180   return (0);
181 } /* }}} int tr_config_add_action */
182
183 static int tr_config_add_meta_action (tr_meta_data_action_t **dest, /* {{{ */
184     const oconfig_item_t *ci, int should_delete)
185 {
186   tr_meta_data_action_t *act;
187   int status;
188
189   if (dest == NULL)
190     return (-EINVAL);
191
192   if (should_delete)
193   {
194     if ((ci->values_num != 2)
195         || (ci->values[0].type != OCONFIG_TYPE_STRING)
196         || (ci->values[1].type != OCONFIG_TYPE_STRING))
197     {
198       ERROR ("Target `replace': The `%s' option requires exactly two string "
199           "arguments.", ci->key);
200       return (-1);
201     }
202   }
203   else
204   {
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))
209     {
210       ERROR ("Target `replace': The `%s' option requires exactly three string "
211           "arguments.", ci->key);
212       return (-1);
213     }
214   }
215
216   if (strlen (ci->values[0].value.string) == 0)
217   {
218     ERROR ("Target `replace': The `%s' option does not accept empty string as "
219         "first argument.", ci->key);
220     return (-1);
221   }
222
223   act = calloc (1, sizeof (*act));
224   if (act == NULL)
225   {
226     ERROR ("tr_config_add_meta_action: calloc failed.");
227     return (-ENOMEM);
228   }
229
230   act->key = NULL;
231   act->replacement = NULL;
232
233   act->key = tr_strdup (ci->values[0].value.string);
234   if (act->key == NULL)
235   {
236     ERROR ("tr_config_add_meta_action: tr_strdup failed.");
237     sfree (act);
238     return (-ENOMEM);
239   }
240
241   status = regcomp (&act->re, ci->values[1].value.string, REG_EXTENDED);
242   if (status != 0)
243   {
244     char errbuf[1024] = "";
245
246     /* regerror assures null termination. */
247     regerror (status, &act->re, errbuf, sizeof (errbuf));
248     ERROR ("Target `replace': Compiling the regular expression `%s' "
249         "failed: %s.",
250         ci->values[1].value.string, errbuf);
251     sfree (act->key);
252     sfree (act);
253     return (-EINVAL);
254   }
255
256   if (!should_delete) {
257     act->replacement = tr_strdup (ci->values[2].value.string);
258     if (act->replacement == NULL)
259     {
260       ERROR ("tr_config_add_meta_action: tr_strdup failed.");
261       sfree (act->key);
262       regfree (&act->re);
263       sfree (act);
264       return (-ENOMEM);
265     }
266   }
267
268   /* Insert action at end of list. */
269   if (*dest == NULL)
270     *dest = act;
271   else
272   {
273     tr_meta_data_action_t *prev;
274
275     prev = *dest;
276     while (prev->next != NULL)
277       prev = prev->next;
278
279     prev->next = act;
280   }
281
282   return (0);
283 } /* }}} int tr_config_add_meta_action */
284
285 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
286     char *buffer_in, size_t buffer_in_size, int may_be_empty)
287 {
288   int status;
289   char buffer[DATA_MAX_NAME_LEN];
290   regmatch_t matches[8] = { [0] = { 0 } };
291
292   if (act_head == NULL)
293     return (-EINVAL);
294
295   sstrncpy (buffer, buffer_in, sizeof (buffer));
296
297   DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
298
299   for (tr_action_t *act = act_head; act != NULL; act = act->next)
300   {
301     char temp[DATA_MAX_NAME_LEN];
302     char *subst_status;
303
304     status = regexec (&act->re, buffer,
305         STATIC_ARRAY_SIZE (matches), matches,
306         /* flags = */ 0);
307     if (status == REG_NOMATCH)
308       continue;
309     else if (status != 0)
310     {
311       char errbuf[1024] = "";
312
313       regerror (status, &act->re, errbuf, sizeof (errbuf));
314       ERROR ("Target `replace': Executing a regular expression failed: %s.",
315           errbuf);
316       continue;
317     }
318
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)
322     {
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,
326           act->replacement);
327       continue;
328     }
329     sstrncpy (buffer, temp, sizeof (buffer));
330
331     DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
332   } /* for (act = act_head; act != NULL; act = act->next) */
333
334   if ((may_be_empty == 0) && (buffer[0] == 0))
335   {
336     WARNING ("Target `replace': Replacement resulted in an empty string, "
337         "which is not allowed for this buffer (`host' or `plugin').");
338     return (0);
339   }
340
341   DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
342   sstrncpy (buffer_in, buffer, buffer_in_size);
343
344   return (0);
345 } /* }}} int tr_action_invoke */
346
347 static int tr_meta_data_action_invoke ( /* {{{ */
348     tr_meta_data_action_t *act_head, meta_data_t **dest)
349 {
350   int status;
351   regmatch_t matches[8] = { [0] = { 0 } };
352
353   if (act_head == NULL)
354     return (-EINVAL);
355
356   if ((*dest) == NULL)  /* nothing to do */
357     return (0);
358
359   for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next)
360   {
361     char temp[DATA_MAX_NAME_LEN];
362     char *subst_status;
363     int value_type;
364     int meta_data_status;
365     char *value;
366
367     value_type = meta_data_type (*dest, act->key);
368     if (value_type == 0)  /* not found */
369       continue;
370     if (value_type != MD_TYPE_STRING)
371     {
372       ERROR ("Target `replace': Attempting replace on metadata key `%s', "
373           "which isn't a string.",
374           act->key);
375       continue;
376     }
377
378     meta_data_status = meta_data_get_string (*dest, act->key, &value);
379     if (meta_data_status != 0)
380     {
381       ERROR ("Target `replace': Unable to retrieve metadata value for `%s'.",
382           act->key);
383       return (meta_data_status);
384     }
385
386     DEBUG ("target_replace plugin: tr_meta_data_action_invoke: `%s' "
387         "old value = `%s'", act->key, value);
388
389     status = regexec (&act->re, value,
390         STATIC_ARRAY_SIZE (matches), matches,
391         /* flags = */ 0);
392     if (status == REG_NOMATCH)
393       continue;
394     else if (status != 0)
395     {
396       char errbuf[1024] = "";
397
398       regerror (status, &act->re, errbuf, sizeof (errbuf));
399       ERROR ("Target `replace': Executing a regular expression failed: %s.",
400           errbuf);
401       continue;
402     }
403
404     if (act->replacement != NULL)
405     {
406       meta_data_t *result;
407
408       subst_status = subst (temp, sizeof (temp), value,
409           (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
410           act->replacement);
411       if (subst_status == NULL)
412       {
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,
416             act->replacement);
417         continue;
418       }
419
420       DEBUG ("target_replace plugin: tr_meta_data_action_invoke: `%s' "
421           "value `%s' -> `%s'", act->key, value, temp);
422
423       if ((result = meta_data_create()) == NULL)
424       {
425         ERROR ("Target `replace': failed to create metadata for `%s'.",
426             act->key);
427         return (-ENOMEM);
428       }
429
430       meta_data_status = meta_data_add_string (result, act->key, temp);
431
432       if (meta_data_status != 0)
433       {
434         ERROR ("Target `replace': Unable to set metadata value for `%s'.",
435             act->key);
436         return (meta_data_status);
437       }
438
439       meta_data_clone_merge (dest, result);
440       meta_data_destroy (result);
441     }
442     else  /* no replacement; delete the key */
443     {
444       DEBUG ("target_replace plugin: tr_meta_data_action_invoke: "
445           "deleting `%s'", act->key);
446       meta_data_delete (*dest, act->key);
447     }
448   } /* for (act = act_head; act != NULL; act = act->next) */
449
450   return (0);
451 } /* }}} int tr_meta_data_action_invoke */
452
453 static int tr_destroy (void **user_data) /* {{{ */
454 {
455   tr_data_t *data;
456
457   if (user_data == NULL)
458     return (-EINVAL);
459
460   data = *user_data;
461   if (data == NULL)
462     return (0);
463
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);
470   sfree (data);
471
472   return (0);
473 } /* }}} int tr_destroy */
474
475 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
476 {
477   tr_data_t *data;
478   int status;
479
480   data = calloc (1, sizeof (*data));
481   if (data == NULL)
482   {
483     ERROR ("tr_create: calloc failed.");
484     return (-ENOMEM);
485   }
486
487   data->host = NULL;
488   data->plugin = NULL;
489   data->plugin_instance = NULL;
490   /* data->type = NULL; */
491   data->type_instance = NULL;
492   data->meta = NULL;
493
494   status = 0;
495   for (int i = 0; i < ci->children_num; i++)
496   {
497     oconfig_item_t *child = ci->children + i;
498
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);
509 #if 0
510     else if (strcasecmp ("Type", child->key) == 0)
511       status = tr_config_add_action (&data->type, child,
512           /* may be empty = */ 0);
513 #endif
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);
523     else
524     {
525       ERROR ("Target `replace': The `%s' configuration option is not understood "
526           "and will be ignored.", child->key);
527       status = 0;
528     }
529
530     if (status != 0)
531       break;
532   }
533
534   /* Additional sanity-checking */
535   while (status == 0)
536   {
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))
543     {
544       ERROR ("Target `replace': You need to set at least one of `Host', "
545           "`Plugin', `PluginInstance' or `TypeInstance'.");
546       status = -1;
547     }
548
549     break;
550   }
551
552   if (status != 0)
553   {
554     tr_destroy ((void *) &data);
555     return (status);
556   }
557
558   *user_data = data;
559   return (0);
560 } /* }}} int tr_create */
561
562 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
563     notification_meta_t __attribute__((unused)) **meta, void **user_data)
564 {
565   tr_data_t *data;
566
567   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
568     return (-EINVAL);
569
570   data = *user_data;
571   if (data == NULL)
572   {
573     ERROR ("Target `replace': Invoke: `data' is NULL.");
574     return (-EINVAL);
575   }
576
577   if (data->meta != NULL)
578   {
579     tr_meta_data_action_invoke (data->meta, &(vl->meta));
580   }
581
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);
590
591   return (FC_TARGET_CONTINUE);
592 } /* }}} int tr_invoke */
593
594 void module_register (void)
595 {
596         target_proc_t tproc = { 0 };
597
598         tproc.create  = tr_create;
599         tproc.destroy = tr_destroy;
600         tproc.invoke  = tr_invoke;
601         fc_register_target ("replace", tproc);
602 } /* module_register */
603
604 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
605