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