Code

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