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"
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 meta_entries = meta_data_toc(vl->meta, &meta_toc);
188 if (meta_entries <= 0)
191 for (int i = 0; i < meta_entries; i++) {
192 char meta_name[DATA_MAX_NAME_LEN];
194 const char *key = meta_toc[i];
196 ssnprintf(meta_name, sizeof(meta_name), "%%{meta:%s}", key);
197 if (meta_data_as_string(vl->meta, key, &value_str) != 0)
200 REPLACE_FIELD(meta_name, value_str);
204 strarray_free(meta_toc, (size_t)meta_entries);
206 } /* }}} int ts_subst */
208 static int ts_destroy(void **user_data) /* {{{ */
212 if (user_data == NULL)
221 free(data->plugin_instance);
222 /* free (data->type); */
223 free(data->type_instance);
224 meta_data_destroy(data->meta);
225 ts_key_list_free(data->meta_delete);
229 } /* }}} int ts_destroy */
231 static int ts_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
236 data = calloc(1, sizeof(*data));
238 ERROR("ts_create: calloc failed.");
244 data->plugin_instance = NULL;
245 /* data->type = NULL; */
246 data->type_instance = NULL;
248 data->meta_delete = NULL;
251 for (int i = 0; i < ci->children_num; i++) {
252 oconfig_item_t *child = ci->children + i;
254 if ((strcasecmp("Host", child->key) == 0) ||
255 (strcasecmp("Hostname", child->key) == 0))
256 status = ts_config_add_string(&data->host, child,
257 /* may be empty = */ 0);
258 else if (strcasecmp("Plugin", child->key) == 0)
259 status = ts_config_add_string(&data->plugin, child,
260 /* may be empty = */ 0);
261 else if (strcasecmp("PluginInstance", child->key) == 0)
262 status = ts_config_add_string(&data->plugin_instance, child,
263 /* may be empty = */ 1);
265 else if (strcasecmp ("Type", child->key) == 0)
266 status = ts_config_add_string (&data->type, child,
267 /* may be empty = */ 0);
269 else if (strcasecmp("TypeInstance", child->key) == 0)
270 status = ts_config_add_string(&data->type_instance, child,
271 /* may be empty = */ 1);
272 else if (strcasecmp("MetaData", child->key) == 0)
273 status = ts_config_add_meta(&data->meta, child,
274 /* may be empty = */ 1);
275 else if (strcasecmp("DeleteMetaData", child->key) == 0)
276 status = ts_config_add_meta_delete(&data->meta_delete, child);
278 ERROR("Target `set': The `%s' configuration option is not understood "
279 "and will be ignored.",
288 /* Additional sanity-checking */
289 while (status == 0) {
290 if ((data->host == NULL) && (data->plugin == NULL) &&
291 (data->plugin_instance == NULL)
292 /* && (data->type == NULL) */
293 && (data->type_instance == NULL) && (data->meta == NULL) &&
294 (data->meta_delete == NULL)) {
295 ERROR("Target `set': You need to set at least one of `Host', "
296 "`Plugin', `PluginInstance', `TypeInstance', "
297 "`MetaData', or `DeleteMetaData'.");
301 if (data->meta != NULL) {
302 /* If data->meta_delete is NULL, this loop is a no-op. */
303 for (ts_key_list_t *l = data->meta_delete; l != NULL; l = l->next) {
304 if (meta_data_type(data->meta, l->key) != 0) {
305 /* MetaData and DeleteMetaData for the same key. */
306 ERROR("Target `set': Can only have one of `MetaData' or "
307 "`DeleteMetaData' for any given key.");
317 ts_destroy((void *)&data);
323 } /* }}} int ts_create */
325 static int ts_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
326 notification_meta_t __attribute__((unused)) * *meta,
330 meta_data_t *new_meta = NULL;
332 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
337 ERROR("Target `set': Invoke: `data' is NULL.");
343 if (data->meta != NULL) {
344 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 meta_entries = meta_data_toc(data->meta, &meta_toc);
354 for (int i = 0; i < meta_entries; i++) {
355 const char *key = meta_toc[i];
359 status = meta_data_get_string(data->meta, key, &string);
361 ERROR("Target `set': Unable to get replacement metadata value `%s'.",
363 strarray_free(meta_toc, (size_t)meta_entries);
364 meta_data_destroy(new_meta);
368 ts_subst(temp, sizeof(temp), string, &orig);
370 DEBUG("target_set: ts_invoke: setting metadata value for key `%s': "
376 status = meta_data_add_string(new_meta, key, temp);
378 ERROR("Target `set': Unable to set metadata value `%s'.", key);
379 strarray_free(meta_toc, (size_t)meta_entries);
380 meta_data_destroy(new_meta);
385 strarray_free(meta_toc, (size_t)meta_entries);
388 #define SUBST_FIELD(f) \
389 if (data->f != NULL) { \
390 ts_subst(vl->f, sizeof(vl->f), data->f, &orig); \
391 DEBUG("target_set: ts_invoke: setting " #f ": `%s'.", vl->f); \
395 SUBST_FIELD(plugin_instance);
396 /* SUBST_FIELD (type); */
397 SUBST_FIELD(type_instance);
399 /* Need to merge the metadata in now, because of the shallow copy. */
400 if (new_meta != NULL) {
401 meta_data_clone_merge(&(vl->meta), new_meta);
402 meta_data_destroy(new_meta);
405 /* If data->meta_delete is NULL, this loop is a no-op. */
406 for (ts_key_list_t *l = data->meta_delete; l != NULL; l = l->next) {
407 DEBUG("target_set: ts_invoke: deleting metadata value for key `%s'.",
409 meta_data_delete(vl->meta, l->key);
412 return (FC_TARGET_CONTINUE);
413 } /* }}} int ts_invoke */
415 void module_register(void) {
416 target_proc_t tproc = {0};
418 tproc.create = ts_create;
419 tproc.destroy = ts_destroy;
420 tproc.invoke = ts_invoke;
421 fc_register_target("set", tproc);
422 } /* module_register */