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