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,
36 FILE *fh, char *buffer)
43 char *time_str = buffer;
44 char *value_str = strchr (time_str, ':');
45 if (value_str == NULL)
47 print_to_socket (fh, "-1 No time found.\n");
50 *value_str = '\0'; value_str++;
52 vl->time = (time_t) atoi (time_str);
54 vl->time = time (NULL);
59 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
63 if (i >= vl->values_len)
65 i = vl->values_len + 1;
69 if (strcmp (ptr, "U") == 0)
70 vl->values[i].gauge = NAN;
71 else if (ds->ds[i].type == DS_TYPE_COUNTER)
72 vl->values[i].counter = atoll (ptr);
73 else if (ds->ds[i].type == DS_TYPE_GAUGE)
74 vl->values[i].gauge = atof (ptr);
77 } /* while (strtok_r) */
79 if (i != vl->values_len)
82 FORMAT_VL (identifier, sizeof (identifier), vl, ds);
83 ERROR ("cmd putval: parse_value: "
84 "Number of values incorrect: "
85 "Got %i, expected %i. Identifier is `%s'.",
86 i, vl->values_len, identifier);
87 print_to_socket (fh, "-1 Number of values incorrect: "
88 "Got %i, expected %i.\n",
93 plugin_dispatch_values (type, vl);
95 } /* int parse_value */
97 static int parse_option (value_list_t *vl, char *buffer)
99 char *option = buffer;
102 if ((vl == NULL) || (option == NULL))
105 value = strchr (option, '=');
108 *value = '\0'; value++;
110 if (strcasecmp ("interval", option) == 0)
112 vl->interval = atoi (value);
113 if (vl->interval <= 0)
114 vl->interval = interval_g;
120 } /* int parse_option */
122 int handle_putval (FILE *fh, char **fields, int fields_num)
126 char *plugin_instance;
132 char *identifier_copy;
134 const data_set_t *ds;
135 value_list_t vl = VALUE_LIST_INIT;
139 DEBUG ("cmd putval: Wrong number of fields: %i",
141 print_to_socket (fh, "-1 Wrong number of fields: Got %i, "
142 "expected at least 3.\n",
147 /* parse_identifier() modifies its first argument,
148 * returning pointers into it */
149 identifier_copy = sstrdup (fields[1]);
151 status = parse_identifier (identifier_copy, &hostname,
152 &plugin, &plugin_instance,
153 &type, &type_instance);
156 DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
157 print_to_socket (fh, "-1 Cannot parse identifier.\n");
158 sfree (identifier_copy);
162 if ((strlen (hostname) >= sizeof (vl.host))
163 || (strlen (plugin) >= sizeof (vl.plugin))
164 || ((plugin_instance != NULL)
165 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
166 || ((type_instance != NULL)
167 && (strlen (type_instance) >= sizeof (vl.type_instance))))
169 print_to_socket (fh, "-1 Identifier too long.\n");
170 sfree (identifier_copy);
174 sstrncpy (vl.host, hostname, sizeof (vl.host));
175 sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
176 if (plugin_instance != NULL)
177 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
178 if (type_instance != NULL)
179 sstrncpy (vl.type_instance, type_instance, sizeof (vl.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, type, 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 */