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 static int parse_value (const data_set_t *ds, value_list_t *vl,
28 FILE *fh, char *buffer)
35 char *time_str = buffer;
36 char *value_str = strchr (time_str, ':');
37 if (value_str == NULL)
39 fprintf (fh, "-1 No time found.");
42 *value_str = '\0'; value_str++;
44 vl->time = (time_t) atoi (time_str);
46 vl->time = time (NULL);
51 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
55 if (i >= vl->values_len)
57 i = vl->values_len + 1;
61 if (strcmp (ptr, "U") == 0)
62 vl->values[i].gauge = NAN;
63 else if (ds->ds[i].type == DS_TYPE_COUNTER)
64 vl->values[i].counter = atoll (ptr);
65 else if (ds->ds[i].type == DS_TYPE_GAUGE)
66 vl->values[i].gauge = atof (ptr);
69 } /* while (strtok_r) */
71 if (i != vl->values_len)
74 FORMAT_VL (identifier, sizeof (identifier), vl, ds);
75 ERROR ("cmd putval: parse_value: "
76 "Number of values incorrect: "
77 "Got %i, expected %i. Identifier is `%s'.",
78 i, vl->values_len, identifier);
79 fprintf (fh, "-1 Number of values incorrect: "
80 "Got %i, expected %i.\n",
85 plugin_dispatch_values (type, vl);
87 } /* int parse_value */
89 static int parse_option (value_list_t *vl, char *buffer)
91 char *option = buffer;
94 if ((vl == NULL) || (option == NULL))
97 value = strchr (option, '=');
100 *value = '\0'; value++;
102 if (strcasecmp ("interval", option) == 0)
104 vl->interval = atoi (value);
105 if (vl->interval <= 0)
106 vl->interval = interval_g;
112 } /* int parse_option */
114 int handle_putval (FILE *fh, char **fields, int fields_num)
118 char *plugin_instance;
124 const data_set_t *ds;
125 value_list_t vl = VALUE_LIST_INIT;
129 DEBUG ("cmd putval: Wrong number of fields: %i",
131 fprintf (fh, "-1 Wrong number of fields: Got %i, "
132 "expected at least 3.\n",
138 status = parse_identifier (fields[1], &hostname,
139 &plugin, &plugin_instance,
140 &type, &type_instance);
143 DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
144 fprintf (fh, "-1 Cannot parse identifier.\n");
149 if ((strlen (hostname) >= sizeof (vl.host))
150 || (strlen (plugin) >= sizeof (vl.plugin))
151 || ((plugin_instance != NULL)
152 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
153 || ((type_instance != NULL)
154 && (strlen (type_instance) >= sizeof (vl.type_instance))))
156 fprintf (fh, "-1 Identifier too long.");
160 strcpy (vl.host, hostname);
161 strcpy (vl.plugin, plugin);
162 if (plugin_instance != NULL)
163 strcpy (vl.plugin_instance, plugin_instance);
164 if (type_instance != NULL)
165 strcpy (vl.type_instance, type_instance);
167 ds = plugin_get_ds (type);
171 vl.values_len = ds->ds_num;
172 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
173 if (vl.values == NULL)
175 fprintf (fh, "-1 malloc failed.");
179 /* All the remaining fields are part of the optionlist. */
180 for (i = 2; i < fields_num; i++)
182 if (strchr (fields[i], ':') != NULL)
184 /* It's parse_value's job to write an error to `fh'.
185 * This is not the case with `parse_option below.
186 * Neither will write an success message. */
187 if (parse_value (ds, &vl, type, fh, fields[i]) != 0)
190 else if (strchr (fields[i], '=') != NULL)
192 if (parse_option (&vl, fields[i]) != 0)
194 fprintf (fh, "-1 Error parsing option `%s'",
201 WARNING ("cmd putval: handle_putval: "
202 "Cannot parse field #%i `%s'; "
207 /* Done parsing the options. */
210 fprintf (fh, "0 Success\n");
216 } /* int handle_putval */