write_http plugin: Send ā€œ\r\nā€ line endings.
[collectd.git] / src / write_http.c
1 /**
2  * collectd - src/write_http.c
3  * Copyright (C) 2009       Paul Sadauskas
4  * Copyright (C) 2009       Doug MacEachern
5  * Copyright (C) 2007-2009  Florian octo Forster
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Florian octo Forster <octo at verplant.org>
22  *   Doug MacEachern <dougm@hyperic.com>
23  *   Paul Sadauskas <psadauskas@gmail.com>
24  **/
25
26 #include "collectd.h"
27 #include "plugin.h"
28 #include "common.h"
29 #include "utils_cache.h"
30 #include "utils_parse_option.h"
31
32 #if HAVE_PTHREAD_H
33 # include <pthread.h>
34 #endif
35
36 #include <curl/curl.h>
37
38 /*
39  * Private variables
40  */
41 struct wh_callback_s
42 {
43         char *location;
44
45         char *user;
46         char *pass;
47         char *credentials;
48         int   verify_peer;
49         int   verify_host;
50         char *cacert;
51
52         CURL *curl;
53         char curl_errbuf[CURL_ERROR_SIZE];
54
55         char   send_buffer[4096];
56         size_t send_buffer_free;
57         size_t send_buffer_fill;
58         time_t send_buffer_init_time;
59
60         pthread_mutex_t send_lock;
61 };
62 typedef struct wh_callback_s wh_callback_t;
63
64 static void wh_reset_buffer (wh_callback_t *cb)  /* {{{ */
65 {
66         memset (cb->send_buffer, 0, sizeof (cb->send_buffer));
67         cb->send_buffer_free = sizeof (cb->send_buffer);
68         cb->send_buffer_fill = 0;
69         cb->send_buffer_init_time = time (NULL);
70 } /* }}} wh_reset_buffer */
71
72 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
73 {
74         int status = 0;
75
76         curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
77         status = curl_easy_perform (cb->curl);
78         if (status != 0)
79         {
80                 ERROR ("write_http plugin: curl_easy_perform failed with "
81                                 "status %i: %s",
82                                 status, cb->curl_errbuf);
83         }
84         return (status);
85 } /* }}} wh_send_buffer */
86
87 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
88 {
89         struct curl_slist *headers;
90
91         if (cb->curl != NULL)
92                 return (0);
93
94         cb->curl = curl_easy_init ();
95         if (cb->curl == NULL)
96         {
97                 ERROR ("curl plugin: curl_easy_init failed.");
98                 return (-1);
99         }
100
101         curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
102
103         headers = NULL;
104         headers = curl_slist_append (headers, "Accept:  */*");
105         headers = curl_slist_append (headers, "Content-Type: text/plain");
106         headers = curl_slist_append (headers, "Expect:");
107         curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
108
109         curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
110         curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
111
112         if (cb->user != NULL)
113         {
114                 size_t credentials_size;
115
116                 credentials_size = strlen (cb->user) + 2;
117                 if (cb->pass != NULL)
118                         credentials_size += strlen (cb->pass);
119
120                 cb->credentials = (char *) malloc (credentials_size);
121                 if (cb->credentials == NULL)
122                 {
123                         ERROR ("curl plugin: malloc failed.");
124                         return (-1);
125                 }
126
127                 ssnprintf (cb->credentials, credentials_size, "%s:%s",
128                                 cb->user, (cb->pass == NULL) ? "" : cb->pass);
129                 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
130                 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
131         }
132
133         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, cb->verify_peer);
134         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
135                         cb->verify_host ? 2 : 0);
136         if (cb->cacert != NULL)
137                 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
138
139         wh_reset_buffer (cb);
140
141         return (0);
142 } /* }}} int wh_callback_init */
143
144 static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */
145 {
146         int status;
147
148         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
149                         "send_buffer_fill = %zu;",
150                         timeout, cb->send_buffer_fill);
151
152         if (timeout > 0)
153         {
154                 time_t now;
155
156                 now = time (NULL);
157                 if ((cb->send_buffer_init_time + timeout) > now)
158                         return (0);
159         }
160
161         if (cb->send_buffer_fill <= 0)
162         {
163                 cb->send_buffer_init_time = time (NULL);
164                 return (0);
165         }
166
167         status = wh_send_buffer (cb);
168         wh_reset_buffer (cb);
169
170         return (status);
171 } /* }}} wh_flush_nolock */
172
173 static int wh_flush (int timeout, /* {{{ */
174                 const char *identifier __attribute__((unused)),
175                 user_data_t *user_data)
176 {
177         wh_callback_t *cb;
178         int status;
179
180         if (user_data == NULL)
181                 return (-EINVAL);
182
183         cb = user_data->data;
184
185         pthread_mutex_lock (&cb->send_lock);
186
187         if (cb->curl == NULL)
188         {
189                 status = wh_callback_init (cb);
190                 if (status != 0)
191                 {
192                         ERROR ("write_http plugin: wh_callback_init failed.");
193                         pthread_mutex_unlock (&cb->send_lock);
194                         return (-1);
195                 }
196         }
197
198         status = wh_flush_nolock (timeout, cb);
199         pthread_mutex_unlock (&cb->send_lock);
200
201         return (status);
202 } /* }}} int wh_flush */
203
204 static void wh_callback_free (void *data) /* {{{ */
205 {
206         wh_callback_t *cb;
207
208         if (data == NULL)
209                 return;
210
211         cb = data;
212
213         wh_flush_nolock (/* timeout = */ -1, cb);
214
215         curl_easy_cleanup (cb->curl);
216         sfree (cb->location);
217         sfree (cb->user);
218         sfree (cb->pass);
219         sfree (cb->credentials);
220         sfree (cb->cacert);
221
222         sfree (cb);
223 } /* }}} void wh_callback_free */
224
225 static int wh_value_list_to_string (char *buffer, /* {{{ */
226                 size_t buffer_size,
227                 const data_set_t *ds, const value_list_t *vl)
228 {
229         size_t offset = 0;
230         int status;
231         int i;
232
233         assert (0 == strcmp (ds->type, vl->type));
234
235         memset (buffer, 0, buffer_size);
236
237 #define BUFFER_ADD(...) do { \
238         status = ssnprintf (buffer + offset, buffer_size - offset, \
239                         __VA_ARGS__); \
240         if (status < 1) \
241                 return (-1); \
242         else if (((size_t) status) >= (buffer_size - offset)) \
243                 return (-1); \
244         else \
245                 offset += ((size_t) status); \
246 } while (0)
247
248         BUFFER_ADD ("%lu", (unsigned long) vl->time);
249
250         for (i = 0; i < ds->ds_num; i++)
251 {
252         if (ds->ds[i].type == DS_TYPE_GAUGE)
253                 BUFFER_ADD (":%f", vl->values[i].gauge);
254         else if (ds->ds[i].type == DS_TYPE_COUNTER)
255                 BUFFER_ADD (":%llu", vl->values[i].counter);
256         else if (ds->ds[i].type == DS_TYPE_DERIVE)
257                 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
258         else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
259                 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
260         else
261         {
262                 ERROR ("write_http plugin: Unknown data source type: %i",
263                                 ds->ds[i].type);
264                 return (-1);
265         }
266 } /* for ds->ds_num */
267
268 #undef BUFFER_ADD
269
270 return (0);
271 } /* }}} int wh_value_list_to_string */
272
273 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
274                 wh_callback_t *cb)
275 {
276         char key[10*DATA_MAX_NAME_LEN];
277         char values[512];
278         char command[1024];
279         size_t command_len;
280
281         int status;
282
283         if (0 != strcmp (ds->type, vl->type)) {
284                 ERROR ("write_http plugin: DS type does not match "
285                                 "value list type");
286                 return -1;
287         }
288
289         /* Copy the identifier to `key' and escape it. */
290         status = FORMAT_VL (key, sizeof (key), vl);
291         if (status != 0) {
292                 ERROR ("write_http plugin: error with format_name");
293                 return (status);
294         }
295         escape_string (key, sizeof (key));
296
297         /* Convert the values to an ASCII representation and put that into
298          * `values'. */
299         status = wh_value_list_to_string (values, sizeof (values), ds, vl);
300         if (status != 0) {
301                 ERROR ("write_http plugin: error with "
302                                 "wh_value_list_to_string");
303                 return (status);
304         }
305
306         command_len = (size_t) ssnprintf (command, sizeof (command),
307                         "PUTVAL %s interval=%i %s\r\n",
308                         key, vl->interval, values);
309         if (command_len >= sizeof (command)) {
310                 ERROR ("write_http plugin: Command buffer too small: "
311                                 "Need %zu bytes.", command_len + 1);
312                 return (-1);
313         }
314
315         pthread_mutex_lock (&cb->send_lock);
316
317         if (cb->curl == NULL)
318         {
319                 status = wh_callback_init (cb);
320                 if (status != 0)
321                 {
322                         ERROR ("write_http plugin: wh_callback_init failed.");
323                         pthread_mutex_unlock (&cb->send_lock);
324                         return (-1);
325                 }
326         }
327
328         if (command_len >= cb->send_buffer_free)
329         {
330                 status = wh_flush_nolock (/* timeout = */ -1, cb);
331                 if (status != 0)
332                 {
333                         pthread_mutex_unlock (&cb->send_lock);
334                         return (status);
335                 }
336         }
337         assert (command_len < cb->send_buffer_free);
338
339         /* `command_len + 1' because `command_len' does not include the
340          * trailing null byte. Neither does `send_buffer_fill'. */
341         memcpy (cb->send_buffer + cb->send_buffer_fill,
342                         command, command_len + 1);
343         cb->send_buffer_fill += command_len;
344         cb->send_buffer_free -= command_len;
345
346         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
347                         cb->location,
348                         cb->send_buffer_fill, sizeof (cb->send_buffer),
349                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
350                         command);
351
352         /* Check if we have enough space for this command. */
353         pthread_mutex_unlock (&cb->send_lock);
354
355         return (0);
356 } /* }}} int wh_write_command */
357
358 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
359                 user_data_t *user_data)
360 {
361         wh_callback_t *cb;
362         int status;
363
364         if (user_data == NULL)
365                 return (-EINVAL);
366
367         cb = user_data->data;
368
369         status = wh_write_command (ds, vl, cb);
370         return (status);
371 } /* }}} int wh_write */
372
373 static int config_set_string (char **ret_string, /* {{{ */
374                 oconfig_item_t *ci)
375 {
376         char *string;
377
378         if ((ci->values_num != 1)
379                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
380         {
381                 WARNING ("write_http plugin: The `%s' config option "
382                                 "needs exactly one string argument.", ci->key);
383                 return (-1);
384         }
385
386         string = strdup (ci->values[0].value.string);
387         if (string == NULL)
388         {
389                 ERROR ("write_http plugin: strdup failed.");
390                 return (-1);
391         }
392
393         if (*ret_string != NULL)
394                 free (*ret_string);
395         *ret_string = string;
396
397         return (0);
398 } /* }}} int config_set_string */
399
400 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
401 {
402         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
403         {
404                 WARNING ("write_http plugin: The `%s' config option "
405                                 "needs exactly one boolean argument.", ci->key);
406                 return (-1);
407         }
408
409         *dest = ci->values[0].value.boolean ? 1 : 0;
410
411         return (0);
412 } /* }}} int config_set_boolean */
413
414 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
415 {
416         wh_callback_t *cb;
417         user_data_t user_data;
418         int i;
419
420         cb = malloc (sizeof (*cb));
421         if (cb == NULL)
422         {
423                 ERROR ("write_http plugin: malloc failed.");
424                 return (-1);
425         }
426         memset (cb, 0, sizeof (*cb));
427         cb->location = NULL;
428         cb->user = NULL;
429         cb->pass = NULL;
430         cb->credentials = NULL;
431         cb->verify_peer = 1;
432         cb->verify_host = 1;
433         cb->cacert = NULL;
434         cb->curl = NULL;
435
436         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
437
438         config_set_string (&cb->location, ci);
439         if (cb->location == NULL)
440                 return (-1);
441
442         for (i = 0; i < ci->children_num; i++)
443         {
444                 oconfig_item_t *child = ci->children + i;
445
446                 if (strcasecmp ("User", child->key) == 0)
447                         config_set_string (&cb->user, child);
448                 else if (strcasecmp ("Password", child->key) == 0)
449                         config_set_string (&cb->pass, child);
450                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
451                         config_set_boolean (&cb->verify_peer, child);
452                 else if (strcasecmp ("VerifyHost", child->key) == 0)
453                         config_set_boolean (&cb->verify_host, child);
454                 else if (strcasecmp ("CACert", child->key) == 0)
455                         config_set_string (&cb->cacert, child);
456                 else
457                 {
458                         ERROR ("write_http plugin: Invalid configuration "
459                                         "option: %s.", child->key);
460                 }
461         }
462
463         DEBUG ("write_http: Registering write callback with URL %s",
464                         cb->location);
465
466         memset (&user_data, 0, sizeof (user_data));
467         user_data.data = cb;
468         user_data.free_func = NULL;
469         plugin_register_flush ("write_http", wh_flush, &user_data);
470
471         user_data.free_func = wh_callback_free;
472         plugin_register_write ("write_http", wh_write, &user_data);
473
474         return (0);
475 } /* }}} int wh_config_url */
476
477 static int wh_config (oconfig_item_t *ci) /* {{{ */
478 {
479         int i;
480
481         for (i = 0; i < ci->children_num; i++)
482         {
483                 oconfig_item_t *child = ci->children + i;
484
485                 if (strcasecmp ("URL", child->key) == 0)
486                         wh_config_url (child);
487                 else
488                 {
489                         ERROR ("write_http plugin: Invalid configuration "
490                                         "option: %s.", child->key);
491                 }
492         }
493
494         return (0);
495 } /* }}} int wh_config */
496
497 void module_register (void) /* {{{ */
498 {
499         plugin_register_complex_config ("write_http", wh_config);
500 } /* }}} void module_register */
501
502 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */