2 * collectd - src/http.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2009 Doug MacEachern
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
21 * Doug MacEachern <dougm@hyperic.com>
27 #include "utils_cache.h"
28 #include "utils_parse_option.h"
34 #include <curl/curl.h>
39 static const char *config_keys[] =
41 "URL", "User", "Password"
43 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45 static char *location = NULL;
52 char curl_errbuf[CURL_ERROR_SIZE];
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;
60 static pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER;
62 static int http_init(void) /* {{{ */
65 curl = curl_easy_init ();
69 ERROR ("curl plugin: curl_easy_init failed.");
73 struct curl_slist *headers=NULL;
75 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
77 headers = curl_slist_append(headers, "Accept: */*");
78 headers = curl_slist_append(headers, "Content-Type: text/plain");
79 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
81 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
82 curl_easy_setopt (curl, CURLOPT_URL, location);
86 size_t credentials_size;
88 credentials_size = strlen (user) + 2;
90 credentials_size += strlen (pass);
92 credentials = (char *) malloc (credentials_size);
93 if (credentials == NULL)
95 ERROR ("curl plugin: malloc failed.");
99 ssnprintf (credentials, credentials_size, "%s:%s",
100 user, (pass == NULL) ? "" : pass);
101 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
102 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
108 static int http_value_list_to_string (char *buffer, /* {{{ */
110 const data_set_t *ds, const value_list_t *vl)
116 assert (0 == strcmp (ds->type, vl->type));
118 memset (buffer, 0, buffer_size);
120 #define BUFFER_ADD(...) do { \
121 status = ssnprintf (buffer + offset, buffer_size - offset, \
125 else if (((size_t) status) >= (buffer_size - offset)) \
128 offset += ((size_t) status); \
131 BUFFER_ADD ("%lu", (unsigned long) vl->time);
133 for (i = 0; i < ds->ds_num; i++)
135 if (ds->ds[i].type == DS_TYPE_GAUGE)
136 BUFFER_ADD (":%f", vl->values[i].gauge);
137 else if (ds->ds[i].type == DS_TYPE_COUNTER)
138 BUFFER_ADD (":%llu", vl->values[i].counter);
139 else if (ds->ds[i].type == DS_TYPE_DERIVE)
140 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
141 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
142 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
145 ERROR ("http plugin: Unknown data source type: %i",
149 } /* for ds->ds_num */
154 } /* }}} int http_value_list_to_string */
156 static int http_config (const char *key, const char *value) /* {{{ */
158 if (strcasecmp ("URL", key) == 0)
160 if (location != NULL)
162 location = strdup (value);
163 if (location != NULL)
165 int len = strlen (location);
166 while ((len > 0) && (location[len - 1] == '/'))
169 location[len] = '\0';
178 else if (strcasecmp ("User", key) == 0)
182 user = strdup (value);
185 int len = strlen (user);
186 while ((len > 0) && (user[len - 1] == '/'))
198 else if (strcasecmp ("Password", key) == 0)
202 pass = strdup (value);
205 int len = strlen (pass);
206 while ((len > 0) && (pass[len - 1] == '/'))
223 } /* }}} int http_config */
225 static void http_init_buffer (void) /* {{{ */
227 memset (send_buffer, 0, sizeof (send_buffer));
228 send_buffer_free = sizeof (send_buffer);
229 send_buffer_fill = 0;
230 send_buffer_init_time = time (NULL);
231 } /* }}} http_init_buffer */
233 static int http_send_buffer (char *buffer) /* {{{ */
237 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
238 status = curl_easy_perform (curl);
241 ERROR ("http plugin: curl_easy_perform failed with staus %i: %s",
242 status, curl_errbuf);
245 } /* }}} http_send_buffer */
247 static int http_flush_nolock (int timeout) /* {{{ */
251 DEBUG ("http plugin: http_flush_nolock: timeout = %i; "
252 "send_buffer =\n %s", timeout, send_buffer);
259 if ((send_buffer_init_time + timeout) > now)
263 status = http_send_buffer (send_buffer);
267 } /* }}} http_flush_nolock */
269 static int http_flush (int timeout, /* {{{ */
270 const char *identifier __attribute__((unused)),
271 user_data_t *user_data __attribute__((unused)))
275 pthread_mutex_lock (&send_lock);
276 status = http_flush_nolock (timeout);
277 pthread_mutex_unlock (&send_lock);
280 } /* }}} int http_flush */
282 static int http_write_command (const data_set_t *ds, const value_list_t *vl) /* {{{ */
284 char key[10*DATA_MAX_NAME_LEN];
291 if (0 != strcmp (ds->type, vl->type)) {
292 ERROR ("http plugin: DS type does not match value list type");
296 /* Copy the identifier to `key' and escape it. */
297 status = FORMAT_VL (key, sizeof (key), vl);
299 ERROR ("http plugin: error with format_name");
302 escape_string (key, sizeof (key));
304 /* Convert the values to an ASCII representation and put that into
306 status = http_value_list_to_string (values, sizeof (values), ds, vl);
308 ERROR ("http plugin: error with http_value_list_to_string");
312 command_len = (size_t) ssnprintf (command, sizeof (command),
313 "PUTVAL %s interval=%i %s\n",
314 key, vl->interval, values);
315 if (command_len >= sizeof (command)) {
316 ERROR ("http plugin: Command buffer too small: "
317 "Need %zu bytes.", command_len + 1);
321 pthread_mutex_lock (&send_lock);
323 /* Check if we have enough space for this command. */
324 if (command_len >= send_buffer_free)
326 status = http_flush_nolock (/* timeout = */ -1);
329 pthread_mutex_unlock (&send_lock);
333 assert (command_len < send_buffer_free);
335 /* `command_len + 1' because `command_len' does not include the
336 * trailing null byte. Neither does `send_buffer_fill'. */
337 memcpy (send_buffer + send_buffer_fill, command, command_len + 1);
338 send_buffer_fill += command_len;
339 send_buffer_free -= command_len;
341 pthread_mutex_unlock (&send_lock);
344 } /* }}} int http_write_command */
346 static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
347 user_data_t __attribute__((unused)) *user_data)
351 status = http_write_command (ds, vl);
354 } /* }}} int http_write */
356 static int http_shutdown (void) /* {{{ */
358 http_flush_nolock (/* timeout = */ -1);
359 curl_easy_cleanup(curl);
363 void module_register (void) /* {{{ */
365 plugin_register_init("http", http_init);
366 plugin_register_config ("http", http_config,
367 config_keys, config_keys_num);
368 plugin_register_write ("http", http_write, /* user_data = */ NULL);
369 plugin_register_flush ("http", http_flush, /* user_data = */ NULL);
370 plugin_register_shutdown("http", http_shutdown);
371 } /* }}} void module_register */
373 /* vim: set fdm=marker sw=8 ts=8 tw=78 et : */