2 * collectd - src/utils_cms_putval.c
3 * Copyright (C) 2007-2009 Florian octo 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 octo Forster <octo at verplant.org>
26 #include "utils_parse_option.h"
27 #include "utils_cmd_putval.h"
29 #define print_to_socket(fh, ...) \
31 if (fprintf (fh, __VA_ARGS__) < 0) { \
33 WARNING ("handle_putval: failed to write to socket #%i: %s", \
34 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
41 static int set_option (value_list_t *vl, const char *key, const char *value)
43 if ((vl == NULL) || (key == NULL) || (value == NULL))
46 if (strcasecmp ("interval", key) == 0)
53 tmp = strtod (value, &endptr);
55 if ((errno == 0) && (endptr != NULL)
56 && (endptr != value) && (tmp > 0.0))
57 vl->interval = DOUBLE_TO_CDTIME_T (tmp);
63 } /* int parse_option */
65 int handle_putval (FILE *fh, char *buffer)
71 char *plugin_instance;
77 char *identifier_copy;
80 value_list_t vl = VALUE_LIST_INIT;
83 DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
87 status = parse_string (&buffer, &command);
90 print_to_socket (fh, "-1 Cannot parse command.\n");
93 assert (command != NULL);
95 if (strcasecmp ("PUTVAL", command) != 0)
97 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
102 status = parse_string (&buffer, &identifier);
105 print_to_socket (fh, "-1 Cannot parse identifier.\n");
108 assert (identifier != NULL);
110 /* parse_identifier() modifies its first argument,
111 * returning pointers into it */
112 identifier_copy = sstrdup (identifier);
114 status = parse_identifier (identifier_copy, &hostname,
115 &plugin, &plugin_instance,
116 &type, &type_instance);
119 DEBUG ("handle_putval: Cannot parse identifier `%s'.",
121 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
123 sfree (identifier_copy);
127 if ((strlen (hostname) >= sizeof (vl.host))
128 || (strlen (plugin) >= sizeof (vl.plugin))
129 || ((plugin_instance != NULL)
130 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
131 || ((type_instance != NULL)
132 && (strlen (type_instance) >= sizeof (vl.type_instance))))
134 print_to_socket (fh, "-1 Identifier too long.\n");
135 sfree (identifier_copy);
139 sstrncpy (vl.host, hostname, sizeof (vl.host));
140 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
141 sstrncpy (vl.type, type, sizeof (vl.type));
142 if (plugin_instance != NULL)
143 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
144 if (type_instance != NULL)
145 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
147 ds = plugin_get_ds (type);
149 print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
150 sfree (identifier_copy);
154 /* Free identifier_copy */
156 plugin = NULL; plugin_instance = NULL;
157 type = NULL; type_instance = NULL;
158 sfree (identifier_copy);
160 vl.values_len = ds->ds_num;
161 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
162 if (vl.values == NULL)
164 print_to_socket (fh, "-1 malloc failed.\n");
168 /* All the remaining fields are part of the optionlist. */
169 values_submitted = 0;
175 status = parse_option (&buffer, &string, &value);
178 /* parse_option failed, buffer has been modified.
179 * => we need to abort */
180 print_to_socket (fh, "-1 Misformatted option.\n");
184 else if (status == 0)
186 assert (string != NULL);
187 assert (value != NULL);
188 set_option (&vl, string, value);
191 /* else: parse_option but buffer has not been modified. This is
192 * the default if no `=' is found.. */
194 status = parse_string (&buffer, &string);
197 print_to_socket (fh, "-1 Misformatted value.\n");
201 assert (string != NULL);
203 status = parse_values (string, &vl, ds);
206 print_to_socket (fh, "-1 Parsing the values string failed.\n");
211 plugin_dispatch_values (&vl);
213 } /* while (*buffer != 0) */
214 /* Done parsing the options. */
216 print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
218 (values_submitted == 1) ? "value has" : "values have");
222 } /* int handle_putval */
224 int create_putval (char *ret, size_t ret_len, /* {{{ */
225 const data_set_t *ds, const value_list_t *vl)
227 char buffer_ident[6 * DATA_MAX_NAME_LEN];
228 char buffer_values[1024];
231 status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
234 escape_string (buffer_ident, sizeof (buffer_ident));
236 status = format_values (buffer_values, sizeof (buffer_values),
237 ds, vl, /* store rates = */ 0);
240 escape_string (buffer_values, sizeof (buffer_values));
242 ssnprintf (ret, ret_len,
243 "PUTVAL %s interval=%.3f %s",
246 ? CDTIME_T_TO_DOUBLE (vl->interval)
247 : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
251 } /* }}} int create_putval */