2 * collectd - src/utils_cms_putval.c
3 * Copyright (C) 2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #define print_to_socket(fh, ...) \
27 if (fprintf (fh, __VA_ARGS__) < 0) { \
29 WARNING ("handle_putval: failed to write to socket #%i: %s", \
30 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
34 static int parse_value (const data_set_t *ds, value_list_t *vl,
35 FILE *fh, char *buffer)
42 char *time_str = buffer;
43 char *value_str = strchr (time_str, ':');
44 if (value_str == NULL)
46 print_to_socket (fh, "-1 No time found.\n");
49 *value_str = '\0'; value_str++;
51 vl->time = (time_t) atoi (time_str);
53 vl->time = time (NULL);
58 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
62 if (i >= vl->values_len)
64 i = vl->values_len + 1;
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);
76 } /* while (strtok_r) */
78 if (i != vl->values_len)
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",
92 plugin_dispatch_values (vl);
94 } /* int parse_value */
96 static int parse_option (value_list_t *vl, char *buffer)
98 char *option = buffer;
101 if ((vl == NULL) || (option == NULL))
104 value = strchr (option, '=');
107 *value = '\0'; value++;
109 if (strcasecmp ("interval", option) == 0)
111 vl->interval = atoi (value);
112 if (vl->interval <= 0)
113 vl->interval = interval_g;
119 } /* int parse_option */
121 int handle_putval (FILE *fh, char **fields, int fields_num)
125 char *plugin_instance;
131 char *identifier_copy;
133 const data_set_t *ds;
134 value_list_t vl = VALUE_LIST_INIT;
138 DEBUG ("cmd putval: Wrong number of fields: %i",
140 print_to_socket (fh, "-1 Wrong number of fields: Got %i, "
141 "expected at least 3.\n",
146 /* parse_identifier() modifies its first argument,
147 * returning pointers into it */
148 identifier_copy = sstrdup (fields[1]);
150 status = parse_identifier (identifier_copy, &hostname,
151 &plugin, &plugin_instance,
152 &type, &type_instance);
155 DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
156 print_to_socket (fh, "-1 Cannot parse identifier.\n");
157 sfree (identifier_copy);
161 if ((strlen (hostname) >= sizeof (vl.host))
162 || (strlen (plugin) >= sizeof (vl.plugin))
163 || ((plugin_instance != NULL)
164 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
165 || ((type_instance != NULL)
166 && (strlen (type_instance) >= sizeof (vl.type_instance))))
168 print_to_socket (fh, "-1 Identifier too long.\n");
169 sfree (identifier_copy);
173 strcpy (vl.host, hostname);
174 strcpy (vl.plugin, plugin);
175 if (plugin_instance != NULL)
176 strcpy (vl.plugin_instance, plugin_instance);
177 strcpy (vl.type, type);
178 if (type_instance != NULL)
179 strcpy (vl.type_instance, type_instance);
181 ds = plugin_get_ds (type);
183 sfree (identifier_copy);
187 vl.values_len = ds->ds_num;
188 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
189 if (vl.values == NULL)
191 print_to_socket (fh, "-1 malloc failed.\n");
192 sfree (identifier_copy);
196 /* All the remaining fields are part of the optionlist. */
197 for (i = 2; i < fields_num; i++)
199 if (strchr (fields[i], ':') != NULL)
201 /* It's parse_value's job to write an error to `fh'.
202 * This is not the case with `parse_option below.
203 * Neither will write an success message. */
204 if (parse_value (ds, &vl, fh, fields[i]) != 0)
207 else if (strchr (fields[i], '=') != NULL)
209 if (parse_option (&vl, fields[i]) != 0)
211 print_to_socket (fh, "-1 Error parsing option `%s'\n",
218 WARNING ("cmd putval: handle_putval: "
219 "Cannot parse field #%i `%s'; "
224 /* Done parsing the options. */
227 print_to_socket (fh, "0 Success\n");
230 sfree (identifier_copy);
233 } /* int handle_putval */