1 /**
2 * collectd - src/utils_cms_putval.c
3 * Copyright (C) 2007,2008 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 if (fprintf (fh, __VA_ARGS__) < 0) { \
30 char errbuf[1024]; \
31 WARNING ("handle_putval: failed to write to socket #%i: %s", \
32 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
33 return -1; \
34 }
36 static int parse_value (const data_set_t *ds, value_list_t *vl,
37 FILE *fh, char *buffer)
38 {
39 char *dummy;
40 char *ptr;
41 char *saveptr;
42 int i;
44 char *time_str = buffer;
45 char *value_str = strchr (time_str, ':');
46 if (value_str == NULL)
47 {
48 print_to_socket (fh, "-1 No time found.\n");
49 return (-1);
50 }
51 *value_str = '\0'; value_str++;
53 vl->time = (time_t) atoi (time_str);
55 i = 0;
56 dummy = value_str;
57 saveptr = NULL;
58 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
59 {
60 dummy = NULL;
62 if (i >= vl->values_len)
63 {
64 i = vl->values_len + 1;
65 break;
66 }
68 if (strcmp (ptr, "U") == 0)
69 vl->values[i].gauge = NAN;
70 else if (ds->ds[i].type == DS_TYPE_COUNTER)
71 vl->values[i].counter = atoll (ptr);
72 else if (ds->ds[i].type == DS_TYPE_GAUGE)
73 vl->values[i].gauge = atof (ptr);
75 i++;
76 } /* while (strtok_r) */
78 if (i != vl->values_len)
79 {
80 char identifier[128];
81 FORMAT_VL (identifier, sizeof (identifier), vl, ds);
82 ERROR ("cmd putval: parse_value: "
83 "Number of values incorrect: "
84 "Got %i, expected %i. Identifier is `%s'.",
85 i, vl->values_len, identifier);
86 print_to_socket (fh, "-1 Number of values incorrect: "
87 "Got %i, expected %i.\n",
88 i, vl->values_len);
89 return (-1);
90 }
92 plugin_dispatch_values (vl);
93 return (0);
94 } /* int parse_value */
96 static int set_option (value_list_t *vl, const char *key, const char *value)
97 {
98 if ((vl == NULL) || (key == NULL) || (value == NULL))
99 return (-1);
101 if (strcasecmp ("interval", key) == 0)
102 {
103 int tmp;
104 char *endptr;
106 endptr = NULL;
107 errno = 0;
108 tmp = strtol (value, &endptr, 0);
110 if ((errno == 0) && (endptr != NULL)
111 && (endptr != value) && (tmp > 0))
112 vl->interval = tmp;
113 }
114 else
115 return (1);
117 return (0);
118 } /* int parse_option */
120 int handle_putval (FILE *fh, char *buffer)
121 {
122 char *command;
123 char *identifier;
124 char *hostname;
125 char *plugin;
126 char *plugin_instance;
127 char *type;
128 char *type_instance;
129 int status;
130 int values_submitted;
132 char *identifier_copy;
134 const data_set_t *ds;
135 value_list_t vl = VALUE_LIST_INIT;
137 DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);",
138 (void *) fh, buffer);
140 command = NULL;
141 status = parse_string (&buffer, &command);
142 if (status != 0)
143 {
144 print_to_socket (fh, "-1 Cannot parse command.\n");
145 return (-1);
146 }
147 assert (command != NULL);
149 if (strcasecmp ("PUTVAL", command) != 0)
150 {
151 print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
152 return (-1);
153 }
155 identifier = NULL;
156 status = parse_string (&buffer, &identifier);
157 if (status != 0)
158 {
159 print_to_socket (fh, "-1 Cannot parse identifier.\n");
160 return (-1);
161 }
162 assert (identifier != NULL);
164 /* parse_identifier() modifies its first argument,
165 * returning pointers into it */
166 identifier_copy = sstrdup (identifier);
168 status = parse_identifier (identifier_copy, &hostname,
169 &plugin, &plugin_instance,
170 &type, &type_instance);
171 if (status != 0)
172 {
173 DEBUG ("handle_putval: Cannot parse identifier `%s'.",
174 identifier);
175 print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n",
176 identifier);
177 sfree (identifier_copy);
178 return (-1);
179 }
181 if ((strlen (hostname) >= sizeof (vl.host))
182 || (strlen (plugin) >= sizeof (vl.plugin))
183 || ((plugin_instance != NULL)
184 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
185 || ((type_instance != NULL)
186 && (strlen (type_instance) >= sizeof (vl.type_instance))))
187 {
188 print_to_socket (fh, "-1 Identifier too long.\n");
189 sfree (identifier_copy);
190 return (-1);
191 }
193 sstrncpy (vl.host, hostname, sizeof (vl.host));
194 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
195 sstrncpy (vl.type, type, sizeof (vl.type));
196 if (plugin_instance != NULL)
197 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
198 if (type_instance != NULL)
199 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
201 ds = plugin_get_ds (type);
202 if (ds == NULL) {
203 print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type);
204 sfree (identifier_copy);
205 return (-1);
206 }
208 /* Free identifier_copy */
209 hostname = NULL;
210 plugin = NULL; plugin_instance = NULL;
211 type = NULL; type_instance = NULL;
212 sfree (identifier_copy);
214 vl.values_len = ds->ds_num;
215 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
216 if (vl.values == NULL)
217 {
218 print_to_socket (fh, "-1 malloc failed.\n");
219 return (-1);
220 }
222 /* All the remaining fields are part of the optionlist. */
223 values_submitted = 0;
224 while (*buffer != 0)
225 {
226 char *string = NULL;
227 char *value = NULL;
229 status = parse_option (&buffer, &string, &value);
230 if (status < 0)
231 {
232 /* parse_option failed, buffer has been modified.
233 * => we need to abort */
234 print_to_socket (fh, "-1 Misformatted option.\n");
235 return (-1);
236 }
237 else if (status == 0)
238 {
239 assert (string != NULL);
240 assert (value != NULL);
241 set_option (&vl, string, value);
242 continue;
243 }
244 /* else: parse_option but buffer has not been modified. This is
245 * the default if no `=' is found.. */
247 status = parse_string (&buffer, &string);
248 if (status != 0)
249 {
250 print_to_socket (fh, "-1 Misformatted value.\n");
251 return (-1);
252 }
253 assert (string != NULL);
255 status = parse_value (ds, &vl, fh, string);
256 if (status != 0)
257 {
258 /* An error has already been printed. */
259 return (-1);
260 }
261 values_submitted++;
262 } /* while (*buffer != 0) */
263 /* Done parsing the options. */
265 print_to_socket (fh, "0 Success: %i %s been dispatched.\n",
266 values_submitted,
267 (values_submitted == 1) ? "value has" : "values have");
269 sfree (vl.values);
271 return (0);
272 } /* int handle_putval */