netapp plugin: Fix a typo.
[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
53 #define WH_FORMAT_COMMAND 0
54 #define WH_FORMAT_JSON    1
55         int format;
56
57         CURL *curl;
58         char curl_errbuf[CURL_ERROR_SIZE];
59
60         char   send_buffer[4096];
61         size_t send_buffer_free;
62         size_t send_buffer_fill;
63         time_t send_buffer_init_time;
64
65         pthread_mutex_t send_lock;
66 };
67 typedef struct wh_callback_s wh_callback_t;
68
69 static void wh_reset_buffer (wh_callback_t *cb)  /* {{{ */
70 {
71         memset (cb->send_buffer, 0, sizeof (cb->send_buffer));
72         cb->send_buffer_free = sizeof (cb->send_buffer);
73         cb->send_buffer_fill = 0;
74         cb->send_buffer_init_time = time (NULL);
75
76         if (cb->format == WH_FORMAT_JSON)
77         {
78                 format_json_initialize (cb->send_buffer,
79                                 &cb->send_buffer_fill,
80                                 &cb->send_buffer_free);
81         }
82 } /* }}} wh_reset_buffer */
83
84 static int wh_send_buffer (wh_callback_t *cb) /* {{{ */
85 {
86         int status = 0;
87
88         curl_easy_setopt (cb->curl, CURLOPT_POSTFIELDS, cb->send_buffer);
89         status = curl_easy_perform (cb->curl);
90         if (status != 0)
91         {
92                 ERROR ("write_http plugin: curl_easy_perform failed with "
93                                 "status %i: %s",
94                                 status, cb->curl_errbuf);
95         }
96         return (status);
97 } /* }}} wh_send_buffer */
98
99 static int wh_callback_init (wh_callback_t *cb) /* {{{ */
100 {
101         struct curl_slist *headers;
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_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
114
115         headers = NULL;
116         headers = curl_slist_append (headers, "Accept:  */*");
117         if (cb->format == WH_FORMAT_JSON)
118                 headers = curl_slist_append (headers, "Content-Type: application/json");
119         else
120                 headers = curl_slist_append (headers, "Content-Type: text/plain");
121         headers = curl_slist_append (headers, "Expect:");
122         curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers);
123
124         curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf);
125         curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location);
126
127         if (cb->user != NULL)
128         {
129                 size_t credentials_size;
130
131                 credentials_size = strlen (cb->user) + 2;
132                 if (cb->pass != NULL)
133                         credentials_size += strlen (cb->pass);
134
135                 cb->credentials = (char *) malloc (credentials_size);
136                 if (cb->credentials == NULL)
137                 {
138                         ERROR ("curl plugin: malloc failed.");
139                         return (-1);
140                 }
141
142                 ssnprintf (cb->credentials, credentials_size, "%s:%s",
143                                 cb->user, (cb->pass == NULL) ? "" : cb->pass);
144                 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
145                 curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
146         }
147
148         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, cb->verify_peer);
149         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST,
150                         cb->verify_host ? 2 : 0);
151         if (cb->cacert != NULL)
152                 curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert);
153
154         wh_reset_buffer (cb);
155
156         return (0);
157 } /* }}} int wh_callback_init */
158
159 static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */
160 {
161         int status;
162
163         DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; "
164                         "send_buffer_fill = %zu;",
165                         timeout, cb->send_buffer_fill);
166
167         if (timeout > 0)
168         {
169                 time_t now;
170
171                 now = time (NULL);
172                 if ((cb->send_buffer_init_time + timeout) > now)
173                         return (0);
174         }
175
176         if (cb->format == WH_FORMAT_COMMAND)
177         {
178                 if (cb->send_buffer_fill <= 0)
179                 {
180                         cb->send_buffer_init_time = time (NULL);
181                         return (0);
182                 }
183
184                 status = wh_send_buffer (cb);
185                 wh_reset_buffer (cb);
186         }
187         else if (cb->format == WH_FORMAT_JSON)
188         {
189                 if (cb->send_buffer_fill <= 2)
190                 {
191                         cb->send_buffer_init_time = time (NULL);
192                         return (0);
193                 }
194
195                 status = format_json_finalize (cb->send_buffer,
196                                 &cb->send_buffer_fill,
197                                 &cb->send_buffer_free);
198                 if (status != 0)
199                 {
200                         ERROR ("write_http: wh_flush_nolock: "
201                                         "format_json_finalize failed.");
202                         wh_reset_buffer (cb);
203                         return (status);
204                 }
205
206                 status = wh_send_buffer (cb);
207                 wh_reset_buffer (cb);
208         }
209         else
210         {
211                 ERROR ("write_http: wh_flush_nolock: "
212                                 "Unknown format: %i",
213                                 cb->format);
214                 return (-1);
215         }
216
217         return (status);
218 } /* }}} wh_flush_nolock */
219
220 static int wh_flush (int timeout, /* {{{ */
221                 const char *identifier __attribute__((unused)),
222                 user_data_t *user_data)
223 {
224         wh_callback_t *cb;
225         int status;
226
227         if (user_data == NULL)
228                 return (-EINVAL);
229
230         cb = user_data->data;
231
232         pthread_mutex_lock (&cb->send_lock);
233
234         if (cb->curl == NULL)
235         {
236                 status = wh_callback_init (cb);
237                 if (status != 0)
238                 {
239                         ERROR ("write_http plugin: wh_callback_init failed.");
240                         pthread_mutex_unlock (&cb->send_lock);
241                         return (-1);
242                 }
243         }
244
245         status = wh_flush_nolock (timeout, cb);
246         pthread_mutex_unlock (&cb->send_lock);
247
248         return (status);
249 } /* }}} int wh_flush */
250
251 static void wh_callback_free (void *data) /* {{{ */
252 {
253         wh_callback_t *cb;
254
255         if (data == NULL)
256                 return;
257
258         cb = data;
259
260         wh_flush_nolock (/* timeout = */ -1, cb);
261
262         curl_easy_cleanup (cb->curl);
263         sfree (cb->location);
264         sfree (cb->user);
265         sfree (cb->pass);
266         sfree (cb->credentials);
267         sfree (cb->cacert);
268
269         sfree (cb);
270 } /* }}} void wh_callback_free */
271
272 static int wh_value_list_to_string (char *buffer, /* {{{ */
273                 size_t buffer_size,
274                 const data_set_t *ds, const value_list_t *vl)
275 {
276         size_t offset = 0;
277         int status;
278         int i;
279
280         assert (0 == strcmp (ds->type, vl->type));
281
282         memset (buffer, 0, buffer_size);
283
284 #define BUFFER_ADD(...) do { \
285         status = ssnprintf (buffer + offset, buffer_size - offset, \
286                         __VA_ARGS__); \
287         if (status < 1) \
288                 return (-1); \
289         else if (((size_t) status) >= (buffer_size - offset)) \
290                 return (-1); \
291         else \
292                 offset += ((size_t) status); \
293 } while (0)
294
295         BUFFER_ADD ("%lu", (unsigned long) vl->time);
296
297         for (i = 0; i < ds->ds_num; i++)
298 {
299         if (ds->ds[i].type == DS_TYPE_GAUGE)
300                 BUFFER_ADD (":%f", vl->values[i].gauge);
301         else if (ds->ds[i].type == DS_TYPE_COUNTER)
302                 BUFFER_ADD (":%llu", vl->values[i].counter);
303         else if (ds->ds[i].type == DS_TYPE_DERIVE)
304                 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
305         else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
306                 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
307         else
308         {
309                 ERROR ("write_http plugin: Unknown data source type: %i",
310                                 ds->ds[i].type);
311                 return (-1);
312         }
313 } /* for ds->ds_num */
314
315 #undef BUFFER_ADD
316
317 return (0);
318 } /* }}} int wh_value_list_to_string */
319
320 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
321                 wh_callback_t *cb)
322 {
323         char key[10*DATA_MAX_NAME_LEN];
324         char values[512];
325         char command[1024];
326         size_t command_len;
327
328         int status;
329
330         if (0 != strcmp (ds->type, vl->type)) {
331                 ERROR ("write_http plugin: DS type does not match "
332                                 "value list type");
333                 return -1;
334         }
335
336         /* Copy the identifier to `key' and escape it. */
337         status = FORMAT_VL (key, sizeof (key), vl);
338         if (status != 0) {
339                 ERROR ("write_http plugin: error with format_name");
340                 return (status);
341         }
342         escape_string (key, sizeof (key));
343
344         /* Convert the values to an ASCII representation and put that into
345          * `values'. */
346         status = wh_value_list_to_string (values, sizeof (values), ds, vl);
347         if (status != 0) {
348                 ERROR ("write_http plugin: error with "
349                                 "wh_value_list_to_string");
350                 return (status);
351         }
352
353         command_len = (size_t) ssnprintf (command, sizeof (command),
354                         "PUTVAL %s interval=%i %s\r\n",
355                         key, vl->interval, values);
356         if (command_len >= sizeof (command)) {
357                 ERROR ("write_http plugin: Command buffer too small: "
358                                 "Need %zu bytes.", command_len + 1);
359                 return (-1);
360         }
361
362         pthread_mutex_lock (&cb->send_lock);
363
364         if (cb->curl == NULL)
365         {
366                 status = wh_callback_init (cb);
367                 if (status != 0)
368                 {
369                         ERROR ("write_http plugin: wh_callback_init failed.");
370                         pthread_mutex_unlock (&cb->send_lock);
371                         return (-1);
372                 }
373         }
374
375         if (command_len >= cb->send_buffer_free)
376         {
377                 status = wh_flush_nolock (/* timeout = */ -1, cb);
378                 if (status != 0)
379                 {
380                         pthread_mutex_unlock (&cb->send_lock);
381                         return (status);
382                 }
383         }
384         assert (command_len < cb->send_buffer_free);
385
386         /* `command_len + 1' because `command_len' does not include the
387          * trailing null byte. Neither does `send_buffer_fill'. */
388         memcpy (cb->send_buffer + cb->send_buffer_fill,
389                         command, command_len + 1);
390         cb->send_buffer_fill += command_len;
391         cb->send_buffer_free -= command_len;
392
393         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%) \"%s\"",
394                         cb->location,
395                         cb->send_buffer_fill, sizeof (cb->send_buffer),
396                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)),
397                         command);
398
399         /* Check if we have enough space for this command. */
400         pthread_mutex_unlock (&cb->send_lock);
401
402         return (0);
403 } /* }}} int wh_write_command */
404
405 static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ */
406                 wh_callback_t *cb)
407 {
408         int status;
409
410         pthread_mutex_lock (&cb->send_lock);
411
412         if (cb->curl == NULL)
413         {
414                 status = wh_callback_init (cb);
415                 if (status != 0)
416                 {
417                         ERROR ("write_http plugin: wh_callback_init failed.");
418                         pthread_mutex_unlock (&cb->send_lock);
419                         return (-1);
420                 }
421         }
422
423         status = format_json_value_list (cb->send_buffer,
424                         &cb->send_buffer_fill,
425                         &cb->send_buffer_free,
426                         ds, vl);
427         if (status == (-ENOMEM))
428         {
429                 status = wh_flush_nolock (/* timeout = */ -1, cb);
430                 if (status != 0)
431                 {
432                         wh_reset_buffer (cb);
433                         pthread_mutex_unlock (&cb->send_lock);
434                         return (status);
435                 }
436
437                 status = format_json_value_list (cb->send_buffer,
438                                 &cb->send_buffer_fill,
439                                 &cb->send_buffer_free,
440                                 ds, vl);
441         }
442         if (status != 0)
443         {
444                 pthread_mutex_unlock (&cb->send_lock);
445                 return (status);
446         }
447
448         DEBUG ("write_http plugin: <%s> buffer %zu/%zu (%g%%)",
449                         cb->location,
450                         cb->send_buffer_fill, sizeof (cb->send_buffer),
451                         100.0 * ((double) cb->send_buffer_fill) / ((double) sizeof (cb->send_buffer)));
452
453         /* Check if we have enough space for this command. */
454         pthread_mutex_unlock (&cb->send_lock);
455
456         return (0);
457 } /* }}} int wh_write_json */
458
459 static int wh_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
460                 user_data_t *user_data)
461 {
462         wh_callback_t *cb;
463         int status;
464
465         if (user_data == NULL)
466                 return (-EINVAL);
467
468         cb = user_data->data;
469
470         if (cb->format == WH_FORMAT_JSON)
471                 status = wh_write_json (ds, vl, cb);
472         else
473                 status = wh_write_command (ds, vl, cb);
474
475         return (status);
476 } /* }}} int wh_write */
477
478 static int config_set_string (char **ret_string, /* {{{ */
479                 oconfig_item_t *ci)
480 {
481         char *string;
482
483         if ((ci->values_num != 1)
484                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
485         {
486                 WARNING ("write_http plugin: The `%s' config option "
487                                 "needs exactly one string argument.", ci->key);
488                 return (-1);
489         }
490
491         string = strdup (ci->values[0].value.string);
492         if (string == NULL)
493         {
494                 ERROR ("write_http plugin: strdup failed.");
495                 return (-1);
496         }
497
498         if (*ret_string != NULL)
499                 free (*ret_string);
500         *ret_string = string;
501
502         return (0);
503 } /* }}} int config_set_string */
504
505 static int config_set_boolean (int *dest, oconfig_item_t *ci) /* {{{ */
506 {
507         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
508         {
509                 WARNING ("write_http plugin: The `%s' config option "
510                                 "needs exactly one boolean argument.", ci->key);
511                 return (-1);
512         }
513
514         *dest = ci->values[0].value.boolean ? 1 : 0;
515
516         return (0);
517 } /* }}} int config_set_boolean */
518
519 static int config_set_format (wh_callback_t *cb, /* {{{ */
520                 oconfig_item_t *ci)
521 {
522         char *string;
523
524         if ((ci->values_num != 1)
525                         || (ci->values[0].type != OCONFIG_TYPE_STRING))
526         {
527                 WARNING ("write_http plugin: The `%s' config option "
528                                 "needs exactly one string argument.", ci->key);
529                 return (-1);
530         }
531
532         string = ci->values[0].value.string;
533         if (strcasecmp ("Command", string) == 0)
534                 cb->format = WH_FORMAT_COMMAND;
535         else if (strcasecmp ("JSON", string) == 0)
536                 cb->format = WH_FORMAT_JSON;
537         else
538         {
539                 ERROR ("write_http plugin: Invalid format string: %s",
540                                 string);
541                 return (-1);
542         }
543
544         return (0);
545 } /* }}} int config_set_string */
546
547 static int wh_config_url (oconfig_item_t *ci) /* {{{ */
548 {
549         wh_callback_t *cb;
550         user_data_t user_data;
551         int i;
552
553         cb = malloc (sizeof (*cb));
554         if (cb == NULL)
555         {
556                 ERROR ("write_http plugin: malloc failed.");
557                 return (-1);
558         }
559         memset (cb, 0, sizeof (*cb));
560         cb->location = NULL;
561         cb->user = NULL;
562         cb->pass = NULL;
563         cb->credentials = NULL;
564         cb->verify_peer = 1;
565         cb->verify_host = 1;
566         cb->cacert = NULL;
567         cb->format = WH_FORMAT_COMMAND;
568         cb->curl = NULL;
569
570         pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
571
572         config_set_string (&cb->location, ci);
573         if (cb->location == NULL)
574                 return (-1);
575
576         for (i = 0; i < ci->children_num; i++)
577         {
578                 oconfig_item_t *child = ci->children + i;
579
580                 if (strcasecmp ("User", child->key) == 0)
581                         config_set_string (&cb->user, child);
582                 else if (strcasecmp ("Password", child->key) == 0)
583                         config_set_string (&cb->pass, child);
584                 else if (strcasecmp ("VerifyPeer", child->key) == 0)
585                         config_set_boolean (&cb->verify_peer, child);
586                 else if (strcasecmp ("VerifyHost", child->key) == 0)
587                         config_set_boolean (&cb->verify_host, child);
588                 else if (strcasecmp ("CACert", child->key) == 0)
589                         config_set_string (&cb->cacert, child);
590                 else if (strcasecmp ("Format", child->key) == 0)
591                         config_set_format (cb, child);
592                 else
593                 {
594                         ERROR ("write_http plugin: Invalid configuration "
595                                         "option: %s.", child->key);
596                 }
597         }
598
599         DEBUG ("write_http: Registering write callback with URL %s",
600                         cb->location);
601
602         memset (&user_data, 0, sizeof (user_data));
603         user_data.data = cb;
604         user_data.free_func = NULL;
605         plugin_register_flush ("write_http", wh_flush, &user_data);
606
607         user_data.free_func = wh_callback_free;
608         plugin_register_write ("write_http", wh_write, &user_data);
609
610         return (0);
611 } /* }}} int wh_config_url */
612
613 static int wh_config (oconfig_item_t *ci) /* {{{ */
614 {
615         int i;
616
617         for (i = 0; i < ci->children_num; i++)
618         {
619                 oconfig_item_t *child = ci->children + i;
620
621                 if (strcasecmp ("URL", child->key) == 0)
622                         wh_config_url (child);
623                 else
624                 {
625                         ERROR ("write_http plugin: Invalid configuration "
626                                         "option: %s.", child->key);
627                 }
628         }
629
630         return (0);
631 } /* }}} int wh_config */
632
633 void module_register (void) /* {{{ */
634 {
635         plugin_register_complex_config ("write_http", wh_config);
636 } /* }}} void module_register */
637
638 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */