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