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"
28 #include "common.h"
29 #include "filter_chain.h"
30 #include "utils_subst.h"
32 #include <regex.h>
34 struct tr_action_s;
35 typedef struct tr_action_s tr_action_t;
36 struct tr_action_s
37 {
38 regex_t re;
39 char *replacement;
40 int may_be_empty;
42 tr_action_t *next;
43 };
45 struct tr_data_s
46 {
47 tr_action_t *host;
48 tr_action_t *plugin;
49 tr_action_t *plugin_instance;
50 /* tr_action_t *type; */
51 tr_action_t *type_instance;
52 };
53 typedef struct tr_data_s tr_data_t;
55 static char *tr_strdup (const char *orig) /* {{{ */
56 {
57 size_t sz;
58 char *dest;
60 if (orig == NULL)
61 return (NULL);
63 sz = strlen (orig) + 1;
64 dest = (char *) malloc (sz);
65 if (dest == NULL)
66 return (NULL);
68 memcpy (dest, orig, sz);
70 return (dest);
71 } /* }}} char *tr_strdup */
73 static void tr_action_destroy (tr_action_t *act) /* {{{ */
74 {
75 if (act == NULL)
76 return;
78 regfree (&act->re);
79 sfree (act->replacement);
81 if (act->next != NULL)
82 tr_action_destroy (act->next);
84 sfree (act);
85 } /* }}} void tr_action_destroy */
87 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
88 const oconfig_item_t *ci, int may_be_empty)
89 {
90 tr_action_t *act;
91 int status;
93 if (dest == NULL)
94 return (-EINVAL);
96 if ((ci->values_num != 2)
97 || (ci->values[0].type != OCONFIG_TYPE_STRING)
98 || (ci->values[1].type != OCONFIG_TYPE_STRING))
99 {
100 ERROR ("Target `replace': The `%s' option requires exactly two string "
101 "arguments.", ci->key);
102 return (-1);
103 }
105 act = (tr_action_t *) malloc (sizeof (*act));
106 if (act == NULL)
107 {
108 ERROR ("tr_config_add_action: malloc failed.");
109 return (-ENOMEM);
110 }
111 memset (act, 0, sizeof (*act));
113 act->replacement = NULL;
114 act->may_be_empty = may_be_empty;
116 status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
117 if (status != 0)
118 {
119 char errbuf[1024] = "";
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 }
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 }
139 /* Insert action at end of list. */
140 if (*dest == NULL)
141 *dest = act;
142 else
143 {
144 tr_action_t *prev;
146 prev = *dest;
147 while (prev->next != NULL)
148 prev = prev->next;
150 prev->next = act;
151 }
153 return (0);
154 } /* }}} int tr_config_add_action */
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];
164 if (act_head == NULL)
165 return (-EINVAL);
167 sstrncpy (buffer, buffer_in, sizeof (buffer));
168 memset (matches, 0, sizeof (matches));
170 DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
172 for (act = act_head; act != NULL; act = act->next)
173 {
174 char temp[DATA_MAX_NAME_LEN];
175 char *subst_status;
177 status = regexec (&act->re, buffer,
178 STATIC_ARRAY_SIZE (matches), matches,
179 /* flags = */ 0);
180 if (status == REG_NOMATCH)
181 continue;
182 else if (status != 0)
183 {
184 char errbuf[1024] = "";
186 regerror (status, &act->re, errbuf, sizeof (errbuf));
187 ERROR ("Target `replace': Executing a regular expression failed: %s.",
188 errbuf);
189 continue;
190 }
192 subst_status = subst (temp, sizeof (temp), buffer,
193 (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, act->replacement);
194 if (subst_status == NULL)
195 {
196 ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
197 "replacement = %s) failed.",
198 buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
199 act->replacement);
200 continue;
201 }
202 sstrncpy (buffer, temp, sizeof (buffer));
204 DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
205 } /* for (act = act_head; act != NULL; act = act->next) */
207 if ((may_be_empty == 0) && (buffer[0] == 0))
208 {
209 WARNING ("Target `replace': Replacement resulted in an empty string, "
210 "which is not allowed for this buffer (`host' or `plugin').");
211 return (0);
212 }
214 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
215 sstrncpy (buffer_in, buffer, buffer_in_size);
217 return (0);
218 } /* }}} int tr_action_invoke */
220 static int tr_destroy (void **user_data) /* {{{ */
221 {
222 tr_data_t *data;
224 if (user_data == NULL)
225 return (-EINVAL);
227 data = *user_data;
228 if (data == NULL)
229 return (0);
231 tr_action_destroy (data->host);
232 tr_action_destroy (data->plugin);
233 tr_action_destroy (data->plugin_instance);
234 /* tr_action_destroy (data->type); */
235 tr_action_destroy (data->type_instance);
236 sfree (data);
238 return (0);
239 } /* }}} int tr_destroy */
241 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
242 {
243 tr_data_t *data;
244 int status;
245 int i;
247 data = (tr_data_t *) malloc (sizeof (*data));
248 if (data == NULL)
249 {
250 ERROR ("tr_create: malloc failed.");
251 return (-ENOMEM);
252 }
253 memset (data, 0, sizeof (*data));
255 data->host = NULL;
256 data->plugin = NULL;
257 data->plugin_instance = NULL;
258 /* data->type = NULL; */
259 data->type_instance = NULL;
261 status = 0;
262 for (i = 0; i < ci->children_num; i++)
263 {
264 oconfig_item_t *child = ci->children + i;
266 if ((strcasecmp ("Host", child->key) == 0)
267 || (strcasecmp ("Hostname", child->key) == 0))
268 status = tr_config_add_action (&data->host, child,
269 /* may be empty = */ 0);
270 else if (strcasecmp ("Plugin", child->key) == 0)
271 status = tr_config_add_action (&data->plugin, child,
272 /* may be empty = */ 0);
273 else if (strcasecmp ("PluginInstance", child->key) == 0)
274 status = tr_config_add_action (&data->plugin_instance, child,
275 /* may be empty = */ 1);
276 #if 0
277 else if (strcasecmp ("Type", child->key) == 0)
278 status = tr_config_add_action (&data->type, child,
279 /* may be empty = */ 0);
280 #endif
281 else if (strcasecmp ("TypeInstance", child->key) == 0)
282 status = tr_config_add_action (&data->type_instance, child,
283 /* may be empty = */ 1);
284 else
285 {
286 ERROR ("Target `replace': The `%s' configuration option is not understood "
287 "and will be ignored.", child->key);
288 status = 0;
289 }
291 if (status != 0)
292 break;
293 }
295 /* Additional sanity-checking */
296 while (status == 0)
297 {
298 if ((data->host == NULL)
299 && (data->plugin == NULL)
300 && (data->plugin_instance == NULL)
301 /* && (data->type == NULL) */
302 && (data->type_instance == NULL))
303 {
304 ERROR ("Target `replace': You need to set at least one of `Host', "
305 "`Plugin', `PluginInstance' or `TypeInstance'.");
306 status = -1;
307 }
309 break;
310 }
312 if (status != 0)
313 {
314 tr_destroy ((void *) &data);
315 return (status);
316 }
318 *user_data = data;
319 return (0);
320 } /* }}} int tr_create */
322 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
323 notification_meta_t __attribute__((unused)) **meta, void **user_data)
324 {
325 tr_data_t *data;
327 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
328 return (-EINVAL);
330 data = *user_data;
331 if (data == NULL)
332 {
333 ERROR ("Target `replace': Invoke: `data' is NULL.");
334 return (-EINVAL);
335 }
337 #define HANDLE_FIELD(f,e) \
338 if (data->f != NULL) \
339 tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
340 HANDLE_FIELD (host, 0);
341 HANDLE_FIELD (plugin, 0);
342 HANDLE_FIELD (plugin_instance, 1);
343 /* HANDLE_FIELD (type); */
344 HANDLE_FIELD (type_instance, 1);
346 return (FC_TARGET_CONTINUE);
347 } /* }}} int tr_invoke */
349 void module_register (void)
350 {
351 target_proc_t tproc;
353 memset (&tproc, 0, sizeof (tproc));
354 tproc.create = tr_create;
355 tproc.destroy = tr_destroy;
356 tproc.invoke = tr_invoke;
357 fc_register_target ("replace", tproc);
358 } /* module_register */
360 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */