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 = (ts_data_t *) malloc (sizeof (*data));
93 ERROR ("ts_create: malloc failed.");
96 memset (data, 0, sizeof (*data));
100 data->plugin_instance = NULL;
101 /* data->type = NULL; */
102 data->type_instance = NULL;
105 for (i = 0; i < ci->children_num; i++)
107 oconfig_item_t *child = ci->children + i;
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);
120 else if (strcasecmp ("Type", child->key) == 0)
121 status = ts_config_add_string (&data->type, child,
122 /* may be empty = */ 0);
124 else if (strcasecmp ("TypeInstance", child->key) == 0)
125 status = ts_config_add_string (&data->type_instance, child,
126 /* may be empty = */ 1);
129 ERROR ("Target `set': The `%s' configuration option is not understood "
130 "and will be ignored.", child->key);
138 /* Additional sanity-checking */
141 if ((data->host == NULL)
142 && (data->plugin == NULL)
143 && (data->plugin_instance == NULL)
144 /* && (data->type == NULL) */
145 && (data->type_instance == NULL))
147 ERROR ("Target `set': You need to set at least one of `Host', "
148 "`Plugin', `PluginInstance' or `TypeInstance'.");
157 ts_destroy ((void *) &data);
163 } /* }}} int ts_create */
165 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
166 notification_meta_t __attribute__((unused)) **meta, void **user_data)
170 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
176 ERROR ("Target `set': Invoke: `data' is NULL.");
180 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
183 SET_FIELD (plugin_instance);
184 /* SET_FIELD (type); */
185 SET_FIELD (type_instance);
187 return (FC_TARGET_CONTINUE);
188 } /* }}} int ts_invoke */
190 void module_register (void)
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 */
201 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */