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"
30 #include "utils/common/common.h"
31 #include "utils/metadata/meta_data.h"
32 #include "utils_subst.h"
34 struct ts_key_list_s {
36 struct ts_key_list_s *next;
38 typedef struct ts_key_list_s ts_key_list_t;
40 static void ts_key_list_free(ts_key_list_t *l) /* {{{ */
48 ts_key_list_free(l->next);
51 } /* }}} 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,
66 char **ret_string) /* {{{ */
68 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
69 (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.",
76 *ret_key = ci->values[0].value.string;
77 *ret_string = ci->values[1].value.string;
80 } /* }}} int ts_util_get_key_and_string_wo_strdup */
82 static int ts_config_add_string(char **dest, /* {{{ */
83 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)) {
92 ERROR("Target `set': The `%s' option does not accept empty strings.",
100 } /* }}} int ts_config_add_string */
102 static int ts_config_add_meta(meta_data_t **dest, /* {{{ */
103 const oconfig_item_t *ci, int may_be_empty) {
108 status = ts_util_get_key_and_string_wo_strdup(ci, &key, &string);
112 if (strlen(key) == 0) {
113 ERROR("Target `set': The `%s' option does not accept empty string as "
119 if (!may_be_empty && (strlen(string) == 0)) {
120 ERROR("Target `set': The `%s' option does not accept empty string as "
126 if ((*dest) == NULL) {
127 /* Create a new meta_data_t */
128 if ((*dest = meta_data_create()) == NULL) {
129 ERROR("Target `set': failed to create a meta data for `%s'.", ci->key);
134 return meta_data_add_string(*dest, key, string);
135 } /* }}} int ts_config_add_meta */
137 static int ts_config_add_meta_delete(ts_key_list_t **dest, /* {{{ */
138 const oconfig_item_t *ci) {
139 ts_key_list_t *entry = NULL;
141 entry = calloc(1, sizeof(*entry));
143 ERROR("ts_config_add_meta_delete: calloc failed.");
147 if (cf_util_get_string(ci, &entry->key) != 0) {
148 ts_key_list_free(entry);
149 return -1; /* An error has already been reported. */
152 if (strlen(entry->key) == 0) {
153 ERROR("Target `set': The `%s' option does not accept empty string as "
156 ts_key_list_free(entry);
164 } /* }}} int ts_config_add_meta_delete */
166 static void ts_subst(char *dest, size_t size, const char *string, /* {{{ */
167 const value_list_t *vl) {
168 char temp[DATA_MAX_NAME_LEN];
170 /* Initialize the field with the template. */
171 sstrncpy(dest, string, size);
173 if (strchr(dest, '%') == NULL)
176 #define REPLACE_FIELD(t, v) \
177 if (subst_string(temp, sizeof(temp), dest, t, v) != NULL) \
178 sstrncpy(dest, temp, size);
179 REPLACE_FIELD("%{host}", vl->host);
180 REPLACE_FIELD("%{plugin}", vl->plugin);
181 REPLACE_FIELD("%{plugin_instance}", vl->plugin_instance);
182 REPLACE_FIELD("%{type}", vl->type);
183 REPLACE_FIELD("%{type_instance}", vl->type_instance);
185 if (vl->meta != NULL) {
186 char **meta_toc = NULL;
187 int status = meta_data_toc(vl->meta, &meta_toc);
190 size_t meta_entries = (size_t)status;
192 for (size_t i = 0; i < meta_entries; i++) {
193 char meta_name[DATA_MAX_NAME_LEN];
195 const char *key = meta_toc[i];
197 snprintf(meta_name, sizeof(meta_name), "%%{meta:%s}", key);
198 if (meta_data_as_string(vl->meta, key, &value_str) != 0)
201 REPLACE_FIELD(meta_name, value_str);
205 strarray_free(meta_toc, (size_t)meta_entries);
207 } /* }}} int ts_subst */
209 static int ts_destroy(void **user_data) /* {{{ */
213 if (user_data == NULL)
222 free(data->plugin_instance);
223 /* free (data->type); */
224 free(data->type_instance);
225 meta_data_destroy(data->meta);
226 ts_key_list_free(data->meta_delete);
230 } /* }}} int ts_destroy */
232 static int ts_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
237 data = calloc(1, sizeof(*data));
239 ERROR("ts_create: calloc failed.");
245 data->plugin_instance = NULL;
246 /* data->type = NULL; */
247 data->type_instance = NULL;
249 data->meta_delete = NULL;
252 for (int i = 0; i < ci->children_num; i++) {
253 oconfig_item_t *child = ci->children + i;
255 if ((strcasecmp("Host", child->key) == 0) ||
256 (strcasecmp("Hostname", child->key) == 0))
257 status = ts_config_add_string(&data->host, child,
258 /* may be empty = */ 0);
259 else if (strcasecmp("Plugin", child->key) == 0)
260 status = ts_config_add_string(&data->plugin, child,
261 /* may be empty = */ 0);
262 else if (strcasecmp("PluginInstance", child->key) == 0)
263 status = ts_config_add_string(&data->plugin_instance, child,
264 /* may be empty = */ 1);
266 else if (strcasecmp ("Type", child->key) == 0)
267 status = ts_config_add_string (&data->type, child,
268 /* may be empty = */ 0);
270 else if (strcasecmp("TypeInstance", child->key) == 0)
271 status = ts_config_add_string(&data->type_instance, child,
272 /* may be empty = */ 1);
273 else if (strcasecmp("MetaData", child->key) == 0)
274 status = ts_config_add_meta(&data->meta, child,
275 /* may be empty = */ 1);
276 else if (strcasecmp("DeleteMetaData", child->key) == 0)
277 status = ts_config_add_meta_delete(&data->meta_delete, child);
279 ERROR("Target `set': The `%s' configuration option is not understood "
280 "and will be ignored.",
289 /* Additional sanity-checking */
290 while (status == 0) {
291 if ((data->host == NULL) && (data->plugin == NULL) &&
292 (data->plugin_instance == NULL)
293 /* && (data->type == NULL) */
294 && (data->type_instance == NULL) && (data->meta == NULL) &&
295 (data->meta_delete == NULL)) {
296 ERROR("Target `set': You need to set at least one of `Host', "
297 "`Plugin', `PluginInstance', `TypeInstance', "
298 "`MetaData', or `DeleteMetaData'.");
302 if (data->meta != NULL) {
303 /* If data->meta_delete is NULL, this loop is a no-op. */
304 for (ts_key_list_t *l = data->meta_delete; l != NULL; l = l->next) {
305 if (meta_data_type(data->meta, l->key) != 0) {
306 /* MetaData and DeleteMetaData for the same key. */
307 ERROR("Target `set': Can only have one of `MetaData' or "
308 "`DeleteMetaData' for any given key.");
318 ts_destroy((void *)&data);
324 } /* }}} int ts_create */
326 static int ts_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
327 notification_meta_t __attribute__((unused)) * *meta,
331 meta_data_t *new_meta = NULL;
333 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
338 ERROR("Target `set': Invoke: `data' is NULL.");
344 if (data->meta != NULL) {
345 char temp[DATA_MAX_NAME_LEN * 2];
348 if ((new_meta = meta_data_create()) == NULL) {
349 ERROR("Target `set': failed to create replacement metadata.");
353 int status = meta_data_toc(data->meta, &meta_toc);
355 ERROR("Target `set': meta_data_toc failed with status %d.", status);
356 meta_data_destroy(new_meta);
359 size_t meta_entries = (size_t)status;
361 for (size_t i = 0; i < meta_entries; i++) {
362 const char *key = meta_toc[i];
366 status = meta_data_get_string(data->meta, key, &string);
368 ERROR("Target `set': Unable to get replacement metadata value `%s'.",
370 strarray_free(meta_toc, meta_entries);
371 meta_data_destroy(new_meta);
375 ts_subst(temp, sizeof(temp), string, &orig);
377 DEBUG("target_set: ts_invoke: setting metadata value for key `%s': "
383 status = meta_data_add_string(new_meta, key, temp);
385 ERROR("Target `set': Unable to set metadata value `%s'.", key);
386 strarray_free(meta_toc, meta_entries);
387 meta_data_destroy(new_meta);
392 strarray_free(meta_toc, meta_entries);
395 #define SUBST_FIELD(f) \
396 if (data->f != NULL) { \
397 ts_subst(vl->f, sizeof(vl->f), data->f, &orig); \
398 DEBUG("target_set: ts_invoke: setting " #f ": `%s'.", vl->f); \
402 SUBST_FIELD(plugin_instance);
403 /* SUBST_FIELD (type); */
404 SUBST_FIELD(type_instance);
406 /* Need to merge the metadata in now, because of the shallow copy. */
407 if (new_meta != NULL) {
408 meta_data_clone_merge(&(vl->meta), new_meta);
409 meta_data_destroy(new_meta);
412 /* If data->meta_delete is NULL, this loop is a no-op. */
413 for (ts_key_list_t *l = data->meta_delete; l != NULL; l = l->next) {
414 DEBUG("target_set: ts_invoke: deleting metadata value for key `%s'.",
416 meta_data_delete(vl->meta, l->key);
419 return FC_TARGET_CONTINUE;
420 } /* }}} int ts_invoke */
422 void module_register(void) {
423 target_proc_t tproc = {0};
425 tproc.create = ts_create;
426 tproc.destroy = ts_destroy;
427 tproc.invoke = ts_invoke;
428 fc_register_target("set", tproc);
429 } /* module_register */