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 static int parse_value (const data_set_t *ds, value_list_t *vl,
27 const char *type,
28 FILE *fh, char *buffer)
29 {
30 char *dummy;
31 char *ptr;
32 char *saveptr;
33 int i;
35 char *time_str = buffer;
36 char *value_str = strchr (time_str, ':');
37 if (value_str == NULL)
38 {
39 fprintf (fh, "-1 No time found.");
40 return (-1);
41 }
42 *value_str = '\0'; value_str++;
44 vl->time = (time_t) atoi (time_str);
45 if (vl->time == 0)
46 vl->time = time (NULL);
48 i = 0;
49 dummy = value_str;
50 saveptr = NULL;
51 while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
52 {
53 dummy = NULL;
55 if (i >= vl->values_len)
56 {
57 i = vl->values_len + 1;
58 break;
59 }
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);
68 i++;
69 } /* while (strtok_r) */
71 if (i != vl->values_len)
72 {
73 char identifier[128];
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",
81 i, vl->values_len);
82 return (-1);
83 }
85 plugin_dispatch_values (type, vl);
86 return (0);
87 } /* int parse_value */
89 static int parse_option (value_list_t *vl, char *buffer)
90 {
91 char *option = buffer;
92 char *value;
94 if ((vl == NULL) || (option == NULL))
95 return (-1);
97 value = strchr (option, '=');
98 if (value == NULL)
99 return (-1);
100 *value = '\0'; value++;
102 if (strcasecmp ("interval", option) == 0)
103 {
104 vl->interval = atoi (value);
105 if (vl->interval <= 0)
106 vl->interval = interval_g;
107 }
108 else
109 return (1);
111 return (0);
112 } /* int parse_option */
114 int handle_putval (FILE *fh, char **fields, int fields_num)
115 {
116 char *hostname;
117 char *plugin;
118 char *plugin_instance;
119 char *type;
120 char *type_instance;
121 int status;
122 int i;
124 const data_set_t *ds;
125 value_list_t vl = VALUE_LIST_INIT;
127 if (fields_num < 3)
128 {
129 DEBUG ("cmd putval: Wrong number of fields: %i",
130 fields_num);
131 fprintf (fh, "-1 Wrong number of fields: Got %i, "
132 "expected at least 3.\n",
133 fields_num);
134 fflush (fh);
135 return (-1);
136 }
138 status = parse_identifier (fields[1], &hostname,
139 &plugin, &plugin_instance,
140 &type, &type_instance);
141 if (status != 0)
142 {
143 DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
144 fprintf (fh, "-1 Cannot parse identifier.\n");
145 fflush (fh);
146 return (-1);
147 }
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))))
155 {
156 fprintf (fh, "-1 Identifier too long.");
157 return (-1);
158 }
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);
168 if (ds == NULL)
169 return (-1);
171 vl.values_len = ds->ds_num;
172 vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
173 if (vl.values == NULL)
174 {
175 fprintf (fh, "-1 malloc failed.");
176 return (-1);
177 }
179 /* All the remaining fields are part of the optionlist. */
180 for (i = 2; i < fields_num; i++)
181 {
182 if (strchr (fields[i], ':') != NULL)
183 {
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)
188 break;
189 }
190 else if (strchr (fields[i], '=') != NULL)
191 {
192 if (parse_option (&vl, fields[i]) != 0)
193 {
194 fprintf (fh, "-1 Error parsing option `%s'",
195 fields[i]);
196 break;
197 }
198 }
199 else
200 {
201 WARNING ("cmd putval: handle_putval: "
202 "Cannot parse field #%i `%s'; "
203 "Ignoring it.\n",
204 i, fields[i]);
205 }
206 }
207 /* Done parsing the options. */
209 if (i == fields_num)
210 fprintf (fh, "0 Success\n");
211 fflush (fh);
213 sfree (vl.values);
215 return (0);
216 } /* int handle_putval */