2 * collectd - src/target_set.c
3 * Copyright (C) 2008 Florian Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian Forster <octo at collectd.org>
30 #include "filter_chain.h"
31 #include "meta_data.h"
32 #include "utils_subst.h"
37 struct ts_key_list_s *next;
39 typedef struct ts_key_list_s ts_key_list_t;
41 static void ts_key_list_free (ts_key_list_t *l) /* {{{ */
49 ts_key_list_free (l->next);
52 } /* }}} void ts_name_list_free */
58 char *plugin_instance;
62 ts_key_list_t *meta_delete;
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) /* {{{ */
68 if ((ci->values_num != 2)
69 || (ci->values[0].type != OCONFIG_TYPE_STRING)
70 || (ci->values[1].type != OCONFIG_TYPE_STRING))
72 ERROR ("ts_util_get_key_and_string_wo_strdup: The %s option requires "
73 "exactly two string arguments.", ci->key);
77 *ret_key = ci->values[0].value.string;
78 *ret_string = ci->values[1].value.string;
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)
89 status = cf_util_get_string (ci, &tmp);
93 if (!may_be_empty && (strlen (tmp) == 0))
95 ERROR ("Target `set': The `%s' option does not accept empty strings.",
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)
112 status = ts_util_get_key_and_string_wo_strdup (ci, &key, &string);
116 if (strlen (key) == 0)
118 ERROR ("Target `set': The `%s' option does not accept empty string as "
119 "first argument.", ci->key);
123 if (!may_be_empty && (strlen (string) == 0))
125 ERROR ("Target `set': The `%s' option does not accept empty string as "
126 "second argument.", ci->key);
132 /* Create a new meta_data_t */
133 if ((*dest = meta_data_create()) == NULL)
135 ERROR ("Target `set': failed to create a meta data for `%s'.", ci->key);
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)
146 ts_key_list_t *entry = NULL;
148 entry = calloc (1, sizeof (*entry));
151 ERROR ("ts_config_add_meta_delete: calloc failed.");
155 if (cf_util_get_string (ci, &entry->key) != 0)
157 ts_key_list_free (entry);
158 return (-1); /* An error has already been reported. */
161 if (strlen (entry->key) == 0)
163 ERROR ("Target `set': The `%s' option does not accept empty string as "
164 "first argument.", ci->key);
165 ts_key_list_free (entry);
173 } /* }}} int ts_config_add_meta_delete */
175 static void ts_subst (char *dest, size_t size, const char *string, /* {{{ */
176 const value_list_t *vl)
178 char temp[DATA_MAX_NAME_LEN];
180 /* Initialize the field with the template. */
181 sstrncpy (dest, string, size);
183 if (strchr (dest, '%') == NULL)
186 #define REPLACE_FIELD(t, v) \
187 if (subst_string (temp, sizeof (temp), dest, t, v) != NULL) \
188 sstrncpy (dest, temp, size);
189 REPLACE_FIELD ("%{host}", vl->host);
190 REPLACE_FIELD ("%{plugin}", vl->plugin);
191 REPLACE_FIELD ("%{plugin_instance}", vl->plugin_instance);
192 REPLACE_FIELD ("%{type}", vl->type);
193 REPLACE_FIELD ("%{type_instance}", vl->type_instance);
195 if (vl->meta != NULL)
198 int meta_entries = meta_data_toc (vl->meta, &meta_toc);
199 for (int i = 0; i < meta_entries; i++)
201 char meta_name[DATA_MAX_NAME_LEN];
203 const char *key = meta_toc[i];
205 ssnprintf (meta_name, sizeof (meta_name), "%%{meta:%s}", key);
206 if (meta_data_as_string (vl->meta, key, &value_str) != 0)
209 REPLACE_FIELD (meta_name, value_str);
213 strarray_free (meta_toc, (size_t) meta_entries);
215 } /* }}} int ts_subst */
217 static int ts_destroy (void **user_data) /* {{{ */
221 if (user_data == NULL)
230 free (data->plugin_instance);
231 /* free (data->type); */
232 free (data->type_instance);
233 meta_data_destroy(data->meta);
234 ts_key_list_free (data->meta_delete);
238 } /* }}} int ts_destroy */
240 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
245 data = calloc (1, sizeof (*data));
248 ERROR ("ts_create: calloc failed.");
254 data->plugin_instance = NULL;
255 /* data->type = NULL; */
256 data->type_instance = NULL;
258 data->meta_delete = NULL;
261 for (int i = 0; i < ci->children_num; i++)
263 oconfig_item_t *child = ci->children + i;
265 if ((strcasecmp ("Host", child->key) == 0)
266 || (strcasecmp ("Hostname", child->key) == 0))
267 status = ts_config_add_string (&data->host, child,
268 /* may be empty = */ 0);
269 else if (strcasecmp ("Plugin", child->key) == 0)
270 status = ts_config_add_string (&data->plugin, child,
271 /* may be empty = */ 0);
272 else if (strcasecmp ("PluginInstance", child->key) == 0)
273 status = ts_config_add_string (&data->plugin_instance, child,
274 /* may be empty = */ 1);
276 else if (strcasecmp ("Type", child->key) == 0)
277 status = ts_config_add_string (&data->type, child,
278 /* may be empty = */ 0);
280 else if (strcasecmp ("TypeInstance", child->key) == 0)
281 status = ts_config_add_string (&data->type_instance, child,
282 /* may be empty = */ 1);
283 else if (strcasecmp ("MetaData", child->key) == 0)
284 status = ts_config_add_meta (&data->meta, child,
285 /* may be empty = */ 1);
286 else if (strcasecmp ("DeleteMetaData", child->key) == 0)
287 status = ts_config_add_meta_delete (&data->meta_delete, child);
290 ERROR ("Target `set': The `%s' configuration option is not understood "
291 "and will be ignored.", child->key);
299 /* Additional sanity-checking */
302 if ((data->host == NULL)
303 && (data->plugin == NULL)
304 && (data->plugin_instance == NULL)
305 /* && (data->type == NULL) */
306 && (data->type_instance == NULL)
307 && (data->meta == NULL)
308 && (data->meta_delete == NULL))
310 ERROR ("Target `set': You need to set at least one of `Host', "
311 "`Plugin', `PluginInstance', `TypeInstance', "
312 "`MetaData', or `DeleteMetaData'.");
316 if (data->meta != NULL)
318 /* If data->meta_delete is NULL, this loop is a no-op. */
319 for (ts_key_list_t *l=data->meta_delete; l != NULL; l = l->next)
321 if (meta_data_type (data->meta, l->key) != 0)
323 /* MetaData and DeleteMetaData for the same key. */
324 ERROR ("Target `set': Can only have one of `MetaData' or "
325 "`DeleteMetaData' for any given key.");
336 ts_destroy ((void *) &data);
342 } /* }}} int ts_create */
344 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
345 notification_meta_t __attribute__((unused)) **meta, void **user_data)
349 meta_data_t *new_meta = NULL;
351 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
357 ERROR ("Target `set': Invoke: `data' is NULL.");
363 if (data->meta != NULL)
365 char temp[DATA_MAX_NAME_LEN*2];
369 if ((new_meta = meta_data_create()) == NULL)
371 ERROR ("Target `set': failed to create replacement metadata.");
375 meta_entries = meta_data_toc (data->meta, &meta_toc);
376 for (int i = 0; i < meta_entries; i++)
378 const char *key = meta_toc[i];
382 status = meta_data_get_string (data->meta, key, &string);
385 ERROR ("Target `set': Unable to get replacement metadata value `%s'.",
387 strarray_free (meta_toc, (size_t) meta_entries);
391 ts_subst (temp, sizeof (temp), string, &orig);
393 DEBUG ("target_set: ts_invoke: setting metadata value for key `%s': "
396 status = meta_data_add_string (new_meta, key, temp);
400 ERROR ("Target `set': Unable to set metadata value `%s'.", key);
401 strarray_free (meta_toc, (size_t) meta_entries);
406 strarray_free (meta_toc, (size_t) meta_entries);
409 #define SUBST_FIELD(f) \
410 if (data->f != NULL) { \
411 ts_subst (vl->f, sizeof (vl->f), data->f, &orig); \
412 DEBUG ("target_set: ts_invoke: setting "#f": `%s'.", vl->f); \
415 SUBST_FIELD (plugin);
416 SUBST_FIELD (plugin_instance);
417 /* SUBST_FIELD (type); */
418 SUBST_FIELD (type_instance);
420 /* Need to merge the metadata in now, because of the shallow copy. */
421 if (new_meta != NULL)
423 meta_data_clone_merge(&(vl->meta), new_meta);
424 meta_data_destroy(new_meta);
427 /* If data->meta_delete is NULL, this loop is a no-op. */
428 for (ts_key_list_t *l=data->meta_delete; l != NULL; l = l->next)
430 DEBUG ("target_set: ts_invoke: deleting metadata value for key `%s'.",
432 meta_data_delete(vl->meta, l->key);
435 return (FC_TARGET_CONTINUE);
436 } /* }}} int ts_invoke */
438 void module_register (void)
440 target_proc_t tproc = { 0 };
442 tproc.create = ts_create;
443 tproc.destroy = ts_destroy;
444 tproc.invoke = ts_invoke;
445 fc_register_target ("set", tproc);
446 } /* module_register */
448 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */