Merge branch 'collectd-4.4'
[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                 FILE *fh, char *buffer)
28 {
29         char *dummy;
30         char *ptr;
31         char *saveptr;
32         int i;
33
34         char *time_str = buffer;
35         char *value_str = strchr (time_str, ':');
36         if (value_str == NULL)
37         {
38                 fprintf (fh, "-1 No time found.\n");
39                 return (-1);
40         }
41         *value_str = '\0'; value_str++;
42
43         vl->time = (time_t) atoi (time_str);
44         if (vl->time == 0)
45                 vl->time = time (NULL);
46
47         i = 0;
48         dummy = value_str;
49         saveptr = NULL;
50         while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
51         {
52                 dummy = NULL;
53
54                 if (i >= vl->values_len)
55                 {
56                         i = vl->values_len + 1;
57                         break;
58                 }
59
60                 if (strcmp (ptr, "U") == 0)
61                         vl->values[i].gauge = NAN;
62                 else if (ds->ds[i].type == DS_TYPE_COUNTER)
63                         vl->values[i].counter = atoll (ptr);
64                 else if (ds->ds[i].type == DS_TYPE_GAUGE)
65                         vl->values[i].gauge = atof (ptr);
66
67                 i++;
68         } /* while (strtok_r) */
69
70         if (i != vl->values_len)
71         {
72                 char identifier[128];
73                 FORMAT_VL (identifier, sizeof (identifier), vl, ds);
74                 ERROR ("cmd putval: parse_value: "
75                                 "Number of values incorrect: "
76                                 "Got %i, expected %i. Identifier is `%s'.",
77                                 i, vl->values_len, identifier);
78                 fprintf (fh, "-1 Number of values incorrect: "
79                                 "Got %i, expected %i.\n",
80                                 i, vl->values_len);
81                 return (-1);
82         }
83
84         plugin_dispatch_values (vl);
85         return (0);
86 } /* int parse_value */
87
88 static int parse_option (value_list_t *vl, char *buffer)
89 {
90         char *option = buffer;
91         char *value;
92
93         if ((vl == NULL) || (option == NULL))
94                 return (-1);
95
96         value = strchr (option, '=');
97         if (value == NULL)
98                 return (-1);
99         *value = '\0'; value++;
100
101         if (strcasecmp ("interval", option) == 0)
102         {
103                 vl->interval = atoi (value);
104                 if (vl->interval <= 0)
105                         vl->interval = interval_g;
106         }
107         else
108                 return (1);
109
110         return (0);
111 } /* int parse_option */
112
113 int handle_putval (FILE *fh, char **fields, int fields_num)
114 {
115         char *hostname;
116         char *plugin;
117         char *plugin_instance;
118         char *type;
119         char *type_instance;
120         int   status;
121         int   i;
122
123         char *identifier_copy;
124
125         const data_set_t *ds;
126         value_list_t vl = VALUE_LIST_INIT;
127
128         if (fields_num < 3)
129         {
130                 DEBUG ("cmd putval: Wrong number of fields: %i",
131                                 fields_num);
132                 fprintf (fh, "-1 Wrong number of fields: Got %i, "
133                                 "expected at least 3.\n",
134                                 fields_num);
135                 fflush (fh);
136                 return (-1);
137         }
138
139         /* parse_identifier() modifies its first argument,
140          * returning pointers into it */
141         identifier_copy = sstrdup (fields[1]);
142
143         status = parse_identifier (identifier_copy, &hostname,
144                         &plugin, &plugin_instance,
145                         &type, &type_instance);
146         if (status != 0)
147         {
148                 DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
149                 fprintf (fh, "-1 Cannot parse identifier.\n");
150                 fflush (fh);
151                 sfree (identifier_copy);
152                 return (-1);
153         }
154
155         if ((strlen (hostname) >= sizeof (vl.host))
156                         || (strlen (plugin) >= sizeof (vl.plugin))
157                         || ((plugin_instance != NULL)
158                                 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
159                         || ((type_instance != NULL)
160                                 && (strlen (type_instance) >= sizeof (vl.type_instance))))
161         {
162                 fprintf (fh, "-1 Identifier too long.\n");
163                 fflush (fh);
164                 sfree (identifier_copy);
165                 return (-1);
166         }
167
168         strcpy (vl.host, hostname);
169         strcpy (vl.plugin, plugin);
170         if (plugin_instance != NULL)
171                 strcpy (vl.plugin_instance, plugin_instance);
172         strcpy (vl.type, type);
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, 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