1 /**
2 * collectd - src/target_set.c
3 * Copyright (C) 2008 Florian Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "filter_chain.h"
32 struct ts_data_s {
33 char *host;
34 char *plugin;
35 char *plugin_instance;
36 /* char *type; */
37 char *type_instance;
38 meta_data_t *meta;
39 };
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,
43 char **ret_key,
44 char **ret_string) /* {{{ */
45 {
46 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
47 (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.",
50 ci->key);
51 return (-1);
52 }
54 *ret_key = ci->values[0].value.string;
55 *ret_string = ci->values[1].value.string;
57 return (0);
58 } /* }}} int ts_util_get_key_and_string_wo_strdup */
60 static int ts_config_add_string(char **dest, /* {{{ */
61 const oconfig_item_t *ci, int may_be_empty) {
62 char *tmp = NULL;
63 int status;
65 status = cf_util_get_string(ci, &tmp);
66 if (status != 0)
67 return (status);
69 if (!may_be_empty && (strlen(tmp) == 0)) {
70 ERROR("Target `set': The `%s' option does not accept empty strings.",
71 ci->key);
72 sfree(tmp);
73 return (-1);
74 }
76 *dest = tmp;
77 return (0);
78 } /* }}} int ts_config_add_string */
80 static int ts_config_add_meta(meta_data_t **dest, /* {{{ */
81 const oconfig_item_t *ci, int may_be_empty) {
82 char *key = NULL;
83 char *string = NULL;
84 int status;
86 status = ts_util_get_key_and_string_wo_strdup(ci, &key, &string);
87 if (status != 0)
88 return (status);
90 if (strlen(key) == 0) {
91 ERROR("Target `set': The `%s' option does not accept empty string as first "
92 "argument.",
93 ci->key);
94 return (-1);
95 }
97 if (!may_be_empty && (strlen(string) == 0)) {
98 ERROR("Target `set': The `%s' option does not accept empty string as "
99 "second argument.",
100 ci->key);
101 return (-1);
102 }
104 if ((*dest) == NULL) {
105 // Create a new meta_data_t
106 if ((*dest = meta_data_create()) == NULL) {
107 ERROR("Target `set': failed to create a meta data for `%s'.", ci->key);
108 return (-1);
109 }
110 }
112 return (meta_data_add_string(*dest, key, string));
113 } /* }}} int ts_config_add_meta */
115 static int ts_destroy(void **user_data) /* {{{ */
116 {
117 ts_data_t *data;
119 if (user_data == NULL)
120 return (-EINVAL);
122 data = *user_data;
123 if (data == NULL)
124 return (0);
126 free(data->host);
127 free(data->plugin);
128 free(data->plugin_instance);
129 /* free (data->type); */
130 free(data->type_instance);
131 meta_data_destroy(data->meta);
132 free(data);
134 return (0);
135 } /* }}} int ts_destroy */
137 static int ts_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
138 {
139 ts_data_t *data;
140 int status;
142 data = calloc(1, sizeof(*data));
143 if (data == NULL) {
144 ERROR("ts_create: calloc failed.");
145 return (-ENOMEM);
146 }
148 data->host = NULL;
149 data->plugin = NULL;
150 data->plugin_instance = NULL;
151 /* data->type = NULL; */
152 data->type_instance = NULL;
153 data->meta = NULL;
155 status = 0;
156 for (int i = 0; i < ci->children_num; i++) {
157 oconfig_item_t *child = ci->children + i;
159 if ((strcasecmp("Host", child->key) == 0) ||
160 (strcasecmp("Hostname", child->key) == 0))
161 status = ts_config_add_string(&data->host, child,
162 /* may be empty = */ 0);
163 else if (strcasecmp("Plugin", child->key) == 0)
164 status = ts_config_add_string(&data->plugin, child,
165 /* may be empty = */ 0);
166 else if (strcasecmp("PluginInstance", child->key) == 0)
167 status = ts_config_add_string(&data->plugin_instance, child,
168 /* may be empty = */ 1);
169 #if 0
170 else if (strcasecmp ("Type", child->key) == 0)
171 status = ts_config_add_string (&data->type, child,
172 /* may be empty = */ 0);
173 #endif
174 else if (strcasecmp("TypeInstance", child->key) == 0)
175 status = ts_config_add_string(&data->type_instance, child,
176 /* may be empty = */ 1);
177 else if (strcasecmp("MetaData", child->key) == 0)
178 status = ts_config_add_meta(&data->meta, child,
179 /* may be empty = */ 1);
180 else {
181 ERROR("Target `set': The `%s' configuration option is not understood "
182 "and will be ignored.",
183 child->key);
184 status = 0;
185 }
187 if (status != 0)
188 break;
189 }
191 /* Additional sanity-checking */
192 while (status == 0) {
193 if ((data->host == NULL) && (data->plugin == NULL) &&
194 (data->plugin_instance == NULL)
195 /* && (data->type == NULL) */
196 && (data->type_instance == NULL) && (data->meta == NULL)) {
197 ERROR("Target `set': You need to set at least one of `Host', "
198 "`Plugin', `PluginInstance', `TypeInstance', `MetaData'.");
199 status = -1;
200 }
202 break;
203 }
205 if (status != 0) {
206 ts_destroy((void *)&data);
207 return (status);
208 }
210 *user_data = data;
211 return (0);
212 } /* }}} int ts_create */
214 static int ts_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
215 notification_meta_t __attribute__((unused)) * *meta,
216 void **user_data) {
217 ts_data_t *data;
219 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
220 return (-EINVAL);
222 data = *user_data;
223 if (data == NULL) {
224 ERROR("Target `set': Invoke: `data' is NULL.");
225 return (-EINVAL);
226 }
228 if (data->meta != NULL) {
229 meta_data_clone_merge(&(vl->meta), data->meta);
230 }
232 #define SET_FIELD(f) \
233 if (data->f != NULL) { \
234 sstrncpy(vl->f, data->f, sizeof(vl->f)); \
235 }
236 SET_FIELD(host);
237 SET_FIELD(plugin);
238 SET_FIELD(plugin_instance);
239 /* SET_FIELD (type); */
240 SET_FIELD(type_instance);
242 return (FC_TARGET_CONTINUE);
243 } /* }}} int ts_invoke */
245 void module_register(void) {
246 target_proc_t tproc = {0};
248 tproc.create = ts_create;
249 tproc.destroy = ts_destroy;
250 tproc.invoke = ts_invoke;
251 fc_register_target("set", tproc);
252 } /* module_register */
254 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */