summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 86928d3)
raw | patch | inline | side by side (parent: 86928d3)
author | Paul Sadauskas <psadauskas@gmail.com> | |
Fri, 12 Jun 2009 22:46:53 +0000 (16:46 -0600) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Fri, 21 Aug 2009 06:41:53 +0000 (08:41 +0200) |
configure.in | patch | blob | history | |
src/Makefile.am | patch | blob | history | |
src/http.c | patch | blob | history |
diff --git a/configure.in b/configure.in
index fcb395386810f8f20b4e2cb37647ff791ecfa5e9..2ce6239bc7a6a20b059050af6a9e5a453acc1711 100644 (file)
--- a/configure.in
+++ b/configure.in
AC_PLUGIN([cpufreq], [$plugin_cpufreq], [CPU frequency statistics])
AC_PLUGIN([cpu], [$plugin_cpu], [CPU usage statistics])
AC_PLUGIN([csv], [yes], [CSV output plugin])
-AC_PLUGIN([http], [yes], [HTTP output plugin])
+AC_PLUGIN([http], [$with_libcurl], [HTTP output plugin])
AC_PLUGIN([curl], [$with_libcurl], [CURL generic web statistics])
AC_PLUGIN([dbi], [$with_libdbi], [General database statistics])
AC_PLUGIN([df], [$plugin_df], [Filesystem usage statistics])
diff --git a/src/Makefile.am b/src/Makefile.am
index 1fd3bba9aec0af3de243c166cc5bac32bc93ed09..cfb726ce1260484610f7cd377bb48a669fffc055 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
pkglib_LTLIBRARIES += http.la
http_la_SOURCES = http.c
http_la_LDFLAGS = -module -avoid-version
+http_la_CFLAGS = $(AM_CFLAGS)
+http_la_LIBADD =
collectd_LDADD += "-dlopen" http.la
+if BUILD_WITH_LIBCURL
+http_la_CFLAGS += $(BUILD_WITH_LIBCURL_CFLAGS)
+http_la_LIBADD += $(BUILD_WITH_LIBCURL_LIBS)
+endif
collectd_DEPENDENCIES += http.la
endif
diff --git a/src/http.c b/src/http.c
index 6c0a296e4ea5dcefd517ac05289f94ba10beded3..fdcd34fb746dc9c7d47bcdd5807993fc58a88c7a 100644 (file)
--- a/src/http.c
+++ b/src/http.c
#include "utils_cache.h"
#include "utils_parse_option.h"
+#include <curl/curl.h>
+
/*
* Private variables
*/
static const char *config_keys[] =
{
- "Location",
+ "Location", "User", "Password"
};
static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
static char *location = NULL;
+char *user;
+char *pass;
+char *credentials;
+
+CURL *curl;
+char curl_errbuf[CURL_ERROR_SIZE];
+struct curl_slist *headers=NULL;
+
+static int http_init(void)
+{
+
+ curl = curl_easy_init ();
+ if (curl == NULL)
+ {
+ ERROR ("curl plugin: curl_easy_init failed.");
+ return (-1);
+ }
+
+ curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
+ headers = curl_slist_append(headers, "Accept: application/vnd.absperf.ssbe+json");
+ headers = curl_slist_append(headers, "Content-Type: text/csv");
+
+ curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
+ curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
+ curl_easy_setopt (curl, CURLOPT_URL, location);
+
+ if (user != NULL)
+ {
+ size_t credentials_size;
+
+ credentials_size = strlen (user) + 2;
+ if (pass != NULL)
+ credentials_size += strlen (pass);
+
+ credentials = (char *) malloc (credentials_size);
+ if (credentials == NULL)
+ {
+ ERROR ("curl plugin: malloc failed.");
+ return (-1);
+ }
+
+ ssnprintf (credentials, credentials_size, "%s:%s",
+ user, (pass == NULL) ? "" : pass);
+ curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
+ curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
+ }
+
+ return (0);
+}
+
static int value_list_to_string (char *buffer, int buffer_len,
const data_set_t *ds, const value_list_t *vl)
{
}
}
}
+ else if (strcasecmp ("User", key) == 0)
+ {
+ if (user != NULL)
+ free (user);
+ user = strdup (value);
+ if (user != NULL)
+ {
+ int len = strlen (user);
+ while ((len > 0) && (user[len - 1] == '/'))
+ {
+ len--;
+ user[len] = '\0';
+ }
+ if (len <= 0)
+ {
+ free (user);
+ user = NULL;
+ }
+ }
+ }
+ else if (strcasecmp ("Password", key) == 0)
+ {
+ if (pass != NULL)
+ free (pass);
+ pass = strdup (value);
+ if (pass != NULL)
+ {
+ int len = strlen (pass);
+ while ((len > 0) && (pass[len - 1] == '/'))
+ {
+ len--;
+ pass[len] = '\0';
+ }
+ if (len <= 0)
+ {
+ free (pass);
+ pass = NULL;
+ }
+ }
+ }
else
{
return (-1);
char values[512];
char timestamp[512];
+ char csv_buffer[1536];
+
+ int status;
+
if (0 != strcmp (ds->type, vl->type)) {
ERROR ("http plugin: DS type does not match value list type");
return -1;
escape_string (metric_name, sizeof (metric_name));
- fprintf (stdout,
+ status = ssnprintf (csv_buffer, 1536,
"\"%s\",%s,%s\n",
metric_name, timestamp, values);
+
+ curl_easy_setopt (curl, CURLOPT_POSTFIELDS, csv_buffer);
+ status = curl_easy_perform (curl);
+ if (status != 0)
+ {
+ ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
+ status, curl_errbuf);
+ return (-1);
+ }
+
return (0);
} /* int http_write */
void module_register (void)
{
+ plugin_register_init("http", http_init);
plugin_register_config ("http", http_config,
config_keys, config_keys_num);
plugin_register_write ("http", http_write, /* user_data = */ NULL);