Merge pull request #774 from trenkel/master
[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 #include "plugin.h"
28 #include "common.h"
29 #include "utils_cache.h"
30 #include "utils_format_json.h"
31
32 #if HAVE_PTHREAD_H
33 # include <pthread.h>
34 #endif
35
36 #include <curl/curl.h>
37
38 #ifndef WRITE_HTTP_DEFAULT_BUFFER_SIZE
39 # define WRITE_HTTP_DEFAULT_BUFFER_SIZE 4096
40 #endif
41
42 /*
43  * Private variables
44  */
45 struct wh_callback_s
46 {
47         char *location;
48
49         char *user;
50         char *pass;
51         char *credentials;
52         _Bool verify_peer;
53         _Bool verify_host;
54         char *cacert;
55         char *capath;
56         char *clientkey;
57         char *clientcert;
58         char *clientkeypass;
59         long sslversion;
60         _Bool store_rates;
61
62 #define WH_FORMAT_COMMAND 0
63 #define WH_FORMAT_JSON    1
64         int format;
65
66         CURL *curl;
67         char curl_errbuf[CURL_ERROR_SIZE];
68
69         char  *send_buffer;
70         size_t send_buffer_size;
71         size_t send_buffer_free;
72         size_t send_buffer_fill;
73         cdtime_t send_buffer_init_time;
74
75         pthread_mutex_t send_lock;
76 };
77 typedef struct wh_callback_s wh_callback_t;
78
79 static void wh_reset_buffer (wh_callback_t *cb)  /* {{{ */
80 {
81         memset (cb->send_buffer, 0, cb->send_buffer_size);
82         cb->send_buffer_free = cb->send_buffer_size;
83         cb->send_buffer_fill = 0;
84         cb->send_buffer_init_time = cdtime ();
85
86         if (cb->format == WH_FORMAT_JSON)
87         {
88                 format_json_initialize (cb->send_buffer,
89                                 &cb->send_buffer_fill,
90                                 &cb->send_buffer_free);
91         }
92 } /* }}} wh_reset_buffer */
93
94 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
95 {
96         int status = 0;
97
98         curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
99         status = curl_easy_perform (cb->curl);
100         if (status != CURLE_OK)
101         {
102                 ERROR ("write_http plugin: curl_easy_perform failed with "
103                                 "status %i: %s",
104                                 status, cb->curl_errbuf);
105         }
106         return (status);
107 } /* }}} wh_send_buffer */
108
109 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
110 {
111         struct curl_slist *headers;
112
113         if (cb->curl != NULL)
114                 return (0);
115
116         cb->curl = curl_easy_init ();
117         if (cb->curl == NULL)
118         {
119                 ERROR ("curl plugin: curl_easy_init failed.");
120                 return (-1);
121         }
122
123         curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
124         curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
125
126         headers = NULL;
127         headers = curl_slist_append (headers, "Accept:  */*");
128         if (cb->format == WH_FORMAT_JSON)
129                 headers = curl_slist_append (headers, "Content-Type: application/json");
130         else
131                 headers = curl_slist_append (headers, "Content-Type: text/plain");
132         headers = curl_slist_append (headers, "Expect:");
133         curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
134
135         curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
136         curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
137
138         if (cb->user != NULL)
139         {
140                 size_t credentials_size;
141
142                 credentials_size = strlen (cb->user) + 2;
143                 if (cb->pass != NULL)
144                         credentials_size += strlen (cb->pass);
145
146                 cb->credentials = (char *) malloc (credentials_size);
147                 if (cb->credentials == NULL)
148                 {
149                         ERROR ("curl plugin: malloc failed.");
150                         return (-1);
151                 }
152
153                 ssnprintf (cb->credentials, credentials_size, "%s:%s",
154                                 cb->user, (cb->pass == NULL) ? "" : cb->pass);
155                 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
156                 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
157         }
158
159         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
160         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
161                         cb->verify_host ? 2L : 0L);
162         curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion);
163         if (cb->cacert != NULL)
164                 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
165         if (cb->capath != NULL)
166                 curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath);
167
168         if (cb->clientkey != NULL && cb->clientcert != NULL)
169         {
170             curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey);
171             curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert);
172
173             if (cb->clientkeypass != NULL)
174                 curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass);
175         }
176
177         wh_reset_buffer (cb);
178
179         return (0);
180 } /* }}} int wh_callback_init */
181
182 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
183 {
184         int status;
185
186         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
187                         "send_buffer_fill = %zu;",
188                         CDTIME_T_TO_DOUBLE (timeout),
189                         cb->send_buffer_fill);
190
191         /* timeout == 0  => flush unconditionally */
192         if (timeout > 0)
193         {
194                 cdtime_t now;
195
196                 now = cdtime ();
197                 if ((cb->send_buffer_init_time + timeout) > now)
198                         return (0);
199         }
200
201         if (cb->format == WH_FORMAT_COMMAND)
202         {
203                 if (cb->send_buffer_fill <= 0)
204                 {
205                         cb->send_buffer_init_time = cdtime ();
206                         return (0);
207                 }
208
209                 status = wh_send_buffer (cb);
210                 wh_reset_buffer (cb);
211         }
212         else if (cb->format == WH_FORMAT_JSON)
213         {
214                 if (cb->send_buffer_fill <= 2)
215                 {
216                         cb->send_buffer_init_time = cdtime ();
217                         return (0);
218                 }
219
220                 status = format_json_finalize (cb->send_buffer,
221                                 &cb->send_buffer_fill,
222                                 &cb->send_buffer_free);
223                 if (status != 0)
224                 {
225                         ERROR ("write_http: wh_flush_nolock: "
226                                         "format_json_finalize failed.");
227                         wh_reset_buffer (cb);
228                         return (status);
229                 }
230
231                 status = wh_send_buffer (cb);
232                 wh_reset_buffer (cb);
233         }
234         else
235         {
236                 ERROR ("write_http: wh_flush_nolock: "
237                                 "Unknown format: %i",
238                                 cb->format);
239                 return (-1);
240         }
241
242         return (status);
243 } /* }}} wh_flush_nolock */
244
245 static int wh_flush (cdtime_t timeout, /* {{{ */
246                 const char *identifier __attribute__((unused)),
247                 user_data_t *user_data)
248 {
249         wh_callback_t *cb;
250         int status;
251
252         if (user_data == NULL)
253                 return (-EINVAL);
254
255         cb = user_data->data;
256
257         pthread_mutex_lock (&cb->send_lock);
258
259         if (cb->curl == NULL)
260         {
261                 status = wh_callback_init (cb);
262                 if (status != 0)
263                 {
264                         ERROR ("write_http plugin: wh_callback_init failed.");
265                         pthread_mutex_unlock (&cb->send_lock);
266                         return (-1);
267                 }
268         }
269
270         status = wh_flush_nolock (timeout, cb);
271         pthread_mutex_unlock (&cb->send_lock);
272
273         return (status);
274 } /* }}} int wh_flush */
275
276 static void wh_callback_free (void *data) /* {{{ */
277 {
278         wh_callback_t *cb;
279
280         if (data == NULL)
281                 return;
282
283         cb = data;
284
285         wh_flush_nolock (/* timeout = */ 0, cb);
286
287         if (cb->curl != NULL)
288         {
289                 curl_easy_cleanup (cb->curl);
290                 cb->curl = NULL;
291         }
292         sfree (cb->location);
293         sfree (cb->user);
294         sfree (cb->pass);
295         sfree (cb->credentials);
296         sfree (cb->cacert);
297         sfree (cb->capath);
298         sfree (cb->clientkey);
299         sfree (cb->clientcert);
300         sfree (cb->clientkeypass);
301         sfree (cb->send_buffer);
302
303         sfree (cb);
304 } /* }}} void wh_callback_free */
305
306 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
307                 wh_callback_t *cb)
308 {
309         char key[10*DATA_MAX_NAME_LEN];
310         char values[512];
311         char command[1024];
312         size_t command_len;
313
314         int status;
315
316         if (0 != strcmp (ds->type, vl->type)) {
317                 ERROR ("write_http plugin: DS type does not match "
318                                 "value list type");
319                 return -1;
320         }
321
322         /* Copy the identifier to `key' and escape it. */
323         status = FORMAT_VL (key, sizeof (key), vl);
324         if (status != 0) {
325                 ERROR ("write_http plugin: error with format_name");
326                 return (status);
327         }
328         escape_string (key, sizeof (key));
329
330         /* Convert the values to an ASCII representation and put that into
331          * `values'. */
332         status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
333         if (status != 0) {
334                 ERROR ("write_http plugin: error with "
335                                 "wh_value_list_to_string");
336                 return (status);
337         }
338
339         command_len = (size_t) ssnprintf (command, sizeof (command),
340                         "PUTVAL %s interval=%.3f %s\r\n",
341                         key,
342                         CDTIME_T_TO_DOUBLE (vl->interval),
343                         values);
344         if (command_len >= sizeof (command)) {
345                 ERROR ("write_http plugin: Command buffer too small: "
346                                 "Need %zu bytes.", command_len + 1);
347                 return (-1);
348         }
349
350         pthread_mutex_lock (&cb->send_lock);
351
352         if (cb->curl == NULL)
353         {
354                 status = wh_callback_init (cb);
355                 if (status != 0)
356                 {
357                         ERROR ("write_http plugin: wh_callback_init failed.");
358                         pthread_mutex_unlock (&cb->send_lock);
359                         return (-1);
360                 }
361         }
362
363         if (command_len >= cb->send_buffer_free)
364         {
365                 status = wh_flush_nolock (/* timeout = */ 0, cb);
366                 if (status != 0)
367                 {
368                         pthread_mutex_unlock (&cb->send_lock);
369                         return (status);
370                 }
371         }
372         assert (command_len < cb->send_buffer_free);
373
374         /* `command_len + 1' because `command_len' does not include the
375          * trailing null byte. Neither does `send_buffer_fill'. */
376         memcpy (cb->send_buffer + cb->send_buffer_fill,
377                         command, command_len + 1);
378         cb->send_buffer_fill += command_len;
379         cb->send_buffer_free -= command_len;
380
381         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
382                         cb->location,
383                         cb->send_buffer_fill, cb->send_buffer_size,
384                         100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size),
385                         command);
386
387         /* Check if we have enough space for this command. */
388         pthread_mutex_unlock (&cb->send_lock);
389
390         return (0);
391 } /* }}} int wh_write_command */
392
393 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
394                 wh_callback_t *cb)
395 {
396         int status;
397
398         pthread_mutex_lock (&cb->send_lock);
399
400         if (cb->curl == NULL)
401         {
402                 status = wh_callback_init (cb);
403                 if (status != 0)
404                 {
405                         ERROR ("write_http plugin: wh_callback_init failed.");
406                         pthread_mutex_unlock (&cb->send_lock);
407                         return (-1);
408                 }
409         }
410
411         status = format_json_value_list (cb->send_buffer,
412                         &cb->send_buffer_fill,
413                         &cb->send_buffer_free,
414                         ds, vl, cb->store_rates);
415         if (status == (-ENOMEM))
416         {
417                 status = wh_flush_nolock (/* timeout = */ 0, cb);
418                 if (status != 0)
419                 {
420                         wh_reset_buffer (cb);
421                         pthread_mutex_unlock (&cb->send_lock);
422                         return (status);
423                 }
424
425                 status = format_json_value_list (cb->send_buffer,
426                                 &cb->send_buffer_fill,
427                                 &cb->send_buffer_free,
428                                 ds, vl, cb->store_rates);
429         }
430         if (status != 0)
431         {
432                 pthread_mutex_unlock (&cb->send_lock);
433                 return (status);
434         }
435
436         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
437                         cb->location,
438                         cb->send_buffer_fill, cb->send_buffer_size,
439                         100.0 * ((double) cb->send_buffer_fill) / ((double) cb->send_buffer_size));
440
441         /* Check if we have enough space for this command. */
442         pthread_mutex_unlock (&cb->send_lock);
443
444         return (0);
445 } /* }}} int wh_write_json */
446
447 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
448                 user_data_t *user_data)
449 {
450         wh_callback_t *cb;
451         int status;
452
453         if (user_data == NULL)
454                 return (-EINVAL);
455
456         cb = user_data->data;
457
458         if (cb->format == WH_FORMAT_JSON)
459                 status = wh_write_json (ds, vl, cb);
460         else
461                 status = wh_write_command (ds, vl, cb);
462
463         return (status);
464 } /* }}} int wh_write */
465
466 static int config_set_format (wh_callback_t *cb, /* {{{ */
467                 oconfig_item_t *ci)
468 {
469         char *string;
470
471         if ((ci->values_num != 1)
472                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
473         {
474                 WARNING ("write_http plugin: The `%s' config option "
475                                 "needs exactly one string argument.", ci->key);
476                 return (-1);
477         }
478
479         string = ci->values[0].value.string;
480         if (strcasecmp ("Command", string) == 0)
481                 cb->format = WH_FORMAT_COMMAND;
482         else if (strcasecmp ("JSON", string) == 0)
483                 cb->format = WH_FORMAT_JSON;
484         else
485         {
486                 ERROR ("write_http plugin: Invalid format string: %s",
487                                 string);
488                 return (-1);
489         }
490
491         return (0);
492 } /* }}} int config_set_format */
493
494 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
495 {
496         wh_callback_t *cb;
497         int buffer_size = 0;
498         user_data_t user_data;
499         int i;
500
501         cb = malloc (sizeof (*cb));
502         if (cb == NULL)
503         {
504                 ERROR ("write_http plugin: malloc failed.");
505                 return (-1);
506         }
507         memset (cb, 0, sizeof (*cb));
508         cb->verify_peer = 1;
509         cb->verify_host = 1;
510         cb->format = WH_FORMAT_COMMAND;
511         cb->sslversion = CURL_SSLVERSION_DEFAULT;
512
513         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
514
515         cf_util_get_string (ci, &cb->location);
516         if (cb->location == NULL)
517                 return (-1);
518
519         for (i = 0; i < ci->children_num; i++)
520         {
521                 oconfig_item_t *child = ci->children + i;
522
523                 if (strcasecmp ("User", child->key) == 0)
524                         cf_util_get_string (child, &cb->user);
525                 else if (strcasecmp ("Password", child->key) == 0)
526                         cf_util_get_string (child, &cb->pass);
527                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
528                         cf_util_get_boolean (child, &cb->verify_peer);
529                 else if (strcasecmp ("VerifyHost", child->key) == 0)
530                         cf_util_get_boolean (child, &cb->verify_host);
531                 else if (strcasecmp ("CACert", child->key) == 0)
532                         cf_util_get_string (child, &cb->cacert);
533                 else if (strcasecmp ("CAPath", child->key) == 0)
534                         cf_util_get_string (child, &cb->capath);
535                 else if (strcasecmp ("ClientKey", child->key) == 0)
536                         cf_util_get_string (child, &cb->clientkey);
537                 else if (strcasecmp ("ClientCert", child->key) == 0)
538                         cf_util_get_string (child, &cb->clientcert);
539                 else if (strcasecmp ("ClientKeyPass", child->key) == 0)
540                         cf_util_get_string (child, &cb->clientkeypass);
541                 else if (strcasecmp ("SSLVersion", child->key) == 0)
542                 {
543                         char *value = NULL;
544
545                         cf_util_get_string (child, &value);
546
547                         if (value == NULL || strcasecmp ("default", value) == 0)
548                                 cb->sslversion = CURL_SSLVERSION_DEFAULT;
549                         else if (strcasecmp ("SSLv2", value) == 0)
550                                 cb->sslversion = CURL_SSLVERSION_SSLv2;
551                         else if (strcasecmp ("SSLv3", value) == 0)
552                                 cb->sslversion = CURL_SSLVERSION_SSLv3;
553                         else if (strcasecmp ("TLSv1", value) == 0)
554                                 cb->sslversion = CURL_SSLVERSION_TLSv1;
555 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 34)
556                         else if (strcasecmp ("TLSv1_0", value) == 0)
557                                 cb->sslversion = CURL_SSLVERSION_TLSv1_0;
558                         else if (strcasecmp ("TLSv1_1", value) == 0)
559                                 cb->sslversion = CURL_SSLVERSION_TLSv1_1;
560                         else if (strcasecmp ("TLSv1_2", value) == 0)
561                                 cb->sslversion = CURL_SSLVERSION_TLSv1_2;
562 #endif
563                         else
564                                 ERROR ("write_http plugin: Invalid SSLVersion "
565                                                 "option: %s.", value);
566
567                         sfree(value);
568                 }
569                 else if (strcasecmp ("Format", child->key) == 0)
570                         config_set_format (cb, child);
571                 else if (strcasecmp ("StoreRates", child->key) == 0)
572                         cf_util_get_boolean (child, &cb->store_rates);
573                 else if (strcasecmp ("BufferSize", child->key) == 0)
574                         cf_util_get_int (child, &buffer_size);
575                 else
576                 {
577                         ERROR ("write_http plugin: Invalid configuration "
578                                         "option: %s.", child->key);
579                 }
580         }
581
582         /* Determine send_buffer_size. */
583         cb->send_buffer_size = WRITE_HTTP_DEFAULT_BUFFER_SIZE;
584         if (buffer_size >= 1024)
585                 cb->send_buffer_size = (size_t) buffer_size;
586         else if (buffer_size != 0)
587                 ERROR ("write_http plugin: Ignoring invalid BufferSize setting (%d).",
588                                 buffer_size);
589
590         /* Allocate the buffer. */
591         cb->send_buffer = malloc (cb->send_buffer_size);
592         if (cb->send_buffer == NULL)
593         {
594                 ERROR ("write_http plugin: malloc(%zu) failed.", cb->send_buffer_size);
595                 wh_callback_free (cb);
596                 return (-1);
597         }
598         /* Nulls the buffer and sets ..._free and ..._fill. */
599         wh_reset_buffer (cb);
600
601         DEBUG ("write_http: Registering write callback with URL %s",
602                         cb->location);
603
604         memset (&user_data, 0, sizeof (user_data));
605         user_data.data = cb;
606         user_data.free_func = NULL;
607         plugin_register_flush ("write_http", wh_flush, &user_data);
608
609         user_data.free_func = wh_callback_free;
610         plugin_register_write ("write_http", wh_write, &user_data);
611
612         return (0);
613 } /* }}} int wh_config_url */
614
615 static int wh_config (oconfig_item_t *ci) /* {{{ */
616 {
617         int i;
618
619         for (i = 0; i < ci->children_num; i++)
620         {
621                 oconfig_item_t *child = ci->children + i;
622
623                 if (strcasecmp ("URL", child->key) == 0)
624                         wh_config_url (child);
625                 else
626                 {
627                         ERROR ("write_http plugin: Invalid configuration "
628                                         "option: %s.", child->key);
629                 }
630         }
631
632         return (0);
633 } /* }}} int wh_config */
634
635 static int wh_init (void) /* {{{ */
636 {
637         /* Call this while collectd is still single-threaded to avoid
638          * initialization issues in libgcrypt. */
639         curl_global_init (CURL_GLOBAL_SSL);
640         return (0);
641 } /* }}} int wh_init */
642
643 void module_register (void) /* {{{ */
644 {
645         plugin_register_complex_config ("write_http", wh_config);
646         plugin_register_init ("write_http", wh_init);
647 } /* }}} void module_register */
648
649 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */