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"
35 struct ts_key_list_s *next;
37 typedef struct ts_key_list_s ts_key_list_t;
39 static void ts_key_list_free (ts_key_list_t *l) /* {{{ */
47 ts_key_list_free (l->next);
50 } /* }}} void ts_name_list_free */
56 char *plugin_instance;
60 ts_key_list_t *meta_delete;
62 typedef struct ts_data_s ts_data_t;
64 static int ts_util_get_key_and_string_wo_strdup (const oconfig_item_t *ci, char **ret_key, char **ret_string) /* {{{ */
66 if ((ci->values_num != 2)
67 || (ci->values[0].type != OCONFIG_TYPE_STRING)
68 || (ci->values[1].type != OCONFIG_TYPE_STRING))
70 ERROR ("ts_util_get_key_and_string_wo_strdup: The %s option requires "
71 "exactly two string arguments.", ci->key);
75 *ret_key = ci->values[0].value.string;
76 *ret_string = ci->values[1].value.string;
79 } /* }}} int ts_util_get_key_and_string_wo_strdup */
81 static int ts_config_add_string (char **dest, /* {{{ */
82 const oconfig_item_t *ci, int may_be_empty)
87 status = cf_util_get_string (ci, &tmp);
91 if (!may_be_empty && (strlen (tmp) == 0))
93 ERROR ("Target `set': The `%s' option does not accept empty strings.",
101 } /* }}} int ts_config_add_string */
103 static int ts_config_add_meta (meta_data_t **dest, /* {{{ */
104 const oconfig_item_t *ci, int may_be_empty)
110 status = ts_util_get_key_and_string_wo_strdup (ci, &key, &string);
114 if (strlen (key) == 0)
116 ERROR ("Target `set': The `%s' option does not accept empty string as "
117 "first argument.", ci->key);
121 if (!may_be_empty && (strlen (string) == 0))
123 ERROR ("Target `set': The `%s' option does not accept empty string as "
124 "second argument.", ci->key);
130 /* Create a new meta_data_t */
131 if ((*dest = meta_data_create()) == NULL)
133 ERROR ("Target `set': failed to create a meta data for `%s'.", ci->key);
138 return (meta_data_add_string (*dest, key, string));
139 } /* }}} int ts_config_add_meta */
141 static int ts_config_add_meta_delete (ts_key_list_t **dest, /* {{{ */
142 const oconfig_item_t *ci)
144 ts_key_list_t *entry = NULL;
146 if ((ci->values_num != 1)
147 || (ci->values[0].type != OCONFIG_TYPE_STRING))
149 ERROR ("ts_config_add_meta_delete: The %s option requires "
150 "exactly one string argument.", ci->key);
154 if (strlen (ci->values[0].value.string) == 0)
156 ERROR ("Target `set': The `%s' option does not accept empty string as "
157 "first argument.", ci->key);
161 entry = calloc (1, sizeof (*entry));
164 ERROR ("ts_config_add_meta_delete: calloc failed.");
168 entry->key = sstrdup (ci->values[0].value.string);
173 } /* }}} int ts_config_add_meta_delete */
175 static int ts_destroy (void **user_data) /* {{{ */
179 if (user_data == NULL)
188 free (data->plugin_instance);
189 /* free (data->type); */
190 free (data->type_instance);
191 meta_data_destroy(data->meta);
192 ts_key_list_free (data->meta_delete);
196 } /* }}} int ts_destroy */
198 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
203 data = calloc (1, sizeof (*data));
206 ERROR ("ts_create: calloc failed.");
212 data->plugin_instance = NULL;
213 /* data->type = NULL; */
214 data->type_instance = NULL;
216 data->meta_delete = NULL;
219 for (int i = 0; i < ci->children_num; i++)
221 oconfig_item_t *child = ci->children + i;
223 if ((strcasecmp ("Host", child->key) == 0)
224 || (strcasecmp ("Hostname", child->key) == 0))
225 status = ts_config_add_string (&data->host, child,
226 /* may be empty = */ 0);
227 else if (strcasecmp ("Plugin", child->key) == 0)
228 status = ts_config_add_string (&data->plugin, child,
229 /* may be empty = */ 0);
230 else if (strcasecmp ("PluginInstance", child->key) == 0)
231 status = ts_config_add_string (&data->plugin_instance, child,
232 /* may be empty = */ 1);
234 else if (strcasecmp ("Type", child->key) == 0)
235 status = ts_config_add_string (&data->type, child,
236 /* may be empty = */ 0);
238 else if (strcasecmp ("TypeInstance", child->key) == 0)
239 status = ts_config_add_string (&data->type_instance, child,
240 /* may be empty = */ 1);
241 else if (strcasecmp ("MetaData", child->key) == 0)
242 status = ts_config_add_meta (&data->meta, child,
243 /* may be empty = */ 1);
244 else if (strcasecmp ("DeleteMetaData", child->key) == 0)
245 status = ts_config_add_meta_delete (&data->meta_delete, child);
248 ERROR ("Target `set': The `%s' configuration option is not understood "
249 "and will be ignored.", child->key);
257 /* Additional sanity-checking */
260 if ((data->host == NULL)
261 && (data->plugin == NULL)
262 && (data->plugin_instance == NULL)
263 /* && (data->type == NULL) */
264 && (data->type_instance == NULL)
265 && (data->meta == NULL)
266 && (data->meta_delete == NULL))
268 ERROR ("Target `set': You need to set at least one of `Host', "
269 "`Plugin', `PluginInstance', `TypeInstance', "
270 "`MetaData', or `DeleteMetaData'.");
274 if (data->meta != NULL)
276 /* If data->meta_delete is NULL, this loop is a no-op. */
277 for (ts_key_list_t *l=data->meta_delete; l != NULL; l = l->next)
279 if (meta_data_type (data->meta, l->key) != 0)
281 /* MetaData and DeleteMetaData for the same key. */
282 ERROR ("Target `set': Can only have one of `MetaData' or "
283 "`DeleteMetaData' for any given key.");
294 ts_destroy ((void *) &data);
300 } /* }}} int ts_create */
302 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
303 notification_meta_t __attribute__((unused)) **meta, void **user_data)
307 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
313 ERROR ("Target `set': Invoke: `data' is NULL.");
317 if (data->meta != NULL)
319 meta_data_clone_merge(&(vl->meta), data->meta);
322 /* If data->meta_delete is NULL, this loop is a no-op. */
323 for (ts_key_list_t *l=data->meta_delete; l != NULL; l = l->next)
325 meta_data_delete(vl->meta, l->key);
328 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
331 SET_FIELD (plugin_instance);
332 /* SET_FIELD (type); */
333 SET_FIELD (type_instance);
335 return (FC_TARGET_CONTINUE);
336 } /* }}} int ts_invoke */
338 void module_register (void)
340 target_proc_t tproc = { 0 };
342 tproc.create = ts_create;
343 tproc.destroy = ts_destroy;
344 tproc.invoke = ts_invoke;
345 fc_register_target ("set", tproc);
346 } /* module_register */
348 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */