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