Tree wide: Reformat with clang-format.
[collectd.git] / src / target_set.c
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 "common.h"
30 #include "filter_chain.h"
31 #include "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;
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];
190       char *value_str;
191       const char *key = meta_toc[i];
192
193       ssnprintf(meta_name, sizeof(meta_name), "%%{meta:%s}", key);
194       if (meta_data_as_string(vl->meta, key, &value_str) != 0)
195         continue;
196
197       REPLACE_FIELD(meta_name, value_str);
198       sfree(value_str);
199     }
200
201     strarray_free(meta_toc, (size_t)meta_entries);
202   }
203 } /* }}} int ts_subst */
204
205 static int ts_destroy(void **user_data) /* {{{ */
206 {
207   ts_data_t *data;
208
209   if (user_data == NULL)
210     return (-EINVAL);
211
212   data = *user_data;
213   if (data == NULL)
214     return (0);
215
216   free(data->host);
217   free(data->plugin);
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);
223   free(data);
224
225   return (0);
226 } /* }}} int ts_destroy */
227
228 static int ts_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
229 {
230   ts_data_t *data;
231   int status;
232
233   data = calloc(1, sizeof(*data));
234   if (data == NULL) {
235     ERROR("ts_create: calloc failed.");
236     return (-ENOMEM);
237   }
238
239   data->host = NULL;
240   data->plugin = NULL;
241   data->plugin_instance = NULL;
242   /* data->type = NULL; */
243   data->type_instance = NULL;
244   data->meta = NULL;
245   data->meta_delete = NULL;
246
247   status = 0;
248   for (int i = 0; i < ci->children_num; i++) {
249     oconfig_item_t *child = ci->children + i;
250
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);
261 #if 0
262     else if (strcasecmp ("Type", child->key) == 0)
263       status = ts_config_add_string (&data->type, child,
264           /* may be empty = */ 0);
265 #endif
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);
274     else {
275       ERROR("Target `set': The `%s' configuration option is not understood "
276             "and will be ignored.",
277             child->key);
278       status = 0;
279     }
280
281     if (status != 0)
282       break;
283   }
284
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'.");
295       status = -1;
296     }
297
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.");
305           status = -1;
306         }
307       }
308     }
309
310     break;
311   }
312
313   if (status != 0) {
314     ts_destroy((void *)&data);
315     return (status);
316   }
317
318   *user_data = data;
319   return (0);
320 } /* }}} int ts_create */
321
322 static int ts_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
323                      notification_meta_t __attribute__((unused)) * *meta,
324                      void **user_data) {
325   ts_data_t *data;
326   value_list_t orig;
327   meta_data_t *new_meta = NULL;
328
329   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
330     return (-EINVAL);
331
332   data = *user_data;
333   if (data == NULL) {
334     ERROR("Target `set': Invoke: `data' is NULL.");
335     return (-EINVAL);
336   }
337
338   orig = *vl;
339
340   if (data->meta != NULL) {
341     char temp[DATA_MAX_NAME_LEN * 2];
342     int meta_entries;
343     char **meta_toc;
344
345     if ((new_meta = meta_data_create()) == NULL) {
346       ERROR("Target `set': failed to create replacement metadata.");
347       return (-ENOMEM);
348     }
349
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];
353       char *string;
354       int status;
355
356       status = meta_data_get_string(data->meta, key, &string);
357       if (status) {
358         ERROR("Target `set': Unable to get replacement metadata value `%s'.",
359               key);
360         strarray_free(meta_toc, (size_t)meta_entries);
361         return (status);
362       }
363
364       ts_subst(temp, sizeof(temp), string, &orig);
365
366       DEBUG("target_set: ts_invoke: setting metadata value for key `%s': "
367             "`%s'.",
368             key, temp);
369
370       sfree(string);
371
372       status = meta_data_add_string(new_meta, key, temp);
373       if (status) {
374         ERROR("Target `set': Unable to set metadata value `%s'.", key);
375         strarray_free(meta_toc, (size_t)meta_entries);
376         return (status);
377       }
378     }
379
380     strarray_free(meta_toc, (size_t)meta_entries);
381   }
382
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);              \
387   }
388   SUBST_FIELD(host);
389   SUBST_FIELD(plugin);
390   SUBST_FIELD(plugin_instance);
391   /* SUBST_FIELD (type); */
392   SUBST_FIELD(type_instance);
393
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);
398   }
399
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'.",
403           l->key);
404     meta_data_delete(vl->meta, l->key);
405   }
406
407   return (FC_TARGET_CONTINUE);
408 } /* }}} int ts_invoke */
409
410 void module_register(void) {
411   target_proc_t tproc = {0};
412
413   tproc.create = ts_create;
414   tproc.destroy = ts_destroy;
415   tproc.invoke = ts_invoke;
416   fc_register_target("set", tproc);
417 } /* module_register */
418
419 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */