Merge pull request #1553 from aerusso/master
[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-2014  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 collectd.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_format_json.h"
31
32 #if HAVE_PTHREAD_H
33 # include <pthread.h>
34 #endif
35
36 #include <curl/curl.h>
37
38 #ifndef WRITE_HTTP_DEFAULT_BUFFER_SIZE
39 # define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096
40 #endif
41
42 /*
43  * Private variables
44  */
45 struct wh_callback_s
46 {
47         char *name;
48
49         char *location;
50         char *user;
51         char *pass;
52         char *credentials;
53         _Bool verify_peer;
54         _Bool verify_host;
55         char *cacert;
56         char *capath;
57         char *clientkey;
58         char *clientcert;
59         char *clientkeypass;
60         long sslversion;
61         _Bool store_rates;
62         _Bool log_http_error;
63         int   low_speed_limit;
64         time_t low_speed_time;
65         int timeout;
66
67 #define WH_FORMAT_COMMAND 0
68 #define WH_FORMAT_JSON    1
69         int format;
70
71         CURL *curl;
72         struct curl_slist *headers;
73         char curl_errbuf[CURL_ERROR_SIZE];
74
75         char  *send_buffer;
76         size_t send_buffer_size;
77         size_t send_buffer_free;
78         size_t send_buffer_fill;
79         cdtime_t send_buffer_init_time;
80
81         pthread_mutex_t send_lock;
82 };
83 typedef struct wh_callback_s wh_callback_t;
84
85 static void wh_log_http_error (wh_callback_t *cb)
86 {
87         if (!cb->log_http_error)
88                 return;
89
90         long http_code = 0;
91
92         curl_easy_getinfo (cb->curl, CURLINFO_RESPONSE_CODE, &http_code);
93
94         if (http_code != 200)
95                 INFO ("write_http plugin: HTTP Error code: %lu", http_code);
96 }
97
98 static void wh_reset_buffer (wh_callback_t *cb)  /* {{{ */
99 {
100         memset (cb->send_buffer, 0, cb->send_buffer_size);
101         cb->send_buffer_free = cb->send_buffer_size;
102         cb->send_buffer_fill = 0;
103         cb->send_buffer_init_time = cdtime ();
104
105         if (cb->format == WH_FORMAT_JSON)
106         {
107                 format_json_initialize (cb->send_buffer,
108                                 &cb->send_buffer_fill,
109                                 &cb->send_buffer_free);
110         }
111 } /* }}} wh_reset_buffer */
112
113 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
114 {
115         int status = 0;
116
117         curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
118         status = curl_easy_perform (cb->curl);
119
120         wh_log_http_error (cb);
121
122         if (status != CURLE_OK)
123         {
124                 ERROR ("write_http plugin: curl_easy_perform failed with "
125                                 "status %i: %s",
126                                 status, cb->curl_errbuf);
127         }
128         return (status);
129 } /* }}} wh_send_buffer */
130
131 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
132 {
133         if (cb->curl != NULL)
134                 return (0);
135
136         cb->curl = curl_easy_init ();
137         if (cb->curl == NULL)
138         {
139                 ERROR ("curl plugin: curl_easy_init failed.");
140                 return (-1);
141         }
142
143         if (cb->low_speed_limit > 0 && cb->low_speed_time > 0)
144         {
145                 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_LIMIT,
146                                   (long) (cb->low_speed_limit * cb->low_speed_time));
147                 curl_easy_setopt (cb->curl, CURLOPT_LOW_SPEED_TIME,
148                                   (long) cb->low_speed_time);
149         }
150
151 #ifdef HAVE_CURLOPT_TIMEOUT_MS
152         if (cb->timeout > 0)
153                 curl_easy_setopt (cb->curl, CURLOPT_TIMEOUT_MS, (long) cb->timeout);
154 #endif
155
156         curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
157         curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
158
159         cb->headers = NULL;
160         cb->headers = curl_slist_append (cb->headers, "Accept:  */*");
161         if (cb->format == WH_FORMAT_JSON)
162                 cb->headers = curl_slist_append (cb->headers, "Content-Type: application/json");
163         else
164                 cb->headers = curl_slist_append (cb->headers, "Content-Type: text/plain");
165         cb->headers = curl_slist_append (cb->headers, "Expect:");
166         curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, cb->headers);
167
168         curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
169         curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
170         curl_easy_setopt (cb->curl, CURLOPT_FOLLOWLOCATION, 1L);
171         curl_easy_setopt (cb->curl, CURLOPT_MAXREDIRS, 50L);
172
173         if (cb->user != NULL)
174         {
175 #ifdef HAVE_CURLOPT_USERNAME
176                 curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user);
177                 curl_easy_setopt (cb->curl, CURLOPT_PASSWORD,
178                         (cb->pass == NULL) ? "" : cb->pass);
179 #else
180                 size_t credentials_size;
181
182                 credentials_size = strlen (cb->user) + 2;
183                 if (cb->pass != NULL)
184                         credentials_size += strlen (cb->pass);
185
186                 cb->credentials = malloc (credentials_size);
187                 if (cb->credentials == NULL)
188                 {
189                         ERROR ("curl plugin: malloc failed.");
190                         return (-1);
191                 }
192
193                 ssnprintf (cb->credentials, credentials_size, "%s:%s",
194                                 cb->user, (cb->pass == NULL) ? "" : cb->pass);
195                 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
196 #endif
197                 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
198         }
199
200         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
201         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
202                         cb->verify_host ? 2L : 0L);
203         curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
204         if (cb->cacert != NULL)
205                 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
206         if (cb->capath != NULL)
207                 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
208
209         if (cb->clientkey != NULL && cb->clientcert != NULL)
210         {
211             curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
212             curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
213
214             if (cb->clientkeypass != NULL)
215                 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
216         }
217
218         wh_reset_buffer (cb);
219
220         return (0);
221 } /* }}} int wh_callback_init */
222
223 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
224 {
225         int status;
226
227         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
228                         "send_buffer_fill = %zu;",
229                         CDTIME_T_TO_DOUBLE (timeout),
230                         cb->send_buffer_fill);
231
232         /* timeout == 0  => flush unconditionally */
233         if (timeout > 0)
234         {
235                 cdtime_t now;
236
237                 now = cdtime ();
238                 if ((cb->send_buffer_init_time + timeout) > now)
239                         return (0);
240         }
241
242         if (cb->format == WH_FORMAT_COMMAND)
243         {
244                 if (cb->send_buffer_fill <= 0)
245                 {
246                         cb->send_buffer_init_time = cdtime ();
247                         return (0);
248                 }
249
250                 status = wh_send_buffer (cb);
251                 wh_reset_buffer (cb);
252         }
253         else if (cb->format == WH_FORMAT_JSON)
254         {
255                 if (cb->send_buffer_fill <= 2)
256                 {
257                         cb->send_buffer_init_time = cdtime ();
258                         return (0);
259                 }
260
261                 status = format_json_finalize (cb->send_buffer,
262                                 &cb->send_buffer_fill,
263                                 &cb->send_buffer_free);
264                 if (status != 0)
265                 {
266                         ERROR ("write_http: wh_flush_nolock: "
267                                         "format_json_finalize failed.");
268                         wh_reset_buffer (cb);
269                         return (status);
270                 }
271
272                 status = wh_send_buffer (cb);
273                 wh_reset_buffer (cb);
274         }
275         else
276         {
277                 ERROR ("write_http: wh_flush_nolock: "
278                                 "Unknown format: %i",
279                                 cb->format);
280                 return (-1);
281         }
282
283         return (status);
284 } /* }}} wh_flush_nolock */
285
286 static int wh_flush (cdtime_t timeout, /* {{{ */
287                 const char *identifier __attribute__((unused)),
288                 user_data_t *user_data)
289 {
290         wh_callback_t *cb;
291         int status;
292
293         if (user_data == NULL)
294                 return (-EINVAL);
295
296         cb = user_data->data;
297
298         pthread_mutex_lock (&cb->send_lock);
299
300         if (cb->curl == NULL)
301         {
302                 status = wh_callback_init (cb);
303                 if (status != 0)
304                 {
305                         ERROR ("write_http plugin: wh_callback_init failed.");
306                         pthread_mutex_unlock (&cb->send_lock);
307                         return (-1);
308                 }
309         }
310
311         status = wh_flush_nolock (timeout, cb);
312         pthread_mutex_unlock (&cb->send_lock);
313
314         return (status);
315 } /* }}} int wh_flush */
316
317 static void wh_callback_free (void *data) /* {{{ */
318 {
319         wh_callback_t *cb;
320
321         if (data == NULL)
322                 return;
323
324         cb = data;
325
326         wh_flush_nolock (/* timeout = */ 0, cb);
327
328         if (cb->curl != NULL)
329         {
330                 curl_easy_cleanup (cb->curl);
331                 cb->curl = NULL;
332         }
333
334         if (cb->headers != NULL)
335         {
336                 curl_slist_free_all (cb->headers);
337                 cb->headers = NULL;
338         }
339
340         sfree (cb->name);
341         sfree (cb->location);
342         sfree (cb->user);
343         sfree (cb->pass);
344         sfree (cb->credentials);
345         sfree (cb->cacert);
346         sfree (cb->capath);
347         sfree (cb->clientkey);
348         sfree (cb->clientcert);
349         sfree (cb->clientkeypass);
350         sfree (cb->send_buffer);
351
352         sfree (cb);
353 } /* }}} void wh_callback_free */
354
355 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
356                 wh_callback_t *cb)
357 {
358         char key[10*DATA_MAX_NAME_LEN];
359         char values[512];
360         char command[1024];
361         size_t command_len;
362
363         int status;
364
365         if (0 != strcmp (ds->type, vl->type)) {
366                 ERROR ("write_http plugin: DS type does not match "
367                                 "value list type");
368                 return -1;
369         }
370
371         /* Copy the identifier to `key' and escape it. */
372         status = FORMAT_VL (key, sizeof (key), vl);
373         if (status != 0) {
374                 ERROR ("write_http plugin: error with format_name");
375                 return (status);
376         }
377         escape_string (key, sizeof (key));
378
379         /* Convert the values to an ASCII representation and put that into
380          * `values'. */
381         status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
382         if (status != 0) {
383                 ERROR ("write_http plugin: error with "
384                                 "wh_value_list_to_string");
385                 return (status);
386         }
387
388         command_len = (size_t) ssnprintf (command, sizeof (command),
389                         "PUTVAL %s interval=%.3f %s\r\n",
390                         key,
391                         CDTIME_T_TO_DOUBLE (vl->interval),
392                         values);
393         if (command_len >= sizeof (command)) {
394                 ERROR ("write_http plugin: Command buffer too small: "
395                                 "Need %zu bytes.", command_len + 1);
396                 return (-1);
397         }
398
399         pthread_mutex_lock (&cb->send_lock);
400
401         if (cb->curl == NULL)
402         {
403                 status = wh_callback_init (cb);
404                 if (status != 0)
405                 {
406                         ERROR ("write_http plugin: wh_callback_init failed.");
407                         pthread_mutex_unlock (&cb->send_lock);
408                         return (-1);
409                 }
410         }
411
412         if (command_len >= cb->send_buffer_free)
413         {
414                 status = wh_flush_nolock (/* timeout = */ 0, cb);
415                 if (status != 0)
416                 {
417                         pthread_mutex_unlock (&cb->send_lock);
418                         return (status);
419                 }
420         }
421         assert (command_len < cb->send_buffer_free);
422
423         /* `command_len + 1' because `command_len' does not include the
424          * trailing null byte. Neither does `send_buffer_fill'. */
425         memcpy (cb->send_buffer + cb->send_buffer_fill,
426                         command, command_len + 1);
427         cb->send_buffer_fill += command_len;
428         cb->send_buffer_free -= command_len;
429
430         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
431                         cb->location,
432                         cb->send_buffer_fill, cb->send_buffer_size,
433                         100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
434                         command);
435
436         /* Check if we have enough space for this command. */
437         pthread_mutex_unlock (&cb->send_lock);
438
439         return (0);
440 } /* }}} int wh_write_command */
441
442 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
443                 wh_callback_t *cb)
444 {
445         int status;
446
447         pthread_mutex_lock (&cb->send_lock);
448
449         if (cb->curl == NULL)
450         {
451                 status = wh_callback_init (cb);
452                 if (status != 0)
453                 {
454                         ERROR ("write_http plugin: wh_callback_init failed.");
455                         pthread_mutex_unlock (&cb->send_lock);
456                         return (-1);
457                 }
458         }
459
460         status = format_json_value_list (cb->send_buffer,
461                         &cb->send_buffer_fill,
462                         &cb->send_buffer_free,
463                         ds, vl, cb->store_rates);
464         if (status == (-ENOMEM))
465         {
466                 status = wh_flush_nolock (/* timeout = */ 0, cb);
467                 if (status != 0)
468                 {
469                         wh_reset_buffer (cb);
470                         pthread_mutex_unlock (&cb->send_lock);
471                         return (status);
472                 }
473
474                 status = format_json_value_list (cb->send_buffer,
475                                 &cb->send_buffer_fill,
476                                 &cb->send_buffer_free,
477                                 ds, vl, cb->store_rates);
478         }
479         if (status != 0)
480         {
481                 pthread_mutex_unlock (&cb->send_lock);
482                 return (status);
483         }
484
485         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
486                         cb->location,
487                         cb->send_buffer_fill, cb->send_buffer_size,
488                         100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
489
490         /* Check if we have enough space for this command. */
491         pthread_mutex_unlock (&cb->send_lock);
492
493         return (0);
494 } /* }}} int wh_write_json */
495
496 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
497                 user_data_t *user_data)
498 {
499         wh_callback_t *cb;
500         int status;
501
502         if (user_data == NULL)
503                 return (-EINVAL);
504
505         cb = user_data->data;
506
507         if (cb->format == WH_FORMAT_JSON)
508                 status = wh_write_json (ds, vl, cb);
509         else
510                 status = wh_write_command (ds, vl, cb);
511
512         return (status);
513 } /* }}} int wh_write */
514
515 static int config_set_format (wh_callback_t *cb, /* {{{ */
516                 oconfig_item_t *ci)
517 {
518         char *string;
519
520         if ((ci->values_num != 1)
521                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
522         {
523                 WARNING ("write_http plugin: The `%s' config option "
524                                 "needs exactly one string argument.", ci->key);
525                 return (-1);
526         }
527
528         string = ci->values[0].value.string;
529         if (strcasecmp ("Command", string) == 0)
530                 cb->format = WH_FORMAT_COMMAND;
531         else if (strcasecmp ("JSON", string) == 0)
532                 cb->format = WH_FORMAT_JSON;
533         else
534         {
535                 ERROR ("write_http plugin: Invalid format string: %s",
536                                 string);
537                 return (-1);
538         }
539
540         return (0);
541 } /* }}} int config_set_format */
542
543 static int wh_config_node (oconfig_item_t *ci) /* {{{ */
544 {
545         wh_callback_t *cb;
546         int buffer_size = 0;
547         user_data_t user_data;
548         char callback_name[DATA_MAX_NAME_LEN];
549         int status = 0;
550         int i;
551
552         cb = calloc (1, sizeof (*cb));
553         if (cb == NULL)
554         {
555                 ERROR ("write_http plugin: calloc failed.");
556                 return (-1);
557         }
558         cb->verify_peer = 1;
559         cb->verify_host = 1;
560         cb->format = WH_FORMAT_COMMAND;
561         cb->sslversion = CURL_SSLVERSION_DEFAULT;
562         cb->low_speed_limit = 0;
563         cb->timeout = 0;
564         cb->log_http_error = 0;
565         cb->headers = NULL;
566
567
568         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
569
570         cf_util_get_string (ci, &cb->name);
571
572         /* FIXME: Remove this legacy mode in version 6. */
573         if (strcasecmp ("URL", ci->key) == 0)
574                 cf_util_get_string (ci, &cb->location);
575
576         for (i = 0; i < ci->children_num; i++)
577         {
578                 oconfig_item_t *child = ci->children + i;
579
580                 if (strcasecmp ("URL", child->key) == 0)
581                         status = cf_util_get_string (child, &cb->location);
582                 else if (strcasecmp ("User", child->key) == 0)
583                         status = cf_util_get_string (child, &cb->user);
584                 else if (strcasecmp ("Password", child->key) == 0)
585                         status = cf_util_get_string (child, &cb->pass);
586                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
587                         status = cf_util_get_boolean (child, &cb->verify_peer);
588                 else if (strcasecmp ("VerifyHost", child->key) == 0)
589                         status = cf_util_get_boolean (child, &cb->verify_host);
590                 else if (strcasecmp ("CACert", child->key) == 0)
591                         status = cf_util_get_string (child, &cb->cacert);
592                 else if (strcasecmp ("CAPath", child->key) == 0)
593                         status = cf_util_get_string (child, &cb->capath);
594                 else if (strcasecmp ("ClientKey", child->key) == 0)
595                         status = cf_util_get_string (child, &cb->clientkey);
596                 else if (strcasecmp ("ClientCert", child->key) == 0)
597                         status = cf_util_get_string (child, &cb->clientcert);
598                 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
599                         status = cf_util_get_string (child, &cb->clientkeypass);
600                 else if (strcasecmp ("SSLVersion", child->key) == 0)
601                 {
602                         char *value = NULL;
603
604                         status = cf_util_get_string (child, &value);
605                         if (status != 0)
606                                 break;
607
608                         if (value == NULL || strcasecmp ("default", value) == 0)
609                                 cb->sslversion = CURL_SSLVERSION_DEFAULT;
610                         else if (strcasecmp ("SSLv2", value) == 0)
611                                 cb->sslversion = CURL_SSLVERSION_SSLv2;
612                         else if (strcasecmp ("SSLv3", value) == 0)
613                                 cb->sslversion = CURL_SSLVERSION_SSLv3;
614                         else if (strcasecmp ("TLSv1", value) == 0)
615                                 cb->sslversion = CURL_SSLVERSION_TLSv1;
616 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
617                         else if (strcasecmp ("TLSv1_0", value) == 0)
618                                 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
619                         else if (strcasecmp ("TLSv1_1", value) == 0)
620                                 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
621                         else if (strcasecmp ("TLSv1_2", value) == 0)
622                                 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
623 #endif
624                         else
625                         {
626                                 ERROR ("write_http plugin: Invalid SSLVersion "
627                                                 "option: %s.", value);
628                                 status = EINVAL;
629                         }
630
631                         sfree(value);
632                 }
633                 else if (strcasecmp ("Format", child->key) == 0)
634                         status = config_set_format (cb, child);
635                 else if (strcasecmp ("StoreRates", child->key) == 0)
636                         status = cf_util_get_boolean (child, &cb->store_rates);
637                 else if (strcasecmp ("BufferSize", child->key) == 0)
638                         status = cf_util_get_int (child, &buffer_size);
639                 else if (strcasecmp ("LowSpeedLimit", child->key) == 0)
640                         status = cf_util_get_int (child, &cb->low_speed_limit);
641                 else if (strcasecmp ("Timeout", child->key) == 0)
642                         status = cf_util_get_int (child, &cb->timeout);
643                 else if (strcasecmp ("LogHttpError", child->key) == 0)
644                         status = cf_util_get_boolean (child, &cb->log_http_error);
645                 else
646                 {
647                         ERROR ("write_http plugin: Invalid configuration "
648                                         "option: %s.", child->key);
649                         status = EINVAL;
650                 }
651
652                 if (status != 0)
653                         break;
654         }
655
656         if (status != 0)
657         {
658                 wh_callback_free (cb);
659                 return (status);
660         }
661
662         if (cb->location == NULL)
663         {
664                 ERROR ("write_http plugin: no URL defined for instance '%s'",
665                         cb->name);
666                 wh_callback_free (cb);
667                 return (-1);
668         }
669
670         if (cb->low_speed_limit > 0)
671                 cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
672
673         /* Determine send_buffer_size. */
674         cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
675         if (buffer_size >= 1024)
676                 cb->send_buffer_size = (size_t) buffer_size;
677         else if (buffer_size != 0)
678                 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
679                                 buffer_size);
680
681         /* Allocate the buffer. */
682         cb->send_buffer = malloc (cb->send_buffer_size);
683         if (cb->send_buffer == NULL)
684         {
685                 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
686                 wh_callback_free (cb);
687                 return (-1);
688         }
689         /* Nulls the buffer and sets ..._free and ..._fill. */
690         wh_reset_buffer (cb);
691
692         ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
693                         cb->name);
694         DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
695                         callback_name, cb->location);
696
697         memset (&user_data, 0, sizeof (user_data));
698         user_data.data = cb;
699         user_data.free_func = NULL;
700         plugin_register_flush (callback_name, wh_flush, &user_data);
701
702         user_data.free_func = wh_callback_free;
703         plugin_register_write (callback_name, wh_write, &user_data);
704
705         return (0);
706 } /* }}} int wh_config_node */
707
708 static int wh_config (oconfig_item_t *ci) /* {{{ */
709 {
710         int i;
711
712         for (i = 0; i < ci->children_num; i++)
713         {
714                 oconfig_item_t *child = ci->children + i;
715
716                 if (strcasecmp ("Node", child->key) == 0)
717                         wh_config_node (child);
718                 /* FIXME: Remove this legacy mode in version 6. */
719                 else if (strcasecmp ("URL", child->key) == 0) {
720                         WARNING ("write_http plugin: Legacy <URL> block found. "
721                                 "Please use <Node> instead.");
722                         wh_config_node (child);
723                 }
724                 else
725                 {
726                         ERROR ("write_http plugin: Invalid configuration "
727                                         "option: %s.", child->key);
728                 }
729         }
730
731         return (0);
732 } /* }}} int wh_config */
733
734 static int wh_init (void) /* {{{ */
735 {
736         /* Call this while collectd is still single-threaded to avoid
737          * initialization issues in libgcrypt. */
738         curl_global_init (CURL_GLOBAL_SSL);
739         return (0);
740 } /* }}} int wh_init */
741
742 void module_register (void) /* {{{ */
743 {
744         plugin_register_complex_config ("write_http", wh_config);
745         plugin_register_init ("write_http", wh_init);
746 } /* }}} void module_register */
747
748 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */