Code

Merge branch 'collectd-4.10'
[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         time_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 = time (NULL);
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 != 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 */
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_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
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);
125         curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
126         curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
128         if (cb->user != NULL)
129         {
130                 size_t credentials_size;
132                 credentials_size = strlen (cb->user) + 2;
133                 if (cb->pass != NULL)
134                         credentials_size += strlen (cb->pass);
136                 cb->credentials = (char *) malloc (credentials_size);
137                 if (cb->credentials == NULL)
138                 {
139                         ERROR ("curl plugin: malloc failed.");
140                         return (-1);
141                 }
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         }
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);
155         wh_reset_buffer (cb);
157         return (0);
158 } /* }}} int wh_callback_init */
160 static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */
162         int status;
164         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
165                         "send_buffer_fill = %zu;",
166                         timeout, cb->send_buffer_fill);
168         if (timeout > 0)
169         {
170                 time_t now;
172                 now = time (NULL);
173                 if ((cb->send_buffer_init_time + timeout) > now)
174                         return (0);
175         }
177         if (cb->format == WH_FORMAT_COMMAND)
178         {
179                 if (cb->send_buffer_fill <= 0)
180                 {
181                         cb->send_buffer_init_time = time (NULL);
182                         return (0);
183                 }
185                 status = wh_send_buffer (cb);
186                 wh_reset_buffer (cb);
187         }
188         else if (cb->format == WH_FORMAT_JSON)
189         {
190                 if (cb->send_buffer_fill <= 2)
191                 {
192                         cb->send_buffer_init_time = time (NULL);
193                         return (0);
194                 }
196                 status = format_json_finalize (cb->send_buffer,
197                                 &cb->send_buffer_fill,
198                                 &cb->send_buffer_free);
199                 if (status != 0)
200                 {
201                         ERROR ("write_http: wh_flush_nolock: "
202                                         "format_json_finalize failed.");
203                         wh_reset_buffer (cb);
204                         return (status);
205                 }
207                 status = wh_send_buffer (cb);
208                 wh_reset_buffer (cb);
209         }
210         else
211         {
212                 ERROR ("write_http: wh_flush_nolock: "
213                                 "Unknown format: %i",
214                                 cb->format);
215                 return (-1);
216         }
218         return (status);
219 } /* }}} wh_flush_nolock */
221 static int wh_flush (int timeout, /* {{{ */
222                 const char *identifier __attribute__((unused)),
223                 user_data_t *user_data)
225         wh_callback_t *cb;
226         int status;
228         if (user_data == NULL)
229                 return (-EINVAL);
231         cb = user_data->data;
233         pthread_mutex_lock (&cb->send_lock);
235         if (cb->curl == NULL)
236         {
237                 status = wh_callback_init (cb);
238                 if (status != 0)
239                 {
240                         ERROR ("write_http plugin: wh_callback_init failed.");
241                         pthread_mutex_unlock (&cb->send_lock);
242                         return (-1);
243                 }
244         }
246         status = wh_flush_nolock (timeout, cb);
247         pthread_mutex_unlock (&cb->send_lock);
249         return (status);
250 } /* }}} int wh_flush */
252 static void wh_callback_free (void *data) /* {{{ */
254         wh_callback_t *cb;
256         if (data == NULL)
257                 return;
259         cb = data;
261         wh_flush_nolock (/* timeout = */ -1, cb);
263         curl_easy_cleanup (cb->curl);
264         sfree (cb->location);
265         sfree (cb->user);
266         sfree (cb->pass);
267         sfree (cb->credentials);
268         sfree (cb->cacert);
270         sfree (cb);
271 } /* }}} void wh_callback_free */
273 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
274                 wh_callback_t *cb)
276         char key[10*DATA_MAX_NAME_LEN];
277         char values[512];
278         char command[1024];
279         size_t command_len;
281         int status;
283         if (0 != strcmp (ds->type, vl->type)) {
284                 ERROR ("write_http plugin: DS type does not match "
285                                 "value list type");
286                 return -1;
287         }
289         /* Copy the identifier to `key' and escape it. */
290         status = FORMAT_VL (key, sizeof (key), vl);
291         if (status != 0) {
292                 ERROR ("write_http plugin: error with format_name");
293                 return (status);
294         }
295         escape_string (key, sizeof (key));
297         /* Convert the values to an ASCII representation and put that into
298          * `values'. */
299         status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
300         if (status != 0) {
301                 ERROR ("write_http plugin: error with "
302                                 "wh_value_list_to_string");
303                 return (status);
304         }
306         command_len = (size_t) ssnprintf (command, sizeof (command),
307                         "PUTVAL %s interval=%i %s\r\n",
308                         key, vl->interval, values);
309         if (command_len >= sizeof (command)) {
310                 ERROR ("write_http plugin: Command buffer too small: "
311                                 "Need %zu bytes.", command_len + 1);
312                 return (-1);
313         }
315         pthread_mutex_lock (&cb->send_lock);
317         if (cb->curl == NULL)
318         {
319                 status = wh_callback_init (cb);
320                 if (status != 0)
321                 {
322                         ERROR ("write_http plugin: wh_callback_init failed.");
323                         pthread_mutex_unlock (&cb->send_lock);
324                         return (-1);
325                 }
326         }
328         if (command_len >= cb->send_buffer_free)
329         {
330                 status = wh_flush_nolock (/* timeout = */ -1, cb);
331                 if (status != 0)
332                 {
333                         pthread_mutex_unlock (&cb->send_lock);
334                         return (status);
335                 }
336         }
337         assert (command_len < cb->send_buffer_free);
339         /* `command_len + 1' because `command_len' does not include the
340          * trailing null byte. Neither does `send_buffer_fill'. */
341         memcpy (cb->send_buffer + cb->send_buffer_fill,
342                         command, command_len + 1);
343         cb->send_buffer_fill += command_len;
344         cb->send_buffer_free -= command_len;
346         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
347                         cb->location,
348                         cb->send_buffer_fill, sizeof (cb->send_buffer),
349                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
350                         command);
352         /* Check if we have enough space for this command. */
353         pthread_mutex_unlock (&cb->send_lock);
355         return (0);
356 } /* }}} int wh_write_command */
358 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
359                 wh_callback_t *cb)
361         int status;
363         pthread_mutex_lock (&cb->send_lock);
365         if (cb->curl == NULL)
366         {
367                 status = wh_callback_init (cb);
368                 if (status != 0)
369                 {
370                         ERROR ("write_http plugin: wh_callback_init failed.");
371                         pthread_mutex_unlock (&cb->send_lock);
372                         return (-1);
373                 }
374         }
376         status = format_json_value_list (cb->send_buffer,
377                         &cb->send_buffer_fill,
378                         &cb->send_buffer_free,
379                         ds, vl, cb->store_rates);
380         if (status == (-ENOMEM))
381         {
382                 status = wh_flush_nolock (/* timeout = */ -1, cb);
383                 if (status != 0)
384                 {
385                         wh_reset_buffer (cb);
386                         pthread_mutex_unlock (&cb->send_lock);
387                         return (status);
388                 }
390                 status = format_json_value_list (cb->send_buffer,
391                                 &cb->send_buffer_fill,
392                                 &cb->send_buffer_free,
393                                 ds, vl, cb->store_rates);
394         }
395         if (status != 0)
396         {
397                 pthread_mutex_unlock (&cb->send_lock);
398                 return (status);
399         }
401         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
402                         cb->location,
403                         cb->send_buffer_fill, sizeof (cb->send_buffer),
404                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
406         /* Check if we have enough space for this command. */
407         pthread_mutex_unlock (&cb->send_lock);
409         return (0);
410 } /* }}} int wh_write_json */
412 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
413                 user_data_t *user_data)
415         wh_callback_t *cb;
416         int status;
418         if (user_data == NULL)
419                 return (-EINVAL);
421         cb = user_data->data;
423         if (cb->format == WH_FORMAT_JSON)
424                 status = wh_write_json (ds, vl, cb);
425         else
426                 status = wh_write_command (ds, vl, cb);
428         return (status);
429 } /* }}} int wh_write */
431 static int config_set_string (char **ret_string, /* {{{ */
432                 oconfig_item_t *ci)
434         char *string;
436         if ((ci->values_num != 1)
437                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
438         {
439                 WARNING ("write_http plugin: The `%s' config option "
440                                 "needs exactly one string argument.", ci->key);
441                 return (-1);
442         }
444         string = strdup (ci->values[0].value.string);
445         if (string == NULL)
446         {
447                 ERROR ("write_http plugin: strdup failed.");
448                 return (-1);
449         }
451         if (*ret_string != NULL)
452                 free (*ret_string);
453         *ret_string = string;
455         return (0);
456 } /* }}} int config_set_string */
458 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
460         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
461         {
462                 WARNING ("write_http plugin: The `%s' config option "
463                                 "needs exactly one boolean argument.", ci->key);
464                 return (-1);
465         }
467         *dest = ci->values[0].value.boolean ? 1 : 0;
469         return (0);
470 } /* }}} int config_set_boolean */
472 static int config_set_format (wh_callback_t *cb, /* {{{ */
473                 oconfig_item_t *ci)
475         char *string;
477         if ((ci->values_num != 1)
478                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
479         {
480                 WARNING ("write_http plugin: The `%s' config option "
481                                 "needs exactly one string argument.", ci->key);
482                 return (-1);
483         }
485         string = ci->values[0].value.string;
486         if (strcasecmp ("Command", string) == 0)
487                 cb->format = WH_FORMAT_COMMAND;
488         else if (strcasecmp ("JSON", string) == 0)
489                 cb->format = WH_FORMAT_JSON;
490         else
491         {
492                 ERROR ("write_http plugin: Invalid format string: %s",
493                                 string);
494                 return (-1);
495         }
497         return (0);
498 } /* }}} int config_set_string */
500 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
502         wh_callback_t *cb;
503         user_data_t user_data;
504         int i;
506         cb = malloc (sizeof (*cb));
507         if (cb == NULL)
508         {
509                 ERROR ("write_http plugin: malloc failed.");
510                 return (-1);
511         }
512         memset (cb, 0, sizeof (*cb));
513         cb->location = NULL;
514         cb->user = NULL;
515         cb->pass = NULL;
516         cb->credentials = NULL;
517         cb->verify_peer = 1;
518         cb->verify_host = 1;
519         cb->cacert = NULL;
520         cb->format = WH_FORMAT_COMMAND;
521         cb->curl = NULL;
523         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
525         config_set_string (&cb->location, ci);
526         if (cb->location == NULL)
527                 return (-1);
529         for (i = 0; i < ci->children_num; i++)
530         {
531                 oconfig_item_t *child = ci->children + i;
533                 if (strcasecmp ("User", child->key) == 0)
534                         config_set_string (&cb->user, child);
535                 else if (strcasecmp ("Password", child->key) == 0)
536                         config_set_string (&cb->pass, child);
537                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
538                         config_set_boolean (&cb->verify_peer, child);
539                 else if (strcasecmp ("VerifyHost", child->key) == 0)
540                         config_set_boolean (&cb->verify_host, child);
541                 else if (strcasecmp ("CACert", child->key) == 0)
542                         config_set_string (&cb->cacert, child);
543                 else if (strcasecmp ("Format", child->key) == 0)
544                         config_set_format (cb, child);
545                 else if (strcasecmp ("StoreRates", child->key) == 0)
546                         config_set_boolean (&cb->store_rates, child);
547                 else
548                 {
549                         ERROR ("write_http plugin: Invalid configuration "
550                                         "option: %s.", child->key);
551                 }
552         }
554         DEBUG ("write_http: Registering write callback with URL %s",
555                         cb->location);
557         memset (&user_data, 0, sizeof (user_data));
558         user_data.data = cb;
559         user_data.free_func = NULL;
560         plugin_register_flush ("write_http", wh_flush, &user_data);
562         user_data.free_func = wh_callback_free;
563         plugin_register_write ("write_http", wh_write, &user_data);
565         return (0);
566 } /* }}} int wh_config_url */
568 static int wh_config (oconfig_item_t *ci) /* {{{ */
570         int i;
572         for (i = 0; i < ci->children_num; i++)
573         {
574                 oconfig_item_t *child = ci->children + i;
576                 if (strcasecmp ("URL", child->key) == 0)
577                         wh_config_url (child);
578                 else
579                 {
580                         ERROR ("write_http plugin: Invalid configuration "
581                                         "option: %s.", child->key);
582                 }
583         }
585         return (0);
586 } /* }}} int wh_config */
588 void module_register (void) /* {{{ */
590         plugin_register_complex_config ("write_http", wh_config);
591 } /* }}} void module_register */
593 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */