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