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