Merge pull request #1832 from rubenk/check-for-c99-compiler
[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_data_s
47 {
48   tr_action_t *host;
49   tr_action_t *plugin;
50   tr_action_t *plugin_instance;
51   /* tr_action_t *type; */
52   tr_action_t *type_instance;
53 };
54 typedef struct tr_data_s tr_data_t;
55
56 static char *tr_strdup (const char *orig) /* {{{ */
57 {
58   size_t sz;
59   char *dest;
60
61   if (orig == NULL)
62     return (NULL);
63
64   sz = strlen (orig) + 1;
65   dest = malloc (sz);
66   if (dest == NULL)
67     return (NULL);
68
69   memcpy (dest, orig, sz);
70
71   return (dest);
72 } /* }}} char *tr_strdup */
73
74 static void tr_action_destroy (tr_action_t *act) /* {{{ */
75 {
76   if (act == NULL)
77     return;
78
79   regfree (&act->re);
80   sfree (act->replacement);
81
82   if (act->next != NULL)
83     tr_action_destroy (act->next);
84
85   sfree (act);
86 } /* }}} void tr_action_destroy */
87
88 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
89     const oconfig_item_t *ci, int may_be_empty)
90 {
91   tr_action_t *act;
92   int status;
93
94   if (dest == NULL)
95     return (-EINVAL);
96
97   if ((ci->values_num != 2)
98       || (ci->values[0].type != OCONFIG_TYPE_STRING)
99       || (ci->values[1].type != OCONFIG_TYPE_STRING))
100   {
101     ERROR ("Target `replace': The `%s' option requires exactly two string "
102         "arguments.", ci->key);
103     return (-1);
104   }
105
106   act = calloc (1, sizeof (*act));
107   if (act == NULL)
108   {
109     ERROR ("tr_config_add_action: calloc failed.");
110     return (-ENOMEM);
111   }
112
113   act->replacement = NULL;
114   act->may_be_empty = may_be_empty;
115
116   status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
117   if (status != 0)
118   {
119     char errbuf[1024] = "";
120
121     /* regerror assures null termination. */
122     regerror (status, &act->re, errbuf, sizeof (errbuf));
123     ERROR ("Target `replace': Compiling the regular expression `%s' "
124         "failed: %s.",
125         ci->values[0].value.string, errbuf);
126     sfree (act);
127     return (-EINVAL);
128   }
129
130   act->replacement = tr_strdup (ci->values[1].value.string);
131   if (act->replacement == NULL)
132   {
133     ERROR ("tr_config_add_action: tr_strdup failed.");
134     regfree (&act->re);
135     sfree (act);
136     return (-ENOMEM);
137   }
138
139   /* Insert action at end of list. */
140   if (*dest == NULL)
141     *dest = act;
142   else
143   {
144     tr_action_t *prev;
145
146     prev = *dest;
147     while (prev->next != NULL)
148       prev = prev->next;
149
150     prev->next = act;
151   }
152
153   return (0);
154 } /* }}} int tr_config_add_action */
155
156 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
157     char *buffer_in, size_t buffer_in_size, int may_be_empty)
158 {
159   tr_action_t *act;
160   int status;
161   char buffer[DATA_MAX_NAME_LEN];
162   regmatch_t matches[8] = { [0] = { 0 } };
163
164   if (act_head == NULL)
165     return (-EINVAL);
166
167   sstrncpy (buffer, buffer_in, sizeof (buffer));
168
169   DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
170
171   for (act = act_head; act != NULL; act = act->next)
172   {
173     char temp[DATA_MAX_NAME_LEN];
174     char *subst_status;
175
176     status = regexec (&act->re, buffer,
177         STATIC_ARRAY_SIZE (matches), matches,
178         /* flags = */ 0);
179     if (status == REG_NOMATCH)
180       continue;
181     else if (status != 0)
182     {
183       char errbuf[1024] = "";
184
185       regerror (status, &act->re, errbuf, sizeof (errbuf));
186       ERROR ("Target `replace': Executing a regular expression failed: %s.",
187           errbuf);
188       continue;
189     }
190
191     subst_status = subst (temp, sizeof (temp), buffer,
192         (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, act->replacement);
193     if (subst_status == NULL)
194     {
195       ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
196           "replacement = %s) failed.",
197           buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
198           act->replacement);
199       continue;
200     }
201     sstrncpy (buffer, temp, sizeof (buffer));
202
203     DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
204   } /* for (act = act_head; act != NULL; act = act->next) */
205
206   if ((may_be_empty == 0) && (buffer[0] == 0))
207   {
208     WARNING ("Target `replace': Replacement resulted in an empty string, "
209         "which is not allowed for this buffer (`host' or `plugin').");
210     return (0);
211   }
212
213   DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
214   sstrncpy (buffer_in, buffer, buffer_in_size);
215
216   return (0);
217 } /* }}} int tr_action_invoke */
218
219 static int tr_destroy (void **user_data) /* {{{ */
220 {
221   tr_data_t *data;
222
223   if (user_data == NULL)
224     return (-EINVAL);
225
226   data = *user_data;
227   if (data == NULL)
228     return (0);
229
230   tr_action_destroy (data->host);
231   tr_action_destroy (data->plugin);
232   tr_action_destroy (data->plugin_instance);
233   /* tr_action_destroy (data->type); */
234   tr_action_destroy (data->type_instance);
235   sfree (data);
236
237   return (0);
238 } /* }}} int tr_destroy */
239
240 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
241 {
242   tr_data_t *data;
243   int status;
244   int i;
245
246   data = calloc (1, sizeof (*data));
247   if (data == NULL)
248   {
249     ERROR ("tr_create: calloc failed.");
250     return (-ENOMEM);
251   }
252
253   data->host = NULL;
254   data->plugin = NULL;
255   data->plugin_instance = NULL;
256   /* data->type = NULL; */
257   data->type_instance = NULL;
258
259   status = 0;
260   for (i = 0; i < ci->children_num; i++)
261   {
262     oconfig_item_t *child = ci->children + i;
263
264     if ((strcasecmp ("Host", child->key) == 0)
265         || (strcasecmp ("Hostname", child->key) == 0))
266       status = tr_config_add_action (&data->host, child,
267           /* may be empty = */ 0);
268     else if (strcasecmp ("Plugin", child->key) == 0)
269       status = tr_config_add_action (&data->plugin, child,
270           /* may be empty = */ 0);
271     else if (strcasecmp ("PluginInstance", child->key) == 0)
272       status = tr_config_add_action (&data->plugin_instance, child,
273           /* may be empty = */ 1);
274 #if 0
275     else if (strcasecmp ("Type", child->key) == 0)
276       status = tr_config_add_action (&data->type, child,
277           /* may be empty = */ 0);
278 #endif
279     else if (strcasecmp ("TypeInstance", child->key) == 0)
280       status = tr_config_add_action (&data->type_instance, child,
281           /* may be empty = */ 1);
282     else
283     {
284       ERROR ("Target `replace': The `%s' configuration option is not understood "
285           "and will be ignored.", child->key);
286       status = 0;
287     }
288
289     if (status != 0)
290       break;
291   }
292
293   /* Additional sanity-checking */
294   while (status == 0)
295   {
296     if ((data->host == NULL)
297         && (data->plugin == NULL)
298         && (data->plugin_instance == NULL)
299         /* && (data->type == NULL) */
300         && (data->type_instance == NULL))
301     {
302       ERROR ("Target `replace': You need to set at least one of `Host', "
303           "`Plugin', `PluginInstance' or `TypeInstance'.");
304       status = -1;
305     }
306
307     break;
308   }
309
310   if (status != 0)
311   {
312     tr_destroy ((void *) &data);
313     return (status);
314   }
315
316   *user_data = data;
317   return (0);
318 } /* }}} int tr_create */
319
320 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
321     notification_meta_t __attribute__((unused)) **meta, void **user_data)
322 {
323   tr_data_t *data;
324
325   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
326     return (-EINVAL);
327
328   data = *user_data;
329   if (data == NULL)
330   {
331     ERROR ("Target `replace': Invoke: `data' is NULL.");
332     return (-EINVAL);
333   }
334
335 #define HANDLE_FIELD(f,e) \
336   if (data->f != NULL) \
337     tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
338   HANDLE_FIELD (host, 0);
339   HANDLE_FIELD (plugin, 0);
340   HANDLE_FIELD (plugin_instance, 1);
341   /* HANDLE_FIELD (type); */
342   HANDLE_FIELD (type_instance, 1);
343
344   return (FC_TARGET_CONTINUE);
345 } /* }}} int tr_invoke */
346
347 void module_register (void)
348 {
349         target_proc_t tproc = { 0 };
350
351         tproc.create  = tr_create;
352         tproc.destroy = tr_destroy;
353         tproc.invoke  = tr_invoke;
354         fc_register_target ("replace", tproc);
355 } /* module_register */
356
357 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
358