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