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