1 /**
2 * collectd - src/target_replace.c
3 * Copyright (C) 2008 Florian Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "filter_chain.h"
25 #include "utils_subst.h"
27 #include <regex.h>
29 struct tr_action_s;
30 typedef struct tr_action_s tr_action_t;
31 struct tr_action_s
32 {
33 regex_t re;
34 char *replacement;
35 int may_be_empty;
37 tr_action_t *next;
38 };
40 struct tr_data_s
41 {
42 tr_action_t *host;
43 tr_action_t *plugin;
44 tr_action_t *plugin_instance;
45 /* tr_action_t *type; */
46 tr_action_t *type_instance;
47 };
48 typedef struct tr_data_s tr_data_t;
50 static char *tr_strdup (const char *orig) /* {{{ */
51 {
52 size_t sz;
53 char *dest;
55 if (orig == NULL)
56 return (NULL);
58 sz = strlen (orig) + 1;
59 dest = (char *) malloc (sz);
60 if (dest == NULL)
61 return (NULL);
63 memcpy (dest, orig, sz);
65 return (dest);
66 } /* }}} char *tr_strdup */
68 static void tr_action_destroy (tr_action_t *act) /* {{{ */
69 {
70 if (act == NULL)
71 return;
73 regfree (&act->re);
74 sfree (act->replacement);
76 if (act->next != NULL)
77 tr_action_destroy (act->next);
79 sfree (act);
80 } /* }}} void tr_action_destroy */
82 static int tr_config_add_action (tr_action_t **dest, /* {{{ */
83 const oconfig_item_t *ci, int may_be_empty)
84 {
85 tr_action_t *act;
86 int status;
88 if (dest == NULL)
89 return (-EINVAL);
91 if ((ci->values_num != 2)
92 || (ci->values[0].type != OCONFIG_TYPE_STRING)
93 || (ci->values[1].type != OCONFIG_TYPE_STRING))
94 {
95 ERROR ("Target `replace': The `%s' option requires exactly two string "
96 "arguments.", ci->key);
97 return (-1);
98 }
100 act = (tr_action_t *) malloc (sizeof (*act));
101 if (act == NULL)
102 {
103 ERROR ("tr_config_add_action: malloc failed.");
104 return (-ENOMEM);
105 }
106 memset (act, 0, sizeof (*act));
108 act->replacement = NULL;
109 act->may_be_empty = may_be_empty;
111 status = regcomp (&act->re, ci->values[0].value.string, REG_EXTENDED);
112 if (status != 0)
113 {
114 char errbuf[1024] = "";
116 /* regerror assures null termination. */
117 regerror (status, &act->re, errbuf, sizeof (errbuf));
118 ERROR ("Target `replace': Compiling the regular expression `%s' "
119 "failed: %s.",
120 ci->values[0].value.string, errbuf);
121 sfree (act);
122 return (-EINVAL);
123 }
125 act->replacement = tr_strdup (ci->values[1].value.string);
126 if (act->replacement == NULL)
127 {
128 ERROR ("tr_config_add_action: tr_strdup failed.");
129 regfree (&act->re);
130 sfree (act);
131 return (-ENOMEM);
132 }
134 /* Insert action at end of list. */
135 if (*dest == NULL)
136 *dest = act;
137 else
138 {
139 tr_action_t *prev;
141 prev = *dest;
142 while (prev->next != NULL)
143 prev = prev->next;
145 prev->next = act;
146 }
148 return (0);
149 } /* }}} int tr_config_add_action */
151 static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
152 char *buffer_in, size_t buffer_in_size, int may_be_empty)
153 {
154 tr_action_t *act;
155 int status;
156 char buffer[DATA_MAX_NAME_LEN];
157 regmatch_t matches[8];
159 if (act_head == NULL)
160 return (-EINVAL);
162 sstrncpy (buffer, buffer_in, sizeof (buffer));
163 memset (matches, 0, sizeof (matches));
165 DEBUG ("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
167 for (act = act_head; act != NULL; act = act->next)
168 {
169 char temp[DATA_MAX_NAME_LEN];
170 char *subst_status;
172 status = regexec (&act->re, buffer,
173 STATIC_ARRAY_SIZE (matches), matches,
174 /* flags = */ 0);
175 if (status == REG_NOMATCH)
176 continue;
177 else if (status != 0)
178 {
179 char errbuf[1024] = "";
181 regerror (status, &act->re, errbuf, sizeof (errbuf));
182 ERROR ("Target `replace': Executing a regular expression failed: %s.",
183 errbuf);
184 continue;
185 }
187 subst_status = subst (temp, sizeof (temp), buffer,
188 matches[0].rm_so, matches[0].rm_eo, act->replacement);
189 if (subst_status == NULL)
190 {
191 ERROR ("Target `replace': subst (buffer = %s, start = %zu, end = %zu, "
192 "replacement = %s) failed.",
193 buffer, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
194 act->replacement);
195 continue;
196 }
197 sstrncpy (buffer, temp, sizeof (buffer));
199 DEBUG ("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
200 } /* for (act = act_head; act != NULL; act = act->next) */
202 if ((may_be_empty == 0) && (buffer[0] == 0))
203 {
204 WARNING ("Target `replace': Replacement resulted in an empty string, "
205 "which is not allowed for this buffer (`host' or `plugin').");
206 return (0);
207 }
209 DEBUG ("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
210 sstrncpy (buffer_in, buffer, buffer_in_size);
212 return (0);
213 } /* }}} int tr_action_invoke */
215 static int tr_destroy (void **user_data) /* {{{ */
216 {
217 tr_data_t *data;
219 if (user_data == NULL)
220 return (-EINVAL);
222 data = *user_data;
223 if (data == NULL)
224 return (0);
226 tr_action_destroy (data->host);
227 tr_action_destroy (data->plugin);
228 tr_action_destroy (data->plugin_instance);
229 /* tr_action_destroy (data->type); */
230 tr_action_destroy (data->type_instance);
231 sfree (data);
233 return (0);
234 } /* }}} int tr_destroy */
236 static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
237 {
238 tr_data_t *data;
239 int status;
240 int i;
242 data = (tr_data_t *) malloc (sizeof (*data));
243 if (data == NULL)
244 {
245 ERROR ("tr_create: malloc failed.");
246 return (-ENOMEM);
247 }
248 memset (data, 0, sizeof (*data));
250 data->host = NULL;
251 data->plugin = NULL;
252 data->plugin_instance = NULL;
253 /* data->type = NULL; */
254 data->type_instance = NULL;
256 status = 0;
257 for (i = 0; i < ci->children_num; i++)
258 {
259 oconfig_item_t *child = ci->children + i;
261 if ((strcasecmp ("Host", child->key) == 0)
262 || (strcasecmp ("Hostname", child->key) == 0))
263 status = tr_config_add_action (&data->host, child,
264 /* may be empty = */ 0);
265 else if (strcasecmp ("Plugin", child->key) == 0)
266 status = tr_config_add_action (&data->plugin, child,
267 /* may be empty = */ 0);
268 else if (strcasecmp ("PluginInstance", child->key) == 0)
269 status = tr_config_add_action (&data->plugin_instance, child,
270 /* may be empty = */ 1);
271 #if 0
272 else if (strcasecmp ("Type", child->key) == 0)
273 status = tr_config_add_action (&data->type, child,
274 /* may be empty = */ 0);
275 #endif
276 else if (strcasecmp ("TypeInstance", child->key) == 0)
277 status = tr_config_add_action (&data->type_instance, child,
278 /* may be empty = */ 1);
279 else
280 {
281 ERROR ("Target `replace': The `%s' configuration option is not understood "
282 "and will be ignored.", child->key);
283 status = 0;
284 }
286 if (status != 0)
287 break;
288 }
290 /* Additional sanity-checking */
291 while (status == 0)
292 {
293 if ((data->host == NULL)
294 && (data->plugin == NULL)
295 && (data->plugin_instance == NULL)
296 /* && (data->type == NULL) */
297 && (data->type_instance == NULL))
298 {
299 ERROR ("Target `replace': You need to set at lease one of `Host', "
300 "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
301 status = -1;
302 }
304 break;
305 }
307 if (status != 0)
308 {
309 tr_destroy ((void *) &data);
310 return (status);
311 }
313 *user_data = data;
314 return (0);
315 } /* }}} int tr_create */
317 static int tr_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
318 notification_meta_t __attribute__((unused)) **meta, void **user_data)
319 {
320 tr_data_t *data;
322 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
323 return (-EINVAL);
325 data = *user_data;
326 if (data == NULL)
327 {
328 ERROR ("Target `replace': Invoke: `data' is NULL.");
329 return (-EINVAL);
330 }
332 #define HANDLE_FIELD(f,e) \
333 if (data->f != NULL) \
334 tr_action_invoke (data->f, vl->f, sizeof (vl->f), e)
335 HANDLE_FIELD (host, 0);
336 HANDLE_FIELD (plugin, 0);
337 HANDLE_FIELD (plugin_instance, 1);
338 /* HANDLE_FIELD (type); */
339 HANDLE_FIELD (type_instance, 1);
341 return (FC_TARGET_CONTINUE);
342 } /* }}} int tr_invoke */
344 void module_register (void)
345 {
346 target_proc_t tproc;
348 memset (&tproc, 0, sizeof (tproc));
349 tproc.create = tr_create;
350 tproc.destroy = tr_destroy;
351 tproc.invoke = tr_invoke;
352 fc_register_target ("replace", tproc);
353 } /* module_register */
355 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */