set target: Don't allow setting of the `type' field.
[collectd.git] / src / target_set.c
1 /**
2  * collectd - src/target_set.c
3  * Copyright (C) 2008  Florian Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian Forster <octo at verplant.org>
20  **/
21
22 /*
23  * First tell the compiler to stick to the C99 and POSIX standards as close as
24  * possible.
25  */
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
28 #endif
29
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
32 #endif
33
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
36 #endif
37 #define _POSIX_C_SOURCE 200112L
38
39 #if 0
40 /* Single UNIX needed for strdup. */
41 #ifdef _XOPEN_SOURCE
42 # undef _XOPEN_SOURCE
43 #endif
44 #define _XOPEN_SOURCE 500
45 #endif
46
47 #ifndef _REENTRANT
48 # define _REENTRANT
49 #endif
50
51 #ifndef _THREAD_SAFE
52 # define _THREAD_SAFE
53 #endif
54
55 #ifdef _GNU_SOURCE
56 # undef _GNU_SOURCE
57 #endif
58 /* }}} */
59
60 #include "collectd.h"
61 #include "common.h"
62 #include "filter_chain.h"
63
64 struct ts_data_s
65 {
66   char *host;
67   char *plugin;
68   char *plugin_instance;
69   /* char *type; */
70   char *type_instance;
71 };
72 typedef struct ts_data_s ts_data_t;
73
74 static char *ts_strdup (const char *orig) /* {{{ */
75 {
76   size_t sz;
77   char *dest;
78
79   if (orig == NULL)
80     return (NULL);
81
82   sz = strlen (orig) + 1;
83   dest = (char *) malloc (sz);
84   if (dest == NULL)
85     return (NULL);
86
87   memcpy (dest, orig, sz);
88
89   return (dest);
90 } /* }}} char *ts_strdup */
91
92 static int ts_config_add_string (char **dest, /* {{{ */
93     const oconfig_item_t *ci, int may_be_empty)
94 {
95   char *temp;
96
97   if (dest == NULL)
98     return (-EINVAL);
99
100   if ((ci->values_num != 1)
101       || (ci->values[0].type != OCONFIG_TYPE_STRING))
102   {
103     ERROR ("Target `set': The `%s' option requires exactly one string "
104         "argument.", ci->key);
105     return (-1);
106   }
107
108   if ((!may_be_empty) && (ci->values[0].value.string[0] == 0))
109   {
110     ERROR ("Target `set': The `%s' option does not accept empty strings.",
111         ci->key);
112     return (-1);
113   }
114
115   temp = ts_strdup (ci->values[0].value.string);
116   if (temp == NULL)
117   {
118     ERROR ("ts_config_add_string: ts_strdup failed.");
119     return (-1);
120   }
121
122   free (*dest);
123   *dest = temp;
124
125   return (0);
126 } /* }}} int ts_config_add_string */
127
128 static int ts_destroy (void **user_data) /* {{{ */
129 {
130   ts_data_t *data;
131
132   if (user_data == NULL)
133     return (-EINVAL);
134
135   data = *user_data;
136   if (data == NULL)
137     return (0);
138
139   free (data->host);
140   free (data->plugin);
141   free (data->plugin_instance);
142   /* free (data->type); */
143   free (data->type_instance);
144   free (data);
145
146   return (0);
147 } /* }}} int ts_destroy */
148
149 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
150 {
151   ts_data_t *data;
152   int status;
153   int i;
154
155   data = (ts_data_t *) malloc (sizeof (*data));
156   if (data == NULL)
157   {
158     ERROR ("ts_create: malloc failed.");
159     return (-ENOMEM);
160   }
161   memset (data, 0, sizeof (*data));
162
163   data->host = NULL;
164   data->plugin = NULL;
165   data->plugin_instance = NULL;
166   /* data->type = NULL; */
167   data->type_instance = NULL;
168
169   status = 0;
170   for (i = 0; i < ci->children_num; i++)
171   {
172     oconfig_item_t *child = ci->children + i;
173
174     if ((strcasecmp ("Host", child->key) == 0)
175         || (strcasecmp ("Hostname", child->key) == 0))
176       status = ts_config_add_string (&data->host, child,
177           /* may be empty = */ 0);
178     else if (strcasecmp ("Plugin", child->key) == 0)
179       status = ts_config_add_string (&data->plugin, child,
180           /* may be empty = */ 0);
181     else if (strcasecmp ("PluginInstance", child->key) == 0)
182       status = ts_config_add_string (&data->plugin_instance, child,
183           /* may be empty = */ 1);
184 #if 0
185     else if (strcasecmp ("Type", child->key) == 0)
186       status = ts_config_add_string (&data->type, child,
187           /* may be empty = */ 0);
188 #endif
189     else if (strcasecmp ("TypeInstance", child->key) == 0)
190       status = ts_config_add_string (&data->type_instance, child,
191           /* may be empty = */ 1);
192     else
193     {
194       ERROR ("Target `set': The `%s' configuration option is not understood "
195           "and will be ignored.", child->key);
196       status = 0;
197     }
198
199     if (status != 0)
200       break;
201   }
202
203   /* Additional sanity-checking */
204   while (status == 0)
205   {
206     if ((data->host == NULL)
207         && (data->plugin == NULL)
208         && (data->plugin_instance == NULL)
209         /* && (data->type == NULL) */
210         && (data->type_instance == NULL))
211     {
212       ERROR ("Target `set': You need to set at lease one of `Host', "
213           "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
214       status = -1;
215     }
216
217     break;
218   }
219
220   if (status != 0)
221   {
222     ts_destroy ((void *) &data);
223     return (status);
224   }
225
226   *user_data = data;
227   return (0);
228 } /* }}} int ts_create */
229
230 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
231     notification_meta_t **meta, void **user_data)
232 {
233   ts_data_t *data;
234
235   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
236     return (-EINVAL);
237
238   data = *user_data;
239   if (data == NULL)
240   {
241     ERROR ("Target `set': Invoke: `data' is NULL.");
242     return (-EINVAL);
243   }
244
245 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
246   SET_FIELD (host);
247   SET_FIELD (plugin);
248   SET_FIELD (plugin_instance);
249   /* SET_FIELD (type); */
250   SET_FIELD (type_instance);
251
252   return (0);
253 } /* }}} int ts_invoke */
254
255 void module_register (void)
256 {
257         target_proc_t tproc;
258
259         memset (&tproc, 0, sizeof (tproc));
260         tproc.create  = ts_create;
261         tproc.destroy = ts_destroy;
262         tproc.invoke  = ts_invoke;
263         fc_register_target ("set", tproc);
264 } /* module_register */
265
266 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */
267