Merge branch 'collectd-4.2' into collectd-4.3
[collectd.git] / src / utils_cmd_putval.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
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;
34
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.\n");
40                 return (-1);
41         }
42         *value_str = '\0'; value_str++;
43
44         vl->time = (time_t) atoi (time_str);
45         if (vl->time == 0)
46                 vl->time = time (NULL);
47
48         i = 0;
49         dummy = value_str;
50         saveptr = NULL;
51         while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
52         {
53                 dummy = NULL;
54
55                 if (i >= vl->values_len)
56                 {
57                         i = vl->values_len + 1;
58                         break;
59                 }
60
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);
67
68                 i++;
69         } /* while (strtok_r) */
70
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         }
84
85         plugin_dispatch_values (type, vl);
86         return (0);
87 } /* int parse_value */
88
89 static int parse_option (value_list_t *vl, char *buffer)
90 {
91         char *option = buffer;
92         char *value;
93
94         if ((vl == NULL) || (option == NULL))
95                 return (-1);
96
97         value = strchr (option, '=');
98         if (value == NULL)
99                 return (-1);
100         *value = '\0'; value++;
101
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);
110
111         return (0);
112 } /* int parse_option */
113
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;
123
124         char *identifier_copy;
125
126         const data_set_t *ds;
127         value_list_t vl = VALUE_LIST_INIT;
128
129         if (fields_num < 3)
130         {
131                 DEBUG ("cmd putval: Wrong number of fields: %i",
132                                 fields_num);
133                 fprintf (fh, "-1 Wrong number of fields: Got %i, "
134                                 "expected at least 3.\n",
135                                 fields_num);
136                 fflush (fh);
137                 return (-1);
138         }
139
140         /* parse_identifier() modifies its first argument,
141          * returning pointers into it */
142         identifier_copy = sstrdup (fields[1]);
143
144         status = parse_identifier (identifier_copy, &hostname,
145                         &plugin, &plugin_instance,
146                         &type, &type_instance);
147         if (status != 0)
148         {
149                 DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
150                 fprintf (fh, "-1 Cannot parse identifier.\n");
151                 fflush (fh);
152                 sfree (identifier_copy);
153                 return (-1);
154         }
155
156         if ((strlen (hostname) >= sizeof (vl.host))
157                         || (strlen (plugin) >= sizeof (vl.plugin))
158                         || ((plugin_instance != NULL)
159                                 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
160                         || ((type_instance != NULL)
161                                 && (strlen (type_instance) >= sizeof (vl.type_instance))))
162         {
163                 fprintf (fh, "-1 Identifier too long.\n");
164                 fflush (fh);
165                 sfree (identifier_copy);
166                 return (-1);
167         }
168
169         strcpy (vl.host, hostname);
170         strcpy (vl.plugin, plugin);
171         if (plugin_instance != NULL)
172                 strcpy (vl.plugin_instance, plugin_instance);
173         if (type_instance != NULL)
174                 strcpy (vl.type_instance, type_instance);
175
176         ds = plugin_get_ds (type);
177         if (ds == NULL) {
178                 sfree (identifier_copy);
179                 return (-1);
180         }
181
182         vl.values_len = ds->ds_num;
183         vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
184         if (vl.values == NULL)
185         {
186                 fprintf (fh, "-1 malloc failed.\n");
187                 fflush (fh);
188                 sfree (identifier_copy);
189                 return (-1);
190         }
191
192         /* All the remaining fields are part of the optionlist. */
193         for (i = 2; i < fields_num; i++)
194         {
195                 if (strchr (fields[i], ':') != NULL)
196                 {
197                         /* It's parse_value's job to write an error to `fh'.
198                          * This is not the case with `parse_option below.
199                          * Neither will write an success message. */
200                         if (parse_value (ds, &vl, type, fh, fields[i]) != 0)
201                                 break;
202                 }
203                 else if (strchr (fields[i], '=') != NULL)
204                 {
205                         if (parse_option (&vl, fields[i]) != 0)
206                         {
207                                 fprintf (fh, "-1 Error parsing option `%s'\n",
208                                                 fields[i]);
209                                 break;
210                         }
211                 }
212                 else
213                 {
214                         WARNING ("cmd putval: handle_putval: "
215                                         "Cannot parse field #%i `%s'; "
216                                         "Ignoring it.\n",
217                                         i, fields[i]);
218                 }
219         }
220         /* Done parsing the options. */
221
222         if (i == fields_num)
223                 fprintf (fh, "0 Success\n");
224         fflush (fh);
225
226         sfree (vl.values); 
227         sfree (identifier_copy);
228
229         return (0);
230 } /* int handle_putval */
231