2 * collectd - src/utils_cmd_putval.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2016 Sebastian tokkee Harl
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Florian octo Forster <octo at collectd.org>
26 * Sebastian tokkee Harl <sh at tokkee.org>
34 #include "utils_cmds.h"
35 #include "utils_cmd_putval.h"
36 #include "utils_parse_option.h"
37 #include "utils_cmd_putval.h"
40 * private helper functions
43 static int set_option (value_list_t *vl, const char *key, const char *value)
45 if ((vl == NULL) || (key == NULL) || (value == NULL))
48 if (strcasecmp ("interval", key) == 0)
55 tmp = strtod (value, &endptr);
57 if ((errno == 0) && (endptr != NULL)
58 && (endptr != value) && (tmp > 0.0))
59 vl->interval = DOUBLE_TO_CDTIME_T (tmp);
65 } /* int set_option */
71 cmd_status_t cmd_parse_putval (size_t argc, char **argv,
72 cmd_putval_t *ret_putval, const cmd_options_t *opts,
73 cmd_error_handler_t *err)
80 char *plugin_instance;
85 char *identifier_copy;
88 value_list_t vl = VALUE_LIST_INIT;
90 if ((ret_putval == NULL) || (opts == NULL))
93 cmd_error (CMD_ERROR, err, "Invalid arguments to cmd_parse_putval.");
99 cmd_error (CMD_PARSE_ERROR, err,
100 "Missing identifier and/or value-list.");
101 return (CMD_PARSE_ERROR);
104 identifier = argv[0];
106 /* parse_identifier() modifies its first argument, returning pointers into
107 * it; retain the old value for later. */
108 identifier_copy = sstrdup (identifier);
110 status = parse_identifier (identifier, &hostname,
111 &plugin, &plugin_instance,
112 &type, &type_instance,
113 opts->identifier_default_host);
116 DEBUG ("cmd_handle_putval: Cannot parse identifier `%s'.",
118 cmd_error (CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
120 sfree (identifier_copy);
121 return (CMD_PARSE_ERROR);
124 if ((strlen (hostname) >= sizeof (vl.host))
125 || (strlen (plugin) >= sizeof (vl.plugin))
126 || ((plugin_instance != NULL)
127 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
128 || ((type_instance != NULL)
129 && (strlen (type_instance) >= sizeof (vl.type_instance))))
131 cmd_error (CMD_PARSE_ERROR, err, "Identifier too long.");
132 sfree (identifier_copy);
133 return (CMD_PARSE_ERROR);
136 sstrncpy (vl.host, hostname, sizeof (vl.host));
137 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
138 sstrncpy (vl.type, type, sizeof (vl.type));
139 if (plugin_instance != NULL)
140 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
141 if (type_instance != NULL)
142 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
144 ds = plugin_get_ds (type);
147 cmd_error (CMD_PARSE_ERROR, err, "1 Type `%s' isn't defined.", type);
148 sfree (identifier_copy);
149 return (CMD_PARSE_ERROR);
153 plugin = NULL; plugin_instance = NULL;
154 type = NULL; type_instance = NULL;
156 vl.values_len = ds->ds_num;
157 vl.values = malloc (vl.values_len * sizeof (*vl.values));
158 if (vl.values == NULL)
160 cmd_error (CMD_ERROR, err, "malloc failed.");
161 sfree (identifier_copy);
165 ret_putval->raw_identifier = identifier_copy;
166 if (ret_putval->raw_identifier == NULL)
168 cmd_error (CMD_ERROR, err, "malloc failed.");
169 cmd_destroy_putval (ret_putval);
174 /* All the remaining fields are part of the option list. */
176 for (size_t i = 1; i < argc; ++i)
183 status = cmd_parse_option (argv[i], &key, &value, err);
184 if (status == CMD_OK)
186 assert (key != NULL);
187 assert (value != NULL);
188 set_option (&vl, key, value);
191 else if (status != CMD_NO_OPTION)
193 /* parse_option failed, buffer has been modified.
194 * => we need to abort */
198 /* else: cmd_parse_option did not find an option; treat this as a
201 status = parse_values (argv[i], &vl, ds);
204 cmd_error (CMD_PARSE_ERROR, err, "Parsing the values string failed.");
205 result = CMD_PARSE_ERROR;
209 tmp = (value_list_t *) realloc (ret_putval->vl,
210 (ret_putval->vl_num + 1) * sizeof(*ret_putval->vl));
213 cmd_error (CMD_ERROR, err, "realloc failed.");
214 cmd_destroy_putval (ret_putval);
219 ret_putval->vl = tmp;
220 ret_putval->vl_num++;
221 memcpy (&ret_putval->vl[ret_putval->vl_num - 1], &vl, sizeof (vl));
222 } /* while (*buffer != 0) */
223 /* Done parsing the options. */
225 if (result != CMD_OK)
227 if (ret_putval->vl_num == 0)
229 cmd_destroy_putval (ret_putval);
233 } /* cmd_status_t cmd_parse_putval */
235 void cmd_destroy_putval (cmd_putval_t *putval)
240 sfree (putval->raw_identifier);
242 for (size_t i = 0; i < putval->vl_num; ++i)
244 if (i == 0) /* values is shared between all entries */
245 sfree (putval->vl[i].values);
246 meta_data_destroy (putval->vl[i].meta);
247 putval->vl[i].meta = NULL;
252 } /* void cmd_destroy_putval */
254 cmd_status_t cmd_handle_putval (FILE *fh, char *buffer)
256 cmd_error_handler_t err = { cmd_error_fh, fh };
261 DEBUG ("utils_cmd_putval: cmd_handle_putval (fh = %p, buffer = %s);",
262 (void *) fh, buffer);
264 if ((status = cmd_parse (buffer, &cmd, NULL, &err)) != CMD_OK)
266 if (cmd.type != CMD_PUTVAL)
268 cmd_error (CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
269 CMD_TO_STRING (cmd.type));
271 return (CMD_UNKNOWN_COMMAND);
274 for (size_t i = 0; i < cmd.cmd.putval.vl_num; ++i)
275 plugin_dispatch_values (&cmd.cmd.putval.vl[i]);
278 cmd_error (CMD_OK, &err, "Success: %i %s been dispatched.",
279 (int)cmd.cmd.putval.vl_num,
280 (cmd.cmd.putval.vl_num == 1) ? "value has" : "values have");
284 } /* int cmd_handle_putval */
286 int cmd_create_putval (char *ret, size_t ret_len, /* {{{ */
287 const data_set_t *ds, const value_list_t *vl)
289 char buffer_ident[6 * DATA_MAX_NAME_LEN];
290 char buffer_values[1024];
293 status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
296 escape_string (buffer_ident, sizeof (buffer_ident));
298 status = format_values (buffer_values, sizeof (buffer_values),
299 ds, vl, /* store rates = */ 0);
302 escape_string (buffer_values, sizeof (buffer_values));
304 ssnprintf (ret, ret_len,
305 "PUTVAL %s interval=%.3f %s",
308 ? CDTIME_T_TO_DOUBLE (vl->interval)
309 : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
313 } /* }}} int cmd_create_putval */