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 int send_buffer_fill;
58 static pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER;
60 static int http_init(void) /* {{{ */
63 curl = curl_easy_init ();
67 ERROR ("curl plugin: curl_easy_init failed.");
71 struct curl_slist *headers=NULL;
73 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
75 headers = curl_slist_append(headers, "Accept: */*");
76 headers = curl_slist_append(headers, "Content-Type: text/plain");
77 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
79 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
80 curl_easy_setopt (curl, CURLOPT_URL, location);
84 size_t credentials_size;
86 credentials_size = strlen (user) + 2;
88 credentials_size += strlen (pass);
90 credentials = (char *) malloc (credentials_size);
91 if (credentials == NULL)
93 ERROR ("curl plugin: malloc failed.");
97 ssnprintf (credentials, credentials_size, "%s:%s",
98 user, (pass == NULL) ? "" : pass);
99 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
100 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
106 static int http_value_list_to_string (char *buffer, /* {{{ */
108 const data_set_t *ds, const value_list_t *vl)
114 assert (0 == strcmp (ds->type, vl->type));
116 memset (buffer, 0, buffer_size);
118 #define BUFFER_ADD(...) do { \
119 status = ssnprintf (buffer + offset, buffer_size - offset, \
123 else if (((size_t) status) >= (buffer_size - offset)) \
126 offset += ((size_t) status); \
129 BUFFER_ADD ("%lu", (unsigned long) vl->time);
131 for (i = 0; i < ds->ds_num; i++)
133 if (ds->ds[i].type == DS_TYPE_GAUGE)
134 BUFFER_ADD (":%f", vl->values[i].gauge);
135 else if (ds->ds[i].type == DS_TYPE_COUNTER)
136 BUFFER_ADD (":%llu", vl->values[i].counter);
137 else if (ds->ds[i].type == DS_TYPE_DERIVE)
138 BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
139 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
140 BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
143 ERROR ("http plugin: Unknown data source type: %i",
147 } /* for ds->ds_num */
152 } /* }}} int http_value_list_to_string */
154 static int http_config (const char *key, const char *value) /* {{{ */
156 if (strcasecmp ("URL", key) == 0)
158 if (location != NULL)
160 location = strdup (value);
161 if (location != NULL)
163 int len = strlen (location);
164 while ((len > 0) && (location[len - 1] == '/'))
167 location[len] = '\0';
176 else if (strcasecmp ("User", key) == 0)
180 user = strdup (value);
183 int len = strlen (user);
184 while ((len > 0) && (user[len - 1] == '/'))
196 else if (strcasecmp ("Password", key) == 0)
200 pass = strdup (value);
203 int len = strlen (pass);
204 while ((len > 0) && (pass[len - 1] == '/'))
221 } /* }}} int http_config */
223 static void http_init_buffer (void) /* {{{ */
225 memset (send_buffer, 0, sizeof (send_buffer));
226 send_buffer_fill = 0;
227 } /* }}} http_init_buffer */
229 static int http_send_buffer (char *buffer) /* {{{ */
233 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
234 status = curl_easy_perform (curl);
237 ERROR ("http plugin: curl_easy_perform failed with staus %i: %s",
238 status, curl_errbuf);
241 } /* }}} http_send_buffer */
243 static int http_flush_buffer (void) /* {{{ */
246 DEBUG ("http plugin: flushing buffer:\n%s", send_buffer);
248 status = http_send_buffer (send_buffer);
252 } /* }}} http_flush_buffer */
254 static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
255 user_data_t __attribute__((unused)) *user_data)
262 if (0 != strcmp (ds->type, vl->type)) {
263 ERROR ("http plugin: DS type does not match value list type");
267 status = FORMAT_VL (key, sizeof (key), vl);
269 ERROR ("http plugin: error with format_name");
273 status = http_value_list_to_string (values, sizeof (values), ds, vl);
275 ERROR ("http plugin: error with http_value_list_to_string");
280 pthread_mutex_lock (&send_lock);
282 /* `values' has a leading `:'. */
283 status = ssnprintf (send_buffer + send_buffer_fill,
284 sizeof (send_buffer) - send_buffer_fill,
285 "PUTVAL %s interval=%i %lu%s\n",
286 key, interval_g, (unsigned long) vl->time, values);
287 send_buffer_fill += status;
289 if ((sizeof (send_buffer) - send_buffer_fill) < (sizeof(key) + sizeof(values)))
291 status = http_flush_buffer();
297 pthread_mutex_unlock (&send_lock);
302 } /* }}} int http_write */
304 static int http_shutdown (void) /* {{{ */
307 curl_easy_cleanup(curl);
311 void module_register (void) /* {{{ */
313 plugin_register_init("http", http_init);
314 plugin_register_config ("http", http_config,
315 config_keys, config_keys_num);
316 plugin_register_write ("http", http_write, /* user_data = */ NULL);
317 plugin_register_shutdown("http", http_shutdown);
318 } /* }}} void module_register */
320 /* vim: set fdm=marker sw=8 ts=8 tw=78 : */