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