1 /**
2 * collectd - src/utils_cmd_putval.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
31 #include "utils_parse_option.h"
33 #define print_to_socket(fh, ...) \
34 do { \
35 if (fprintf (fh, __VA_ARGS__) < 0) { \
36 char errbuf[1024]; \
37 WARNING ("handle_putval: failed to write to socket #%i: %s", \
38 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
39 return -1; \
40 } \
41 fflush(fh); \
42 } while (0)
44 static int dispatch_values (const data_set_t *ds, value_list_t *vl,
45 FILE *fh, char *buffer)
46 {
47 int status;
49 status = parse_values (buffer, vl, ds);
50 if (status != 0)
51 {
52 print_to_socket (fh, "-1 Parsing the values string failed.\n");
53 return (-1);
54 }
56 plugin_dispatch_values (vl);
57 return (0);
58 } /* int dispatch_values */
60 static int set_option (value_list_t *vl, const char *key, const char *value)
61 {
62 if ((vl == NULL) || (key == NULL) || (value == NULL))
63 return (-1);
65 if (strcasecmp ("interval", key) == 0)
66 {
67 double tmp;
68 char *endptr;
70 endptr = NULL;
71 errno = 0;
72 tmp = strtod (value, &endptr);
74 if ((errno == 0) && (endptr != NULL)
75 && (endptr != value) && (tmp > 0.0))
76 vl->interval = DOUBLE_TO_CDTIME_T (tmp);
77 }
78 else
79 return (1);
81 return (0);
82 } /* int parse_option */
84 int handle_putval (FILE *fh, char *buffer)
85 {
86 char *command;
87 char *identifier;
88 char *hostname;
89 char *plugin;
90 char *plugin_instance;
91 char *type;
92 char *type_instance;
93 int status;
94 int values_submitted;
96 char *identifier_copy;
98 const data_set_t *ds;
99 value_list_t vl = VALUE_LIST_INIT;
101 DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
102 (void *) fh, buffer);
104 command = NULL;
105 status = parse_string (&buffer, &command);
106 if (status != 0)
107 {
108 print_to_socket (fh, "-1 Cannot parse command.\n");
109 return (-1);
110 }
111 assert (command != NULL);
113 if (strcasecmp ("PUTVAL", command) != 0)
114 {
115 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
116 return (-1);
117 }
119 identifier = NULL;
120 status = parse_string (&buffer, &identifier);
121 if (status != 0)
122 {
123 print_to_socket (fh, "-1 Cannot parse identifier.\n");
124 return (-1);
125 }
126 assert (identifier != NULL);
128 /* parse_identifier() modifies its first argument,
129 * returning pointers into it */
130 identifier_copy = sstrdup (identifier);
132 status = parse_identifier (identifier_copy, &hostname,
133 &plugin, &plugin_instance,
134 &type, &type_instance);
135 if (status != 0)
136 {
137 DEBUG ("handle_putval: Cannot parse identifier `%s'.",
138 identifier);
139 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
140 identifier);
141 sfree (identifier_copy);
142 return (-1);
143 }
145 if ((strlen (hostname) >= sizeof (vl.host))
146 || (strlen (plugin) >= sizeof (vl.plugin))
147 || ((plugin_instance != NULL)
148 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
149 || ((type_instance != NULL)
150 && (strlen (type_instance) >= sizeof (vl.type_instance))))
151 {
152 print_to_socket (fh, "-1 Identifier too long.\n");
153 sfree (identifier_copy);
154 return (-1);
155 }
157 sstrncpy (vl.host, hostname, sizeof (vl.host));
158 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
159 sstrncpy (vl.type, type, sizeof (vl.type));
160 if (plugin_instance != NULL)
161 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
162 if (type_instance != NULL)
163 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
165 ds = plugin_get_ds (type);
166 if (ds == NULL) {
167 print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
168 sfree (identifier_copy);
169 return (-1);
170 }
172 /* Free identifier_copy */
173 hostname = NULL;
174 plugin = NULL; plugin_instance = NULL;
175 type = NULL; type_instance = NULL;
176 sfree (identifier_copy);
178 vl.values_len = ds->ds_num;
179 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
180 if (vl.values == NULL)
181 {
182 print_to_socket (fh, "-1 malloc failed.\n");
183 return (-1);
184 }
186 /* All the remaining fields are part of the optionlist. */
187 values_submitted = 0;
188 while (*buffer != 0)
189 {
190 char *string = NULL;
191 char *value = NULL;
193 status = parse_option (&buffer, &string, &value);
194 if (status < 0)
195 {
196 /* parse_option failed, buffer has been modified.
197 * => we need to abort */
198 print_to_socket (fh, "-1 Misformatted option.\n");
199 return (-1);
200 }
201 else if (status == 0)
202 {
203 assert (string != NULL);
204 assert (value != NULL);
205 set_option (&vl, string, value);
206 continue;
207 }
208 /* else: parse_option but buffer has not been modified. This is
209 * the default if no `=' is found.. */
211 status = parse_string (&buffer, &string);
212 if (status != 0)
213 {
214 print_to_socket (fh, "-1 Misformatted value.\n");
215 return (-1);
216 }
217 assert (string != NULL);
219 status = dispatch_values (ds, &vl, fh, string);
220 if (status != 0)
221 {
222 /* An error has already been printed. */
223 return (-1);
224 }
225 values_submitted++;
226 } /* while (*buffer != 0) */
227 /* Done parsing the options. */
229 print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
230 values_submitted,
231 (values_submitted == 1) ? "value has" : "values have");
233 sfree (vl.values);
235 return (0);
236 } /* int handle_putval */
238 int create_putval (char *ret, size_t ret_len, /* {{{ */
239 const data_set_t *ds, const value_list_t *vl)
240 {
241 char buffer_ident[6 * DATA_MAX_NAME_LEN];
242 char buffer_values[1024];
243 int status;
245 status = FORMAT_VL (buffer_ident, sizeof (buffer_ident), vl);
246 if (status != 0)
247 return (status);
248 escape_string (buffer_ident, sizeof (buffer_ident));
250 status = format_values (buffer_values, sizeof (buffer_values),
251 ds, vl, /* store rates = */ 0);
252 if (status != 0)
253 return (status);
254 escape_string (buffer_values, sizeof (buffer_values));
256 ssnprintf (ret, ret_len,
257 "PUTVAL %s interval=%.3f %s",
258 buffer_ident,
259 (vl->interval > 0)
260 ? CDTIME_T_TO_DOUBLE (vl->interval)
261 : CDTIME_T_TO_DOUBLE (plugin_get_interval ()),
262 buffer_values);
264 return (0);
265 } /* }}} int create_putval */