Debian Codegraph @

src/target_set.c

raw
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 **/
26
27 #include "collectd.h"
28
29 #include "filter_chain.h"
30 #include "utils/common/common.h"
31 #include "utils/metadata/meta_data.h"
32 #include "utils_subst.h"
33
34 struct ts_key_list_s {
35 char *key;
36 struct ts_key_list_s *next;
37 };
38 typedef struct ts_key_list_s ts_key_list_t;
39
40 static void ts_key_list_free(ts_key_list_t *l) /* {{{ */
41 {
42 if (l == NULL)
43 return;
44
45 sfree(l->key);
46
47 if (l->next != NULL)
48 ts_key_list_free(l->next);
49
50 sfree(l);
51 } /* }}} void ts_name_list_free */
52
53 struct ts_data_s {
54 char *host;
55 char *plugin;
56 char *plugin_instance;
57 /* char *type; */
58 char *type_instance;
59 meta_data_t *meta;
60 ts_key_list_t *meta_delete;
61 };
62 typedef struct ts_data_s ts_data_t;
63
64 static int ts_util_get_key_and_string_wo_strdup(const oconfig_item_t *ci,
65 char **ret_key,
66 char **ret_string) /* {{{ */
67 {
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.",
72 ci->key);
73 return -1;
74 }
75
76 *ret_key = ci->values[0].value.string;
77 *ret_string = ci->values[1].value.string;
78
79 return 0;
80 } /* }}} int ts_util_get_key_and_string_wo_strdup */
81
82 static int ts_config_add_string(char **dest, /* {{{ */
83 const oconfig_item_t *ci, int may_be_empty) {
84 char *tmp = NULL;
85 int status;
86
87 status = cf_util_get_string(ci, &tmp);
88 if (status != 0)
89 return status;
90
91 if (!may_be_empty && (strlen(tmp) == 0)) {
92 ERROR("Target `set': The `%s' option does not accept empty strings.",
93 ci->key);
94 sfree(tmp);
95 return -1;
96 }
97
98 *dest = tmp;
99 return 0;
100 } /* }}} int ts_config_add_string */
101
102 static int ts_config_add_meta(meta_data_t **dest, /* {{{ */
103 const oconfig_item_t *ci, int may_be_empty) {
104 char *key = NULL;
105 char *string = NULL;
106 int status;
107
108 status = ts_util_get_key_and_string_wo_strdup(ci, &key, &string);
109 if (status != 0)
110 return status;
111
112 if (strlen(key) == 0) {
113 ERROR("Target `set': The `%s' option does not accept empty string as "
114 "first argument.",
115 ci->key);
116 return -1;
117 }
118
119 if (!may_be_empty && (strlen(string) == 0)) {
120 ERROR("Target `set': The `%s' option does not accept empty string as "
121 "second argument.",
122 ci->key);
123 return -1;
124 }
125
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);
130 return -ENOMEM;
131 }
132 }
133
134 return meta_data_add_string(*dest, key, string);
135 } /* }}} int ts_config_add_meta */
136
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;
140
141 entry = calloc(1, sizeof(*entry));
142 if (entry == NULL) {
143 ERROR("ts_config_add_meta_delete: calloc failed.");
144 return -ENOMEM;
145 }
146
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. */
150 }
151
152 if (strlen(entry->key) == 0) {
153 ERROR("Target `set': The `%s' option does not accept empty string as "
154 "first argument.",
155 ci->key);
156 ts_key_list_free(entry);
157 return -1;
158 }
159
160 entry->next = *dest;
161 *dest = entry;
162
163 return 0;
164 } /* }}} int ts_config_add_meta_delete */
165
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];
169
170 /* Initialize the field with the template. */
171 sstrncpy(dest, string, size);
172
173 if (strchr(dest, '%') == NULL)
174 return;
175
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);
184
185 if (vl->meta != NULL) {
186 char **meta_toc = NULL;
187 int status = meta_data_toc(vl->meta, &meta_toc);
188 if (status <= 0)
189 return;
190 size_t meta_entries = (size_t)status;
191
192 for (size_t i = 0; i < meta_entries; i++) {
193 char meta_name[DATA_MAX_NAME_LEN];
194 char *value_str;
195 const char *key = meta_toc[i];
196
197 snprintf(meta_name, sizeof(meta_name), "%%{meta:%s}", key);
198 if (meta_data_as_string(vl->meta, key, &value_str) != 0)
199 continue;
200
201 REPLACE_FIELD(meta_name, value_str);
202 sfree(value_str);
203 }
204
205 strarray_free(meta_toc, (size_t)meta_entries);
206 }
207 } /* }}} int ts_subst */
208
209 static int ts_destroy(void **user_data) /* {{{ */
210 {
211 ts_data_t *data;
212
213 if (user_data == NULL)
214 return -EINVAL;
215
216 data = *user_data;
217 if (data == NULL)
218 return 0;
219
220 free(data->host);
221 free(data->plugin);
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);
227 free(data);
228
229 return 0;
230 } /* }}} int ts_destroy */
231
232 static int ts_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
233 {
234 ts_data_t *data;
235 int status;
236
237 data = calloc(1, sizeof(*data));
238 if (data == NULL) {
239 ERROR("ts_create: calloc failed.");
240 return -ENOMEM;
241 }
242
243 data->host = NULL;
244 data->plugin = NULL;
245 data->plugin_instance = NULL;
246 /* data->type = NULL; */
247 data->type_instance = NULL;
248 data->meta = NULL;
249 data->meta_delete = NULL;
250
251 status = 0;
252 for (int i = 0; i < ci->children_num; i++) {
253 oconfig_item_t *child = ci->children + i;
254
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);
265 #if 0
266 else if (strcasecmp ("Type", child->key) == 0)
267 status = ts_config_add_string (&data->type, child,
268 /* may be empty = */ 0);
269 #endif
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);
278 else {
279 ERROR("Target `set': The `%s' configuration option is not understood "
280 "and will be ignored.",
281 child->key);
282 status = 0;
283 }
284
285 if (status != 0)
286 break;
287 }
288
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'.");
299 status = -1;
300 }
301
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.");
309 status = -1;
310 }
311 }
312 }
313
314 break;
315 }
316
317 if (status != 0) {
318 ts_destroy((void *)&data);
319 return status;
320 }
321
322 *user_data = data;
323 return 0;
324 } /* }}} int ts_create */
325
326 static int ts_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
327 notification_meta_t __attribute__((unused)) * *meta,
328 void **user_data) {
329 ts_data_t *data;
330 value_list_t orig;
331 meta_data_t *new_meta = NULL;
332
333 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
334 return -EINVAL;
335
336 data = *user_data;
337 if (data == NULL) {
338 ERROR("Target `set': Invoke: `data' is NULL.");
339 return -EINVAL;
340 }
341
342 orig = *vl;
343
344 if (data->meta != NULL) {
345 char temp[DATA_MAX_NAME_LEN * 2];
346 char **meta_toc;
347
348 if ((new_meta = meta_data_create()) == NULL) {
349 ERROR("Target `set': failed to create replacement metadata.");
350 return -ENOMEM;
351 }
352
353 int status = meta_data_toc(data->meta, &meta_toc);
354 if (status < 0) {
355 ERROR("Target `set': meta_data_toc failed with status %d.", status);
356 meta_data_destroy(new_meta);
357 return status;
358 }
359 size_t meta_entries = (size_t)status;
360
361 for (size_t i = 0; i < meta_entries; i++) {
362 const char *key = meta_toc[i];
363 char *string;
364 int status;
365
366 status = meta_data_get_string(data->meta, key, &string);
367 if (status) {
368 ERROR("Target `set': Unable to get replacement metadata value `%s'.",
369 key);
370 strarray_free(meta_toc, meta_entries);
371 meta_data_destroy(new_meta);
372 return status;
373 }
374
375 ts_subst(temp, sizeof(temp), string, &orig);
376
377 DEBUG("target_set: ts_invoke: setting metadata value for key `%s': "
378 "`%s'.",
379 key, temp);
380
381 sfree(string);
382
383 status = meta_data_add_string(new_meta, key, temp);
384 if (status) {
385 ERROR("Target `set': Unable to set metadata value `%s'.", key);
386 strarray_free(meta_toc, meta_entries);
387 meta_data_destroy(new_meta);
388 return status;
389 }
390 }
391
392 strarray_free(meta_toc, meta_entries);
393 }
394
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); \
399 }
400 SUBST_FIELD(host);
401 SUBST_FIELD(plugin);
402 SUBST_FIELD(plugin_instance);
403 /* SUBST_FIELD (type); */
404 SUBST_FIELD(type_instance);
405
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);
410 }
411
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'.",
415 l->key);
416 meta_data_delete(vl->meta, l->key);
417 }
418
419 return FC_TARGET_CONTINUE;
420 } /* }}} int ts_invoke */
421
422 void module_register(void) {
423 target_proc_t tproc = {0};
424
425 tproc.create = ts_create;
426 tproc.destroy = ts_destroy;
427 tproc.invoke = ts_invoke;
428 fc_register_target("set", tproc);
429 } /* module_register */