Merge branch 'ff/highres'
[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         cdtime_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 = cdtime ();
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_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
115
116         headers = NULL;
117         headers = curl_slist_append (headers, "Accept:  */*");
118         if (cb->format == WH_FORMAT_JSON)
119                 headers = curl_slist_append (headers, "Content-Type: application/json");
120         else
121                 headers = curl_slist_append (headers, "Content-Type: text/plain");
122         headers = curl_slist_append (headers, "Expect:");
123         curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
124
125         curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
126         curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
127
128         if (cb->user != NULL)
129         {
130                 size_t credentials_size;
131
132                 credentials_size = strlen (cb->user) + 2;
133                 if (cb->pass != NULL)
134                         credentials_size += strlen (cb->pass);
135
136                 cb->credentials = (char *) malloc (credentials_size);
137                 if (cb->credentials == NULL)
138                 {
139                         ERROR ("curl plugin: malloc failed.");
140                         return (-1);
141                 }
142
143                 ssnprintf (cb->credentials, credentials_size, "%s:%s",
144                                 cb->user, (cb->pass == NULL) ? "" : cb->pass);
145                 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
146                 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
147         }
148
149         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, cb->verify_peer);
150         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
151                         cb->verify_host ? 2 : 0);
152         if (cb->cacert != NULL)
153                 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
154
155         wh_reset_buffer (cb);
156
157         return (0);
158 } /* }}} int wh_callback_init */
159
160 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
161 {
162         int status;
163
164         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
165                         "send_buffer_fill = %zu;",
166                         CDTIME_T_TO_DOUBLE (timeout),
167                         cb->send_buffer_fill);
168
169         /* timeout == 0  => flush unconditionally */
170         if (timeout > 0)
171         {
172                 cdtime_t now;
173
174                 now = cdtime ();
175                 if ((cb->send_buffer_init_time + timeout) > now)
176                         return (0);
177         }
178
179         if (cb->format == WH_FORMAT_COMMAND)
180         {
181                 if (cb->send_buffer_fill <= 0)
182                 {
183                         cb->send_buffer_init_time = cdtime ();
184                         return (0);
185                 }
186
187                 status = wh_send_buffer (cb);
188                 wh_reset_buffer (cb);
189         }
190         else if (cb->format == WH_FORMAT_JSON)
191         {
192                 if (cb->send_buffer_fill <= 2)
193                 {
194                         cb->send_buffer_init_time = cdtime ();
195                         return (0);
196                 }
197
198                 status = format_json_finalize (cb->send_buffer,
199                                 &cb->send_buffer_fill,
200                                 &cb->send_buffer_free);
201                 if (status != 0)
202                 {
203                         ERROR ("write_http: wh_flush_nolock: "
204                                         "format_json_finalize failed.");
205                         wh_reset_buffer (cb);
206                         return (status);
207                 }
208
209                 status = wh_send_buffer (cb);
210                 wh_reset_buffer (cb);
211         }
212         else
213         {
214                 ERROR ("write_http: wh_flush_nolock: "
215                                 "Unknown format: %i",
216                                 cb->format);
217                 return (-1);
218         }
219
220         return (status);
221 } /* }}} wh_flush_nolock */
222
223 static int wh_flush (cdtime_t timeout, /* {{{ */
224                 const char *identifier __attribute__((unused)),
225                 user_data_t *user_data)
226 {
227         wh_callback_t *cb;
228         int status;
229
230         if (user_data == NULL)
231                 return (-EINVAL);
232
233         cb = user_data->data;
234
235         pthread_mutex_lock (&cb->send_lock);
236
237         if (cb->curl == NULL)
238         {
239                 status = wh_callback_init (cb);
240                 if (status != 0)
241                 {
242                         ERROR ("write_http plugin: wh_callback_init failed.");
243                         pthread_mutex_unlock (&cb->send_lock);
244                         return (-1);
245                 }
246         }
247
248         status = wh_flush_nolock (timeout, cb);
249         pthread_mutex_unlock (&cb->send_lock);
250
251         return (status);
252 } /* }}} int wh_flush */
253
254 static void wh_callback_free (void *data) /* {{{ */
255 {
256         wh_callback_t *cb;
257
258         if (data == NULL)
259                 return;
260
261         cb = data;
262
263         wh_flush_nolock (/* timeout = */ 0, cb);
264
265         curl_easy_cleanup (cb->curl);
266         sfree (cb->location);
267         sfree (cb->user);
268         sfree (cb->pass);
269         sfree (cb->credentials);
270         sfree (cb->cacert);
271
272         sfree (cb);
273 } /* }}} void wh_callback_free */
274
275 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
276                 wh_callback_t *cb)
277 {
278         char key[10*DATA_MAX_NAME_LEN];
279         char values[512];
280         char command[1024];
281         size_t command_len;
282
283         int status;
284
285         if (0 != strcmp (ds->type, vl->type)) {
286                 ERROR ("write_http plugin: DS type does not match "
287                                 "value list type");
288                 return -1;
289         }
290
291         /* Copy the identifier to `key' and escape it. */
292         status = FORMAT_VL (key, sizeof (key), vl);
293         if (status != 0) {
294                 ERROR ("write_http plugin: error with format_name");
295                 return (status);
296         }
297         escape_string (key, sizeof (key));
298
299         /* Convert the values to an ASCII representation and put that into
300          * `values'. */
301         status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
302         if (status != 0) {
303                 ERROR ("write_http plugin: error with "
304                                 "wh_value_list_to_string");
305                 return (status);
306         }
307
308         command_len = (size_t) ssnprintf (command, sizeof (command),
309                         "PUTVAL %s interval=%.3f %s\r\n",
310                         key,
311                         CDTIME_T_TO_DOUBLE (vl->interval),
312                         values);
313         if (command_len >= sizeof (command)) {
314                 ERROR ("write_http plugin: Command buffer too small: "
315                                 "Need %zu bytes.", command_len + 1);
316                 return (-1);
317         }
318
319         pthread_mutex_lock (&cb->send_lock);
320
321         if (cb->curl == NULL)
322         {
323                 status = wh_callback_init (cb);
324                 if (status != 0)
325                 {
326                         ERROR ("write_http plugin: wh_callback_init failed.");
327                         pthread_mutex_unlock (&cb->send_lock);
328                         return (-1);
329                 }
330         }
331
332         if (command_len >= cb->send_buffer_free)
333         {
334                 status = wh_flush_nolock (/* timeout = */ 0, cb);
335                 if (status != 0)
336                 {
337                         pthread_mutex_unlock (&cb->send_lock);
338                         return (status);
339                 }
340         }
341         assert (command_len < cb->send_buffer_free);
342
343         /* `command_len + 1' because `command_len' does not include the
344          * trailing null byte. Neither does `send_buffer_fill'. */
345         memcpy (cb->send_buffer + cb->send_buffer_fill,
346                         command, command_len + 1);
347         cb->send_buffer_fill += command_len;
348         cb->send_buffer_free -= command_len;
349
350         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
351                         cb->location,
352                         cb->send_buffer_fill, sizeof (cb->send_buffer),
353                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
354                         command);
355
356         /* Check if we have enough space for this command. */
357         pthread_mutex_unlock (&cb->send_lock);
358
359         return (0);
360 } /* }}} int wh_write_command */
361
362 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
363                 wh_callback_t *cb)
364 {
365         int status;
366
367         pthread_mutex_lock (&cb->send_lock);
368
369         if (cb->curl == NULL)
370         {
371                 status = wh_callback_init (cb);
372                 if (status != 0)
373                 {
374                         ERROR ("write_http plugin: wh_callback_init failed.");
375                         pthread_mutex_unlock (&cb->send_lock);
376                         return (-1);
377                 }
378         }
379
380         status = format_json_value_list (cb->send_buffer,
381                         &cb->send_buffer_fill,
382                         &cb->send_buffer_free,
383                         ds, vl, cb->store_rates);
384         if (status == (-ENOMEM))
385         {
386                 status = wh_flush_nolock (/* timeout = */ 0, cb);
387                 if (status != 0)
388                 {
389                         wh_reset_buffer (cb);
390                         pthread_mutex_unlock (&cb->send_lock);
391                         return (status);
392                 }
393
394                 status = format_json_value_list (cb->send_buffer,
395                                 &cb->send_buffer_fill,
396                                 &cb->send_buffer_free,
397                                 ds, vl, cb->store_rates);
398         }
399         if (status != 0)
400         {
401                 pthread_mutex_unlock (&cb->send_lock);
402                 return (status);
403         }
404
405         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
406                         cb->location,
407                         cb->send_buffer_fill, sizeof (cb->send_buffer),
408                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
409
410         /* Check if we have enough space for this command. */
411         pthread_mutex_unlock (&cb->send_lock);
412
413         return (0);
414 } /* }}} int wh_write_json */
415
416 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
417                 user_data_t *user_data)
418 {
419         wh_callback_t *cb;
420         int status;
421
422         if (user_data == NULL)
423                 return (-EINVAL);
424
425         cb = user_data->data;
426
427         if (cb->format == WH_FORMAT_JSON)
428                 status = wh_write_json (ds, vl, cb);
429         else
430                 status = wh_write_command (ds, vl, cb);
431
432         return (status);
433 } /* }}} int wh_write */
434
435 static int config_set_string (char **ret_string, /* {{{ */
436                 oconfig_item_t *ci)
437 {
438         char *string;
439
440         if ((ci->values_num != 1)
441                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
442         {
443                 WARNING ("write_http plugin: The `%s' config option "
444                                 "needs exactly one string argument.", ci->key);
445                 return (-1);
446         }
447
448         string = strdup (ci->values[0].value.string);
449         if (string == NULL)
450         {
451                 ERROR ("write_http plugin: strdup failed.");
452                 return (-1);
453         }
454
455         if (*ret_string != NULL)
456                 free (*ret_string);
457         *ret_string = string;
458
459         return (0);
460 } /* }}} int config_set_string */
461
462 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
463 {
464         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
465         {
466                 WARNING ("write_http plugin: The `%s' config option "
467                                 "needs exactly one boolean argument.", ci->key);
468                 return (-1);
469         }
470
471         *dest = ci->values[0].value.boolean ? 1 : 0;
472
473         return (0);
474 } /* }}} int config_set_boolean */
475
476 static int config_set_format (wh_callback_t *cb, /* {{{ */
477                 oconfig_item_t *ci)
478 {
479         char *string;
480
481         if ((ci->values_num != 1)
482                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
483         {
484                 WARNING ("write_http plugin: The `%s' config option "
485                                 "needs exactly one string argument.", ci->key);
486                 return (-1);
487         }
488
489         string = ci->values[0].value.string;
490         if (strcasecmp ("Command", string) == 0)
491                 cb->format = WH_FORMAT_COMMAND;
492         else if (strcasecmp ("JSON", string) == 0)
493                 cb->format = WH_FORMAT_JSON;
494         else
495         {
496                 ERROR ("write_http plugin: Invalid format string: %s",
497                                 string);
498                 return (-1);
499         }
500
501         return (0);
502 } /* }}} int config_set_string */
503
504 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
505 {
506         wh_callback_t *cb;
507         user_data_t user_data;
508         int i;
509
510         cb = malloc (sizeof (*cb));
511         if (cb == NULL)
512         {
513                 ERROR ("write_http plugin: malloc failed.");
514                 return (-1);
515         }
516         memset (cb, 0, sizeof (*cb));
517         cb->location = NULL;
518         cb->user = NULL;
519         cb->pass = NULL;
520         cb->credentials = NULL;
521         cb->verify_peer = 1;
522         cb->verify_host = 1;
523         cb->cacert = NULL;
524         cb->format = WH_FORMAT_COMMAND;
525         cb->curl = NULL;
526
527         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
528
529         config_set_string (&cb->location, ci);
530         if (cb->location == NULL)
531                 return (-1);
532
533         for (i = 0; i < ci->children_num; i++)
534         {
535                 oconfig_item_t *child = ci->children + i;
536
537                 if (strcasecmp ("User", child->key) == 0)
538                         config_set_string (&cb->user, child);
539                 else if (strcasecmp ("Password", child->key) == 0)
540                         config_set_string (&cb->pass, child);
541                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
542                         config_set_boolean (&cb->verify_peer, child);
543                 else if (strcasecmp ("VerifyHost", child->key) == 0)
544                         config_set_boolean (&cb->verify_host, child);
545                 else if (strcasecmp ("CACert", child->key) == 0)
546                         config_set_string (&cb->cacert, child);
547                 else if (strcasecmp ("Format", child->key) == 0)
548                         config_set_format (cb, child);
549                 else if (strcasecmp ("StoreRates", child->key) == 0)
550                         config_set_boolean (&cb->store_rates, child);
551                 else
552                 {
553                         ERROR ("write_http plugin: Invalid configuration "
554                                         "option: %s.", child->key);
555                 }
556         }
557
558         DEBUG ("write_http: Registering write callback with URL %s",
559                         cb->location);
560
561         memset (&user_data, 0, sizeof (user_data));
562         user_data.data = cb;
563         user_data.free_func = NULL;
564         plugin_register_flush ("write_http", wh_flush, &user_data);
565
566         user_data.free_func = wh_callback_free;
567         plugin_register_write ("write_http", wh_write, &user_data);
568
569         return (0);
570 } /* }}} int wh_config_url */
571
572 static int wh_config (oconfig_item_t *ci) /* {{{ */
573 {
574         int i;
575
576         for (i = 0; i < ci->children_num; i++)
577         {
578                 oconfig_item_t *child = ci->children + i;
579
580                 if (strcasecmp ("URL", child->key) == 0)
581                         wh_config_url (child);
582                 else
583                 {
584                         ERROR ("write_http plugin: Invalid configuration "
585                                         "option: %s.", child->key);
586                 }
587         }
588
589         return (0);
590 } /* }}} int wh_config */
591
592 void module_register (void) /* {{{ */
593 {
594         plugin_register_complex_config ("write_http", wh_config);
595 } /* }}} void module_register */
596
597 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */