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