Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / utils_cmd_putval.c
1 /**
2  * collectd - src/utils_cmd_putval.c
3  * Copyright (C) 2007-2009  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30
31 #include "utils_parse_option.h"
32
33 #define print_to_socket(fh, ...) \
34     do { \
35         if (fprintf (fh, __VA_ARGS__) < 0) { \
36             char errbuf[1024]; \
37             WARNING ("handle_putval: failed to write to socket #%i: %s", \
38                     fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
39             sfree (vl.values); \
40             return -1; \
41         } \
42         fflush(fh); \
43     } while (0)
44
45 static int set_option (value_list_t *vl, const char *key, const char *value)
46 {
47         if ((vl == NULL) || (key == NULL) || (value == NULL))
48                 return (-1);
49
50         if (strcasecmp ("interval", key) == 0)
51         {
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)
60                                 && (endptr != value) && (tmp > 0.0))
61                         vl->interval = DOUBLE_TO_CDTIME_T (tmp);
62         }
63         else
64                 return (1);
65
66         return (0);
67 } /* int parse_option */
68
69 int handle_putval (FILE *fh, char *buffer)
70 {
71         char *command;
72         char *identifier;
73         char *hostname;
74         char *plugin;
75         char *plugin_instance;
76         char *type;
77         char *type_instance;
78         int   status;
79         int   values_submitted;
80
81         char *identifier_copy;
82
83         const data_set_t *ds;
84         value_list_t vl = VALUE_LIST_INIT;
85         vl.values = NULL;
86
87         DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
88                         (void *) fh, buffer);
89
90         command = NULL;
91         status = parse_string (&buffer, &command);
92         if (status != 0)
93         {
94                 print_to_socket (fh, "-1 Cannot parse command.\n");
95                 return (-1);
96         }
97         assert (command != NULL);
98
99         if (strcasecmp ("PUTVAL", command) != 0)
100         {
101                 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
102                 return (-1);
103         }
104
105         identifier = NULL;
106         status = parse_string (&buffer, &identifier);
107         if (status != 0)
108         {
109                 print_to_socket (fh, "-1 Cannot parse identifier.\n");
110                 return (-1);
111         }
112         assert (identifier != NULL);
113
114         /* parse_identifier() modifies its first argument,
115          * returning pointers into it */
116         identifier_copy = sstrdup (identifier);
117
118         status = parse_identifier (identifier_copy, &hostname,
119                         &plugin, &plugin_instance,
120                         &type, &type_instance);
121         if (status != 0)
122         {
123                 DEBUG ("handle_putval: Cannot parse identifier `%s'.",
124                                 identifier);
125                 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
126                                 identifier);
127                 sfree (identifier_copy);
128                 return (-1);
129         }
130
131         if ((strlen (hostname) >= sizeof (vl.host))
132                         || (strlen (plugin) >= sizeof (vl.plugin))
133                         || ((plugin_instance != NULL)
134                                 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
135                         || ((type_instance != NULL)
136                                 && (strlen (type_instance) >= sizeof (vl.type_instance))))
137         {
138                 print_to_socket (fh, "-1 Identifier too long.\n");
139                 sfree (identifier_copy);
140                 return (-1);
141         }
142
143         sstrncpy (vl.host, hostname, sizeof (vl.host));
144         sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
145         sstrncpy (vl.type, type, sizeof (vl.type));
146         if (plugin_instance != NULL)
147                 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
148         if (type_instance != NULL)
149                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
150
151         ds = plugin_get_ds (type);
152         if (ds == NULL) {
153                 print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
154                 sfree (identifier_copy);
155                 return (-1);
156         }
157
158         /* Free identifier_copy */
159         hostname = NULL;
160         plugin = NULL; plugin_instance = NULL;
161         type = NULL;   type_instance = NULL;
162         sfree (identifier_copy);
163
164         vl.values_len = ds->ds_num;
165         vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
166         if (vl.values == NULL)
167         {
168                 print_to_socket (fh, "-1 malloc failed.\n");
169                 return (-1);
170         }
171
172         /* All the remaining fields are part of the optionlist. */
173         values_submitted = 0;
174         while (*buffer != 0)
175         {
176                 char *string = NULL;
177                 char *value  = NULL;
178
179                 status = parse_option (&buffer, &string, &value);
180                 if (status < 0)
181                 {
182                         /* parse_option failed, buffer has been modified.
183                          * => we need to abort */
184                         print_to_socket (fh, "-1 Misformatted option.\n");
185                         sfree (vl.values);
186                         return (-1);
187                 }
188                 else if (status == 0)
189                 {
190                         assert (string != NULL);
191                         assert (value != NULL);
192                         set_option (&vl, string, value);
193                         continue;
194                 }
195                 /* else: parse_option but buffer has not been modified. This is
196                  * the default if no `=' is found.. */
197
198                 status = parse_string (&buffer, &string);
199                 if (status != 0)
200                 {
201                         print_to_socket (fh, "-1 Misformatted value.\n");
202                         sfree (vl.values);
203                         return (-1);
204                 }
205                 assert (string != NULL);
206
207                 status = parse_values (string, &vl, ds);
208                 if (status != 0)
209                 {
210                         print_to_socket (fh, "-1 Parsing the values string failed.\n");
211                         sfree (vl.values);
212                         return (-1);
213                 }
214
215                 plugin_dispatch_values (&vl);
216                 values_submitted++;
217         } /* while (*buffer != 0) */
218         /* Done parsing the options. */
219
220         print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
221                         values_submitted,
222                         (values_submitted == 1) ? "value has" : "values have");
223
224         sfree (vl.values);
225         return (0);
226 } /* int handle_putval */
227
228 int create_putval (char *ret, size_t ret_len, /* {{{ */
229         const data_set_t *ds, const value_list_t *vl)
230 {
231         char buffer_ident[6 * DATA_MAX_NAME_LEN];
232         char buffer_values[1024];
233         int status;
234
235         status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
236         if (status != 0)
237                 return (status);
238         escape_string (buffer_ident, sizeof (buffer_ident));
239
240         status = format_values (buffer_values, sizeof (buffer_values),
241                         ds, vl, /* store rates = */ 0);
242         if (status != 0)
243                 return (status);
244         escape_string (buffer_values, sizeof (buffer_values));
245
246         ssnprintf (ret, ret_len,
247                         "PUTVAL %s interval=%.3f %s",
248                         buffer_ident,
249                         (vl->interval > 0)
250                         ? CDTIME_T_TO_DOUBLE (vl->interval)
251                         : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
252                         buffer_values);
253
254         return (0);
255 } /* }}} int create_putval */