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