36419c6a55e85da02632bc76b7f2e355206542e1
[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 (char *buffer,
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;
81
82         char *identifier_copy;
83
84         const data_set_t *ds;
85         value_list_t vl = VALUE_LIST_INIT;
86         vl.values = NULL;
87
88         identifier = NULL;
89         status = parse_string (&buffer, &identifier);
90         if (status != 0)
91         {
92                 cmd_error (CMD_PARSE_ERROR, err, "Cannot parse identifier.");
93                 return (CMD_PARSE_ERROR);
94         }
95         assert (identifier != NULL);
96
97         /* parse_identifier() modifies its first argument,
98          * returning pointers into it */
99         identifier_copy = sstrdup (identifier);
100
101         status = parse_identifier (identifier_copy, &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);
108                 cmd_error (CMD_PARSE_ERROR, err, "Cannot parse identifier `%s'.",
109                                 identifier);
110                 sfree (identifier_copy);
111                 return (CMD_PARSE_ERROR);
112         }
113
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         }
125
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));
133
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         }
141
142         /* Free identifier_copy */
143         hostname = NULL;
144         plugin = NULL; plugin_instance = NULL;
145         type = NULL;   type_instance = NULL;
146         sfree (identifier_copy);
147
148         vl.values_len = ds->ds_num;
149         vl.values = malloc (vl.values_len * sizeof (*vl.values));
150         if (vl.values == NULL)
151         {
152                 cmd_error (CMD_ERROR, err, "malloc failed.");
153                 return (CMD_ERROR);
154         }
155
156         ret_putval->identifier = strdup (identifier);
157         if (ret_putval->identifier == NULL)
158         {
159                 cmd_error (CMD_ERROR, err, "malloc failed.");
160                 cmd_destroy_putval (ret_putval);
161                 return (CMD_ERROR);
162         }
163
164         /* All the remaining fields are part of the optionlist. */
165         while (*buffer != 0)
166         {
167                 value_list_t *tmp;
168
169                 char *string = NULL;
170                 char *value  = NULL;
171
172                 status = parse_option (&buffer, &string, &value);
173                 if (status < 0)
174                 {
175                         /* parse_option failed, buffer has been modified.
176                          * => we need to abort */
177                         cmd_error (CMD_PARSE_ERROR, err, "Misformatted option.");
178                         cmd_destroy_putval (ret_putval);
179                         return (CMD_PARSE_ERROR);
180                 }
181                 else if (status == 0)
182                 {
183                         assert (string != NULL);
184                         assert (value != NULL);
185                         set_option (&vl, string, value);
186                         continue;
187                 }
188                 /* else: parse_option but buffer has not been modified. This is
189                  * the default if no `=' is found.. */
190
191                 status = parse_string (&buffer, &string);
192                 if (status != 0)
193                 {
194                         cmd_error (CMD_PARSE_ERROR, err, "Misformatted value.");
195                         cmd_destroy_putval (ret_putval);
196                         return (CMD_PARSE_ERROR);
197                 }
198                 assert (string != NULL);
199
200                 status = parse_values (string, &vl, ds);
201                 if (status != 0)
202                 {
203                         cmd_error (CMD_PARSE_ERROR, err, "Parsing the values string failed.");
204                         cmd_destroy_putval (ret_putval);
205                         return (CMD_PARSE_ERROR);
206                 }
207
208                 tmp = (value_list_t *) realloc (ret_putval->vl,
209                                 (ret_putval->vl_num + 1) * sizeof(*ret_putval->vl));
210                 if (tmp == NULL)
211                 {
212                         cmd_error (CMD_ERROR, err, "realloc failed.");
213                         cmd_destroy_putval (ret_putval);
214                         return (CMD_ERROR);
215                 }
216
217                 ret_putval->vl = tmp;
218                 ret_putval->vl_num++;
219                 memcpy (&ret_putval->vl[ret_putval->vl_num - 1], &vl, sizeof (vl));
220         } /* while (*buffer != 0) */
221         /* Done parsing the options. */
222
223         return (CMD_OK);
224 } /* cmd_status_t cmd_parse_putval */
225
226 void cmd_destroy_putval (cmd_putval_t *putval)
227 {
228         size_t i;
229
230         if (putval == NULL)
231                 return;
232
233         sfree (putval->identifier);
234
235         for (i = 0; i < putval->vl_num; ++i)
236         {
237                 sfree (putval->vl[i].values);
238                 meta_data_destroy (putval->vl[i].meta);
239                 putval->vl[i].meta = NULL;
240         }
241         sfree (putval->vl);
242         putval->vl = NULL;
243         putval->vl_num = 0;
244 } /* void cmd_destroy_putval */
245
246 cmd_status_t cmd_handle_putval (FILE *fh, char *buffer)
247 {
248         cmd_error_handler_t err = { cmd_error_fh, fh };
249         cmd_t cmd;
250         size_t i;
251
252         int status;
253
254         DEBUG ("utils_cmd_putval: cmd_handle_putval (fh = %p, buffer = %s);",
255                         (void *) fh, buffer);
256
257         if ((status = cmd_parse (buffer, &cmd, &err)) != CMD_OK)
258                 return (status);
259         if (cmd.type != CMD_PUTVAL)
260         {
261                 cmd_error (CMD_UNKNOWN_COMMAND, &err, "Unexpected command: `%s'.",
262                                 CMD_TO_STRING (cmd.type));
263                 cmd_destroy (&cmd);
264                 return (CMD_UNKNOWN_COMMAND);
265         }
266
267         for (i = 0; i < cmd.cmd.putval.vl_num; ++i)
268                 plugin_dispatch_values (&cmd.cmd.putval.vl[i]);
269
270         if (fh != stdout)
271                 cmd_error (CMD_OK, &err, "Success: %i %s been dispatched.",
272                                 (int)cmd.cmd.putval.vl_num,
273                                 (cmd.cmd.putval.vl_num == 1) ? "value has" : "values have");
274
275         cmd_destroy (&cmd);
276         return (CMD_OK);
277 } /* int cmd_handle_putval */
278
279 int cmd_create_putval (char *ret, size_t ret_len, /* {{{ */
280         const data_set_t *ds, const value_list_t *vl)
281 {
282         char buffer_ident[6 * DATA_MAX_NAME_LEN];
283         char buffer_values[1024];
284         int status;
285
286         status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
287         if (status != 0)
288                 return (status);
289         escape_string (buffer_ident, sizeof (buffer_ident));
290
291         status = format_values (buffer_values, sizeof (buffer_values),
292                         ds, vl, /* store rates = */ 0);
293         if (status != 0)
294                 return (status);
295         escape_string (buffer_values, sizeof (buffer_values));
296
297         ssnprintf (ret, ret_len,
298                         "PUTVAL %s interval=%.3f %s",
299                         buffer_ident,
300                         (vl->interval > 0)
301                         ? CDTIME_T_TO_DOUBLE (vl->interval)
302                         : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
303                         buffer_values);
304
305         return (0);
306 } /* }}} int cmd_create_putval */