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