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