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