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 "Location", "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 char *send_buffer_ptr;
57 static int send_buffer_fill;
59 static pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER;
61 static int http_init(void) /* {{{ */
64 curl = curl_easy_init ();
68 ERROR ("curl plugin: curl_easy_init failed.");
72 struct curl_slist *headers=NULL;
74 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
76 headers = curl_slist_append(headers, "Accept: text/csv;q=0.8, */*;q=0.2");
77 headers = curl_slist_append(headers, "Content-Type: text/csv");
78 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
80 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
81 curl_easy_setopt (curl, CURLOPT_URL, location);
85 size_t credentials_size;
87 credentials_size = strlen (user) + 2;
89 credentials_size += strlen (pass);
91 credentials = (char *) malloc (credentials_size);
92 if (credentials == NULL)
94 ERROR ("curl plugin: malloc failed.");
98 ssnprintf (credentials, credentials_size, "%s:%s",
99 user, (pass == NULL) ? "" : pass);
100 curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
101 curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
107 static int http_value_list_to_string (char *buffer, int buffer_len, /* {{{ */
108 const data_set_t *ds, const value_list_t *vl, int index)
112 gauge_t *rates = NULL;
114 assert (0 == strcmp (ds->type, vl->type));
116 memset (buffer, '\0', buffer_len);
118 if ((ds->ds[index].type != DS_TYPE_COUNTER)
119 && (ds->ds[index].type != DS_TYPE_GAUGE))
122 if (ds->ds[index].type == DS_TYPE_COUNTER)
125 rates = uc_get_rate (ds, vl);
128 WARNING ("http plugin: "
129 "uc_get_rate failed.");
132 if (isnan(rates[index]))
137 status = ssnprintf (buffer + offset,
139 "%lf", rates[index]);
141 else /* if (ds->ds[index].type == DS_TYPE_GAUGE) */
143 status = ssnprintf (buffer + offset, buffer_len - offset,
144 "%lf", vl->values[index].gauge);
147 if ((status < 1) || (status >= (buffer_len - offset)))
157 } /* }}} int http_value_list_to_string */
159 static int http_value_list_to_timestamp (char *buffer, int buffer_len, /* {{{ */
160 const data_set_t *ds, const value_list_t *vl)
165 assert (0 == strcmp (ds->type, vl->type));
167 memset (buffer, '\0', buffer_len);
169 status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
170 if ((status < 1) || (status >= buffer_len))
175 } /* }}} int http_value_list_to_timestamp */
177 static int http_value_list_to_metric_name (char *buffer, int buffer_len, /* {{{ */
178 const data_set_t *ds, const value_list_t *vl)
183 assert (0 == strcmp (ds->type, vl->type));
186 status = ssnprintf (buffer + offset, buffer_len - offset,
188 if ((status < 1) || (status >= buffer_len - offset))
193 status = ssnprintf (buffer + offset, buffer_len - offset,
195 if ((status < 1) || (status >= buffer_len - offset))
199 /* plugin_instance */
200 if (strlen (vl->plugin_instance) > 0)
202 status = ssnprintf (buffer + offset, buffer_len - offset,
203 ",%s", vl->plugin_instance);
204 if ((status < 1) || (status >= buffer_len - offset))
209 /* type (if its the same as plugin, don't bother repeating it */
210 if (0 != strcmp (vl->type, vl->plugin))
212 status = ssnprintf (buffer + offset, buffer_len - offset,
214 if ((status < 1) || (status >= buffer_len - offset))
220 if (strlen (vl->type_instance) > 0)
222 status = ssnprintf (buffer + offset, buffer_len - offset,
223 ",%s", vl->type_instance);
224 if ((status < 1) || (status >= buffer_len - offset))
230 } /* }}} int http_value_list_to_metric_name */
232 static int http_config (const char *key, const char *value) /* {{{ */
234 if (strcasecmp ("Location", key) == 0)
236 if (location != NULL)
238 location = strdup (value);
239 if (location != NULL)
241 int len = strlen (location);
242 while ((len > 0) && (location[len - 1] == '/'))
245 location[len] = '\0';
254 else if (strcasecmp ("User", key) == 0)
258 user = strdup (value);
261 int len = strlen (user);
262 while ((len > 0) && (user[len - 1] == '/'))
274 else if (strcasecmp ("Password", key) == 0)
278 pass = strdup (value);
281 int len = strlen (pass);
282 while ((len > 0) && (pass[len - 1] == '/'))
299 } /* }}} int http_config */
301 static void http_init_buffer (void) /* {{{ */
303 memset (send_buffer, 0, sizeof (send_buffer));
304 send_buffer_ptr = send_buffer;
305 send_buffer_fill = 0;
306 } /* }}} http_init_buffer */
308 static void http_send_buffer (char *buffer) /* {{{ */
311 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
312 status = curl_easy_perform (curl);
315 ERROR ("http plugin: curl_easy_perform failed with staus %i: %s",
316 status, curl_errbuf);
318 } /* }}} http_send_buffer */
320 static void http_flush_buffer (void) /* {{{ */
322 DEBUG ("http plugin: flush_buffer: send_buffer_fill = %i",
325 http_send_buffer (send_buffer);
327 } /* }}} http_flush_buffer */
329 static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
330 user_data_t __attribute__((unused)) *user_data)
333 char metric_name[512];
334 int metric_prefix_len;
341 if (0 != strcmp (ds->type, vl->type)) {
342 ERROR ("http plugin: DS type does not match value list type");
346 metric_prefix_len = http_value_list_to_metric_name (metric_name,
347 sizeof (metric_name), ds, vl);
349 if (metric_prefix_len == -1)
352 DEBUG ("http plugin: http_write: metric_name = %s", metric_name);
354 if (http_value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
357 for (i = 0; i < ds->ds_num; i++)
360 if (http_value_list_to_string (value, sizeof (value), ds, vl, i) != 0)
363 ssnprintf(metric_name + metric_prefix_len, sizeof (metric_name) - metric_prefix_len,
364 ",%s", ds->ds[i].name);
366 escape_string (metric_name, sizeof (metric_name));
368 pthread_mutex_lock (&send_lock);
370 status = ssnprintf (send_buffer + send_buffer_fill, sizeof (send_buffer) - send_buffer_fill,
372 metric_name, timestamp, value);
373 send_buffer_fill += status;
375 if ((sizeof (send_buffer) - send_buffer_fill) < 128)
380 pthread_mutex_unlock (&send_lock);
387 } /* }}} int http_write */
389 static int http_shutdown (void) /* {{{ */
391 curl_easy_cleanup(curl);
395 void module_register (void) /* {{{ */
397 plugin_register_init("http", http_init);
398 plugin_register_config ("http", http_config,
399 config_keys, config_keys_num);
400 plugin_register_write ("http", http_write, /* user_data = */ NULL);
401 plugin_register_shutdown("http", http_shutdown);
402 } /* }}} void module_register */
404 /* vim: set fdm=marker sw=8 ts=8 tw=78 : */