Replace zu with PRIu64 and llu with new macro, PRIsz, which will make it easier to...
[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 = %" PRIsz ";",
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 %" PRIsz " 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 %" PRIsz "/%" PRIsz " (%g%%) \"%s\"",
414         cb->location, 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 %" PRIsz "/%" PRIsz " (%g%%)",
456         cb->location, 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 %" PRIsz "/%" PRIsz " (%g%%)",
505         cb->location, 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   if (cb->metrics_prefix == NULL) {
641     ERROR("write_http plugin: strdup failed.");
642     sfree(cb);
643     return -1;
644   }
645
646   pthread_mutex_init(&cb->send_lock, /* attr = */ NULL);
647
648   cf_util_get_string(ci, &cb->name);
649
650   /* FIXME: Remove this legacy mode in version 6. */
651   if (strcasecmp("URL", ci->key) == 0)
652     cf_util_get_string(ci, &cb->location);
653
654   for (int i = 0; i < ci->children_num; i++) {
655     oconfig_item_t *child = ci->children + i;
656
657     if (strcasecmp("URL", child->key) == 0)
658       status = cf_util_get_string(child, &cb->location);
659     else if (strcasecmp("User", child->key) == 0)
660       status = cf_util_get_string(child, &cb->user);
661     else if (strcasecmp("Password", child->key) == 0)
662       status = cf_util_get_string(child, &cb->pass);
663     else if (strcasecmp("VerifyPeer", child->key) == 0)
664       status = cf_util_get_boolean(child, &cb->verify_peer);
665     else if (strcasecmp("VerifyHost", child->key) == 0)
666       status = cf_util_get_boolean(child, &cb->verify_host);
667     else if (strcasecmp("CACert", child->key) == 0)
668       status = cf_util_get_string(child, &cb->cacert);
669     else if (strcasecmp("CAPath", child->key) == 0)
670       status = cf_util_get_string(child, &cb->capath);
671     else if (strcasecmp("ClientKey", child->key) == 0)
672       status = cf_util_get_string(child, &cb->clientkey);
673     else if (strcasecmp("ClientCert", child->key) == 0)
674       status = cf_util_get_string(child, &cb->clientcert);
675     else if (strcasecmp("ClientKeyPass", child->key) == 0)
676       status = cf_util_get_string(child, &cb->clientkeypass);
677     else if (strcasecmp("SSLVersion", child->key) == 0) {
678       char *value = NULL;
679
680       status = cf_util_get_string(child, &value);
681       if (status != 0)
682         break;
683
684       if (value == NULL || strcasecmp("default", value) == 0)
685         cb->sslversion = CURL_SSLVERSION_DEFAULT;
686       else if (strcasecmp("SSLv2", value) == 0)
687         cb->sslversion = CURL_SSLVERSION_SSLv2;
688       else if (strcasecmp("SSLv3", value) == 0)
689         cb->sslversion = CURL_SSLVERSION_SSLv3;
690       else if (strcasecmp("TLSv1", value) == 0)
691         cb->sslversion = CURL_SSLVERSION_TLSv1;
692 #if (LIBCURL_VERSION_MAJOR > 7) ||                                             \
693     (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
694       else if (strcasecmp("TLSv1_0", value) == 0)
695         cb->sslversion = CURL_SSLVERSION_TLSv1_0;
696       else if (strcasecmp("TLSv1_1", value) == 0)
697         cb->sslversion = CURL_SSLVERSION_TLSv1_1;
698       else if (strcasecmp("TLSv1_2", value) == 0)
699         cb->sslversion = CURL_SSLVERSION_TLSv1_2;
700 #endif
701       else {
702         ERROR("write_http plugin: Invalid SSLVersion "
703               "option: %s.",
704               value);
705         status = EINVAL;
706       }
707
708       sfree(value);
709     } else if (strcasecmp("Format", child->key) == 0)
710       status = config_set_format(cb, child);
711     else if (strcasecmp("Metrics", child->key) == 0)
712       cf_util_get_boolean(child, &cb->send_metrics);
713     else if (strcasecmp("Notifications", child->key) == 0)
714       cf_util_get_boolean(child, &cb->send_notifications);
715     else if (strcasecmp("StoreRates", child->key) == 0)
716       status = cf_util_get_boolean(child, &cb->store_rates);
717     else if (strcasecmp("BufferSize", child->key) == 0)
718       status = cf_util_get_int(child, &buffer_size);
719     else if (strcasecmp("LowSpeedLimit", child->key) == 0)
720       status = cf_util_get_int(child, &cb->low_speed_limit);
721     else if (strcasecmp("Timeout", child->key) == 0)
722       status = cf_util_get_int(child, &cb->timeout);
723     else if (strcasecmp("LogHttpError", child->key) == 0)
724       status = cf_util_get_boolean(child, &cb->log_http_error);
725     else if (strcasecmp("Header", child->key) == 0)
726       status = wh_config_append_string("Header", &cb->headers, child);
727     else if (strcasecmp("Attribute", child->key) == 0) {
728       char *key = NULL;
729       char *val = NULL;
730
731       if (child->values_num != 2) {
732         WARNING("write_http plugin: Attribute need both a key and a value.");
733         break;
734       }
735       if (child->values[0].type != OCONFIG_TYPE_STRING ||
736           child->values[1].type != OCONFIG_TYPE_STRING) {
737         WARNING("write_http plugin: Attribute needs string arguments.");
738         break;
739       }
740       if ((key = strdup(child->values[0].value.string)) == NULL) {
741         WARNING("cannot allocate memory for attribute key.");
742         break;
743       }
744       if ((val = strdup(child->values[1].value.string)) == NULL) {
745         WARNING("cannot allocate memory for attribute value.");
746         sfree(key);
747         break;
748       }
749       strarray_add(&http_attrs, &http_attrs_num, key);
750       strarray_add(&http_attrs, &http_attrs_num, val);
751       DEBUG("write_http plugin: got attribute: %s => %s", key, val);
752       sfree(key);
753       sfree(val);
754     } else if (strcasecmp("TTL", child->key) == 0) {
755       status = cf_util_get_int(child, &cb->data_ttl);
756     } else if (strcasecmp("Prefix", child->key) == 0) {
757       status = cf_util_get_string(child, &cb->metrics_prefix);
758     } else {
759       ERROR("write_http plugin: Invalid configuration "
760             "option: %s.",
761             child->key);
762       status = EINVAL;
763     }
764
765     if (status != 0)
766       break;
767   }
768
769   if (status != 0) {
770     wh_callback_free(cb);
771     return status;
772   }
773
774   if (cb->location == NULL) {
775     ERROR("write_http plugin: no URL defined for instance '%s'", cb->name);
776     wh_callback_free(cb);
777     return -1;
778   }
779
780   if (!cb->send_metrics && !cb->send_notifications) {
781     ERROR("write_http plugin: Neither metrics nor notifications "
782           "are enabled for \"%s\".",
783           cb->name);
784     wh_callback_free(cb);
785     return -1;
786   }
787
788   if (strlen(cb->metrics_prefix) == 0)
789     sfree(cb->metrics_prefix);
790
791   if (cb->low_speed_limit > 0)
792     cb->low_speed_time = CDTIME_T_TO_TIME_T(plugin_get_interval());
793
794   /* Determine send_buffer_size. */
795   cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
796   if (buffer_size >= 1024)
797     cb->send_buffer_size = (size_t)buffer_size;
798   else if (buffer_size != 0)
799     ERROR("write_http plugin: Ignoring invalid BufferSize setting (%d).",
800           buffer_size);
801
802   /* Allocate the buffer. */
803   cb->send_buffer = malloc(cb->send_buffer_size);
804   if (cb->send_buffer == NULL) {
805     ERROR("write_http plugin: malloc(%" PRIsz ") failed.",
806           cb->send_buffer_size);
807     wh_callback_free(cb);
808     return -1;
809   }
810   /* Nulls the buffer and sets ..._free and ..._fill. */
811   wh_reset_buffer(cb);
812
813   snprintf(callback_name, sizeof(callback_name), "write_http/%s", cb->name);
814   DEBUG("write_http: Registering write callback '%s' with URL '%s'",
815         callback_name, cb->location);
816
817   user_data_t user_data = {
818       .data = cb, .free_func = wh_callback_free,
819   };
820
821   if (cb->send_metrics) {
822     plugin_register_write(callback_name, wh_write, &user_data);
823     user_data.free_func = NULL;
824
825     plugin_register_flush(callback_name, wh_flush, &user_data);
826   }
827
828   if (cb->send_notifications) {
829     plugin_register_notification(callback_name, wh_notify, &user_data);
830     user_data.free_func = NULL;
831   }
832
833   return 0;
834 } /* }}} int wh_config_node */
835
836 static int wh_config(oconfig_item_t *ci) /* {{{ */
837 {
838   for (int i = 0; i < ci->children_num; i++) {
839     oconfig_item_t *child = ci->children + i;
840
841     if (strcasecmp("Node", child->key) == 0)
842       wh_config_node(child);
843     /* FIXME: Remove this legacy mode in version 6. */
844     else if (strcasecmp("URL", child->key) == 0) {
845       WARNING("write_http plugin: Legacy <URL> block found. "
846               "Please use <Node> instead.");
847       wh_config_node(child);
848     } else {
849       ERROR("write_http plugin: Invalid configuration "
850             "option: %s.",
851             child->key);
852     }
853   }
854
855   return 0;
856 } /* }}} int wh_config */
857
858 static int wh_init(void) /* {{{ */
859 {
860   /* Call this while collectd is still single-threaded to avoid
861    * initialization issues in libgcrypt. */
862   curl_global_init(CURL_GLOBAL_SSL);
863   return 0;
864 } /* }}} int wh_init */
865
866 void module_register(void) /* {{{ */
867 {
868   plugin_register_complex_config("write_http", wh_config);
869   plugin_register_init("write_http", wh_init);
870 } /* }}} void module_register */