Merged branch 'sh/collectd-4.6' into sh/collectd-4.7.
[collectd.git] / src / utils_cmd_putval.c
1 /**
2  * collectd - src/utils_cms_putval.c
3  * Copyright (C) 2007-2009  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include "utils_parse_option.h"
27
28 #define print_to_socket(fh, ...) \
29         if (fprintf (fh, __VA_ARGS__) < 0) { \
30                 char errbuf[1024]; \
31                 WARNING ("handle_putval: failed to write to socket #%i: %s", \
32                                 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
33                 return -1; \
34         }
35
36 static int dispatch_values (const data_set_t *ds, value_list_t *vl,
37                 FILE *fh, char *buffer)
38 {
39         char *dummy;
40         char *ptr;
41         char *saveptr;
42         int i;
43
44         char *time_str = buffer;
45         char *value_str = strchr (time_str, ':');
46         if (value_str == NULL)
47         {
48                 print_to_socket (fh, "-1 No time found.\n");
49                 return (-1);
50         }
51         *value_str = '\0'; value_str++;
52
53         vl->time = (time_t) atoi (time_str);
54
55         i = 0;
56         dummy = value_str;
57         saveptr = NULL;
58         while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
59         {
60                 dummy = NULL;
61
62                 if (i >= vl->values_len)
63                 {
64                         i = vl->values_len + 1;
65                         break;
66                 }
67
68                 if ((strcmp (ptr, "U") == 0) && (ds->ds[i].type == DS_TYPE_GAUGE))
69                         vl->values[i].gauge = NAN;
70                 else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i]))
71                 {
72                         print_to_socket (fh, "-1 Failed to parse value `%s'.\n", ptr);
73                         return (-1);
74                 }
75
76                 i++;
77         } /* while (strtok_r) */
78
79         if (i != vl->values_len)
80         {
81                 char identifier[128];
82                 FORMAT_VL (identifier, sizeof (identifier), vl, ds);
83                 ERROR ("cmd putval: dispatch_values: "
84                                 "Number of values incorrect: "
85                                 "Got %i, expected %i. Identifier is `%s'.",
86                                 i, vl->values_len, identifier);
87                 print_to_socket (fh, "-1 Number of values incorrect: "
88                                 "Got %i, expected %i.\n",
89                                 i, vl->values_len);
90                 return (-1);
91         }
92
93         plugin_dispatch_values (vl);
94         return (0);
95 } /* int dispatch_values */
96
97 static int set_option (value_list_t *vl, const char *key, const char *value)
98 {
99         if ((vl == NULL) || (key == NULL) || (value == NULL))
100                 return (-1);
101
102         if (strcasecmp ("interval", key) == 0)
103         {
104                 int tmp;
105                 char *endptr;
106
107                 endptr = NULL;
108                 errno = 0;
109                 tmp = strtol (value, &endptr, 0);
110
111                 if ((errno == 0) && (endptr != NULL)
112                                 && (endptr != value) && (tmp > 0))
113                         vl->interval = tmp;
114         }
115         else
116                 return (1);
117
118         return (0);
119 } /* int parse_option */
120
121 int handle_putval (FILE *fh, char *buffer)
122 {
123         char *command;
124         char *identifier;
125         char *hostname;
126         char *plugin;
127         char *plugin_instance;
128         char *type;
129         char *type_instance;
130         int   status;
131         int   values_submitted;
132
133         char *identifier_copy;
134
135         const data_set_t *ds;
136         value_list_t vl = VALUE_LIST_INIT;
137
138         DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
139                         (void *) fh, buffer);
140
141         command = NULL;
142         status = parse_string (&buffer, &command);
143         if (status != 0)
144         {
145                 print_to_socket (fh, "-1 Cannot parse command.\n");
146                 return (-1);
147         }
148         assert (command != NULL);
149
150         if (strcasecmp ("PUTVAL", command) != 0)
151         {
152                 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
153                 return (-1);
154         }
155
156         identifier = NULL;
157         status = parse_string (&buffer, &identifier);
158         if (status != 0)
159         {
160                 print_to_socket (fh, "-1 Cannot parse identifier.\n");
161                 return (-1);
162         }
163         assert (identifier != NULL);
164
165         /* parse_identifier() modifies its first argument,
166          * returning pointers into it */
167         identifier_copy = sstrdup (identifier);
168
169         status = parse_identifier (identifier_copy, &hostname,
170                         &plugin, &plugin_instance,
171                         &type, &type_instance);
172         if (status != 0)
173         {
174                 DEBUG ("handle_putval: Cannot parse identifier `%s'.",
175                                 identifier);
176                 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
177                                 identifier);
178                 sfree (identifier_copy);
179                 return (-1);
180         }
181
182         if ((strlen (hostname) >= sizeof (vl.host))
183                         || (strlen (plugin) >= sizeof (vl.plugin))
184                         || ((plugin_instance != NULL)
185                                 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
186                         || ((type_instance != NULL)
187                                 && (strlen (type_instance) >= sizeof (vl.type_instance))))
188         {
189                 print_to_socket (fh, "-1 Identifier too long.\n");
190                 sfree (identifier_copy);
191                 return (-1);
192         }
193
194         sstrncpy (vl.host, hostname, sizeof (vl.host));
195         sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
196         sstrncpy (vl.type, type, sizeof (vl.type));
197         if (plugin_instance != NULL)
198                 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
199         if (type_instance != NULL)
200                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
201
202         ds = plugin_get_ds (type);
203         if (ds == NULL) {
204                 print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
205                 sfree (identifier_copy);
206                 return (-1);
207         }
208
209         /* Free identifier_copy */
210         hostname = NULL;
211         plugin = NULL; plugin_instance = NULL;
212         type = NULL;   type_instance = NULL;
213         sfree (identifier_copy);
214
215         vl.values_len = ds->ds_num;
216         vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
217         if (vl.values == NULL)
218         {
219                 print_to_socket (fh, "-1 malloc failed.\n");
220                 return (-1);
221         }
222
223         /* All the remaining fields are part of the optionlist. */
224         values_submitted = 0;
225         while (*buffer != 0)
226         {
227                 char *string = NULL;
228                 char *value  = NULL;
229
230                 status = parse_option (&buffer, &string, &value);
231                 if (status < 0)
232                 {
233                         /* parse_option failed, buffer has been modified.
234                          * => we need to abort */
235                         print_to_socket (fh, "-1 Misformatted option.\n");
236                         return (-1);
237                 }
238                 else if (status == 0)
239                 {
240                         assert (string != NULL);
241                         assert (value != NULL);
242                         set_option (&vl, string, value);
243                         continue;
244                 }
245                 /* else: parse_option but buffer has not been modified. This is
246                  * the default if no `=' is found.. */
247
248                 status = parse_string (&buffer, &string);
249                 if (status != 0)
250                 {
251                         print_to_socket (fh, "-1 Misformatted value.\n");
252                         return (-1);
253                 }
254                 assert (string != NULL);
255
256                 status = dispatch_values (ds, &vl, fh, string);
257                 if (status != 0)
258                 {
259                         /* An error has already been printed. */
260                         return (-1);
261                 }
262                 values_submitted++;
263         } /* while (*buffer != 0) */
264         /* Done parsing the options. */
265
266         print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
267                         values_submitted,
268                         (values_submitted == 1) ? "value has" : "values have");
269
270         sfree (vl.values); 
271
272         return (0);
273 } /* int handle_putval */
274