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