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