From: Paul Sadauskas Date: Fri, 12 Jun 2009 22:46:53 +0000 (-0600) Subject: http plugin: it works, i think X-Git-Tag: collectd-4.8.0~26^2~30 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=2b9ba6a970b33397dc3b58b22b4c84e39ab53378;p=collectd.git http plugin: it works, i think --- diff --git a/configure.in b/configure.in index fcb39538..2ce6239b 100644 --- a/configure.in +++ b/configure.in @@ -3698,7 +3698,7 @@ AC_PLUGIN([couchdb], [$plugin_couchdb], [CouchDB statistics]) 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 1fd3bba9..cfb726ce 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -228,7 +228,13 @@ if BUILD_PLUGIN_HTTP 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 6c0a296e..fdcd34fb 100644 --- a/src/http.c +++ b/src/http.c @@ -27,17 +27,69 @@ #include "utils_cache.h" #include "utils_parse_option.h" +#include + /* * 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) { @@ -196,6 +248,46 @@ static int http_config (const char *key, const char *value) } } } + 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); @@ -210,6 +302,10 @@ static int http_write (const data_set_t *ds, const value_list_t *vl, 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; @@ -228,15 +324,26 @@ static int http_write (const data_set_t *ds, const value_list_t *vl, 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);