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>
29 #include "filter_chain.h"
35 char *plugin_instance;
40 typedef struct ts_data_s ts_data_t;
42 static int ts_util_get_key_and_string_wo_strdup (const oconfig_item_t *ci, char **ret_key, char **ret_string) /* {{{ */
44 if ((ci->values_num != 2)
45 || (ci->values[0].type != OCONFIG_TYPE_STRING)
46 || (ci->values[1].type != OCONFIG_TYPE_STRING))
48 ERROR ("ts_util_get_key_and_string_wo_strdup: The %s option requires "
49 "exactly two string argument.", ci->key);
53 *ret_key = ci->values[0].value.string;
54 *ret_string = ci->values[1].value.string;
57 } /* }}} int ts_util_get_key_and_string_wo_strdup */
59 static int ts_config_add_string (char **dest, /* {{{ */
60 const oconfig_item_t *ci, int may_be_empty)
65 status = cf_util_get_string (ci, &tmp);
69 if (!may_be_empty && (strlen (tmp) == 0))
71 ERROR ("Target `set': The `%s' option does not accept empty strings.",
79 } /* }}} int ts_config_add_string */
81 static int ts_config_add_meta (meta_data_t **dest, /* {{{ */
82 const oconfig_item_t *ci, int may_be_empty)
88 status = ts_util_get_key_and_string_wo_strdup (ci, &key, &string);
92 if (strlen (key) == 0)
94 ERROR ("Target `set': The `%s' option does not accept empty string as first argument.",
99 if (!may_be_empty && (strlen (string) == 0))
101 ERROR ("Target `set': The `%s' option does not accept empty string as second argument.",
108 // Create a new meta_data_t
109 if ((*dest = meta_data_create()) == NULL)
111 ERROR ("Target `set': failed to create a meta data for `%s'.", ci->key);
116 return (meta_data_add_string (*dest, key, string));
117 } /* }}} int ts_config_add_meta */
119 static int ts_destroy (void **user_data) /* {{{ */
123 if (user_data == NULL)
132 free (data->plugin_instance);
133 /* free (data->type); */
134 free (data->type_instance);
135 meta_data_destroy(data->meta);
139 } /* }}} int ts_destroy */
141 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
147 data = calloc (1, sizeof (*data));
150 ERROR ("ts_create: calloc failed.");
156 data->plugin_instance = NULL;
157 /* data->type = NULL; */
158 data->type_instance = NULL;
162 for (i = 0; i < ci->children_num; i++)
164 oconfig_item_t *child = ci->children + i;
166 if ((strcasecmp ("Host", child->key) == 0)
167 || (strcasecmp ("Hostname", child->key) == 0))
168 status = ts_config_add_string (&data->host, child,
169 /* may be empty = */ 0);
170 else if (strcasecmp ("Plugin", child->key) == 0)
171 status = ts_config_add_string (&data->plugin, child,
172 /* may be empty = */ 0);
173 else if (strcasecmp ("PluginInstance", child->key) == 0)
174 status = ts_config_add_string (&data->plugin_instance, child,
175 /* may be empty = */ 1);
177 else if (strcasecmp ("Type", child->key) == 0)
178 status = ts_config_add_string (&data->type, child,
179 /* may be empty = */ 0);
181 else if (strcasecmp ("TypeInstance", child->key) == 0)
182 status = ts_config_add_string (&data->type_instance, child,
183 /* may be empty = */ 1);
184 else if (strcasecmp ("MetaDataSet", child->key) == 0)
185 status = ts_config_add_meta (&data->meta, child,
186 /* may be empty = */ 1);
189 ERROR ("Target `set': The `%s' configuration option is not understood "
190 "and will be ignored.", child->key);
198 /* Additional sanity-checking */
201 if ((data->host == NULL)
202 && (data->plugin == NULL)
203 && (data->plugin_instance == NULL)
204 /* && (data->type == NULL) */
205 && (data->type_instance == NULL)
206 && (data->meta == NULL))
208 ERROR ("Target `set': You need to set at least one of `Host', "
209 "`Plugin', `PluginInstance', `TypeInstance', "
210 "`MetaDataSet' or `MetaDataEval'.");
219 ts_destroy ((void *) &data);
225 } /* }}} int ts_create */
227 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
228 notification_meta_t __attribute__((unused)) **meta, void **user_data)
232 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
238 ERROR ("Target `set': Invoke: `data' is NULL.");
242 if (data->meta != NULL)
244 meta_data_clone_merge(&(vl->meta), data->meta);
247 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
250 SET_FIELD (plugin_instance);
251 /* SET_FIELD (type); */
252 SET_FIELD (type_instance);
254 return (FC_TARGET_CONTINUE);
255 } /* }}} int ts_invoke */
257 void module_register (void)
261 memset (&tproc, 0, sizeof (tproc));
262 tproc.create = ts_create;
263 tproc.destroy = ts_destroy;
264 tproc.invoke = ts_invoke;
265 fc_register_target ("set", tproc);
266 } /* module_register */
268 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */