2 * collectd - src/target_set.c
3 * Copyright (C) 2008 Florian Forster
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.
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.
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
19 * Florian Forster <octo at verplant.org>
23 * First tell the compiler to stick to the C99 and POSIX standards as close as
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
37 #define _POSIX_C_SOURCE 200112L
40 /* Single UNIX needed for strdup. */
44 #define _XOPEN_SOURCE 500
62 #include "filter_chain.h"
68 char *plugin_instance;
72 typedef struct ts_data_s ts_data_t;
74 static char *ts_strdup (const char *orig) /* {{{ */
82 sz = strlen (orig) + 1;
83 dest = (char *) malloc (sz);
87 memcpy (dest, orig, sz);
90 } /* }}} char *ts_strdup */
92 static int ts_config_add_string (char **dest, /* {{{ */
93 const oconfig_item_t *ci)
100 if ((ci->values_num != 1)
101 || (ci->values[0].type != OCONFIG_TYPE_STRING))
103 ERROR ("Target `set': The `%s' option requires exactly one string "
104 "argument.", ci->key);
108 temp = ts_strdup (ci->values[0].value.string);
111 ERROR ("ts_config_add_string: ts_strdup failed.");
119 } /* }}} int ts_config_add_string */
121 static int ts_destroy (void **user_data) /* {{{ */
125 if (user_data == NULL)
134 free (data->plugin_instance);
136 free (data->type_instance);
140 } /* }}} int ts_destroy */
142 static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
148 data = (ts_data_t *) malloc (sizeof (*data));
151 ERROR ("ts_create: malloc failed.");
154 memset (data, 0, sizeof (*data));
158 data->plugin_instance = NULL;
160 data->type_instance = NULL;
163 for (i = 0; i < ci->children_num; i++)
165 oconfig_item_t *child = ci->children + i;
167 if ((strcasecmp ("Host", child->key) == 0)
168 || (strcasecmp ("Hostname", child->key) == 0))
169 status = ts_config_add_string (&data->host, child);
170 else if (strcasecmp ("Plugin", child->key) == 0)
171 status = ts_config_add_string (&data->plugin, child);
172 else if (strcasecmp ("PluginInstance", child->key) == 0)
173 status = ts_config_add_string (&data->plugin_instance, child);
174 else if (strcasecmp ("Type", child->key) == 0)
175 status = ts_config_add_string (&data->type, child);
176 else if (strcasecmp ("TypeInstance", child->key) == 0)
177 status = ts_config_add_string (&data->type_instance, child);
180 ERROR ("Target `set': The `%s' configuration option is not understood "
181 "and will be ignored.", child->key);
189 /* Additional sanity-checking */
192 if ((data->host == NULL)
193 && (data->plugin == NULL)
194 && (data->plugin_instance == NULL)
195 && (data->type == NULL)
196 && (data->type_instance == NULL))
198 ERROR ("Target `set': You need to set at lease one of `Host', "
199 "`Plugin', `PluginInstance', `Type', or `TypeInstance'.");
208 ts_destroy ((void *) &data);
214 } /* }}} int ts_create */
216 static int ts_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */
217 notification_meta_t **meta, void **user_data)
221 if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
227 ERROR ("Target `set': Invoke: `data' is NULL.");
231 #define SET_FIELD(f) if (data->f != NULL) { sstrncpy (vl->f, data->f, sizeof (vl->f)); }
234 SET_FIELD (plugin_instance);
236 SET_FIELD (type_instance);
239 } /* }}} int ts_invoke */
241 void module_register (void)
245 memset (&tproc, 0, sizeof (tproc));
246 tproc.create = ts_create;
247 tproc.destroy = ts_destroy;
248 tproc.invoke = ts_invoke;
249 fc_register_target ("set", tproc);
250 } /* module_register */
252 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */