{GPL, other}: Relicense to MIT license.
[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         if (fprintf (fh, __VA_ARGS__) < 0) { \
35                 char errbuf[1024]; \
36                 WARNING ("handle_putval: failed to write to socket #%i: %s", \
37                                 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
38                 return -1; \
39         }
40
41 static int dispatch_values (const data_set_t *ds, value_list_t *vl,
42                 FILE *fh, char *buffer)
43 {
44         int status;
45
46         status = parse_values (buffer, vl, ds);
47         if (status != 0)
48         {
49                 print_to_socket (fh, "-1 Parsing the values string failed.\n");
50                 return (-1);
51         }
52
53         plugin_dispatch_values (vl);
54         return (0);
55 } /* int dispatch_values */
56
57 static int set_option (value_list_t *vl, const char *key, const char *value)
58 {
59         if ((vl == NULL) || (key == NULL) || (value == NULL))
60                 return (-1);
61
62         if (strcasecmp ("interval", key) == 0)
63         {
64                 double tmp;
65                 char *endptr;
66
67                 endptr = NULL;
68                 errno = 0;
69                 tmp = strtod (value, &endptr);
70
71                 if ((errno == 0) && (endptr != NULL)
72                                 && (endptr != value) && (tmp > 0.0))
73                         vl->interval = DOUBLE_TO_CDTIME_T (tmp);
74         }
75         else
76                 return (1);
77
78         return (0);
79 } /* int parse_option */
80
81 int handle_putval (FILE *fh, char *buffer)
82 {
83         char *command;
84         char *identifier;
85         char *hostname;
86         char *plugin;
87         char *plugin_instance;
88         char *type;
89         char *type_instance;
90         int   status;
91         int   values_submitted;
92
93         char *identifier_copy;
94
95         const data_set_t *ds;
96         value_list_t vl = VALUE_LIST_INIT;
97
98         DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
99                         (void *) fh, buffer);
100
101         command = NULL;
102         status = parse_string (&buffer, &command);
103         if (status != 0)
104         {
105                 print_to_socket (fh, "-1 Cannot parse command.\n");
106                 return (-1);
107         }
108         assert (command != NULL);
109
110         if (strcasecmp ("PUTVAL", command) != 0)
111         {
112                 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
113                 return (-1);
114         }
115
116         identifier = NULL;
117         status = parse_string (&buffer, &identifier);
118         if (status != 0)
119         {
120                 print_to_socket (fh, "-1 Cannot parse identifier.\n");
121                 return (-1);
122         }
123         assert (identifier != NULL);
124
125         /* parse_identifier() modifies its first argument,
126          * returning pointers into it */
127         identifier_copy = sstrdup (identifier);
128
129         status = parse_identifier (identifier_copy, &hostname,
130                         &plugin, &plugin_instance,
131                         &type, &type_instance);
132         if (status != 0)
133         {
134                 DEBUG ("handle_putval: Cannot parse identifier `%s'.",
135                                 identifier);
136                 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
137                                 identifier);
138                 sfree (identifier_copy);
139                 return (-1);
140         }
141
142         if ((strlen (hostname) >= sizeof (vl.host))
143                         || (strlen (plugin) >= sizeof (vl.plugin))
144                         || ((plugin_instance != NULL)
145                                 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
146                         || ((type_instance != NULL)
147                                 && (strlen (type_instance) >= sizeof (vl.type_instance))))
148         {
149                 print_to_socket (fh, "-1 Identifier too long.\n");
150                 sfree (identifier_copy);
151                 return (-1);
152         }
153
154         sstrncpy (vl.host, hostname, sizeof (vl.host));
155         sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
156         sstrncpy (vl.type, type, sizeof (vl.type));
157         if (plugin_instance != NULL)
158                 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
159         if (type_instance != NULL)
160                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
161
162         ds = plugin_get_ds (type);
163         if (ds == NULL) {
164                 print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
165                 sfree (identifier_copy);
166                 return (-1);
167         }
168
169         /* Free identifier_copy */
170         hostname = NULL;
171         plugin = NULL; plugin_instance = NULL;
172         type = NULL;   type_instance = NULL;
173         sfree (identifier_copy);
174
175         vl.values_len = ds->ds_num;
176         vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
177         if (vl.values == NULL)
178         {
179                 print_to_socket (fh, "-1 malloc failed.\n");
180                 return (-1);
181         }
182
183         /* All the remaining fields are part of the optionlist. */
184         values_submitted = 0;
185         while (*buffer != 0)
186         {
187                 char *string = NULL;
188                 char *value  = NULL;
189
190                 status = parse_option (&buffer, &string, &value);
191                 if (status < 0)
192                 {
193                         /* parse_option failed, buffer has been modified.
194                          * => we need to abort */
195                         print_to_socket (fh, "-1 Misformatted option.\n");
196                         return (-1);
197                 }
198                 else if (status == 0)
199                 {
200                         assert (string != NULL);
201                         assert (value != NULL);
202                         set_option (&vl, string, value);
203                         continue;
204                 }
205                 /* else: parse_option but buffer has not been modified. This is
206                  * the default if no `=' is found.. */
207
208                 status = parse_string (&buffer, &string);
209                 if (status != 0)
210                 {
211                         print_to_socket (fh, "-1 Misformatted value.\n");
212                         return (-1);
213                 }
214                 assert (string != NULL);
215
216                 status = dispatch_values (ds, &vl, fh, string);
217                 if (status != 0)
218                 {
219                         /* An error has already been printed. */
220                         return (-1);
221                 }
222                 values_submitted++;
223         } /* while (*buffer != 0) */
224         /* Done parsing the options. */
225
226         print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
227                         values_submitted,
228                         (values_submitted == 1) ? "value has" : "values have");
229
230         sfree (vl.values); 
231
232         return (0);
233 } /* int handle_putval */
234
235 int create_putval (char *ret, size_t ret_len, /* {{{ */
236         const data_set_t *ds, const value_list_t *vl)
237 {
238         char buffer_ident[6 * DATA_MAX_NAME_LEN];
239         char buffer_values[1024];
240         int status;
241
242         status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
243         if (status != 0)
244                 return (status);
245         escape_string (buffer_ident, sizeof (buffer_ident));
246
247         status = format_values (buffer_values, sizeof (buffer_values),
248                         ds, vl, /* store rates = */ 0);
249         if (status != 0)
250                 return (status);
251         escape_string (buffer_values, sizeof (buffer_values));
252
253         ssnprintf (ret, ret_len,
254                         "PUTVAL %s interval=%.3f %s",
255                         buffer_ident,
256                         (vl->interval > 0)
257                         ? CDTIME_T_TO_DOUBLE (vl->interval)
258                         : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
259                         buffer_values);
260
261         return (0);
262 } /* }}} int create_putval */