Code

Merge remote-tracking branch 'github/pr/387'
[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  **/
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"
33 #if HAVE_PTHREAD_H
34 # include <pthread.h>
35 #endif
37 #include <curl/curl.h>
39 /*
40  * Private variables
41  */
42 struct wh_callback_s
43 {
44         char *location;
46         char *user;
47         char *pass;
48         char *credentials;
49         int   verify_peer;
50         int   verify_host;
51         char *cacert;
52         int   store_rates;
54 #define WH_FORMAT_COMMAND 0
55 #define WH_FORMAT_JSON    1
56         int format;
58         CURL *curl;
59         char curl_errbuf[CURL_ERROR_SIZE];
61         char   send_buffer[4096];
62         size_t send_buffer_free;
63         size_t send_buffer_fill;
64         cdtime_t send_buffer_init_time;
66         pthread_mutex_t send_lock;
67 };
68 typedef struct wh_callback_s wh_callback_t;
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 ();
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 */
85 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
86 {
87         int status = 0;
89         curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
90         status = curl_easy_perform (cb->curl);
91         if (status != CURLE_OK)
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 */
100 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
102         struct curl_slist *headers;
104         if (cb->curl != NULL)
105                 return (0);
107         cb->curl = curl_easy_init ();
108         if (cb->curl == NULL)
109         {
110                 ERROR ("curl plugin: curl_easy_init failed.");
111                 return (-1);
112         }
114         curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
115         curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
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);
126         curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
127         curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
129         if (cb->user != NULL)
130         {
131                 size_t credentials_size;
133                 credentials_size = strlen (cb->user) + 2;
134                 if (cb->pass != NULL)
135                         credentials_size += strlen (cb->pass);
137                 cb->credentials = (char *) malloc (credentials_size);
138                 if (cb->credentials == NULL)
139                 {
140                         ERROR ("curl plugin: malloc failed.");
141                         return (-1);
142                 }
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         }
150         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer);
151         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
152                         cb->verify_host ? 2L : 0L);
153         if (cb->cacert != NULL)
154                 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
156         wh_reset_buffer (cb);
158         return (0);
159 } /* }}} int wh_callback_init */
161 static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */
163         int status;
165         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; "
166                         "send_buffer_fill = %zu;",
167                         CDTIME_T_TO_DOUBLE (timeout),
168                         cb->send_buffer_fill);
170         /* timeout == 0  => flush unconditionally */
171         if (timeout > 0)
172         {
173                 cdtime_t now;
175                 now = cdtime ();
176                 if ((cb->send_buffer_init_time + timeout) > now)
177                         return (0);
178         }
180         if (cb->format == WH_FORMAT_COMMAND)
181         {
182                 if (cb->send_buffer_fill <= 0)
183                 {
184                         cb->send_buffer_init_time = cdtime ();
185                         return (0);
186                 }
188                 status = wh_send_buffer (cb);
189                 wh_reset_buffer (cb);
190         }
191         else if (cb->format == WH_FORMAT_JSON)
192         {
193                 if (cb->send_buffer_fill <= 2)
194                 {
195                         cb->send_buffer_init_time = cdtime ();
196                         return (0);
197                 }
199                 status = format_json_finalize (cb->send_buffer,
200                                 &cb->send_buffer_fill,
201                                 &cb->send_buffer_free);
202                 if (status != 0)
203                 {
204                         ERROR ("write_http: wh_flush_nolock: "
205                                         "format_json_finalize failed.");
206                         wh_reset_buffer (cb);
207                         return (status);
208                 }
210                 status = wh_send_buffer (cb);
211                 wh_reset_buffer (cb);
212         }
213         else
214         {
215                 ERROR ("write_http: wh_flush_nolock: "
216                                 "Unknown format: %i",
217                                 cb->format);
218                 return (-1);
219         }
221         return (status);
222 } /* }}} wh_flush_nolock */
224 static int wh_flush (cdtime_t timeout, /* {{{ */
225                 const char *identifier __attribute__((unused)),
226                 user_data_t *user_data)
228         wh_callback_t *cb;
229         int status;
231         if (user_data == NULL)
232                 return (-EINVAL);
234         cb = user_data->data;
236         pthread_mutex_lock (&cb->send_lock);
238         if (cb->curl == NULL)
239         {
240                 status = wh_callback_init (cb);
241                 if (status != 0)
242                 {
243                         ERROR ("write_http plugin: wh_callback_init failed.");
244                         pthread_mutex_unlock (&cb->send_lock);
245                         return (-1);
246                 }
247         }
249         status = wh_flush_nolock (timeout, cb);
250         pthread_mutex_unlock (&cb->send_lock);
252         return (status);
253 } /* }}} int wh_flush */
255 static void wh_callback_free (void *data) /* {{{ */
257         wh_callback_t *cb;
259         if (data == NULL)
260                 return;
262         cb = data;
264         wh_flush_nolock (/* timeout = */ 0, cb);
266         curl_easy_cleanup (cb->curl);
267         sfree (cb->location);
268         sfree (cb->user);
269         sfree (cb->pass);
270         sfree (cb->credentials);
271         sfree (cb->cacert);
273         sfree (cb);
274 } /* }}} void wh_callback_free */
276 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
277                 wh_callback_t *cb)
279         char key[10*DATA_MAX_NAME_LEN];
280         char values[512];
281         char command[1024];
282         size_t command_len;
284         int status;
286         if (0 != strcmp (ds->type, vl->type)) {
287                 ERROR ("write_http plugin: DS type does not match "
288                                 "value list type");
289                 return -1;
290         }
292         /* Copy the identifier to `key' and escape it. */
293         status = FORMAT_VL (key, sizeof (key), vl);
294         if (status != 0) {
295                 ERROR ("write_http plugin: error with format_name");
296                 return (status);
297         }
298         escape_string (key, sizeof (key));
300         /* Convert the values to an ASCII representation and put that into
301          * `values'. */
302         status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
303         if (status != 0) {
304                 ERROR ("write_http plugin: error with "
305                                 "wh_value_list_to_string");
306                 return (status);
307         }
309         command_len = (size_t) ssnprintf (command, sizeof (command),
310                         "PUTVAL %s interval=%.3f %s\r\n",
311                         key,
312                         CDTIME_T_TO_DOUBLE (vl->interval),
313                         values);
314         if (command_len >= sizeof (command)) {
315                 ERROR ("write_http plugin: Command buffer too small: "
316                                 "Need %zu bytes.", command_len + 1);
317                 return (-1);
318         }
320         pthread_mutex_lock (&cb->send_lock);
322         if (cb->curl == NULL)
323         {
324                 status = wh_callback_init (cb);
325                 if (status != 0)
326                 {
327                         ERROR ("write_http plugin: wh_callback_init failed.");
328                         pthread_mutex_unlock (&cb->send_lock);
329                         return (-1);
330                 }
331         }
333         if (command_len >= cb->send_buffer_free)
334         {
335                 status = wh_flush_nolock (/* timeout = */ 0, cb);
336                 if (status != 0)
337                 {
338                         pthread_mutex_unlock (&cb->send_lock);
339                         return (status);
340                 }
341         }
342         assert (command_len < cb->send_buffer_free);
344         /* `command_len + 1' because `command_len' does not include the
345          * trailing null byte. Neither does `send_buffer_fill'. */
346         memcpy (cb->send_buffer + cb->send_buffer_fill,
347                         command, command_len + 1);
348         cb->send_buffer_fill += command_len;
349         cb->send_buffer_free -= command_len;
351         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
352                         cb->location,
353                         cb->send_buffer_fill, sizeof (cb->send_buffer),
354                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
355                         command);
357         /* Check if we have enough space for this command. */
358         pthread_mutex_unlock (&cb->send_lock);
360         return (0);
361 } /* }}} int wh_write_command */
363 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
364                 wh_callback_t *cb)
366         int status;
368         pthread_mutex_lock (&cb->send_lock);
370         if (cb->curl == NULL)
371         {
372                 status = wh_callback_init (cb);
373                 if (status != 0)
374                 {
375                         ERROR ("write_http plugin: wh_callback_init failed.");
376                         pthread_mutex_unlock (&cb->send_lock);
377                         return (-1);
378                 }
379         }
381         status = format_json_value_list (cb->send_buffer,
382                         &cb->send_buffer_fill,
383                         &cb->send_buffer_free,
384                         ds, vl, cb->store_rates);
385         if (status == (-ENOMEM))
386         {
387                 status = wh_flush_nolock (/* timeout = */ 0, cb);
388                 if (status != 0)
389                 {
390                         wh_reset_buffer (cb);
391                         pthread_mutex_unlock (&cb->send_lock);
392                         return (status);
393                 }
395                 status = format_json_value_list (cb->send_buffer,
396                                 &cb->send_buffer_fill,
397                                 &cb->send_buffer_free,
398                                 ds, vl, cb->store_rates);
399         }
400         if (status != 0)
401         {
402                 pthread_mutex_unlock (&cb->send_lock);
403                 return (status);
404         }
406         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
407                         cb->location,
408                         cb->send_buffer_fill, sizeof (cb->send_buffer),
409                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
411         /* Check if we have enough space for this command. */
412         pthread_mutex_unlock (&cb->send_lock);
414         return (0);
415 } /* }}} int wh_write_json */
417 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
418                 user_data_t *user_data)
420         wh_callback_t *cb;
421         int status;
423         if (user_data == NULL)
424                 return (-EINVAL);
426         cb = user_data->data;
428         if (cb->format == WH_FORMAT_JSON)
429                 status = wh_write_json (ds, vl, cb);
430         else
431                 status = wh_write_command (ds, vl, cb);
433         return (status);
434 } /* }}} int wh_write */
436 static int config_set_string (char **ret_string, /* {{{ */
437                 oconfig_item_t *ci)
439         char *string;
441         if ((ci->values_num != 1)
442                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
443         {
444                 WARNING ("write_http plugin: The `%s' config option "
445                                 "needs exactly one string argument.", ci->key);
446                 return (-1);
447         }
449         string = strdup (ci->values[0].value.string);
450         if (string == NULL)
451         {
452                 ERROR ("write_http plugin: strdup failed.");
453                 return (-1);
454         }
456         if (*ret_string != NULL)
457                 free (*ret_string);
458         *ret_string = string;
460         return (0);
461 } /* }}} int config_set_string */
463 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
465         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
466         {
467                 WARNING ("write_http plugin: The `%s' config option "
468                                 "needs exactly one boolean argument.", ci->key);
469                 return (-1);
470         }
472         *dest = ci->values[0].value.boolean ? 1 : 0;
474         return (0);
475 } /* }}} int config_set_boolean */
477 static int config_set_format (wh_callback_t *cb, /* {{{ */
478                 oconfig_item_t *ci)
480         char *string;
482         if ((ci->values_num != 1)
483                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
484         {
485                 WARNING ("write_http plugin: The `%s' config option "
486                                 "needs exactly one string argument.", ci->key);
487                 return (-1);
488         }
490         string = ci->values[0].value.string;
491         if (strcasecmp ("Command", string) == 0)
492                 cb->format = WH_FORMAT_COMMAND;
493         else if (strcasecmp ("JSON", string) == 0)
494                 cb->format = WH_FORMAT_JSON;
495         else
496         {
497                 ERROR ("write_http plugin: Invalid format string: %s",
498                                 string);
499                 return (-1);
500         }
502         return (0);
503 } /* }}} int config_set_string */
505 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
507         wh_callback_t *cb;
508         user_data_t user_data;
509         int i;
511         cb = malloc (sizeof (*cb));
512         if (cb == NULL)
513         {
514                 ERROR ("write_http plugin: malloc failed.");
515                 return (-1);
516         }
517         memset (cb, 0, sizeof (*cb));
518         cb->location = NULL;
519         cb->user = NULL;
520         cb->pass = NULL;
521         cb->credentials = NULL;
522         cb->verify_peer = 1;
523         cb->verify_host = 1;
524         cb->cacert = NULL;
525         cb->format = WH_FORMAT_COMMAND;
526         cb->curl = NULL;
528         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
530         config_set_string (&cb->location, ci);
531         if (cb->location == NULL)
532                 return (-1);
534         for (i = 0; i < ci->children_num; i++)
535         {
536                 oconfig_item_t *child = ci->children + i;
538                 if (strcasecmp ("User", child->key) == 0)
539                         config_set_string (&cb->user, child);
540                 else if (strcasecmp ("Password", child->key) == 0)
541                         config_set_string (&cb->pass, child);
542                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
543                         config_set_boolean (&cb->verify_peer, child);
544                 else if (strcasecmp ("VerifyHost", child->key) == 0)
545                         config_set_boolean (&cb->verify_host, child);
546                 else if (strcasecmp ("CACert", child->key) == 0)
547                         config_set_string (&cb->cacert, child);
548                 else if (strcasecmp ("Format", child->key) == 0)
549                         config_set_format (cb, child);
550                 else if (strcasecmp ("StoreRates", child->key) == 0)
551                         config_set_boolean (&cb->store_rates, child);
552                 else
553                 {
554                         ERROR ("write_http plugin: Invalid configuration "
555                                         "option: %s.", child->key);
556                 }
557         }
559         DEBUG ("write_http: Registering write callback with URL %s",
560                         cb->location);
562         memset (&user_data, 0, sizeof (user_data));
563         user_data.data = cb;
564         user_data.free_func = NULL;
565         plugin_register_flush ("write_http", wh_flush, &user_data);
567         user_data.free_func = wh_callback_free;
568         plugin_register_write ("write_http", wh_write, &user_data);
570         return (0);
571 } /* }}} int wh_config_url */
573 static int wh_config (oconfig_item_t *ci) /* {{{ */
575         int i;
577         for (i = 0; i < ci->children_num; i++)
578         {
579                 oconfig_item_t *child = ci->children + i;
581                 if (strcasecmp ("URL", child->key) == 0)
582                         wh_config_url (child);
583                 else
584                 {
585                         ERROR ("write_http plugin: Invalid configuration "
586                                         "option: %s.", child->key);
587                 }
588         }
590         return (0);
591 } /* }}} int wh_config */
593 void module_register (void) /* {{{ */
595         plugin_register_complex_config ("write_http", wh_config);
596 } /* }}} void module_register */
598 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */