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_cmd_putval.h"
35 #include "utils_cmd_putval.h"
36 #include "utils_cmds.h"
37 #include "utils_parse_option.h"
39 /*
40 * private helper functions
41 */
43 static int set_option(value_list_t *vl, const char *key, const char *value) {
44 if ((vl == NULL) || (key == NULL) || (value == NULL))
45 return (-1);
47 if (strcasecmp("interval", key) == 0) {
48 double tmp;
49 char *endptr;
51 endptr = NULL;
52 errno = 0;
53 tmp = strtod(value, &endptr);
55 if ((errno == 0) && (endptr != NULL) && (endptr != value) && (tmp > 0.0))
56 vl->interval = DOUBLE_TO_CDTIME_T(tmp);
57 } else
58 return (1);
60 return (0);
61 } /* int set_option */
63 /*
64 * public API
65 */
67 cmd_status_t cmd_parse_putval(size_t argc, char **argv,
68 cmd_putval_t *ret_putval,
69 const cmd_options_t *opts,
70 cmd_error_handler_t *err) {
71 cmd_status_t result;
73 char *identifier;
74 char *hostname;
75 char *plugin;
76 char *plugin_instance;
77 char *type;
78 char *type_instance;
79 int status;
81 char *identifier_copy;
83 const data_set_t *ds;
84 value_list_t vl = VALUE_LIST_INIT;
86 if ((ret_putval == NULL) || (opts == NULL)) {
87 errno = EINVAL;
88 cmd_error(CMD_ERROR, err, "Invalid arguments to cmd_parse_putval.");
89 return (CMD_ERROR);
90 }
92 if (argc < 2) {
93 cmd_error(CMD_PARSE_ERROR, err, "Missing identifier and/or value-list.");
94 return (CMD_PARSE_ERROR);
95 }
97 identifier = argv[0];
99 /* parse_identifier() modifies its first argument, returning pointers into
100 * it; retain the old value for later. */
101 identifier_copy = sstrdup(identifier);
103 status =
104 parse_identifier(identifier, &hostname, &plugin, &plugin_instance, &type,
105 &type_instance, opts->identifier_default_host);
106 if (status != 0) {
107 DEBUG("cmd_handle_putval: Cannot parse identifier `%s'.", 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 cmd_error(CMD_PARSE_ERROR, err, "Identifier too long.");
121 sfree(identifier_copy);
122 return (CMD_PARSE_ERROR);
123 }
125 sstrncpy(vl.host, hostname, sizeof(vl.host));
126 sstrncpy(vl.plugin, plugin, sizeof(vl.plugin));
127 sstrncpy(vl.type, type, sizeof(vl.type));
128 if (plugin_instance != NULL)
129 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
130 if (type_instance != NULL)
131 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
133 ds = plugin_get_ds(type);
134 if (ds == NULL) {
135 cmd_error(CMD_PARSE_ERROR, err, "1 Type `%s' isn't defined.", type);
136 sfree(identifier_copy);
137 return (CMD_PARSE_ERROR);
138 }
140 hostname = NULL;
141 plugin = NULL;
142 plugin_instance = NULL;
143 type = NULL;
144 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 cmd_error(CMD_ERROR, err, "malloc failed.");
150 sfree(identifier_copy);
151 return (CMD_ERROR);
152 }
154 ret_putval->raw_identifier = identifier_copy;
155 if (ret_putval->raw_identifier == NULL) {
156 cmd_error(CMD_ERROR, err, "malloc failed.");
157 cmd_destroy_putval(ret_putval);
158 sfree(vl.values);
159 return (CMD_ERROR);
160 }
162 /* All the remaining fields are part of the option list. */
163 result = CMD_OK;
164 for (size_t i = 1; i < argc; ++i) {
165 value_list_t *tmp;
167 char *key = NULL;
168 char *value = NULL;
170 status = cmd_parse_option(argv[i], &key, &value, err);
171 if (status == CMD_OK) {
172 assert(key != NULL);
173 assert(value != NULL);
174 set_option(&vl, key, value);
175 continue;
176 } else if (status != CMD_NO_OPTION) {
177 /* parse_option failed, buffer has been modified.
178 * => we need to abort */
179 result = status;
180 break;
181 }
182 /* else: cmd_parse_option did not find an option; treat this as a
183 * value list. */
185 status = parse_values(argv[i], &vl, ds);
186 if (status != 0) {
187 cmd_error(CMD_PARSE_ERROR, err, "Parsing the values string failed.");
188 result = CMD_PARSE_ERROR;
189 break;
190 }
192 tmp = (value_list_t *)realloc(ret_putval->vl, (ret_putval->vl_num + 1) *
193 sizeof(*ret_putval->vl));
194 if (tmp == NULL) {
195 cmd_error(CMD_ERROR, err, "realloc failed.");
196 cmd_destroy_putval(ret_putval);
197 result = CMD_ERROR;
198 break;
199 }
201 ret_putval->vl = tmp;
202 ret_putval->vl_num++;
203 memcpy(&ret_putval->vl[ret_putval->vl_num - 1], &vl, sizeof(vl));
204 } /* while (*buffer != 0) */
205 /* Done parsing the options. */
207 if (result != CMD_OK) {
208 if (ret_putval->vl_num == 0)
209 sfree(vl.values);
210 cmd_destroy_putval(ret_putval);
211 }
213 return (result);
214 } /* cmd_status_t cmd_parse_putval */
216 void cmd_destroy_putval(cmd_putval_t *putval) {
217 if (putval == NULL)
218 return;
220 sfree(putval->raw_identifier);
222 for (size_t i = 0; i < putval->vl_num; ++i) {
223 if (i == 0) /* values is shared between all entries */
224 sfree(putval->vl[i].values);
225 meta_data_destroy(putval->vl[i].meta);
226 putval->vl[i].meta = NULL;
227 }
228 sfree(putval->vl);
229 putval->vl = NULL;
230 putval->vl_num = 0;
231 } /* void cmd_destroy_putval */
233 cmd_status_t cmd_handle_putval(FILE *fh, char *buffer) {
234 cmd_error_handler_t err = {cmd_error_fh, fh};
235 cmd_t cmd;
237 int status;
239 DEBUG("utils_cmd_putval: cmd_handle_putval (fh = %p, buffer = %s);",
240 (void *)fh, buffer);
242 if ((status = cmd_parse(buffer, &cmd, NULL, &err)) != CMD_OK)
243 return (status);
244 if (cmd.type != CMD_PUTVAL) {
245 cmd_error(CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
246 CMD_TO_STRING(cmd.type));
247 cmd_destroy(&cmd);
248 return (CMD_UNKNOWN_COMMAND);
249 }
251 for (size_t i = 0; i < cmd.cmd.putval.vl_num; ++i)
252 plugin_dispatch_values(&cmd.cmd.putval.vl[i]);
254 if (fh != stdout)
255 cmd_error(CMD_OK, &err, "Success: %i %s been dispatched.",
256 (int)cmd.cmd.putval.vl_num,
257 (cmd.cmd.putval.vl_num == 1) ? "value has" : "values have");
259 cmd_destroy(&cmd);
260 return (CMD_OK);
261 } /* int cmd_handle_putval */
263 int cmd_create_putval(char *ret, size_t ret_len, /* {{{ */
264 const data_set_t *ds, const value_list_t *vl) {
265 char buffer_ident[6 * DATA_MAX_NAME_LEN];
266 char buffer_values[1024];
267 int status;
269 status = FORMAT_VL(buffer_ident, sizeof(buffer_ident), vl);
270 if (status != 0)
271 return (status);
272 escape_string(buffer_ident, sizeof(buffer_ident));
274 status = format_values(buffer_values, sizeof(buffer_values), ds, vl,
275 /* store rates = */ 0);
276 if (status != 0)
277 return (status);
278 escape_string(buffer_values, sizeof(buffer_values));
280 ssnprintf(ret, ret_len, "PUTVAL %s interval=%.3f %s", buffer_ident,
281 (vl->interval > 0) ? CDTIME_T_TO_DOUBLE(vl->interval)
282 : CDTIME_T_TO_DOUBLE(plugin_get_interval()),
283 buffer_values);
285 return (0);
286 } /* }}} int cmd_create_putval */