95f03173eede11176983907205f9c18587568513
1 /**
2 * collectd - src/target_set.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"
29 #include "common.h"
30 #include "filter_chain.h"
31 #include "meta_data.h"
32 #include "utils_subst.h"
34 struct ts_key_list_s
35 {
36 char *key;
37 struct ts_key_list_s *next;
38 };
39 typedef struct ts_key_list_s ts_key_list_t;
41 static void ts_key_list_free (ts_key_list_t *l) /* {{{ */
42 {
43 if (l == NULL)
44 return;
46 free (l->key);
48 if (l->next != NULL)
49 ts_key_list_free (l->next);
51 free (l);
52 } /* }}} void ts_name_list_free */
54 struct ts_data_s
55 {
56 char *host;
57 char *plugin;
58 char *plugin_instance;
59 /* char *type; */
60 char *type_instance;
61 meta_data_t *meta;
62 ts_key_list_t *meta_delete;
63 };
64 typedef struct ts_data_s ts_data_t;
66 static int ts_util_get_key_and_string_wo_strdup (const oconfig_item_t *ci, char **ret_key, char **ret_string) /* {{{ */
67 {
68 if ((ci->values_num != 2)
69 || (ci->values[0].type != OCONFIG_TYPE_STRING)
70 || (ci->values[1].type != OCONFIG_TYPE_STRING))
71 {
72 ERROR ("ts_util_get_key_and_string_wo_strdup: The %s option requires "
73 "exactly two string arguments.", ci->key);
74 return (-1);
75 }
77 *ret_key = ci->values[0].value.string;
78 *ret_string = ci->values[1].value.string;
80 return (0);
81 } /* }}} int ts_util_get_key_and_string_wo_strdup */
83 static int ts_config_add_string (char **dest, /* {{{ */
84 const oconfig_item_t *ci, int may_be_empty)
85 {
86 char *tmp = NULL;
87 int status;
89 status = cf_util_get_string (ci, &tmp);
90 if (status != 0)
91 return (status);
93 if (!may_be_empty && (strlen (tmp) == 0))
94 {
95 ERROR ("Target `set': The `%s' option does not accept empty strings.",
96 ci->key);
97 sfree (tmp);
98 return (-1);
99 }
101 *dest = tmp;
102 return (0);
103 } /* }}} int ts_config_add_string */
105 static int ts_config_add_meta (meta_data_t **dest, /* {{{ */
106 const oconfig_item_t *ci, int may_be_empty)
107 {
108 char *key = NULL;
109 char *string = NULL;
110 int status;
112 status = ts_util_get_key_and_string_wo_strdup (ci, &key, &string);
113 if (status != 0)
114 return (status);
116 if (strlen (key) == 0)
117 {
118 ERROR ("Target `set': The `%s' option does not accept empty string as "
119 "first argument.", ci->key);
120 return (-1);
121 }
123 if (!may_be_empty && (strlen (string) == 0))
124 {
125 ERROR ("Target `set': The `%s' option does not accept empty string as "
126 "second argument.", ci->key);
127 return (-1);
128 }
130 if ((*dest) == NULL)
131 {
132 /* Create a new meta_data_t */
133 if ((*dest = meta_data_create()) == NULL)
134 {
135 ERROR ("Target `set': failed to create a meta data for `%s'.", ci->key);
136 return (-ENOMEM);
137 }
138 }
140 return (meta_data_add_string (*dest, key, string));
141 } /* }}} int ts_config_add_meta */
143 static int ts_config_add_meta_delete (ts_key_list_t **dest, /* {{{ */
144 const oconfig_item_t *ci)
145 {
146 ts_key_list_t *entry = NULL;
148 entry = calloc (1, sizeof (*entry));
149 if (entry == NULL)
150 {
151 ERROR ("ts_config_add_meta_delete: calloc failed.");
152 return (-ENOMEM);
153 }
155 if (cf_util_get_string (ci, &entry->key) != 0)
156 return (-1); /* An error has already been reported. */
158 if (strlen (entry->key) == 0)
159 {
160 ERROR ("Target `set': The `%s' option does not accept empty string as "
161 "first argument.", ci->key);
162 sfree (entry->key);
163 return (-1);
164 }
166 entry->next = *dest;
167 *dest = entry;
169 return (0);
170 } /* }}} int ts_config_add_meta_delete */
172 static void ts_subst (char *dest, size_t size, const char *string, /* {{{ */
173 const value_list_t *vl)
174 {
175 char temp[DATA_MAX_NAME_LEN];
177 /* Initialize the field with the template. */
178 sstrncpy (dest, string, size);
180 if (strchr (dest, '%') == NULL)
181 return;
183 #define REPLACE_FIELD(t, v) \
184 if (subst_string (temp, sizeof (temp), dest, t, v) != NULL) \
185 sstrncpy (dest, temp, size);
186 REPLACE_FIELD ("%{host}", vl->host);
187 REPLACE_FIELD ("%{plugin}", vl->plugin);
188 REPLACE_FIELD ("%{plugin_instance}", vl->plugin_instance);
189 REPLACE_FIELD ("%{type}", vl->type);
190 REPLACE_FIELD ("%{type_instance}", vl->type_instance);
192 if (vl->meta != NULL)
193 {
194 char **meta_toc;
195 int meta_entries = meta_data_toc (vl->meta, &meta_toc);
196 for (int i = 0; i < meta_entries; i++)
197 {
198 char meta_name[DATA_MAX_NAME_LEN];
199 char *value_str;
200 const char *key = meta_toc[i];
202 ssnprintf (meta_name, sizeof (meta_name), "%%{meta:%s}", key);
203 if (meta_data_as_string (vl->meta, key, &value_str) != 0)
204 continue;
206 REPLACE_FIELD (meta_name, value_str);
207 sfree (value_str);
208 }
210 strarray_free (meta_toc, (size_t) meta_entries);
211 }
212 } /* }}} int ts_subst */
214 static int ts_destroy (void **user_data) /* {{{ */
215 {
216 ts_data_t *data;
218 if (user_data == NULL)
219 return (-EINVAL);
221 data = *user_data;
222 if (data == NULL)
223 return (0);
225 free (data->host);
226 free (data->plugin);
227 free (data->plugin_instance);
228 /* free (data->type); */
229 free (data->type_instance);
230 meta_data_destroy(data->meta);
231 ts_key_list_free (data->meta_delete);
232 free (data);
234 return (0);
235 } /* }}} int ts_destroy */
237 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
238 {
239 ts_data_t *data;
240 int status;
242 data = calloc (1, sizeof (*data));
243 if (data == NULL)
244 {
245 ERROR ("ts_create: calloc failed.");
246 return (-ENOMEM);
247 }
249 data->host = NULL;
250 data->plugin = NULL;
251 data->plugin_instance = NULL;
252 /* data->type = NULL; */
253 data->type_instance = NULL;
254 data->meta = NULL;
255 data->meta_delete = NULL;
257 status = 0;
258 for (int i = 0; i < ci->children_num; i++)
259 {
260 oconfig_item_t *child = ci->children + i;
262 if ((strcasecmp ("Host", child->key) == 0)
263 || (strcasecmp ("Hostname", child->key) == 0))
264 status = ts_config_add_string (&data->host, child,
265 /* may be empty = */ 0);
266 else if (strcasecmp ("Plugin", child->key) == 0)
267 status = ts_config_add_string (&data->plugin, child,
268 /* may be empty = */ 0);
269 else if (strcasecmp ("PluginInstance", child->key) == 0)
270 status = ts_config_add_string (&data->plugin_instance, child,
271 /* may be empty = */ 1);
272 #if 0
273 else if (strcasecmp ("Type", child->key) == 0)
274 status = ts_config_add_string (&data->type, child,
275 /* may be empty = */ 0);
276 #endif
277 else if (strcasecmp ("TypeInstance", child->key) == 0)
278 status = ts_config_add_string (&data->type_instance, child,
279 /* may be empty = */ 1);
280 else if (strcasecmp ("MetaData", child->key) == 0)
281 status = ts_config_add_meta (&data->meta, child,
282 /* may be empty = */ 1);
283 else if (strcasecmp ("DeleteMetaData", child->key) == 0)
284 status = ts_config_add_meta_delete (&data->meta_delete, child);
285 else
286 {
287 ERROR ("Target `set': The `%s' configuration option is not understood "
288 "and will be ignored.", child->key);
289 status = 0;
290 }
292 if (status != 0)
293 break;
294 }
296 /* Additional sanity-checking */
297 while (status == 0)
298 {
299 if ((data->host == NULL)
300 && (data->plugin == NULL)
301 && (data->plugin_instance == NULL)
302 /* && (data->type == NULL) */
303 && (data->type_instance == NULL)
304 && (data->meta == NULL)
305 && (data->meta_delete == NULL))
306 {
307 ERROR ("Target `set': You need to set at least one of `Host', "
308 "`Plugin', `PluginInstance', `TypeInstance', "
309 "`MetaData', or `DeleteMetaData'.");
310 status = -1;
311 }
313 if (data->meta != NULL)
314 {
315 /* If data->meta_delete is NULL, this loop is a no-op. */
316 for (ts_key_list_t *l=data->meta_delete; l != NULL; l = l->next)
317 {
318 if (meta_data_type (data->meta, l->key) != 0)
319 {
320 /* MetaData and DeleteMetaData for the same key. */
321 ERROR ("Target `set': Can only have one of `MetaData' or "
322 "`DeleteMetaData' for any given key.");
323 status = -1;
324 }
325 }
326 }
328 break;
329 }
331 if (status != 0)
332 {
333 ts_destroy ((void *) &data);
334 return (status);
335 }
337 *user_data = data;
338 return (0);
339 } /* }}} int ts_create */
341 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
342 notification_meta_t __attribute__((unused)) **meta, void **user_data)
343 {
344 ts_data_t *data;
345 value_list_t orig;
346 meta_data_t *new_meta = NULL;
348 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
349 return (-EINVAL);
351 data = *user_data;
352 if (data == NULL)
353 {
354 ERROR ("Target `set': Invoke: `data' is NULL.");
355 return (-EINVAL);
356 }
358 orig = *vl;
360 if (data->meta != NULL)
361 {
362 char temp[DATA_MAX_NAME_LEN*2];
363 int meta_entries;
364 char **meta_toc;
366 if ((new_meta = meta_data_create()) == NULL)
367 {
368 ERROR ("Target `set': failed to create replacement metadata.");
369 return (-ENOMEM);
370 }
372 meta_entries = meta_data_toc (data->meta, &meta_toc);
373 for (int i = 0; i < meta_entries; i++)
374 {
375 const char *key = meta_toc[i];
376 char *string;
377 int status;
379 status = meta_data_get_string (data->meta, key, &string);
380 if (status)
381 {
382 ERROR ("Target `set': Unable to get replacement metadata value `%s'.",
383 key);
384 strarray_free (meta_toc, (size_t) meta_entries);
385 return (status);
386 }
388 ts_subst (temp, sizeof (temp), string, &orig);
390 DEBUG ("target_set: ts_invoke: setting metadata value for key `%s': "
391 "`%s'.", key, temp);
393 status = meta_data_add_string (new_meta, key, temp);
395 if (status)
396 {
397 ERROR ("Target `set': Unable to set metadata value `%s'.", key);
398 strarray_free (meta_toc, (size_t) meta_entries);
399 return (status);
400 }
401 }
403 strarray_free (meta_toc, (size_t) meta_entries);
404 }
406 #define SUBST_FIELD(f) \
407 if (data->f != NULL) { \
408 ts_subst (vl->f, sizeof (vl->f), data->f, &orig); \
409 DEBUG ("target_set: ts_invoke: setting "#f": `%s'.", vl->f); \
410 }
411 SUBST_FIELD (host);
412 SUBST_FIELD (plugin);
413 SUBST_FIELD (plugin_instance);
414 /* SUBST_FIELD (type); */
415 SUBST_FIELD (type_instance);
417 /* Need to merge the metadata in now, because of the shallow copy. */
418 if (new_meta != NULL)
419 {
420 meta_data_clone_merge(&(vl->meta), new_meta);
421 meta_data_destroy(new_meta);
422 }
424 /* If data->meta_delete is NULL, this loop is a no-op. */
425 for (ts_key_list_t *l=data->meta_delete; l != NULL; l = l->next)
426 {
427 DEBUG ("target_set: ts_invoke: deleting metadata value for key `%s'.",
428 l->key);
429 meta_data_delete(vl->meta, l->key);
430 }
432 return (FC_TARGET_CONTINUE);
433 } /* }}} int ts_invoke */
435 void module_register (void)
436 {
437 target_proc_t tproc = { 0 };
439 tproc.create = ts_create;
440 tproc.destroy = ts_destroy;
441 tproc.invoke = ts_invoke;
442 fc_register_target ("set", tproc);
443 } /* module_register */
445 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */