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