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"
28 #define print_to_socket(fh, ...) \
30 if (fprintf (fh, __VA_ARGS__) < 0) { \
32 WARNING ("handle_putval: failed to write to socket #%i: %s", \
33 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
40 static int set_option (value_list_t *vl, const char *key, const char *value)
42 if ((vl == NULL) || (key == NULL) || (value == NULL))
45 if (strcasecmp ("interval", key) == 0)
52 tmp = strtod (value, &endptr);
54 if ((errno == 0) && (endptr != NULL)
55 && (endptr != value) && (tmp > 0.0))
56 vl->interval = DOUBLE_TO_CDTIME_T (tmp);
62 } /* int parse_option */
64 int handle_putval (FILE *fh, char *buffer)
70 char *plugin_instance;
76 char *identifier_copy;
79 value_list_t vl = VALUE_LIST_INIT;
82 DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
86 status = parse_string (&buffer, &command);
89 print_to_socket (fh, "-1 Cannot parse command.\n");
92 assert (command != NULL);
94 if (strcasecmp ("PUTVAL", command) != 0)
96 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
101 status = parse_string (&buffer, &identifier);
104 print_to_socket (fh, "-1 Cannot parse identifier.\n");
107 assert (identifier != NULL);
109 /* parse_identifier() modifies its first argument,
110 * returning pointers into it */
111 identifier_copy = sstrdup (identifier);
113 status = parse_identifier (identifier_copy, &hostname,
114 &plugin, &plugin_instance,
115 &type, &type_instance);
118 DEBUG ("handle_putval: Cannot parse identifier `%s'.",
120 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
122 sfree (identifier_copy);
126 if ((strlen (hostname) >= sizeof (vl.host))
127 || (strlen (plugin) >= sizeof (vl.plugin))
128 || ((plugin_instance != NULL)
129 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
130 || ((type_instance != NULL)
131 && (strlen (type_instance) >= sizeof (vl.type_instance))))
133 print_to_socket (fh, "-1 Identifier too long.\n");
134 sfree (identifier_copy);
138 sstrncpy (vl.host, hostname, sizeof (vl.host));
139 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
140 sstrncpy (vl.type, type, sizeof (vl.type));
141 if (plugin_instance != NULL)
142 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
143 if (type_instance != NULL)
144 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
146 ds = plugin_get_ds (type);
148 print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
149 sfree (identifier_copy);
153 /* Free identifier_copy */
155 plugin = NULL; plugin_instance = NULL;
156 type = NULL; type_instance = NULL;
157 sfree (identifier_copy);
159 vl.values_len = ds->ds_num;
160 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
161 if (vl.values == NULL)
163 print_to_socket (fh, "-1 malloc failed.\n");
167 /* All the remaining fields are part of the optionlist. */
168 values_submitted = 0;
174 status = parse_option (&buffer, &string, &value);
177 /* parse_option failed, buffer has been modified.
178 * => we need to abort */
179 print_to_socket (fh, "-1 Misformatted option.\n");
183 else if (status == 0)
185 assert (string != NULL);
186 assert (value != NULL);
187 set_option (&vl, string, value);
190 /* else: parse_option but buffer has not been modified. This is
191 * the default if no `=' is found.. */
193 status = parse_string (&buffer, &string);
196 print_to_socket (fh, "-1 Misformatted value.\n");
200 assert (string != NULL);
202 status = parse_values (string, &vl, ds);
205 print_to_socket (fh, "-1 Parsing the values string failed.\n");
210 plugin_dispatch_values (&vl);
212 } /* while (*buffer != 0) */
213 /* Done parsing the options. */
215 print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
217 (values_submitted == 1) ? "value has" : "values have");
221 } /* int handle_putval */
223 int create_putval (char *ret, size_t ret_len, /* {{{ */
224 const data_set_t *ds, const value_list_t *vl)
226 char buffer_ident[6 * DATA_MAX_NAME_LEN];
227 char buffer_values[1024];
230 status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
233 escape_string (buffer_ident, sizeof (buffer_ident));
235 status = format_values (buffer_values, sizeof (buffer_values),
236 ds, vl, /* store rates = */ 0);
239 escape_string (buffer_values, sizeof (buffer_values));
241 ssnprintf (ret, ret_len,
242 "PUTVAL %s interval=%.3f %s",
245 ? CDTIME_T_TO_DOUBLE (vl->interval)
246 : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
250 } /* }}} int create_putval */