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 int ts_config_add_string (char **dest, /* {{{ */
42 const oconfig_item_t *ci, int may_be_empty)
47 status = cf_util_get_string (ci, &tmp);
51 if (!may_be_empty && (strlen (tmp) == 0))
53 ERROR ("Target `set': The `%s' option does not accept empty strings.",
61 } /* }}} int ts_config_add_string */
63 static int ts_destroy (void **user_data) /* {{{ */
67 if (user_data == NULL)
76 free (data->plugin_instance);
77 /* free (data->type); */
78 free (data->type_instance);
82 } /* }}} int ts_destroy */
84 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
90 data = calloc (1, sizeof (*data));
93 ERROR ("ts_create: calloc failed.");
99 data->plugin_instance = NULL;
100 /* data->type = NULL; */
101 data->type_instance = NULL;
104 for (i = 0; i < ci->children_num; i++)
106 oconfig_item_t *child = ci->children + i;
108 if ((strcasecmp ("Host", child->key) == 0)
109 || (strcasecmp ("Hostname", child->key) == 0))
110 status = ts_config_add_string (&data->host, child,
111 /* may be empty = */ 0);
112 else if (strcasecmp ("Plugin", child->key) == 0)
113 status = ts_config_add_string (&data->plugin, child,
114 /* may be empty = */ 0);
115 else if (strcasecmp ("PluginInstance", child->key) == 0)
116 status = ts_config_add_string (&data->plugin_instance, child,
117 /* may be empty = */ 1);
119 else if (strcasecmp ("Type", child->key) == 0)
120 status = ts_config_add_string (&data->type, child,
121 /* may be empty = */ 0);
123 else if (strcasecmp ("TypeInstance", child->key) == 0)
124 status = ts_config_add_string (&data->type_instance, child,
125 /* may be empty = */ 1);
128 ERROR ("Target `set': The `%s' configuration option is not understood "
129 "and will be ignored.", child->key);
137 /* Additional sanity-checking */
140 if ((data->host == NULL)
141 && (data->plugin == NULL)
142 && (data->plugin_instance == NULL)
143 /* && (data->type == NULL) */
144 && (data->type_instance == NULL))
146 ERROR ("Target `set': You need to set at least one of `Host', "
147 "`Plugin', `PluginInstance' or `TypeInstance'.");
156 ts_destroy ((void *) &data);
162 } /* }}} int ts_create */
164 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
165 notification_meta_t __attribute__((unused)) **meta, void **user_data)
169 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
175 ERROR ("Target `set': Invoke: `data' is NULL.");
179 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
182 SET_FIELD (plugin_instance);
183 /* SET_FIELD (type); */
184 SET_FIELD (type_instance);
186 return (FC_TARGET_CONTINUE);
187 } /* }}} int ts_invoke */
189 void module_register (void)
193 memset (&tproc, 0, sizeof (tproc));
194 tproc.create = ts_create;
195 tproc.destroy = ts_destroy;
196 tproc.invoke = ts_invoke;
197 fc_register_target ("set", tproc);
198 } /* module_register */
200 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */