2 * collectd - src/target_set.c
3 * Copyright (C) 2008 Florian Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian Forster <octo at collectd.org>
29 #include "filter_chain.h"
35 char *plugin_instance;
39 typedef struct ts_data_s ts_data_t;
41 static char *ts_strdup (const char *orig) /* {{{ */
49 sz = strlen (orig) + 1;
50 dest = (char *) malloc (sz);
54 memcpy (dest, orig, sz);
57 } /* }}} char *ts_strdup */
59 static int ts_config_add_string (char **dest, /* {{{ */
60 const oconfig_item_t *ci, int may_be_empty)
67 if ((ci->values_num != 1)
68 || (ci->values[0].type != OCONFIG_TYPE_STRING))
70 ERROR ("Target `set': The `%s' option requires exactly one string "
71 "argument.", ci->key);
75 if ((!may_be_empty) && (ci->values[0].value.string[0] == 0))
77 ERROR ("Target `set': The `%s' option does not accept empty strings.",
82 temp = ts_strdup (ci->values[0].value.string);
85 ERROR ("ts_config_add_string: ts_strdup failed.");
93 } /* }}} int ts_config_add_string */
95 static int ts_destroy (void **user_data) /* {{{ */
99 if (user_data == NULL)
108 free (data->plugin_instance);
109 /* free (data->type); */
110 free (data->type_instance);
114 } /* }}} int ts_destroy */
116 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
122 data = (ts_data_t *) malloc (sizeof (*data));
125 ERROR ("ts_create: malloc failed.");
128 memset (data, 0, sizeof (*data));
132 data->plugin_instance = NULL;
133 /* data->type = NULL; */
134 data->type_instance = NULL;
137 for (i = 0; i < ci->children_num; i++)
139 oconfig_item_t *child = ci->children + i;
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);
152 else if (strcasecmp ("Type", child->key) == 0)
153 status = ts_config_add_string (&data->type, child,
154 /* may be empty = */ 0);
156 else if (strcasecmp ("TypeInstance", child->key) == 0)
157 status = ts_config_add_string (&data->type_instance, child,
158 /* may be empty = */ 1);
161 ERROR ("Target `set': The `%s' configuration option is not understood "
162 "and will be ignored.", child->key);
170 /* Additional sanity-checking */
173 if ((data->host == NULL)
174 && (data->plugin == NULL)
175 && (data->plugin_instance == NULL)
176 /* && (data->type == NULL) */
177 && (data->type_instance == NULL))
179 ERROR ("Target `set': You need to set at least one of `Host', "
180 "`Plugin', `PluginInstance' or `TypeInstance'.");
189 ts_destroy ((void *) &data);
195 } /* }}} int ts_create */
197 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
198 notification_meta_t __attribute__((unused)) **meta, void **user_data)
202 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
208 ERROR ("Target `set': Invoke: `data' is NULL.");
212 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
215 SET_FIELD (plugin_instance);
216 /* SET_FIELD (type); */
217 SET_FIELD (type_instance);
219 return (FC_TARGET_CONTINUE);
220 } /* }}} int ts_invoke */
222 void module_register (void)
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 */
233 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */