1 /**
2 * collectd - src/utils_cms_putval.c
3 * Copyright (C) 2007 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 #define print_to_socket(fh, ...) \
27 if (fprintf (fh, __VA_ARGS__) < 0) { \
28 char errbuf[1024]; \
29 WARNING ("handle_putval: failed to write to socket #%i: %s", \
30 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
31 return -1; \
32 }
34 static int parse_value (const data_set_t *ds, value_list_t *vl,
35 FILE *fh, char *buffer)
36 {
37 char *dummy;
38 char *ptr;
39 char *saveptr;
40 int i;
42 char *time_str = buffer;
43 char *value_str = strchr (time_str, ':');
44 if (value_str == NULL)
45 {
46 print_to_socket (fh, "-1 No time found.\n");
47 return (-1);
48 }
49 *value_str = '\0'; value_str++;
51 vl->time = (time_t) atoi (time_str);
52 if (vl->time == 0)
53 vl->time = time (NULL);
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 parse_option (value_list_t *vl, char *buffer)
97 {
98 char *option = buffer;
99 char *value;
101 if ((vl == NULL) || (option == NULL))
102 return (-1);
104 value = strchr (option, '=');
105 if (value == NULL)
106 return (-1);
107 *value = '\0'; value++;
109 if (strcasecmp ("interval", option) == 0)
110 {
111 vl->interval = atoi (value);
112 if (vl->interval <= 0)
113 vl->interval = interval_g;
114 }
115 else
116 return (1);
118 return (0);
119 } /* int parse_option */
121 int handle_putval (FILE *fh, char **fields, int fields_num)
122 {
123 char *hostname;
124 char *plugin;
125 char *plugin_instance;
126 char *type;
127 char *type_instance;
128 int status;
129 int i;
131 char *identifier_copy;
133 const data_set_t *ds;
134 value_list_t vl = VALUE_LIST_INIT;
136 if (fields_num < 3)
137 {
138 DEBUG ("cmd putval: Wrong number of fields: %i",
139 fields_num);
140 print_to_socket (fh, "-1 Wrong number of fields: Got %i, "
141 "expected at least 3.\n",
142 fields_num);
143 return (-1);
144 }
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);
153 if (status != 0)
154 {
155 DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
156 print_to_socket (fh, "-1 Cannot parse identifier.\n");
157 sfree (identifier_copy);
158 return (-1);
159 }
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))))
167 {
168 print_to_socket (fh, "-1 Identifier too long.\n");
169 sfree (identifier_copy);
170 return (-1);
171 }
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);
182 if (ds == NULL) {
183 sfree (identifier_copy);
184 return (-1);
185 }
187 vl.values_len = ds->ds_num;
188 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
189 if (vl.values == NULL)
190 {
191 print_to_socket (fh, "-1 malloc failed.\n");
192 sfree (identifier_copy);
193 return (-1);
194 }
196 /* All the remaining fields are part of the optionlist. */
197 for (i = 2; i < fields_num; i++)
198 {
199 if (strchr (fields[i], ':') != NULL)
200 {
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)
205 break;
206 }
207 else if (strchr (fields[i], '=') != NULL)
208 {
209 if (parse_option (&vl, fields[i]) != 0)
210 {
211 print_to_socket (fh, "-1 Error parsing option `%s'\n",
212 fields[i]);
213 break;
214 }
215 }
216 else
217 {
218 WARNING ("cmd putval: handle_putval: "
219 "Cannot parse field #%i `%s'; "
220 "Ignoring it.\n",
221 i, fields[i]);
222 }
223 }
224 /* Done parsing the options. */
226 if (i == fields_num)
227 print_to_socket (fh, "0 Success\n");
229 sfree (vl.values);
230 sfree (identifier_copy);
232 return (0);
233 } /* int handle_putval */