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