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