e68f2a198594a45b19973448f1d4b6e48b74a643
[collectd.git] / src / http.c
1 /**
2  * collectd - src/http.c
3  * Copyright (C) 2009       Paul Sadauskas
4  * Copyright (C) 2007-2009  Florian octo Forster
5  * Copyright (C) 2009       Doug MacEachern
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 static const char *config_keys[] =
42 {
43         "URL", "User", "Password"
44 };
45 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
46
47 static char *location   = NULL;
48
49 char *user;
50 char *pass;
51 char *credentials;
52
53 CURL *curl;
54 char curl_errbuf[CURL_ERROR_SIZE];
55
56 #define SEND_BUFFER_SIZE 4096
57 static char   send_buffer[SEND_BUFFER_SIZE];
58 static size_t send_buffer_free;
59 static size_t send_buffer_fill;
60 static time_t send_buffer_init_time;
61
62 static pthread_mutex_t  send_lock = PTHREAD_MUTEX_INITIALIZER;
63
64 static void http_init_buffer (void)  /* {{{ */
65 {
66         memset (send_buffer, 0, sizeof (send_buffer));
67         send_buffer_free = sizeof (send_buffer);
68         send_buffer_fill = 0;
69         send_buffer_init_time = time (NULL);
70 } /* }}} http_init_buffer */
71
72 static int http_init(void) /* {{{ */
73 {
74
75         curl = curl_easy_init ();
76
77         if (curl == NULL)
78         {
79                 ERROR ("curl plugin: curl_easy_init failed.");
80                 return (-1);
81         }
82
83         struct curl_slist *headers=NULL;
84
85         curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
86
87         headers = curl_slist_append(headers, "Accept:  */*");
88         headers = curl_slist_append(headers, "Content-Type: text/plain");
89         curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
90
91         curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
92         curl_easy_setopt (curl, CURLOPT_URL, location);
93
94         if (user != NULL)
95         {
96                 size_t credentials_size;
97
98                 credentials_size = strlen (user) + 2;
99                 if (pass != NULL)
100                         credentials_size += strlen (pass);
101
102                 credentials = (char *) malloc (credentials_size);
103                 if (credentials == NULL)
104                 {
105                         ERROR ("curl plugin: malloc failed.");
106                         return (-1);
107                 }
108
109                 ssnprintf (credentials, credentials_size, "%s:%s",
110                                 user, (pass == NULL) ? "" : pass);
111                 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
112                 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
113         }
114
115         http_init_buffer ();
116
117         return (0);
118 } /* }}} */
119
120 static int http_value_list_to_string (char *buffer, /* {{{ */
121                 size_t buffer_size,
122                 const data_set_t *ds, const value_list_t *vl)
123 {
124         size_t offset = 0;
125         int status;
126         int i;
127
128         assert (0 == strcmp (ds->type, vl->type));
129
130         memset (buffer, 0, buffer_size);
131
132 #define BUFFER_ADD(...) do { \
133         status = ssnprintf (buffer + offset, buffer_size - offset, \
134                         __VA_ARGS__); \
135         if (status < 1) \
136                 return (-1); \
137         else if (((size_t) status) >= (buffer_size - offset)) \
138                 return (-1); \
139         else \
140                 offset += ((size_t) status); \
141 } while (0)
142
143         BUFFER_ADD ("%lu", (unsigned long) vl->time);
144
145         for (i = 0; i < ds->ds_num; i++)
146 {
147         if (ds->ds[i].type == DS_TYPE_GAUGE)
148                 BUFFER_ADD (":%f", vl->values[i].gauge);
149         else if (ds->ds[i].type == DS_TYPE_COUNTER)
150                 BUFFER_ADD (":%llu", vl->values[i].counter);
151         else if (ds->ds[i].type == DS_TYPE_DERIVE)
152                 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
153         else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
154                 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
155         else
156         {
157                 ERROR ("http plugin: Unknown data source type: %i",
158                                 ds->ds[i].type);
159                 return (-1);
160         }
161 } /* for ds->ds_num */
162
163 #undef BUFFER_ADD
164
165 return (0);
166 } /* }}} int http_value_list_to_string */
167
168 static int http_config (const char *key, const char *value) /* {{{ */
169 {
170         if (strcasecmp ("URL", key) == 0)
171         {
172                 if (location != NULL)
173                         free (location);
174                 location = strdup (value);
175                 if (location != NULL)
176                 {
177                         int len = strlen (location);
178                         while ((len > 0) && (location[len - 1] == '/'))
179                         {
180                                 len--;
181                                 location[len] = '\0';
182                         }
183                         if (len <= 0)
184                         {
185                                 free (location);
186                                 location = NULL;
187                         }
188                 }
189         }
190         else if (strcasecmp ("User", key) == 0)
191         {
192                 if (user != NULL)
193                         free (user);
194                 user = strdup (value);
195                 if (user != NULL)
196                 {
197                         int len = strlen (user);
198                         while ((len > 0) && (user[len - 1] == '/'))
199                         {
200                                 len--;
201                                 user[len] = '\0';
202                         }
203                         if (len <= 0)
204                         {
205                                 free (user);
206                                 user = NULL;
207                         }
208                 }
209         }
210         else if (strcasecmp ("Password", key) == 0)
211         {
212                 if (pass != NULL)
213                         free (pass);
214                 pass = strdup (value);
215                 if (pass != NULL)
216                 {
217                         int len = strlen (pass);
218                         while ((len > 0) && (pass[len - 1] == '/'))
219                         {
220                                 len--;
221                                 pass[len] = '\0';
222                         }
223                         if (len <= 0)
224                         {
225                                 free (pass);
226                                 pass = NULL;
227                         }
228                 }
229         }
230         else
231         {
232                 return (-1);
233         }
234         return (0);
235 } /* }}} int http_config */
236
237 static int http_send_buffer (char *buffer) /* {{{ */
238 {
239         int status = 0;
240
241         curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
242         status = curl_easy_perform (curl);
243         if (status != 0)
244         {
245                 ERROR ("http plugin: curl_easy_perform failed with staus %i: %s",
246                                 status, curl_errbuf);
247         }
248         return (status);
249 } /* }}} http_send_buffer */
250
251 static int http_flush_nolock (int timeout) /* {{{ */
252 {
253         int status;
254
255         DEBUG ("http plugin: http_flush_nolock: timeout = %i; "
256                         "send_buffer =\n  %s", timeout, send_buffer);
257
258         if (timeout > 0)
259         {
260                 time_t now;
261
262                 now = time (NULL);
263                 if ((send_buffer_init_time + timeout) > now)
264                         return (0);
265         }
266
267         if (send_buffer_fill <= 0)
268         {
269                 send_buffer_init_time = time (NULL);
270                 return (0);
271         }
272
273         status = http_send_buffer (send_buffer);
274         http_init_buffer ();
275
276         return (status);
277 } /* }}} http_flush_nolock */
278
279 static int http_flush (int timeout, /* {{{ */
280                 const char *identifier __attribute__((unused)),
281                 user_data_t *user_data __attribute__((unused)))
282 {
283         int status;
284
285         pthread_mutex_lock (&send_lock);
286         status = http_flush_nolock (timeout);
287         pthread_mutex_unlock (&send_lock);
288
289         return (status);
290 } /* }}} int http_flush */
291
292 static int http_write_command (const data_set_t *ds, const value_list_t *vl) /* {{{ */
293 {
294         char key[10*DATA_MAX_NAME_LEN];
295         char values[512];
296         char command[1024];
297         size_t command_len;
298
299         int status;
300
301         if (0 != strcmp (ds->type, vl->type)) {
302                 ERROR ("http plugin: DS type does not match value list type");
303                 return -1;
304         }
305
306         /* Copy the identifier to `key' and escape it. */
307         status = FORMAT_VL (key, sizeof (key), vl);
308         if (status != 0) {
309                 ERROR ("http plugin: error with format_name");
310                 return (status);
311         }
312         escape_string (key, sizeof (key));
313
314         /* Convert the values to an ASCII representation and put that into
315          * `values'. */
316         status = http_value_list_to_string (values, sizeof (values), ds, vl);
317         if (status != 0) {
318                 ERROR ("http plugin: error with http_value_list_to_string");
319                 return (status);
320         }
321
322         command_len = (size_t) ssnprintf (command, sizeof (command),
323                         "PUTVAL %s interval=%i %s\n",
324                         key, vl->interval, values);
325         if (command_len >= sizeof (command)) {
326                 ERROR ("http plugin: Command buffer too small: "
327                                 "Need %zu bytes.", command_len + 1);
328                 return (-1);
329         }
330
331         pthread_mutex_lock (&send_lock);
332
333         /* Check if we have enough space for this command. */
334         if (command_len >= send_buffer_free)
335         {
336                 status = http_flush_nolock (/* timeout = */ -1);
337                 if (status != 0)
338                 {
339                         pthread_mutex_unlock (&send_lock);
340                         return status;
341                 }
342         }
343         assert (command_len < send_buffer_free);
344
345         /* `command_len + 1' because `command_len' does not include the
346          * trailing null byte. Neither does `send_buffer_fill'. */
347         memcpy (send_buffer + send_buffer_fill, command, command_len + 1);
348         send_buffer_fill += command_len;
349         send_buffer_free -= command_len;
350
351         pthread_mutex_unlock (&send_lock);
352
353         return (0);
354 } /* }}} int http_write_command */
355
356 static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
357                 user_data_t __attribute__((unused)) *user_data)
358 {
359         int status;
360
361         status = http_write_command (ds, vl);
362
363         return (status);
364 } /* }}} int http_write */
365
366 static int http_shutdown (void) /* {{{ */
367 {
368         http_flush_nolock (/* timeout = */ -1);
369         curl_easy_cleanup(curl);
370         return (0);
371 } /* }}} int http_shutdown */
372
373 void module_register (void) /* {{{ */
374 {
375         plugin_register_init("http", http_init);
376         plugin_register_config ("http", http_config,
377                         config_keys, config_keys_num);
378         plugin_register_write ("http", http_write, /* user_data = */ NULL);
379         plugin_register_flush ("http", http_flush, /* user_data = */ NULL);
380         plugin_register_shutdown("http", http_shutdown);
381 } /* }}} void module_register */
382
383 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */