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