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