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