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
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.
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.
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
21 * Florian octo Forster <octo at verplant.org>
22 * Doug MacEachern <dougm@hyperic.com>
23 * Paul Sadauskas <psadauskas@gmail.com>
29 #include "utils_cache.h"
30 #include "utils_parse_option.h"
36 #include <curl/curl.h>
41 static const char *config_keys[] =
43 "URL", "User", "Password"
45 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
47 static char *location = NULL;
54 char curl_errbuf[CURL_ERROR_SIZE];
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;
62 static pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER;
64 static void http_init_buffer (void) /* {{{ */
66 memset (send_buffer, 0, sizeof (send_buffer));
67 send_buffer_free = sizeof (send_buffer);
69 send_buffer_init_time = time (NULL);
70 } /* }}} http_init_buffer */
72 static int http_init(void) /* {{{ */
75 curl = curl_easy_init ();
79 ERROR ("curl plugin: curl_easy_init failed.");
83 struct curl_slist *headers=NULL;
85 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
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);
91 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
92 curl_easy_setopt (curl, CURLOPT_URL, location);
96 size_t credentials_size;
98 credentials_size = strlen (user) + 2;
100 credentials_size += strlen (pass);
102 credentials = (char *) malloc (credentials_size);
103 if (credentials == NULL)
105 ERROR ("curl plugin: malloc failed.");
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);
120 static int http_value_list_to_string (char *buffer, /* {{{ */
122 const data_set_t *ds, const value_list_t *vl)
128 assert (0 == strcmp (ds->type, vl->type));
130 memset (buffer, 0, buffer_size);
132 #define BUFFER_ADD(...) do { \
133 status = ssnprintf (buffer + offset, buffer_size - offset, \
137 else if (((size_t) status) >= (buffer_size - offset)) \
140 offset += ((size_t) status); \
143 BUFFER_ADD ("%lu", (unsigned long) vl->time);
145 for (i = 0; i < ds->ds_num; i++)
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);
157 ERROR ("http plugin: Unknown data source type: %i",
161 } /* for ds->ds_num */
166 } /* }}} int http_value_list_to_string */
168 static int http_config (const char *key, const char *value) /* {{{ */
170 if (strcasecmp ("URL", key) == 0)
172 if (location != NULL)
174 location = strdup (value);
175 if (location != NULL)
177 int len = strlen (location);
178 while ((len > 0) && (location[len - 1] == '/'))
181 location[len] = '\0';
190 else if (strcasecmp ("User", key) == 0)
194 user = strdup (value);
197 int len = strlen (user);
198 while ((len > 0) && (user[len - 1] == '/'))
210 else if (strcasecmp ("Password", key) == 0)
214 pass = strdup (value);
217 int len = strlen (pass);
218 while ((len > 0) && (pass[len - 1] == '/'))
235 } /* }}} int http_config */
237 static int http_send_buffer (char *buffer) /* {{{ */
241 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
242 status = curl_easy_perform (curl);
245 ERROR ("http plugin: curl_easy_perform failed with staus %i: %s",
246 status, curl_errbuf);
249 } /* }}} http_send_buffer */
251 static int http_flush_nolock (int timeout) /* {{{ */
255 DEBUG ("http plugin: http_flush_nolock: timeout = %i; "
256 "send_buffer =\n %s", timeout, send_buffer);
263 if ((send_buffer_init_time + timeout) > now)
267 if (send_buffer_fill <= 0)
269 send_buffer_init_time = time (NULL);
273 status = http_send_buffer (send_buffer);
277 } /* }}} http_flush_nolock */
279 static int http_flush (int timeout, /* {{{ */
280 const char *identifier __attribute__((unused)),
281 user_data_t *user_data __attribute__((unused)))
285 pthread_mutex_lock (&send_lock);
286 status = http_flush_nolock (timeout);
287 pthread_mutex_unlock (&send_lock);
290 } /* }}} int http_flush */
292 static int http_write_command (const data_set_t *ds, const value_list_t *vl) /* {{{ */
294 char key[10*DATA_MAX_NAME_LEN];
301 if (0 != strcmp (ds->type, vl->type)) {
302 ERROR ("http plugin: DS type does not match value list type");
306 /* Copy the identifier to `key' and escape it. */
307 status = FORMAT_VL (key, sizeof (key), vl);
309 ERROR ("http plugin: error with format_name");
312 escape_string (key, sizeof (key));
314 /* Convert the values to an ASCII representation and put that into
316 status = http_value_list_to_string (values, sizeof (values), ds, vl);
318 ERROR ("http plugin: error with http_value_list_to_string");
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);
331 pthread_mutex_lock (&send_lock);
333 /* Check if we have enough space for this command. */
334 if (command_len >= send_buffer_free)
336 status = http_flush_nolock (/* timeout = */ -1);
339 pthread_mutex_unlock (&send_lock);
343 assert (command_len < send_buffer_free);
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;
351 pthread_mutex_unlock (&send_lock);
354 } /* }}} int http_write_command */
356 static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
357 user_data_t __attribute__((unused)) *user_data)
361 status = http_write_command (ds, vl);
364 } /* }}} int http_write */
366 static int http_shutdown (void) /* {{{ */
368 http_flush_nolock (/* timeout = */ -1);
369 curl_easy_cleanup(curl);
371 } /* }}} int http_shutdown */
373 void module_register (void) /* {{{ */
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 */
383 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */