Debian Codegraph @

src/utils/cmds/putval.c

raw
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 **/
28
29 #include "collectd.h"
30
31 #include "utils/cmds/putval.h"
32 #include "utils/common/common.h"
33
34 /*
35 * private helper functions
36 */
37
38 static int is_quoted(const char *str, size_t len) {
39 if (len < 2) {
40 return 0;
41 }
42
43 return (str[0] == '"' && str[len - 1] == '"');
44 }
45
46 static int set_option(value_list_t *vl, const char *key, const char *value,
47 cmd_error_handler_t *err) {
48 if ((vl == NULL) || (key == NULL) || (value == NULL))
49 return -1;
50
51 if (strcasecmp("interval", key) == 0) {
52 double tmp;
53 char *endptr;
54
55 endptr = NULL;
56 errno = 0;
57 tmp = strtod(value, &endptr);
58
59 if ((errno == 0) && (endptr != NULL) && (endptr != value) && (tmp > 0.0))
60 vl->interval = DOUBLE_TO_CDTIME_T(tmp);
61 } else if (strncasecmp("meta:", key, 5) == 0) {
62 const char *meta_key = key + 5;
63 size_t value_len = strlen(value);
64
65 if (vl->meta == NULL) {
66 vl->meta = meta_data_create();
67 if (vl->meta == NULL) {
68 return CMD_ERROR;
69 }
70 }
71
72 if (is_quoted(value, value_len)) {
73 int metadata_err;
74 const char *value_str = sstrndup(value + 1, value_len - 2);
75 if (value_str == NULL) {
76 return CMD_ERROR;
77 }
78 metadata_err = meta_data_add_string(vl->meta, meta_key, value_str);
79 free((void *)value_str);
80 if (metadata_err != 0) {
81 return CMD_ERROR;
82 }
83 return CMD_OK;
84 }
85
86 cmd_error(CMD_NO_OPTION, err, "Non-string metadata not supported yet");
87 return CMD_NO_OPTION;
88 } else {
89 return CMD_ERROR;
90 }
91 return CMD_OK;
92 } /* int set_option */
93
94 /*
95 * public API
96 */
97
98 cmd_status_t cmd_parse_putval(size_t argc, char **argv,
99 cmd_putval_t *ret_putval,
100 const cmd_options_t *opts,
101 cmd_error_handler_t *err) {
102 cmd_status_t result;
103
104 char *identifier;
105 char *hostname;
106 char *plugin;
107 char *plugin_instance;
108 char *type;
109 char *type_instance;
110 int status;
111
112 char *identifier_copy;
113
114 const data_set_t *ds;
115 value_list_t vl = VALUE_LIST_INIT;
116
117 if ((ret_putval == NULL) || (opts == NULL)) {
118 errno = EINVAL;
119 cmd_error(CMD_ERROR, err, "Invalid arguments to cmd_parse_putval.");
120 return CMD_ERROR;
121 }
122
123 if (argc < 2) {
124 cmd_error(CMD_PARSE_ERROR, err, "Missing identifier and/or value-list.");
125 return CMD_PARSE_ERROR;
126 }
127
128 identifier = argv[0];
129
130 /* parse_identifier() modifies its first argument, returning pointers into
131 * it; retain the old value for later. */
132 identifier_copy = sstrdup(identifier);
133
134 status =
135 parse_identifier(identifier, &hostname, &plugin, &plugin_instance, &type,
136 &type_instance, opts->identifier_default_host);
137 if (status != 0) {
138 DEBUG("cmd_handle_putval: Cannot parse identifier `%s'.", identifier_copy);
139 cmd_error(CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
140 identifier_copy);
141 sfree(identifier_copy);
142 return CMD_PARSE_ERROR;
143 }
144
145 if ((strlen(hostname) >= sizeof(vl.host)) ||
146 (strlen(plugin) >= sizeof(vl.plugin)) ||
147 ((plugin_instance != NULL) &&
148 (strlen(plugin_instance) >= sizeof(vl.plugin_instance))) ||
149 ((type_instance != NULL) &&
150 (strlen(type_instance) >= sizeof(vl.type_instance)))) {
151 cmd_error(CMD_PARSE_ERROR, err, "Identifier too long.");
152 sfree(identifier_copy);
153 return CMD_PARSE_ERROR;
154 }
155
156 sstrncpy(vl.host, hostname, sizeof(vl.host));
157 sstrncpy(vl.plugin, plugin, sizeof(vl.plugin));
158 sstrncpy(vl.type, type, sizeof(vl.type));
159 if (plugin_instance != NULL)
160 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
161 if (type_instance != NULL)
162 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
163
164 ds = plugin_get_ds(type);
165 if (ds == NULL) {
166 cmd_error(CMD_PARSE_ERROR, err, "1 Type `%s' isn't defined.", type);
167 sfree(identifier_copy);
168 return CMD_PARSE_ERROR;
169 }
170
171 hostname = NULL;
172 plugin = NULL;
173 plugin_instance = NULL;
174 type = NULL;
175 type_instance = NULL;
176
177 ret_putval->raw_identifier = identifier_copy;
178 if (ret_putval->raw_identifier == NULL) {
179 cmd_error(CMD_ERROR, err, "malloc failed.");
180 cmd_destroy_putval(ret_putval);
181 sfree(vl.values);
182 return CMD_ERROR;
183 }
184
185 /* All the remaining fields are part of the option list. */
186 result = CMD_OK;
187 for (size_t i = 1; i < argc; ++i) {
188 value_list_t *tmp;
189
190 char *key = NULL;
191 char *value = NULL;
192
193 status = cmd_parse_option(argv[i], &key, &value, err);
194 if (status == CMD_OK) {
195 int option_err;
196
197 assert(key != NULL);
198 assert(value != NULL);
199 option_err = set_option(&vl, key, value, err);
200 if (option_err != CMD_OK && option_err != CMD_NO_OPTION) {
201 result = option_err;
202 break;
203 }
204 continue;
205 } else if (status != CMD_NO_OPTION) {
206 /* parse_option failed, buffer has been modified.
207 * => we need to abort */
208 result = status;
209 break;
210 }
211 /* else: cmd_parse_option did not find an option; treat this as a
212 * value list. */
213
214 vl.values_len = ds->ds_num;
215 vl.values = calloc(vl.values_len, sizeof(*vl.values));
216 if (vl.values == NULL) {
217 cmd_error(CMD_ERROR, err, "malloc failed.");
218 result = CMD_ERROR;
219 break;
220 }
221
222 status = parse_values(argv[i], &vl, ds);
223 if (status != 0) {
224 cmd_error(CMD_PARSE_ERROR, err, "Parsing the values string failed.");
225 result = CMD_PARSE_ERROR;
226 vl.values_len = 0;
227 sfree(vl.values);
228 break;
229 }
230
231 tmp = realloc(ret_putval->vl,
232 (ret_putval->vl_num + 1) * sizeof(*ret_putval->vl));
233 if (tmp == NULL) {
234 cmd_error(CMD_ERROR, err, "realloc failed.");
235 cmd_destroy_putval(ret_putval);
236 result = CMD_ERROR;
237 vl.values_len = 0;
238 sfree(vl.values);
239 break;
240 }
241
242 ret_putval->vl = tmp;
243 ret_putval->vl_num++;
244 memcpy(&ret_putval->vl[ret_putval->vl_num - 1], &vl, sizeof(vl));
245
246 /* pointer is now owned by ret_putval->vl[] */
247 vl.values_len = 0;
248 vl.values = NULL;
249 } /* while (*buffer != 0) */
250 /* Done parsing the options. */
251
252 if (result != CMD_OK)
253 cmd_destroy_putval(ret_putval);
254
255 return result;
256 } /* cmd_status_t cmd_parse_putval */
257
258 void cmd_destroy_putval(cmd_putval_t *putval) {
259 if (putval == NULL)
260 return;
261
262 sfree(putval->raw_identifier);
263
264 for (size_t i = 0; i < putval->vl_num; ++i) {
265 sfree(putval->vl[i].values);
266 meta_data_destroy(putval->vl[i].meta);
267 putval->vl[i].meta = NULL;
268 }
269 sfree(putval->vl);
270 putval->vl = NULL;
271 putval->vl_num = 0;
272 } /* void cmd_destroy_putval */
273
274 cmd_status_t cmd_handle_putval(FILE *fh, char *buffer) {
275 cmd_error_handler_t err = {cmd_error_fh, fh};
276 cmd_t cmd;
277
278 int status;
279
280 DEBUG("utils_cmd_putval: cmd_handle_putval (fh = %p, buffer = %s);",
281 (void *)fh, buffer);
282
283 if ((status = cmd_parse(buffer, &cmd, NULL, &err)) != CMD_OK)
284 return status;
285 if (cmd.type != CMD_PUTVAL) {
286 cmd_error(CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
287 CMD_TO_STRING(cmd.type));
288 cmd_destroy(&cmd);
289 return CMD_UNKNOWN_COMMAND;
290 }
291
292 for (size_t i = 0; i < cmd.cmd.putval.vl_num; ++i)
293 plugin_dispatch_values(&cmd.cmd.putval.vl[i]);
294
295 if (fh != stdout)
296 cmd_error(CMD_OK, &err, "Success: %i %s been dispatched.",
297 (int)cmd.cmd.putval.vl_num,
298 (cmd.cmd.putval.vl_num == 1) ? "value has" : "values have");
299
300 cmd_destroy(&cmd);
301 return CMD_OK;
302 } /* int cmd_handle_putval */
303
304 int cmd_create_putval(char *ret, size_t ret_len, /* {{{ */
305 const data_set_t *ds, const value_list_t *vl) {
306 char buffer_ident[6 * DATA_MAX_NAME_LEN];
307 char buffer_values[1024];
308 int status;
309
310 status = FORMAT_VL(buffer_ident, sizeof(buffer_ident), vl);
311 if (status != 0)
312 return status;
313 escape_string(buffer_ident, sizeof(buffer_ident));
314
315 status = format_values(buffer_values, sizeof(buffer_values), ds, vl,
316 /* store rates = */ false);
317 if (status != 0)
318 return status;
319 escape_string(buffer_values, sizeof(buffer_values));
320
321 snprintf(ret, ret_len, "PUTVAL %s interval=%.3f %s", buffer_ident,
322 (vl->interval > 0) ? CDTIME_T_TO_DOUBLE(vl->interval)
323 : CDTIME_T_TO_DOUBLE(plugin_get_interval()),
324 buffer_values);
325
326 return 0;
327 } /* }}} int cmd_create_putval */