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