write_http plugin: Add a StoreRates option.
[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         time_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 = time (NULL);
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 (int timeout, wh_callback_t *cb) /* {{{ */
161 {
162         int status;
163
164         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
165                         "send_buffer_fill = %zu;",
166                         timeout, cb->send_buffer_fill);
167
168         if (timeout > 0)
169         {
170                 time_t now;
171
172                 now = time (NULL);
173                 if ((cb->send_buffer_init_time + timeout) > now)
174                         return (0);
175         }
176
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                 }
184
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                 }
195
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                 }
206
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         }
217
218         return (status);
219 } /* }}} wh_flush_nolock */
220
221 static int wh_flush (int timeout, /* {{{ */
222                 const char *identifier __attribute__((unused)),
223                 user_data_t *user_data)
224 {
225         wh_callback_t *cb;
226         int status;
227
228         if (user_data == NULL)
229                 return (-EINVAL);
230
231         cb = user_data->data;
232
233         pthread_mutex_lock (&cb->send_lock);
234
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         }
245
246         status = wh_flush_nolock (timeout, cb);
247         pthread_mutex_unlock (&cb->send_lock);
248
249         return (status);
250 } /* }}} int wh_flush */
251
252 static void wh_callback_free (void *data) /* {{{ */
253 {
254         wh_callback_t *cb;
255
256         if (data == NULL)
257                 return;
258
259         cb = data;
260
261         wh_flush_nolock (/* timeout = */ -1, cb);
262
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);
269
270         sfree (cb);
271 } /* }}} void wh_callback_free */
272
273 static int wh_value_list_to_string (char *buffer, /* {{{ */
274                 size_t buffer_size,
275                 const data_set_t *ds, const value_list_t *vl,
276                 wh_callback_t *cb)
277 {
278         size_t offset = 0;
279         int status;
280         int i;
281         gauge_t *rates = NULL;
282
283         assert (0 == strcmp (ds->type, vl->type));
284
285         memset (buffer, 0, buffer_size);
286
287 #define BUFFER_ADD(...) do { \
288         status = ssnprintf (buffer + offset, buffer_size - offset, \
289                         __VA_ARGS__); \
290         if (status < 1) \
291                 return (-1); \
292         else if (((size_t) status) >= (buffer_size - offset)) \
293                 return (-1); \
294         else \
295                 offset += ((size_t) status); \
296 } while (0)
297
298         BUFFER_ADD ("%lu", (unsigned long) vl->time);
299
300         for (i = 0; i < ds->ds_num; i++)
301 {
302         if (ds->ds[i].type == DS_TYPE_GAUGE)
303                 BUFFER_ADD (":%f", vl->values[i].gauge);
304         else if (ds->ds[i].type == DS_TYPE_COUNTER)
305         {
306                 if (cb->store_rates != 0) 
307                 {
308                         if (rates == NULL)
309                                 rates = uc_get_rate (ds, vl);
310                         if (rates == NULL)
311                         {
312                                 WARNING ("write_http plugin: "
313                                                 "uc_get_rate failed.");
314                                 return (-1);
315                         }
316                         BUFFER_ADD (":%lf", rates[i]);
317                 }
318                 else
319                         BUFFER_ADD (":%llu", vl->values[i].counter);
320         }
321         else if (ds->ds[i].type == DS_TYPE_DERIVE)
322                 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
323         else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
324                 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
325         else
326         {
327                 ERROR ("write_http plugin: Unknown data source type: %i",
328                                 ds->ds[i].type);
329                 return (-1);
330         }
331 } /* for ds->ds_num */
332
333 #undef BUFFER_ADD
334
335 return (0);
336 } /* }}} int wh_value_list_to_string */
337
338 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
339                 wh_callback_t *cb)
340 {
341         char key[10*DATA_MAX_NAME_LEN];
342         char values[512];
343         char command[1024];
344         size_t command_len;
345
346         int status;
347
348         if (0 != strcmp (ds->type, vl->type)) {
349                 ERROR ("write_http plugin: DS type does not match "
350                                 "value list type");
351                 return -1;
352         }
353
354         /* Copy the identifier to `key' and escape it. */
355         status = FORMAT_VL (key, sizeof (key), vl);
356         if (status != 0) {
357                 ERROR ("write_http plugin: error with format_name");
358                 return (status);
359         }
360         escape_string (key, sizeof (key));
361
362         /* Convert the values to an ASCII representation and put that into
363          * `values'. */
364         status = wh_value_list_to_string (values, sizeof (values), ds, vl, cb);
365         if (status != 0) {
366                 ERROR ("write_http plugin: error with "
367                                 "wh_value_list_to_string");
368                 return (status);
369         }
370
371         command_len = (size_t) ssnprintf (command, sizeof (command),
372                         "PUTVAL %s interval=%i %s\r\n",
373                         key, vl->interval, values);
374         if (command_len >= sizeof (command)) {
375                 ERROR ("write_http plugin: Command buffer too small: "
376                                 "Need %zu bytes.", command_len + 1);
377                 return (-1);
378         }
379
380         pthread_mutex_lock (&cb->send_lock);
381
382         if (cb->curl == NULL)
383         {
384                 status = wh_callback_init (cb);
385                 if (status != 0)
386                 {
387                         ERROR ("write_http plugin: wh_callback_init failed.");
388                         pthread_mutex_unlock (&cb->send_lock);
389                         return (-1);
390                 }
391         }
392
393         if (command_len >= cb->send_buffer_free)
394         {
395                 status = wh_flush_nolock (/* timeout = */ -1, cb);
396                 if (status != 0)
397                 {
398                         pthread_mutex_unlock (&cb->send_lock);
399                         return (status);
400                 }
401         }
402         assert (command_len < cb->send_buffer_free);
403
404         /* `command_len + 1' because `command_len' does not include the
405          * trailing null byte. Neither does `send_buffer_fill'. */
406         memcpy (cb->send_buffer + cb->send_buffer_fill,
407                         command, command_len + 1);
408         cb->send_buffer_fill += command_len;
409         cb->send_buffer_free -= command_len;
410
411         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
412                         cb->location,
413                         cb->send_buffer_fill, sizeof (cb->send_buffer),
414                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
415                         command);
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_command */
422
423 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
424                 wh_callback_t *cb)
425 {
426         int status;
427
428         pthread_mutex_lock (&cb->send_lock);
429
430         if (cb->curl == NULL)
431         {
432                 status = wh_callback_init (cb);
433                 if (status != 0)
434                 {
435                         ERROR ("write_http plugin: wh_callback_init failed.");
436                         pthread_mutex_unlock (&cb->send_lock);
437                         return (-1);
438                 }
439         }
440
441         status = format_json_value_list (cb->send_buffer,
442                         &cb->send_buffer_fill,
443                         &cb->send_buffer_free,
444                         ds, vl);
445         if (status == (-ENOMEM))
446         {
447                 status = wh_flush_nolock (/* timeout = */ -1, cb);
448                 if (status != 0)
449                 {
450                         wh_reset_buffer (cb);
451                         pthread_mutex_unlock (&cb->send_lock);
452                         return (status);
453                 }
454
455                 status = format_json_value_list (cb->send_buffer,
456                                 &cb->send_buffer_fill,
457                                 &cb->send_buffer_free,
458                                 ds, vl);
459         }
460         if (status != 0)
461         {
462                 pthread_mutex_unlock (&cb->send_lock);
463                 return (status);
464         }
465
466         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
467                         cb->location,
468                         cb->send_buffer_fill, sizeof (cb->send_buffer),
469                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
470
471         /* Check if we have enough space for this command. */
472         pthread_mutex_unlock (&cb->send_lock);
473
474         return (0);
475 } /* }}} int wh_write_json */
476
477 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
478                 user_data_t *user_data)
479 {
480         wh_callback_t *cb;
481         int status;
482
483         if (user_data == NULL)
484                 return (-EINVAL);
485
486         cb = user_data->data;
487
488         if (cb->format == WH_FORMAT_JSON)
489                 status = wh_write_json (ds, vl, cb);
490         else
491                 status = wh_write_command (ds, vl, cb);
492
493         return (status);
494 } /* }}} int wh_write */
495
496 static int config_set_string (char **ret_string, /* {{{ */
497                 oconfig_item_t *ci)
498 {
499         char *string;
500
501         if ((ci->values_num != 1)
502                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
503         {
504                 WARNING ("write_http plugin: The `%s' config option "
505                                 "needs exactly one string argument.", ci->key);
506                 return (-1);
507         }
508
509         string = strdup (ci->values[0].value.string);
510         if (string == NULL)
511         {
512                 ERROR ("write_http plugin: strdup failed.");
513                 return (-1);
514         }
515
516         if (*ret_string != NULL)
517                 free (*ret_string);
518         *ret_string = string;
519
520         return (0);
521 } /* }}} int config_set_string */
522
523 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
524 {
525         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
526         {
527                 WARNING ("write_http plugin: The `%s' config option "
528                                 "needs exactly one boolean argument.", ci->key);
529                 return (-1);
530         }
531
532         *dest = ci->values[0].value.boolean ? 1 : 0;
533
534         return (0);
535 } /* }}} int config_set_boolean */
536
537 static int config_set_format (wh_callback_t *cb, /* {{{ */
538                 oconfig_item_t *ci)
539 {
540         char *string;
541
542         if ((ci->values_num != 1)
543                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
544         {
545                 WARNING ("write_http plugin: The `%s' config option "
546                                 "needs exactly one string argument.", ci->key);
547                 return (-1);
548         }
549
550         string = ci->values[0].value.string;
551         if (strcasecmp ("Command", string) == 0)
552                 cb->format = WH_FORMAT_COMMAND;
553         else if (strcasecmp ("JSON", string) == 0)
554                 cb->format = WH_FORMAT_JSON;
555         else
556         {
557                 ERROR ("write_http plugin: Invalid format string: %s",
558                                 string);
559                 return (-1);
560         }
561
562         return (0);
563 } /* }}} int config_set_string */
564
565 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
566 {
567         wh_callback_t *cb;
568         user_data_t user_data;
569         int i;
570
571         cb = malloc (sizeof (*cb));
572         if (cb == NULL)
573         {
574                 ERROR ("write_http plugin: malloc failed.");
575                 return (-1);
576         }
577         memset (cb, 0, sizeof (*cb));
578         cb->location = NULL;
579         cb->user = NULL;
580         cb->pass = NULL;
581         cb->credentials = NULL;
582         cb->verify_peer = 1;
583         cb->verify_host = 1;
584         cb->cacert = NULL;
585         cb->format = WH_FORMAT_COMMAND;
586         cb->curl = NULL;
587
588         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
589
590         config_set_string (&cb->location, ci);
591         if (cb->location == NULL)
592                 return (-1);
593
594         for (i = 0; i < ci->children_num; i++)
595         {
596                 oconfig_item_t *child = ci->children + i;
597
598                 if (strcasecmp ("User", child->key) == 0)
599                         config_set_string (&cb->user, child);
600                 else if (strcasecmp ("Password", child->key) == 0)
601                         config_set_string (&cb->pass, child);
602                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
603                         config_set_boolean (&cb->verify_peer, child);
604                 else if (strcasecmp ("VerifyHost", child->key) == 0)
605                         config_set_boolean (&cb->verify_host, child);
606                 else if (strcasecmp ("CACert", child->key) == 0)
607                         config_set_string (&cb->cacert, child);
608                 else if (strcasecmp ("Format", child->key) == 0)
609                         config_set_format (cb, child);
610                 else if (strcasecmp ("StoreRates", child->key) == 0)
611                         config_set_boolean (&cb->store_rates, child);
612                 else
613                 {
614                         ERROR ("write_http plugin: Invalid configuration "
615                                         "option: %s.", child->key);
616                 }
617         }
618
619         DEBUG ("write_http: Registering write callback with URL %s",
620                         cb->location);
621
622         memset (&user_data, 0, sizeof (user_data));
623         user_data.data = cb;
624         user_data.free_func = NULL;
625         plugin_register_flush ("write_http", wh_flush, &user_data);
626
627         user_data.free_func = wh_callback_free;
628         plugin_register_write ("write_http", wh_write, &user_data);
629
630         return (0);
631 } /* }}} int wh_config_url */
632
633 static int wh_config (oconfig_item_t *ci) /* {{{ */
634 {
635         int i;
636
637         for (i = 0; i < ci->children_num; i++)
638         {
639                 oconfig_item_t *child = ci->children + i;
640
641                 if (strcasecmp ("URL", child->key) == 0)
642                         wh_config_url (child);
643                 else
644                 {
645                         ERROR ("write_http plugin: Invalid configuration "
646                                         "option: %s.", child->key);
647                 }
648         }
649
650         return (0);
651 } /* }}} int wh_config */
652
653 void module_register (void) /* {{{ */
654 {
655         plugin_register_complex_config ("write_http", wh_config);
656 } /* }}} void module_register */
657
658 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */