Merge branch 'ym/target_set_add_meta' of github.com:ymettier/collectd into target_set...
[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 #include "common.h"
29 #include "filter_chain.h"
30
31 struct ts_data_s
32 {
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;
41
42 int ts_util_get_key_and_string_wo_strdup (const oconfig_item_t *ci, char **ret_key, char **ret_string) /* {{{ */
43 {
44   if ((ci->values_num != 2)
45       || (ci->values[0].type != OCONFIG_TYPE_STRING)
46       || (ci->values[1].type != OCONFIG_TYPE_STRING))
47   {
48     ERROR ("ts_util_get_key_and_string_wo_strdup: The %s option requires "
49         "exactly two string argument.", ci->key);
50     return (-1);
51   }
52
53   *ret_key = ci->values[0].value.string;
54   *ret_string = ci->values[1].value.string;
55
56   return (0);
57 } /* }}} int ts_util_get_key_and_string_wo_strdup */
58
59 static int ts_config_add_string (char **dest, /* {{{ */
60     const oconfig_item_t *ci, int may_be_empty)
61 {
62   char *tmp = NULL;
63   int status;
64
65   status = cf_util_get_string (ci, &tmp);
66   if (status != 0)
67     return (status);
68
69   if (!may_be_empty && (strlen (tmp) == 0))
70   {
71     ERROR ("Target `set': The `%s' option does not accept empty strings.",
72         ci->key);
73     sfree (tmp);
74     return (-1);
75   }
76
77   *dest = tmp;
78   return (0);
79 } /* }}} int ts_config_add_string */
80
81 static int ts_config_add_meta (meta_data_t **dest, /* {{{ */
82     const oconfig_item_t *ci, int may_be_empty)
83 {
84   char *key = NULL;
85   char *string = NULL;
86   int status;
87
88   status = ts_util_get_key_and_string_wo_strdup (ci, &key, &string);
89   if (status != 0)
90     return (status);
91
92   if (strlen (key) == 0)
93   {
94     ERROR ("Target `set': The `%s' option does not accept empty string as first argument.",
95         ci->key);
96     return (-1);
97   }
98
99   if (!may_be_empty && (strlen (string) == 0))
100   {
101     ERROR ("Target `set': The `%s' option does not accept empty string as second argument.",
102         ci->key);
103     return (-1);
104   }
105
106   if(NULL == (*dest)) {
107     // Create a new meta_data_t
108     if( NULL == (*dest = meta_data_create())) {
109       ERROR ("Target `set': failed to create a meta data for `%s'.", ci->key);
110       return (-1);
111     }
112   }
113
114   return(meta_data_add_string (*dest, key, string));
115 } /* }}} int ts_config_add_meta */
116
117 static int ts_destroy (void **user_data) /* {{{ */
118 {
119   ts_data_t *data;
120
121   if (user_data == NULL)
122     return (-EINVAL);
123
124   data = *user_data;
125   if (data == NULL)
126     return (0);
127
128   free (data->host);
129   free (data->plugin);
130   free (data->plugin_instance);
131   /* free (data->type); */
132   free (data->type_instance);
133   meta_data_destroy(data->meta);
134   free (data);
135
136   return (0);
137 } /* }}} int ts_destroy */
138
139 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
140 {
141   ts_data_t *data;
142   int status;
143   int i;
144
145   data = calloc (1, sizeof (*data));
146   if (data == NULL)
147   {
148     ERROR ("ts_create: calloc failed.");
149     return (-ENOMEM);
150   }
151
152   data->host = NULL;
153   data->plugin = NULL;
154   data->plugin_instance = NULL;
155   /* data->type = NULL; */
156   data->type_instance = NULL;
157   data->meta = NULL;
158
159   status = 0;
160   for (i = 0; i < ci->children_num; i++)
161   {
162     oconfig_item_t *child = ci->children + i;
163
164     if ((strcasecmp ("Host", child->key) == 0)
165         || (strcasecmp ("Hostname", child->key) == 0))
166       status = ts_config_add_string (&data->host, child,
167           /* may be empty = */ 0);
168     else if (strcasecmp ("Plugin", child->key) == 0)
169       status = ts_config_add_string (&data->plugin, child,
170           /* may be empty = */ 0);
171     else if (strcasecmp ("PluginInstance", child->key) == 0)
172       status = ts_config_add_string (&data->plugin_instance, child,
173           /* may be empty = */ 1);
174 #if 0
175     else if (strcasecmp ("Type", child->key) == 0)
176       status = ts_config_add_string (&data->type, child,
177           /* may be empty = */ 0);
178 #endif
179     else if (strcasecmp ("TypeInstance", child->key) == 0)
180       status = ts_config_add_string (&data->type_instance, child,
181           /* may be empty = */ 1);
182     else if (strcasecmp ("MetaDataSet", child->key) == 0)
183       status = ts_config_add_meta (&data->meta, child,
184           /* may be empty = */ 1);
185     else
186     {
187       ERROR ("Target `set': The `%s' configuration option is not understood "
188           "and will be ignored.", child->key);
189       status = 0;
190     }
191
192     if (status != 0)
193       break;
194   }
195
196   /* Additional sanity-checking */
197   while (status == 0)
198   {
199     if ((data->host == NULL)
200         && (data->plugin == NULL)
201         && (data->plugin_instance == NULL)
202         /* && (data->type == NULL) */
203         && (data->type_instance == NULL)
204         && (data->meta == NULL))
205     {
206       ERROR ("Target `set': You need to set at least one of `Host', "
207           "`Plugin', `PluginInstance', `TypeInstance', "
208           "`MetaDataSet' or `MetaDataEval'.");
209       status = -1;
210     }
211
212     break;
213   }
214
215   if (status != 0)
216   {
217     ts_destroy ((void *) &data);
218     return (status);
219   }
220
221   *user_data = data;
222   return (0);
223 } /* }}} int ts_create */
224
225 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
226     notification_meta_t __attribute__((unused)) **meta, void **user_data)
227 {
228   ts_data_t *data;
229
230   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
231     return (-EINVAL);
232
233   data = *user_data;
234   if (data == NULL)
235   {
236     ERROR ("Target `set': Invoke: `data' is NULL.");
237     return (-EINVAL);
238   }
239
240   if(NULL != data->meta) {
241     meta_data_clone_merge(&(vl->meta), data->meta);
242   }
243
244 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
245   SET_FIELD (host);
246   SET_FIELD (plugin);
247   SET_FIELD (plugin_instance);
248   /* SET_FIELD (type); */
249   SET_FIELD (type_instance);
250
251   return (FC_TARGET_CONTINUE);
252 } /* }}} int ts_invoke */
253
254 void module_register (void)
255 {
256         target_proc_t tproc;
257
258         memset (&tproc, 0, sizeof (tproc));
259         tproc.create  = ts_create;
260         tproc.destroy = ts_destroy;
261         tproc.invoke  = ts_invoke;
262         fc_register_target ("set", tproc);
263 } /* module_register */
264
265 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
266