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) {
187 int meta_entries = meta_data_toc(vl->meta, &meta_toc);
188 for (int i = 0; i < meta_entries; i++) {
189 char meta_name[DATA_MAX_NAME_LEN];
191 const char *key = meta_toc[i];
193 ssnprintf(meta_name, sizeof(meta_name), "%%{meta:%s}", key);
194 if (meta_data_as_string(vl->meta, key, &value_str) != 0)
197 REPLACE_FIELD(meta_name, value_str);
201 strarray_free(meta_toc, (size_t)meta_entries);
203 } /* }}} int ts_subst */
205 static int ts_destroy(void **user_data) /* {{{ */
209 if (user_data == NULL)
218 free(data->plugin_instance);
219 /* free (data->type); */
220 free(data->type_instance);
221 meta_data_destroy(data->meta);
222 ts_key_list_free(data->meta_delete);
226 } /* }}} int ts_destroy */
228 static int ts_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
233 data = calloc(1, sizeof(*data));
235 ERROR("ts_create: calloc failed.");
241 data->plugin_instance = NULL;
242 /* data->type = NULL; */
243 data->type_instance = NULL;
245 data->meta_delete = NULL;
248 for (int i = 0; i < ci->children_num; i++) {
249 oconfig_item_t *child = ci->children + i;
251 if ((strcasecmp("Host", child->key) == 0) ||
252 (strcasecmp("Hostname", child->key) == 0))
253 status = ts_config_add_string(&data->host, child,
254 /* may be empty = */ 0);
255 else if (strcasecmp("Plugin", child->key) == 0)
256 status = ts_config_add_string(&data->plugin, child,
257 /* may be empty = */ 0);
258 else if (strcasecmp("PluginInstance", child->key) == 0)
259 status = ts_config_add_string(&data->plugin_instance, child,
260 /* may be empty = */ 1);
262 else if (strcasecmp ("Type", child->key) == 0)
263 status = ts_config_add_string (&data->type, child,
264 /* may be empty = */ 0);
266 else if (strcasecmp("TypeInstance", child->key) == 0)
267 status = ts_config_add_string(&data->type_instance, child,
268 /* may be empty = */ 1);
269 else if (strcasecmp("MetaData", child->key) == 0)
270 status = ts_config_add_meta(&data->meta, child,
271 /* may be empty = */ 1);
272 else if (strcasecmp("DeleteMetaData", child->key) == 0)
273 status = ts_config_add_meta_delete(&data->meta_delete, child);
275 ERROR("Target `set': The `%s' configuration option is not understood "
276 "and will be ignored.",
285 /* Additional sanity-checking */
286 while (status == 0) {
287 if ((data->host == NULL) && (data->plugin == NULL) &&
288 (data->plugin_instance == NULL)
289 /* && (data->type == NULL) */
290 && (data->type_instance == NULL) && (data->meta == NULL) &&
291 (data->meta_delete == NULL)) {
292 ERROR("Target `set': You need to set at least one of `Host', "
293 "`Plugin', `PluginInstance', `TypeInstance', "
294 "`MetaData', or `DeleteMetaData'.");
298 if (data->meta != NULL) {
299 /* If data->meta_delete is NULL, this loop is a no-op. */
300 for (ts_key_list_t *l = data->meta_delete; l != NULL; l = l->next) {
301 if (meta_data_type(data->meta, l->key) != 0) {
302 /* MetaData and DeleteMetaData for the same key. */
303 ERROR("Target `set': Can only have one of `MetaData' or "
304 "`DeleteMetaData' for any given key.");
314 ts_destroy((void *)&data);
320 } /* }}} int ts_create */
322 static int ts_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
323 notification_meta_t __attribute__((unused)) * *meta,
327 meta_data_t *new_meta = NULL;
329 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
334 ERROR("Target `set': Invoke: `data' is NULL.");
340 if (data->meta != NULL) {
341 char temp[DATA_MAX_NAME_LEN * 2];
345 if ((new_meta = meta_data_create()) == NULL) {
346 ERROR("Target `set': failed to create replacement metadata.");
350 meta_entries = meta_data_toc(data->meta, &meta_toc);
351 for (int i = 0; i < meta_entries; i++) {
352 const char *key = meta_toc[i];
356 status = meta_data_get_string(data->meta, key, &string);
358 ERROR("Target `set': Unable to get replacement metadata value `%s'.",
360 strarray_free(meta_toc, (size_t)meta_entries);
364 ts_subst(temp, sizeof(temp), string, &orig);
366 DEBUG("target_set: ts_invoke: setting metadata value for key `%s': "
372 status = meta_data_add_string(new_meta, key, temp);
374 ERROR("Target `set': Unable to set metadata value `%s'.", key);
375 strarray_free(meta_toc, (size_t)meta_entries);
380 strarray_free(meta_toc, (size_t)meta_entries);
383 #define SUBST_FIELD(f) \
384 if (data->f != NULL) { \
385 ts_subst(vl->f, sizeof(vl->f), data->f, &orig); \
386 DEBUG("target_set: ts_invoke: setting " #f ": `%s'.", vl->f); \
390 SUBST_FIELD(plugin_instance);
391 /* SUBST_FIELD (type); */
392 SUBST_FIELD(type_instance);
394 /* Need to merge the metadata in now, because of the shallow copy. */
395 if (new_meta != NULL) {
396 meta_data_clone_merge(&(vl->meta), new_meta);
397 meta_data_destroy(new_meta);
400 /* If data->meta_delete is NULL, this loop is a no-op. */
401 for (ts_key_list_t *l = data->meta_delete; l != NULL; l = l->next) {
402 DEBUG("target_set: ts_invoke: deleting metadata value for key `%s'.",
404 meta_data_delete(vl->meta, l->key);
407 return (FC_TARGET_CONTINUE);
408 } /* }}} int ts_invoke */
410 void module_register(void) {
411 target_proc_t tproc = {0};
413 tproc.create = ts_create;
414 tproc.destroy = ts_destroy;
415 tproc.invoke = ts_invoke;
416 fc_register_target("set", tproc);
417 } /* module_register */