1 /**
2 * collectd - src/utils_cmd_putval.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2016 Sebastian tokkee Harl
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 * Florian octo Forster <octo at collectd.org>
26 * Sebastian tokkee Harl <sh at tokkee.org>
27 **/
29 #include "collectd.h"
31 #include "common.h"
32 #include "plugin.h"
34 #include "utils_cmds.h"
35 #include "utils_cmd_putval.h"
36 #include "utils_parse_option.h"
37 #include "utils_cmd_putval.h"
39 /*
40 * private helper functions
41 */
43 static int set_option (value_list_t *vl, const char *key, const char *value)
44 {
45 if ((vl == NULL) || (key == NULL) || (value == NULL))
46 return (-1);
48 if (strcasecmp ("interval", key) == 0)
49 {
50 double tmp;
51 char *endptr;
53 endptr = NULL;
54 errno = 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);
60 }
61 else
62 return (1);
64 return (0);
65 } /* int set_option */
67 /*
68 * public API
69 */
71 cmd_status_t cmd_parse_putval (size_t argc, char **argv,
72 cmd_putval_t *ret_putval, cmd_error_handler_t *err)
73 {
74 char *identifier;
75 char *hostname;
76 char *plugin;
77 char *plugin_instance;
78 char *type;
79 char *type_instance;
80 int status;
82 char *identifier_copy;
84 const data_set_t *ds;
85 value_list_t vl = VALUE_LIST_INIT;
86 size_t i;
88 if (argc < 2)
89 {
90 cmd_error (CMD_PARSE_ERROR, err,
91 "Missing identifier and/or value-list.");
92 return (CMD_PARSE_ERROR);
93 }
95 identifier = argv[0];
97 /* parse_identifier() modifies its first argument, returning pointers into
98 * it; retain the old value for later. */
99 identifier_copy = sstrdup (identifier);
101 status = parse_identifier (identifier, &hostname,
102 &plugin, &plugin_instance,
103 &type, &type_instance);
104 if (status != 0)
105 {
106 DEBUG ("cmd_handle_putval: Cannot parse identifier `%s'.",
107 identifier_copy);
108 cmd_error (CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
109 identifier_copy);
110 sfree (identifier_copy);
111 return (CMD_PARSE_ERROR);
112 }
114 if ((strlen (hostname) >= sizeof (vl.host))
115 || (strlen (plugin) >= sizeof (vl.plugin))
116 || ((plugin_instance != NULL)
117 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
118 || ((type_instance != NULL)
119 && (strlen (type_instance) >= sizeof (vl.type_instance))))
120 {
121 cmd_error (CMD_PARSE_ERROR, err, "Identifier too long.");
122 sfree (identifier_copy);
123 return (CMD_PARSE_ERROR);
124 }
126 sstrncpy (vl.host, hostname, sizeof (vl.host));
127 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
128 sstrncpy (vl.type, type, sizeof (vl.type));
129 if (plugin_instance != NULL)
130 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
131 if (type_instance != NULL)
132 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
134 ds = plugin_get_ds (type);
135 if (ds == NULL)
136 {
137 cmd_error (CMD_PARSE_ERROR, err, "1 Type `%s' isn't defined.", type);
138 sfree (identifier_copy);
139 return (CMD_PARSE_ERROR);
140 }
142 hostname = NULL;
143 plugin = NULL; plugin_instance = NULL;
144 type = NULL; type_instance = NULL;
146 vl.values_len = ds->ds_num;
147 vl.values = malloc (vl.values_len * sizeof (*vl.values));
148 if (vl.values == NULL)
149 {
150 cmd_error (CMD_ERROR, err, "malloc failed.");
151 sfree (identifier_copy);
152 return (CMD_ERROR);
153 }
155 ret_putval->raw_identifier = identifier_copy;
156 if (ret_putval->raw_identifier == NULL)
157 {
158 cmd_error (CMD_ERROR, err, "malloc failed.");
159 cmd_destroy_putval (ret_putval);
160 return (CMD_ERROR);
161 }
163 /* All the remaining fields are part of the option list. */
164 for (i = 1; i < argc; ++i)
165 {
166 value_list_t *tmp;
168 char *key = NULL;
169 char *value = NULL;
171 status = cmd_parse_option (argv[i], &key, &value, err);
172 if (status == CMD_OK)
173 {
174 assert (key != NULL);
175 assert (value != NULL);
176 set_option (&vl, key, value);
177 continue;
178 }
179 else if (status != CMD_NO_OPTION)
180 {
181 /* parse_option failed, buffer has been modified.
182 * => we need to abort */
183 cmd_destroy_putval (ret_putval);
184 return (status);
185 }
186 /* else: cmd_parse_option did not find an option; treat this as a
187 * value list. */
189 status = parse_values (argv[i], &vl, ds);
190 if (status != 0)
191 {
192 cmd_error (CMD_PARSE_ERROR, err, "Parsing the values string failed.");
193 cmd_destroy_putval (ret_putval);
194 return (CMD_PARSE_ERROR);
195 }
197 tmp = (value_list_t *) realloc (ret_putval->vl,
198 (ret_putval->vl_num + 1) * sizeof(*ret_putval->vl));
199 if (tmp == NULL)
200 {
201 cmd_error (CMD_ERROR, err, "realloc failed.");
202 cmd_destroy_putval (ret_putval);
203 return (CMD_ERROR);
204 }
206 ret_putval->vl = tmp;
207 ret_putval->vl_num++;
208 memcpy (&ret_putval->vl[ret_putval->vl_num - 1], &vl, sizeof (vl));
209 } /* while (*buffer != 0) */
210 /* Done parsing the options. */
212 return (CMD_OK);
213 } /* cmd_status_t cmd_parse_putval */
215 void cmd_destroy_putval (cmd_putval_t *putval)
216 {
217 size_t i;
219 if (putval == NULL)
220 return;
222 sfree (putval->raw_identifier);
224 for (i = 0; i < putval->vl_num; ++i)
225 {
226 sfree (putval->vl[i].values);
227 meta_data_destroy (putval->vl[i].meta);
228 putval->vl[i].meta = NULL;
229 }
230 sfree (putval->vl);
231 putval->vl = NULL;
232 putval->vl_num = 0;
233 } /* void cmd_destroy_putval */
235 cmd_status_t cmd_handle_putval (FILE *fh, char *buffer)
236 {
237 cmd_error_handler_t err = { cmd_error_fh, fh };
238 cmd_t cmd;
239 size_t i;
241 int status;
243 DEBUG ("utils_cmd_putval: cmd_handle_putval (fh = %p, buffer = %s);",
244 (void *) fh, buffer);
246 if ((status = cmd_parse (buffer, &cmd, &err)) != CMD_OK)
247 return (status);
248 if (cmd.type != CMD_PUTVAL)
249 {
250 cmd_error (CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
251 CMD_TO_STRING (cmd.type));
252 cmd_destroy (&cmd);
253 return (CMD_UNKNOWN_COMMAND);
254 }
256 for (i = 0; i < cmd.cmd.putval.vl_num; ++i)
257 plugin_dispatch_values (&cmd.cmd.putval.vl[i]);
259 if (fh != stdout)
260 cmd_error (CMD_OK, &err, "Success: %i %s been dispatched.",
261 (int)cmd.cmd.putval.vl_num,
262 (cmd.cmd.putval.vl_num == 1) ? "value has" : "values have");
264 cmd_destroy (&cmd);
265 return (CMD_OK);
266 } /* int cmd_handle_putval */
268 int cmd_create_putval (char *ret, size_t ret_len, /* {{{ */
269 const data_set_t *ds, const value_list_t *vl)
270 {
271 char buffer_ident[6 * DATA_MAX_NAME_LEN];
272 char buffer_values[1024];
273 int status;
275 status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
276 if (status != 0)
277 return (status);
278 escape_string (buffer_ident, sizeof (buffer_ident));
280 status = format_values (buffer_values, sizeof (buffer_values),
281 ds, vl, /* store rates = */ 0);
282 if (status != 0)
283 return (status);
284 escape_string (buffer_values, sizeof (buffer_values));
286 ssnprintf (ret, ret_len,
287 "PUTVAL %s interval=%.3f %s",
288 buffer_ident,
289 (vl->interval > 0)
290 ? CDTIME_T_TO_DOUBLE (vl->interval)
291 : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
292 buffer_values);
294 return (0);
295 } /* }}} int cmd_create_putval */