Merge pull request #1546 from mfournier/processname_length_1284
[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         struct curl_slist *headers;
60         char curl_errbuf[CURL_ERROR_SIZE];
61
62         char   send_buffer[4096];
63         size_t send_buffer_free;
64         size_t send_buffer_fill;
65         cdtime_t send_buffer_init_time;
66
67         pthread_mutex_t send_lock;
68 };
69 typedef struct wh_callback_s wh_callback_t;
70
71 static void wh_reset_buffer (wh_callback_t *cb)  /* {{{ */
72 {
73         memset (cb->send_buffer, 0, sizeof (cb->send_buffer));
74         cb->send_buffer_free = sizeof (cb->send_buffer);
75         cb->send_buffer_fill = 0;
76         cb->send_buffer_init_time = cdtime ();
77
78         if (cb->format == WH_FORMAT_JSON)
79         {
80                 format_json_initialize (cb->send_buffer,
81                                 &cb->send_buffer_fill,
82                                 &cb->send_buffer_free);
83         }
84 } /* }}} wh_reset_buffer */
85
86 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
87 {
88         int status = 0;
89
90         curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
91         status = curl_easy_perform (cb->curl);
92         if (status != CURLE_OK)
93         {
94                 ERROR ("write_http plugin: curl_easy_perform failed with "
95                                 "status %i: %s",
96                                 status, cb->curl_errbuf);
97         }
98         return (status);
99 } /* }}} wh_send_buffer */
100
101 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
102 {
103         if (cb->curl != NULL)
104                 return (0);
105
106         cb->curl = curl_easy_init ();
107         if (cb->curl == NULL)
108         {
109                 ERROR ("curl plugin: curl_easy_init failed.");
110                 return (-1);
111         }
112
113         curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L);
114         curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
115
116         cb->headers = NULL;
117         cb->headers = curl_slist_append (cb->headers, "Accept:  */*");
118         if (cb->format == WH_FORMAT_JSON)
119                 cb->headers = curl_slist_append (cb->headers, "Content-Type: application/json");
120         else
121                 cb->headers = curl_slist_append (cb->headers, "Content-Type: text/plain");
122         cb->headers = curl_slist_append (cb->headers, "Expect:");
123         curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, cb->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, (long) cb->verify_peer);
150         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
151                         cb->verify_host ? 2L : 0L);
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
267         if (cb->headers != NULL)
268         {
269                 curl_slist_free_all (cb->headers);
270                 cb->headers = NULL;
271         }
272
273         sfree (cb->location);
274         sfree (cb->user);
275         sfree (cb->pass);
276         sfree (cb->credentials);
277         sfree (cb->cacert);
278
279         sfree (cb);
280 } /* }}} void wh_callback_free */
281
282 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
283                 wh_callback_t *cb)
284 {
285         char key[10*DATA_MAX_NAME_LEN];
286         char values[512];
287         char command[1024];
288         size_t command_len;
289
290         int status;
291
292         if (0 != strcmp (ds->type, vl->type)) {
293                 ERROR ("write_http plugin: DS type does not match "
294                                 "value list type");
295                 return -1;
296         }
297
298         /* Copy the identifier to `key' and escape it. */
299         status = FORMAT_VL (key, sizeof (key), vl);
300         if (status != 0) {
301                 ERROR ("write_http plugin: error with format_name");
302                 return (status);
303         }
304         escape_string (key, sizeof (key));
305
306         /* Convert the values to an ASCII representation and put that into
307          * `values'. */
308         status = format_values (values, sizeof (values), ds, vl, cb->store_rates);
309         if (status != 0) {
310                 ERROR ("write_http plugin: error with "
311                                 "wh_value_list_to_string");
312                 return (status);
313         }
314
315         command_len = (size_t) ssnprintf (command, sizeof (command),
316                         "PUTVAL %s interval=%.3f %s\r\n",
317                         key,
318                         CDTIME_T_TO_DOUBLE (vl->interval),
319                         values);
320         if (command_len >= sizeof (command)) {
321                 ERROR ("write_http plugin: Command buffer too small: "
322                                 "Need %zu bytes.", command_len + 1);
323                 return (-1);
324         }
325
326         pthread_mutex_lock (&cb->send_lock);
327
328         if (cb->curl == NULL)
329         {
330                 status = wh_callback_init (cb);
331                 if (status != 0)
332                 {
333                         ERROR ("write_http plugin: wh_callback_init failed.");
334                         pthread_mutex_unlock (&cb->send_lock);
335                         return (-1);
336                 }
337         }
338
339         if (command_len >= cb->send_buffer_free)
340         {
341                 status = wh_flush_nolock (/* timeout = */ 0, cb);
342                 if (status != 0)
343                 {
344                         pthread_mutex_unlock (&cb->send_lock);
345                         return (status);
346                 }
347         }
348         assert (command_len < cb->send_buffer_free);
349
350         /* `command_len + 1' because `command_len' does not include the
351          * trailing null byte. Neither does `send_buffer_fill'. */
352         memcpy (cb->send_buffer + cb->send_buffer_fill,
353                         command, command_len + 1);
354         cb->send_buffer_fill += command_len;
355         cb->send_buffer_free -= command_len;
356
357         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
358                         cb->location,
359                         cb->send_buffer_fill, sizeof (cb->send_buffer),
360                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
361                         command);
362
363         /* Check if we have enough space for this command. */
364         pthread_mutex_unlock (&cb->send_lock);
365
366         return (0);
367 } /* }}} int wh_write_command */
368
369 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
370                 wh_callback_t *cb)
371 {
372         int status;
373
374         pthread_mutex_lock (&cb->send_lock);
375
376         if (cb->curl == NULL)
377         {
378                 status = wh_callback_init (cb);
379                 if (status != 0)
380                 {
381                         ERROR ("write_http plugin: wh_callback_init failed.");
382                         pthread_mutex_unlock (&cb->send_lock);
383                         return (-1);
384                 }
385         }
386
387         status = format_json_value_list (cb->send_buffer,
388                         &cb->send_buffer_fill,
389                         &cb->send_buffer_free,
390                         ds, vl, cb->store_rates);
391         if (status == (-ENOMEM))
392         {
393                 status = wh_flush_nolock (/* timeout = */ 0, cb);
394                 if (status != 0)
395                 {
396                         wh_reset_buffer (cb);
397                         pthread_mutex_unlock (&cb->send_lock);
398                         return (status);
399                 }
400
401                 status = format_json_value_list (cb->send_buffer,
402                                 &cb->send_buffer_fill,
403                                 &cb->send_buffer_free,
404                                 ds, vl, cb->store_rates);
405         }
406         if (status != 0)
407         {
408                 pthread_mutex_unlock (&cb->send_lock);
409                 return (status);
410         }
411
412         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
413                         cb->location,
414                         cb->send_buffer_fill, sizeof (cb->send_buffer),
415                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
416
417         /* Check if we have enough space for this command. */
418         pthread_mutex_unlock (&cb->send_lock);
419
420         return (0);
421 } /* }}} int wh_write_json */
422
423 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
424                 user_data_t *user_data)
425 {
426         wh_callback_t *cb;
427         int status;
428
429         if (user_data == NULL)
430                 return (-EINVAL);
431
432         cb = user_data->data;
433
434         if (cb->format == WH_FORMAT_JSON)
435                 status = wh_write_json (ds, vl, cb);
436         else
437                 status = wh_write_command (ds, vl, cb);
438
439         return (status);
440 } /* }}} int wh_write */
441
442 static int config_set_string (char **ret_string, /* {{{ */
443                 oconfig_item_t *ci)
444 {
445         char *string;
446
447         if ((ci->values_num != 1)
448                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
449         {
450                 WARNING ("write_http plugin: The `%s' config option "
451                                 "needs exactly one string argument.", ci->key);
452                 return (-1);
453         }
454
455         string = strdup (ci->values[0].value.string);
456         if (string == NULL)
457         {
458                 ERROR ("write_http plugin: strdup failed.");
459                 return (-1);
460         }
461
462         if (*ret_string != NULL)
463                 free (*ret_string);
464         *ret_string = string;
465
466         return (0);
467 } /* }}} int config_set_string */
468
469 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
470 {
471         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
472         {
473                 WARNING ("write_http plugin: The `%s' config option "
474                                 "needs exactly one boolean argument.", ci->key);
475                 return (-1);
476         }
477
478         *dest = ci->values[0].value.boolean ? 1 : 0;
479
480         return (0);
481 } /* }}} int config_set_boolean */
482
483 static int config_set_format (wh_callback_t *cb, /* {{{ */
484                 oconfig_item_t *ci)
485 {
486         char *string;
487
488         if ((ci->values_num != 1)
489                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
490         {
491                 WARNING ("write_http plugin: The `%s' config option "
492                                 "needs exactly one string argument.", ci->key);
493                 return (-1);
494         }
495
496         string = ci->values[0].value.string;
497         if (strcasecmp ("Command", string) == 0)
498                 cb->format = WH_FORMAT_COMMAND;
499         else if (strcasecmp ("JSON", string) == 0)
500                 cb->format = WH_FORMAT_JSON;
501         else
502         {
503                 ERROR ("write_http plugin: Invalid format string: %s",
504                                 string);
505                 return (-1);
506         }
507
508         return (0);
509 } /* }}} int config_set_string */
510
511 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
512 {
513         wh_callback_t *cb;
514         user_data_t user_data;
515         char callback_name[DATA_MAX_NAME_LEN];
516         int i;
517
518         cb = malloc (sizeof (*cb));
519         if (cb == NULL)
520         {
521                 ERROR ("write_http plugin: malloc failed.");
522                 return (-1);
523         }
524         memset (cb, 0, sizeof (*cb));
525         cb->location = NULL;
526         cb->user = NULL;
527         cb->pass = NULL;
528         cb->credentials = NULL;
529         cb->verify_peer = 1;
530         cb->verify_host = 1;
531         cb->cacert = NULL;
532         cb->format = WH_FORMAT_COMMAND;
533         cb->curl = NULL;
534         cb->headers = NULL;
535
536         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
537
538         config_set_string (&cb->location, ci);
539         if (cb->location == NULL)
540                 return (-1);
541
542         for (i = 0; i < ci->children_num; i++)
543         {
544                 oconfig_item_t *child = ci->children + i;
545
546                 if (strcasecmp ("User", child->key) == 0)
547                         config_set_string (&cb->user, child);
548                 else if (strcasecmp ("Password", child->key) == 0)
549                         config_set_string (&cb->pass, child);
550                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
551                         config_set_boolean (&cb->verify_peer, child);
552                 else if (strcasecmp ("VerifyHost", child->key) == 0)
553                         config_set_boolean (&cb->verify_host, child);
554                 else if (strcasecmp ("CACert", child->key) == 0)
555                         config_set_string (&cb->cacert, child);
556                 else if (strcasecmp ("Format", child->key) == 0)
557                         config_set_format (cb, child);
558                 else if (strcasecmp ("StoreRates", child->key) == 0)
559                         config_set_boolean (&cb->store_rates, child);
560                 else
561                 {
562                         ERROR ("write_http plugin: Invalid configuration "
563                                         "option: %s.", child->key);
564                 }
565         }
566
567         ssnprintf (callback_name, sizeof (callback_name), "write_http/%s",
568                         cb->location);
569         DEBUG ("write_http: Registering write callback '%s' with URL '%s'",
570                         callback_name, cb->location);
571
572         memset (&user_data, 0, sizeof (user_data));
573         user_data.data = cb;
574         user_data.free_func = NULL;
575         plugin_register_flush (callback_name, wh_flush, &user_data);
576
577         user_data.free_func = wh_callback_free;
578         plugin_register_write (callback_name, wh_write, &user_data);
579
580         return (0);
581 } /* }}} int wh_config_url */
582
583 static int wh_config (oconfig_item_t *ci) /* {{{ */
584 {
585         int i;
586
587         for (i = 0; i < ci->children_num; i++)
588         {
589                 oconfig_item_t *child = ci->children + i;
590
591                 if (strcasecmp ("URL", child->key) == 0)
592                         wh_config_url (child);
593                 else
594                 {
595                         ERROR ("write_http plugin: Invalid configuration "
596                                         "option: %s.", child->key);
597                 }
598         }
599
600         return (0);
601 } /* }}} int wh_config */
602
603 static int wh_init (void) /* {{{ */
604 {
605         /* Call this while collectd is still single-threaded to avoid
606          * initialization issues in libgcrypt. */
607         curl_global_init (CURL_GLOBAL_SSL);
608         return (0);
609 } /* }}} int wh_init */
610
611 void module_register (void) /* {{{ */
612 {
613         plugin_register_complex_config ("write_http", wh_config);
614         plugin_register_init ("write_http", wh_init);
615 } /* }}} void module_register */
616
617 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */