write_http plugin: Implement the usual SSL options.
[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
32 #if HAVE_PTHREAD_H
33 # include <pthread.h>
34 #endif
35
36 #include <curl/curl.h>
37
38 /*
39  * Private variables
40  */
41 struct wh_callback_s
42 {
43         char *location;
44
45         char *user;
46         char *pass;
47         char *credentials;
48         int   verify_peer;
49         int   verify_host;
50         char *cacert;
51
52         CURL *curl;
53         char curl_errbuf[CURL_ERROR_SIZE];
54
55         char   send_buffer[4096];
56         size_t send_buffer_free;
57         size_t send_buffer_fill;
58         time_t send_buffer_init_time;
59
60         pthread_mutex_t send_lock;
61 };
62 typedef struct wh_callback_s wh_callback_t;
63
64 static void wh_reset_buffer (wh_callback_t *cb)  /* {{{ */
65 {
66         memset (cb->send_buffer, 0, sizeof (cb->send_buffer));
67         cb->send_buffer_free = sizeof (cb->send_buffer);
68         cb->send_buffer_fill = 0;
69         cb->send_buffer_init_time = time (NULL);
70 } /* }}} wh_reset_buffer */
71
72 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
73 {
74         int status = 0;
75
76         curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
77         status = curl_easy_perform (cb->curl);
78         if (status != 0)
79         {
80                 ERROR ("write_http plugin: curl_easy_perform failed with "
81                                 "staus %i: %s",
82                                 status, cb->curl_errbuf);
83         }
84         return (status);
85 } /* }}} wh_send_buffer */
86
87 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
88 {
89         struct curl_slist *headers;
90
91         if (cb->curl != NULL)
92                 return (0);
93
94         cb->curl = curl_easy_init ();
95         if (cb->curl == NULL)
96         {
97                 ERROR ("curl plugin: curl_easy_init failed.");
98                 return (-1);
99         }
100
101         curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
102
103         headers = NULL;
104         headers = curl_slist_append (headers, "Accept:  */*");
105         headers = curl_slist_append (headers, "Content-Type: text/plain");
106         curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
107
108         curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
109         curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
110
111         if (cb->user != NULL)
112         {
113                 size_t credentials_size;
114
115                 credentials_size = strlen (cb->user) + 2;
116                 if (cb->pass != NULL)
117                         credentials_size += strlen (cb->pass);
118
119                 cb->credentials = (char *) malloc (credentials_size);
120                 if (cb->credentials == NULL)
121                 {
122                         ERROR ("curl plugin: malloc failed.");
123                         return (-1);
124                 }
125
126                 ssnprintf (cb->credentials, credentials_size, "%s:%s",
127                                 cb->user, (cb->pass == NULL) ? "" : cb->pass);
128                 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
129                 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
130         }
131
132         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, cb->verify_peer);
133         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
134                         cb->verify_host ? 2 : 0);
135         if (cb->cacert != NULL)
136                 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
137
138         wh_reset_buffer (cb);
139
140         return (0);
141 } /* }}} int wh_callback_init */
142
143 static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */
144 {
145         int status;
146
147         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
148                         "send_buffer_fill = %zu;",
149                         timeout, cb->send_buffer_fill);
150
151         if (timeout > 0)
152         {
153                 time_t now;
154
155                 now = time (NULL);
156                 if ((cb->send_buffer_init_time + timeout) > now)
157                         return (0);
158         }
159
160         if (cb->send_buffer_fill <= 0)
161         {
162                 cb->send_buffer_init_time = time (NULL);
163                 return (0);
164         }
165
166         status = wh_send_buffer (cb);
167         wh_reset_buffer (cb);
168
169         return (status);
170 } /* }}} wh_flush_nolock */
171
172 static int wh_flush (int timeout, /* {{{ */
173                 const char *identifier __attribute__((unused)),
174                 user_data_t *user_data)
175 {
176         wh_callback_t *cb;
177         int status;
178
179         if (user_data == NULL)
180                 return (-EINVAL);
181
182         cb = user_data->data;
183
184         pthread_mutex_lock (&cb->send_lock);
185
186         if (cb->curl == NULL)
187         {
188                 status = wh_callback_init (cb);
189                 if (status != 0)
190                 {
191                         ERROR ("write_http plugin: wh_callback_init failed.");
192                         pthread_mutex_unlock (&cb->send_lock);
193                         return (-1);
194                 }
195         }
196
197         status = wh_flush_nolock (timeout, cb);
198         pthread_mutex_unlock (&cb->send_lock);
199
200         return (status);
201 } /* }}} int wh_flush */
202
203 static void wh_callback_free (void *data) /* {{{ */
204 {
205         wh_callback_t *cb;
206
207         if (data == NULL)
208                 return;
209
210         cb = data;
211
212         wh_flush_nolock (/* timeout = */ -1, cb);
213
214         curl_easy_cleanup (cb->curl);
215         sfree (cb->location);
216         sfree (cb->user);
217         sfree (cb->pass);
218         sfree (cb->credentials);
219         sfree (cb->cacert);
220
221         sfree (cb);
222 } /* }}} void wh_callback_free */
223
224 static int wh_value_list_to_string (char *buffer, /* {{{ */
225                 size_t buffer_size,
226                 const data_set_t *ds, const value_list_t *vl)
227 {
228         size_t offset = 0;
229         int status;
230         int i;
231
232         assert (0 == strcmp (ds->type, vl->type));
233
234         memset (buffer, 0, buffer_size);
235
236 #define BUFFER_ADD(...) do { \
237         status = ssnprintf (buffer + offset, buffer_size - offset, \
238                         __VA_ARGS__); \
239         if (status < 1) \
240                 return (-1); \
241         else if (((size_t) status) >= (buffer_size - offset)) \
242                 return (-1); \
243         else \
244                 offset += ((size_t) status); \
245 } while (0)
246
247         BUFFER_ADD ("%lu", (unsigned long) vl->time);
248
249         for (i = 0; i < ds->ds_num; i++)
250 {
251         if (ds->ds[i].type == DS_TYPE_GAUGE)
252                 BUFFER_ADD (":%f", vl->values[i].gauge);
253         else if (ds->ds[i].type == DS_TYPE_COUNTER)
254                 BUFFER_ADD (":%llu", vl->values[i].counter);
255         else if (ds->ds[i].type == DS_TYPE_DERIVE)
256                 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
257         else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
258                 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
259         else
260         {
261                 ERROR ("write_http plugin: Unknown data source type: %i",
262                                 ds->ds[i].type);
263                 return (-1);
264         }
265 } /* for ds->ds_num */
266
267 #undef BUFFER_ADD
268
269 return (0);
270 } /* }}} int wh_value_list_to_string */
271
272 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
273                 wh_callback_t *cb)
274 {
275         char key[10*DATA_MAX_NAME_LEN];
276         char values[512];
277         char command[1024];
278         size_t command_len;
279
280         int status;
281
282         if (0 != strcmp (ds->type, vl->type)) {
283                 ERROR ("write_http plugin: DS type does not match "
284                                 "value list type");
285                 return -1;
286         }
287
288         /* Copy the identifier to `key' and escape it. */
289         status = FORMAT_VL (key, sizeof (key), vl);
290         if (status != 0) {
291                 ERROR ("write_http plugin: error with format_name");
292                 return (status);
293         }
294         escape_string (key, sizeof (key));
295
296         /* Convert the values to an ASCII representation and put that into
297          * `values'. */
298         status = wh_value_list_to_string (values, sizeof (values), ds, vl);
299         if (status != 0) {
300                 ERROR ("write_http plugin: error with "
301                                 "wh_value_list_to_string");
302                 return (status);
303         }
304
305         command_len = (size_t) ssnprintf (command, sizeof (command),
306                         "PUTVAL %s interval=%i %s\n",
307                         key, vl->interval, values);
308         if (command_len >= sizeof (command)) {
309                 ERROR ("write_http plugin: Command buffer too small: "
310                                 "Need %zu bytes.", command_len + 1);
311                 return (-1);
312         }
313
314         pthread_mutex_lock (&cb->send_lock);
315
316         if (cb->curl == NULL)
317         {
318                 status = wh_callback_init (cb);
319                 if (status != 0)
320                 {
321                         ERROR ("write_http plugin: wh_callback_init failed.");
322                         pthread_mutex_unlock (&cb->send_lock);
323                         return (-1);
324                 }
325         }
326
327         if (command_len >= cb->send_buffer_free)
328         {
329                 status = wh_flush_nolock (/* timeout = */ -1, cb);
330                 if (status != 0)
331                 {
332                         pthread_mutex_unlock (&cb->send_lock);
333                         return (status);
334                 }
335         }
336         assert (command_len < cb->send_buffer_free);
337
338         /* `command_len + 1' because `command_len' does not include the
339          * trailing null byte. Neither does `send_buffer_fill'. */
340         memcpy (cb->send_buffer + cb->send_buffer_fill,
341                         command, command_len + 1);
342         cb->send_buffer_fill += command_len;
343         cb->send_buffer_free -= command_len;
344
345         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
346                         cb->location,
347                         cb->send_buffer_fill, sizeof (cb->send_buffer),
348                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
349                         command);
350
351         /* Check if we have enough space for this command. */
352         pthread_mutex_unlock (&cb->send_lock);
353
354         return (0);
355 } /* }}} int wh_write_command */
356
357 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
358                 user_data_t *user_data)
359 {
360         wh_callback_t *cb;
361         int status;
362
363         if (user_data == NULL)
364                 return (-EINVAL);
365
366         cb = user_data->data;
367
368         status = wh_write_command (ds, vl, cb);
369         return (status);
370 } /* }}} int wh_write */
371
372 static int config_set_string (char **ret_string, /* {{{ */
373                 oconfig_item_t *ci)
374 {
375         char *string;
376
377         if ((ci->values_num != 1)
378                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
379         {
380                 WARNING ("write_http plugin: The `%s' config option "
381                                 "needs exactly one string argument.", ci->key);
382                 return (-1);
383         }
384
385         string = strdup (ci->values[0].value.string);
386         if (string == NULL)
387         {
388                 ERROR ("write_http plugin: strdup failed.");
389                 return (-1);
390         }
391
392         if (*ret_string != NULL)
393                 free (*ret_string);
394         *ret_string = string;
395
396         return (0);
397 } /* }}} int config_set_string */
398
399 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
400 {
401         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
402         {
403                 WARNING ("write_http plugin: The `%s' config option "
404                                 "needs exactly one boolean argument.", ci->key);
405                 return (-1);
406         }
407
408         *dest = ci->values[0].value.boolean ? 1 : 0;
409
410         return (0);
411 } /* }}} int config_set_boolean */
412
413 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
414 {
415         wh_callback_t *cb;
416         user_data_t user_data;
417         int i;
418
419         cb = malloc (sizeof (*cb));
420         if (cb == NULL)
421         {
422                 ERROR ("write_http plugin: malloc failed.");
423                 return (-1);
424         }
425         memset (cb, 0, sizeof (*cb));
426         cb->location = NULL;
427         cb->user = NULL;
428         cb->pass = NULL;
429         cb->credentials = NULL;
430         cb->verify_peer = 1;
431         cb->verify_host = 1;
432         cb->cacert = NULL;
433         cb->curl = NULL;
434
435         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
436
437         config_set_string (&cb->location, ci);
438         if (cb->location == NULL)
439                 return (-1);
440
441         for (i = 0; i < ci->children_num; i++)
442         {
443                 oconfig_item_t *child = ci->children + i;
444
445                 if (strcasecmp ("User", child->key) == 0)
446                         config_set_string (&cb->user, child);
447                 else if (strcasecmp ("Password", child->key) == 0)
448                         config_set_string (&cb->pass, child);
449                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
450                         config_set_boolean (&cb->verify_peer, child);
451                 else if (strcasecmp ("VerifyHost", child->key) == 0)
452                         config_set_boolean (&cb->verify_host, child);
453                 else if (strcasecmp ("CACert", child->key) == 0)
454                         config_set_string (&cb->cacert, child);
455                 else
456                 {
457                         ERROR ("write_http plugin: Invalid configuration "
458                                         "option: %s.", child->key);
459                 }
460         }
461
462         DEBUG ("write_http: Registering write callback with URL %s",
463                         cb->location);
464
465         memset (&user_data, 0, sizeof (user_data));
466         user_data.data = cb;
467         user_data.free_func = NULL;
468         plugin_register_flush ("write_http", wh_flush, &user_data);
469
470         user_data.free_func = wh_callback_free;
471         plugin_register_write ("write_http", wh_write, &user_data);
472
473         return (0);
474 } /* }}} int wh_config_url */
475
476 static int wh_config (oconfig_item_t *ci) /* {{{ */
477 {
478         int i;
479
480         for (i = 0; i < ci->children_num; i++)
481         {
482                 oconfig_item_t *child = ci->children + i;
483
484                 if (strcasecmp ("URL", child->key) == 0)
485                         wh_config_url (child);
486                 else
487                 {
488                         ERROR ("write_http plugin: Invalid configuration "
489                                         "option: %s.", child->key);
490                 }
491         }
492
493         return (0);
494 } /* }}} int wh_config */
495
496 void module_register (void) /* {{{ */
497 {
498         plugin_register_complex_config ("write_http", wh_config);
499 } /* }}} void module_register */
500
501 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */