55ce2283cb5a2550df8590766fedc1e78856783d
[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 {
36   char *key;
37   struct ts_key_list_s *next;
38 };
39 typedef struct ts_key_list_s ts_key_list_t;
40
41 static void ts_key_list_free (ts_key_list_t *l) /* {{{ */
42 {
43   if (l == NULL)
44     return;
45
46   free (l->key);
47
48   if (l->next != NULL)
49     ts_key_list_free (l->next);
50
51   free (l);
52 } /* }}} void ts_name_list_free */
53
54 struct ts_data_s
55 {
56   char *host;
57   char *plugin;
58   char *plugin_instance;
59   /* char *type; */
60   char *type_instance;
61   meta_data_t *meta;
62   ts_key_list_t *meta_delete;
63 };
64 typedef struct ts_data_s ts_data_t;
65
66 static int ts_util_get_key_and_string_wo_strdup (const oconfig_item_t *ci, char **ret_key, char **ret_string) /* {{{ */
67 {
68   if ((ci->values_num != 2)
69       || (ci->values[0].type != OCONFIG_TYPE_STRING)
70       || (ci->values[1].type != OCONFIG_TYPE_STRING))
71   {
72     ERROR ("ts_util_get_key_and_string_wo_strdup: The %s option requires "
73         "exactly two string arguments.", ci->key);
74     return (-1);
75   }
76
77   *ret_key = ci->values[0].value.string;
78   *ret_string = ci->values[1].value.string;
79
80   return (0);
81 } /* }}} int ts_util_get_key_and_string_wo_strdup */
82
83 static int ts_config_add_string (char **dest, /* {{{ */
84     const oconfig_item_t *ci, int may_be_empty)
85 {
86   char *tmp = NULL;
87   int status;
88
89   status = cf_util_get_string (ci, &tmp);
90   if (status != 0)
91     return (status);
92
93   if (!may_be_empty && (strlen (tmp) == 0))
94   {
95     ERROR ("Target `set': The `%s' option does not accept empty strings.",
96         ci->key);
97     sfree (tmp);
98     return (-1);
99   }
100
101   *dest = tmp;
102   return (0);
103 } /* }}} int ts_config_add_string */
104
105 static int ts_config_add_meta (meta_data_t **dest, /* {{{ */
106     const oconfig_item_t *ci, int may_be_empty)
107 {
108   char *key = NULL;
109   char *string = NULL;
110   int status;
111
112   status = ts_util_get_key_and_string_wo_strdup (ci, &key, &string);
113   if (status != 0)
114     return (status);
115
116   if (strlen (key) == 0)
117   {
118     ERROR ("Target `set': The `%s' option does not accept empty string as "
119         "first argument.", ci->key);
120     return (-1);
121   }
122
123   if (!may_be_empty && (strlen (string) == 0))
124   {
125     ERROR ("Target `set': The `%s' option does not accept empty string as "
126         "second argument.", ci->key);
127     return (-1);
128   }
129
130   if ((*dest) == NULL)
131   {
132     /* Create a new meta_data_t */
133     if ((*dest = meta_data_create()) == NULL)
134     {
135       ERROR ("Target `set': failed to create a meta data for `%s'.", ci->key);
136       return (-ENOMEM);
137     }
138   }
139
140   return (meta_data_add_string (*dest, key, string));
141 } /* }}} int ts_config_add_meta */
142
143 static int ts_config_add_meta_delete (ts_key_list_t **dest, /* {{{ */
144     const oconfig_item_t *ci)
145 {
146   ts_key_list_t *entry = NULL;
147
148   if ((ci->values_num != 1)
149       || (ci->values[0].type != OCONFIG_TYPE_STRING))
150   {
151     ERROR ("ts_config_add_meta_delete: The %s option requires "
152         "exactly one string argument.", ci->key);
153     return (-1);
154   }
155
156   if (strlen (ci->values[0].value.string) == 0)
157   {
158     ERROR ("Target `set': The `%s' option does not accept empty string as "
159         "first argument.", ci->key);
160     return (-1);
161   }
162
163   entry = calloc (1, sizeof (*entry));
164   if (entry == NULL)
165   {
166     ERROR ("ts_config_add_meta_delete: calloc failed.");
167     return (-ENOMEM);
168   }
169
170   entry->key = sstrdup (ci->values[0].value.string);
171   entry->next = *dest;
172   *dest = entry;
173
174   return (0);
175 } /* }}} int ts_config_add_meta_delete */
176
177 static void ts_subst (char *dest, size_t size, const char *string, /* {{{ */
178     const value_list_t *vl)
179 {
180   char temp[DATA_MAX_NAME_LEN];
181   int meta_entries;
182   char **meta_toc;
183
184   /* Initialize the field with the template. */
185   sstrncpy (dest, string, size);
186
187 #define REPLACE_FIELD(t, v) \
188   if (subst_string (temp, sizeof (temp), dest, t, v) != NULL) \
189     sstrncpy (dest, temp, size);
190   REPLACE_FIELD ("%{host}", vl->host);
191   REPLACE_FIELD ("%{plugin}", vl->plugin);
192   REPLACE_FIELD ("%{plugin_instance}", vl->plugin_instance);
193   REPLACE_FIELD ("%{type}", vl->type);
194   REPLACE_FIELD ("%{type_instance}", vl->type_instance);
195
196   meta_entries = meta_data_toc (vl->meta, &meta_toc);
197   for (int i = 0; i < meta_entries; i++)
198   {
199     char meta_name[DATA_MAX_NAME_LEN];
200     char value_str[DATA_MAX_NAME_LEN];
201     int meta_type;
202     const char *key = meta_toc[i];
203
204     ssnprintf (meta_name, sizeof (meta_name), "%%{meta:%s}", key);
205
206     meta_type = meta_data_type (vl->meta, key);
207     switch (meta_type)
208     {
209       case MD_TYPE_STRING:
210         {
211           char *meta_value;
212           if (meta_data_get_string (vl->meta, key, &meta_value))
213           {
214             ERROR ("Target `set': Unable to get string metadata value `%s'.",
215                 key);
216             continue;
217           }
218           sstrncpy (value_str, meta_value, sizeof (value_str));
219         }
220         break;
221       case MD_TYPE_SIGNED_INT:
222         {
223           int64_t meta_value;
224           if (meta_data_get_signed_int (vl->meta, key, &meta_value))
225           {
226             ERROR ("Target `set': Unable to get signed int metadata value "
227                 "`%s'.", key);
228             continue;
229           }
230           ssnprintf (value_str, sizeof (value_str), "%"PRIi64, meta_value);
231         }
232         break;
233       case MD_TYPE_UNSIGNED_INT:
234         {
235           uint64_t meta_value;
236           if (meta_data_get_unsigned_int (vl->meta, key, &meta_value))
237           {
238             ERROR ("Target `set': Unable to get unsigned int metadata value "
239                 "`%s'.", key);
240             continue;
241           }
242           ssnprintf (value_str, sizeof (value_str), "%"PRIu64, meta_value);
243         }
244         break;
245       case MD_TYPE_DOUBLE:
246         {
247           double meta_value;
248           if (meta_data_get_double (vl->meta, key, &meta_value))
249           {
250             ERROR ("Target `set': Unable to get double metadata value `%s'.",
251                 key);
252             continue;
253           }
254           ssnprintf (value_str, sizeof (value_str), GAUGE_FORMAT, meta_value);
255         }
256         break;
257       case MD_TYPE_BOOLEAN:
258         {
259           _Bool meta_value;
260           if (meta_data_get_boolean (vl->meta, key, &meta_value))
261           {
262             ERROR ("Target `set': Unable to get boolean metadata value `%s'.",
263                 key);
264             continue;
265           }
266           sstrncpy (value_str, meta_value ? "true" : "false",
267               sizeof (value_str));
268         }
269         break;
270       default:
271         ERROR ("Target `set': Unable to retrieve metadata type for `%s'.",
272             key);
273         continue;
274     }
275
276     REPLACE_FIELD (meta_name, value_str);
277   }
278 } /* }}} int ts_subst */
279
280 static int ts_destroy (void **user_data) /* {{{ */
281 {
282   ts_data_t *data;
283
284   if (user_data == NULL)
285     return (-EINVAL);
286
287   data = *user_data;
288   if (data == NULL)
289     return (0);
290
291   free (data->host);
292   free (data->plugin);
293   free (data->plugin_instance);
294   /* free (data->type); */
295   free (data->type_instance);
296   meta_data_destroy(data->meta);
297   ts_key_list_free (data->meta_delete);
298   free (data);
299
300   return (0);
301 } /* }}} int ts_destroy */
302
303 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
304 {
305   ts_data_t *data;
306   int status;
307
308   data = calloc (1, sizeof (*data));
309   if (data == NULL)
310   {
311     ERROR ("ts_create: calloc failed.");
312     return (-ENOMEM);
313   }
314
315   data->host = NULL;
316   data->plugin = NULL;
317   data->plugin_instance = NULL;
318   /* data->type = NULL; */
319   data->type_instance = NULL;
320   data->meta = NULL;
321   data->meta_delete = NULL;
322
323   status = 0;
324   for (int i = 0; i < ci->children_num; i++)
325   {
326     oconfig_item_t *child = ci->children + i;
327
328     if ((strcasecmp ("Host", child->key) == 0)
329         || (strcasecmp ("Hostname", child->key) == 0))
330       status = ts_config_add_string (&data->host, child,
331           /* may be empty = */ 0);
332     else if (strcasecmp ("Plugin", child->key) == 0)
333       status = ts_config_add_string (&data->plugin, child,
334           /* may be empty = */ 0);
335     else if (strcasecmp ("PluginInstance", child->key) == 0)
336       status = ts_config_add_string (&data->plugin_instance, child,
337           /* may be empty = */ 1);
338 #if 0
339     else if (strcasecmp ("Type", child->key) == 0)
340       status = ts_config_add_string (&data->type, child,
341           /* may be empty = */ 0);
342 #endif
343     else if (strcasecmp ("TypeInstance", child->key) == 0)
344       status = ts_config_add_string (&data->type_instance, child,
345           /* may be empty = */ 1);
346     else if (strcasecmp ("MetaData", child->key) == 0)
347       status = ts_config_add_meta (&data->meta, child,
348           /* may be empty = */ 1);
349     else if (strcasecmp ("DeleteMetaData", child->key) == 0)
350       status = ts_config_add_meta_delete (&data->meta_delete, child);
351     else
352     {
353       ERROR ("Target `set': The `%s' configuration option is not understood "
354           "and will be ignored.", child->key);
355       status = 0;
356     }
357
358     if (status != 0)
359       break;
360   }
361
362   /* Additional sanity-checking */
363   while (status == 0)
364   {
365     if ((data->host == NULL)
366         && (data->plugin == NULL)
367         && (data->plugin_instance == NULL)
368         /* && (data->type == NULL) */
369         && (data->type_instance == NULL)
370         && (data->meta == NULL)
371         && (data->meta_delete == NULL))
372     {
373       ERROR ("Target `set': You need to set at least one of `Host', "
374           "`Plugin', `PluginInstance', `TypeInstance', "
375           "`MetaData', or `DeleteMetaData'.");
376       status = -1;
377     }
378
379     if (data->meta != NULL)
380     {
381       /* If data->meta_delete is NULL, this loop is a no-op. */
382       for (ts_key_list_t *l=data->meta_delete; l != NULL; l = l->next)
383       {
384         if (meta_data_type (data->meta, l->key) != 0)
385         {
386           /* MetaData and DeleteMetaData for the same key. */
387           ERROR ("Target `set': Can only have one of `MetaData' or "
388               "`DeleteMetaData' for any given key.");
389           status = -1;
390         }
391       }
392     }
393
394     break;
395   }
396
397   if (status != 0)
398   {
399     ts_destroy ((void *) &data);
400     return (status);
401   }
402
403   *user_data = data;
404   return (0);
405 } /* }}} int ts_create */
406
407 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
408     notification_meta_t __attribute__((unused)) **meta, void **user_data)
409 {
410   ts_data_t *data;
411   value_list_t orig;
412   meta_data_t *new_meta = NULL;
413
414   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
415     return (-EINVAL);
416
417   data = *user_data;
418   if (data == NULL)
419   {
420     ERROR ("Target `set': Invoke: `data' is NULL.");
421     return (-EINVAL);
422   }
423
424   orig = *vl;
425
426   if (data->meta != NULL)
427   {
428     char temp[DATA_MAX_NAME_LEN*2];
429     int meta_entries;
430     char **meta_toc;
431
432     if ((new_meta = meta_data_create()) == NULL)
433     {
434       ERROR ("Target `set': failed to create replacement metadata.");
435       return (-ENOMEM);
436     }
437
438     meta_entries = meta_data_toc (data->meta, &meta_toc);
439     for (int i = 0; i < meta_entries; i++)
440     {
441       const char *key = meta_toc[i];
442       char *string;
443       int status;
444
445       status = meta_data_get_string (data->meta, key, &string);
446       if (status)
447       {
448         ERROR ("Target `set': Unable to get replacement metadata value `%s'.",
449             key);
450         return (status);
451       }
452
453       ts_subst (temp, sizeof (temp), string, &orig);
454
455       DEBUG ("target_set: ts_invoke: setting metadata value for key `%s': "
456           "`%s'.", key, temp);
457
458       status = meta_data_add_string (new_meta, key, temp);
459
460       if (status)
461       {
462         ERROR ("Target `set': Unable to set metadata value `%s'.", key);
463         return (status);
464       }
465     }
466   }
467
468 #define SUBST_FIELD(f) \
469   if (data->f != NULL) { \
470     ts_subst (vl->f, sizeof (vl->f), data->f, &orig); \
471     DEBUG ("target_set: ts_invoke: setting "#f": `%s'.", vl->f); \
472   }
473   SUBST_FIELD (host);
474   SUBST_FIELD (plugin);
475   SUBST_FIELD (plugin_instance);
476   /* SUBST_FIELD (type); */
477   SUBST_FIELD (type_instance);
478
479   /* Need to merge the metadata in now, because of the shallow copy. */
480   if (new_meta != NULL)
481   {
482     meta_data_clone_merge(&(vl->meta), new_meta);
483     meta_data_destroy(new_meta);
484   }
485
486   /* If data->meta_delete is NULL, this loop is a no-op. */
487   for (ts_key_list_t *l=data->meta_delete; l != NULL; l = l->next)
488   {
489     DEBUG ("target_set: ts_invoke: deleting metadata value for key `%s'.",
490         l->key);
491     meta_data_delete(vl->meta, l->key);
492   }
493
494   return (FC_TARGET_CONTINUE);
495 } /* }}} int ts_invoke */
496
497 void module_register (void)
498 {
499         target_proc_t tproc = { 0 };
500
501         tproc.create  = ts_create;
502         tproc.destroy = ts_destroy;
503         tproc.invoke  = ts_invoke;
504         fc_register_target ("set", tproc);
505 } /* module_register */
506
507 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
508