src/utils_cmd_putval.[ch]: Allow identifiers to include spaces.
[collectd.git] / src / utils_cmd_putval.c
1 /**
2  * collectd - src/utils_cms_putval.c
3  * Copyright (C) 2007,2008  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 parse_value (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         if (vl->time == 0)
55                 vl->time = time (NULL);
56
57         i = 0;
58         dummy = value_str;
59         saveptr = NULL;
60         while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
61         {
62                 dummy = NULL;
63
64                 if (i >= vl->values_len)
65                 {
66                         i = vl->values_len + 1;
67                         break;
68                 }
69
70                 if (strcmp (ptr, "U") == 0)
71                         vl->values[i].gauge = NAN;
72                 else if (ds->ds[i].type == DS_TYPE_COUNTER)
73                         vl->values[i].counter = atoll (ptr);
74                 else if (ds->ds[i].type == DS_TYPE_GAUGE)
75                         vl->values[i].gauge = atof (ptr);
76
77                 i++;
78         } /* while (strtok_r) */
79
80         if (i != vl->values_len)
81         {
82                 char identifier[128];
83                 FORMAT_VL (identifier, sizeof (identifier), vl, ds);
84                 ERROR ("cmd putval: parse_value: "
85                                 "Number of values incorrect: "
86                                 "Got %i, expected %i. Identifier is `%s'.",
87                                 i, vl->values_len, identifier);
88                 print_to_socket (fh, "-1 Number of values incorrect: "
89                                 "Got %i, expected %i.\n",
90                                 i, vl->values_len);
91                 return (-1);
92         }
93
94         plugin_dispatch_values (vl);
95         return (0);
96 } /* int parse_value */
97
98 static int set_option (value_list_t *vl, const char *key, const char *value)
99 {
100         if ((vl == NULL) || (key == NULL) || (value == NULL))
101                 return (-1);
102
103         if (strcasecmp ("interval", key) == 0)
104         {
105                 int tmp;
106                 char *endptr;
107
108                 endptr = NULL;
109                 errno = 0;
110                 tmp = strtol (value, &endptr, 0);
111
112                 if ((errno == 0) && (endptr != NULL)
113                                 && (endptr != value) && (tmp > 0))
114                         vl->interval = tmp;
115         }
116         else
117                 return (1);
118
119         return (0);
120 } /* int parse_option */
121
122 int handle_putval (FILE *fh, char *buffer)
123 {
124         char *command;
125         char *identifier;
126         char *hostname;
127         char *plugin;
128         char *plugin_instance;
129         char *type;
130         char *type_instance;
131         int   status;
132         int   values_submitted;
133
134         char *identifier_copy;
135
136         const data_set_t *ds;
137         value_list_t vl = VALUE_LIST_INIT;
138
139         DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
140                         (void *) fh, buffer);
141
142         command = NULL;
143         status = parse_string (&buffer, &command);
144         if (status != 0)
145         {
146                 print_to_socket (fh, "-1 Cannot parse command.\n");
147                 return (-1);
148         }
149         assert (command != NULL);
150
151         if (strcasecmp ("PUTVAL", command) != 0)
152         {
153                 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
154                 return (-1);
155         }
156
157         identifier = NULL;
158         status = parse_string (&buffer, &identifier);
159         if (status != 0)
160         {
161                 print_to_socket (fh, "-1 Cannot parse identifier.\n");
162                 return (-1);
163         }
164         assert (identifier != NULL);
165
166         /* parse_identifier() modifies its first argument,
167          * returning pointers into it */
168         identifier_copy = sstrdup (identifier);
169
170         status = parse_identifier (identifier_copy, &hostname,
171                         &plugin, &plugin_instance,
172                         &type, &type_instance);
173         if (status != 0)
174         {
175                 DEBUG ("handle_putval: Cannot parse identifier `%s'.",
176                                 identifier);
177                 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
178                                 identifier);
179                 sfree (identifier_copy);
180                 return (-1);
181         }
182
183         if ((strlen (hostname) >= sizeof (vl.host))
184                         || (strlen (plugin) >= sizeof (vl.plugin))
185                         || ((plugin_instance != NULL)
186                                 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
187                         || ((type_instance != NULL)
188                                 && (strlen (type_instance) >= sizeof (vl.type_instance))))
189         {
190                 print_to_socket (fh, "-1 Identifier too long.\n");
191                 sfree (identifier_copy);
192                 return (-1);
193         }
194
195         sstrncpy (vl.host, hostname, sizeof (vl.host));
196         sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
197         sstrncpy (vl.type, type, sizeof (vl.type));
198         if (plugin_instance != NULL)
199                 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
200         if (type_instance != NULL)
201                 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
202
203         ds = plugin_get_ds (type);
204         if (ds == NULL) {
205                 print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
206                 sfree (identifier_copy);
207                 return (-1);
208         }
209
210         /* Free identifier_copy */
211         hostname = NULL;
212         plugin = NULL; plugin_instance = NULL;
213         type = NULL;   type_instance = NULL;
214         sfree (identifier_copy);
215
216         vl.values_len = ds->ds_num;
217         vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
218         if (vl.values == NULL)
219         {
220                 print_to_socket (fh, "-1 malloc failed.\n");
221                 return (-1);
222         }
223
224         /* All the remaining fields are part of the optionlist. */
225         values_submitted = 0;
226         while (*buffer != 0)
227         {
228                 char *string = NULL;
229                 char *value  = NULL;
230
231                 status = parse_option (&buffer, &string, &value);
232                 if (status < 0)
233                 {
234                         /* parse_option failed, buffer has been modified.
235                          * => we need to abort */
236                         print_to_socket (fh, "-1 Misformatted option.\n");
237                         return (-1);
238                 }
239                 else if (status == 0)
240                 {
241                         assert (string != NULL);
242                         assert (value != NULL);
243                         set_option (&vl, string, value);
244                         continue;
245                 }
246                 /* else: parse_option but buffer has not been modified. This is
247                  * the default if no `=' is found.. */
248
249                 status = parse_string (&buffer, &string);
250                 if (status != 0)
251                 {
252                         print_to_socket (fh, "-1 Misformatted value.\n");
253                         return (-1);
254                 }
255                 assert (string != NULL);
256
257                 status = parse_value (ds, &vl, fh, string);
258                 if (status != 0)
259                 {
260                         /* An error has already been printed. */
261                         return (-1);
262                 }
263                 values_submitted++;
264         } /* while (*buffer != 0) */
265         /* Done parsing the options. */
266
267         print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
268                         values_submitted,
269                         (values_submitted == 1) ? "value has" : "values have");
270
271         sfree (vl.values); 
272
273         return (0);
274 } /* int handle_putval */
275