http plugin: http_write: Clean-up.
[collectd.git] / src / http.c
index 06737a3..8027220 100644 (file)
 #include "utils_cache.h"
 #include "utils_parse_option.h"
 
+#if HAVE_PTHREAD_H
+# include <pthread.h>
+#endif
+
 #include <curl/curl.h>
 
 /*
@@ -34,7 +38,7 @@
  */
 static const char *config_keys[] =
 {
-  "Location", "User", "Password"
+        "URL", "User", "Password"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
@@ -44,325 +48,299 @@ char *user;
 char *pass;
 char *credentials;
 
-static int http_init_curl(CURL *curl, char *curl_errbuf)
-{
-  struct curl_slist *headers=NULL;
-
-  curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
-
-  headers = curl_slist_append(headers, "Accept: text/csv");
-  headers = curl_slist_append(headers, "Content-Type: text/csv");
-  curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
-
-  curl_easy_setopt (curl, CURLOPT_FORBID_REUSE, 1);
-
-  curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
-  curl_easy_setopt (curl, CURLOPT_URL, location);
-
-  if (user != NULL)
-  {
-    size_t credentials_size;
+CURL *curl;
+char curl_errbuf[CURL_ERROR_SIZE];
 
-    credentials_size = strlen (user) + 2;
-    if (pass != NULL)
-      credentials_size += strlen (pass);
+#define SEND_BUFFER_SIZE 4096
+static char   send_buffer[SEND_BUFFER_SIZE];
+static size_t send_buffer_free;
+static size_t send_buffer_fill;
 
-    credentials = (char *) malloc (credentials_size);
-    if (credentials == NULL)
-    {
-      ERROR ("curl plugin: malloc failed.");
-      return (-1);
-    }
+static pthread_mutex_t  send_lock = PTHREAD_MUTEX_INITIALIZER;
 
-    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 http_init(void)
-{
-  return (0);
-}
-
-static int value_list_to_string (char *buffer, int buffer_len,
-    const data_set_t *ds, const value_list_t *vl, int index)
-{
-  int offset = 0;
-  int status;
-  gauge_t *rates = NULL;
-
-  assert (0 == strcmp (ds->type, vl->type));
-
-  memset (buffer, '\0', buffer_len);
-
-  if ((ds->ds[index].type != DS_TYPE_COUNTER)
-      && (ds->ds[index].type != DS_TYPE_GAUGE))
-    return (-1);
-
-  if (ds->ds[index].type == DS_TYPE_COUNTER)
-  {
-    if (rates == NULL)
-      rates = uc_get_rate (ds, vl);
-    if (rates == NULL)
-    {
-      WARNING ("http plugin: "
-          "uc_get_rate failed.");
-      return (-1);
-    }
-    if (isnan(rates[index]))
-    {
-      /* dont output */
-      return (-1);
-    }
-    status = ssnprintf (buffer + offset,
-        buffer_len - offset,
-        "%lf", rates[index]);
-  }
-  else /* if (ds->ds[index].type == DS_TYPE_GAUGE) */
-  {
-    status = ssnprintf (buffer + offset, buffer_len - offset,
-        "%lf", vl->values[index].gauge);
-  }
-
-  if ((status < 1) || (status >= (buffer_len - offset)))
-  {
-    sfree (rates);
-    return (-1);
-  }
-
-  offset += status;
-
-  sfree (rates);
-  return (0);
-} /* int value_list_to_string */
-
-static int value_list_to_timestamp (char *buffer, int buffer_len,
-    const data_set_t *ds, const value_list_t *vl)
+static int http_init(void) /* {{{ */
 {
-  int offset = 0;
-  int status;
 
-  assert (0 == strcmp (ds->type, vl->type));
+        curl = curl_easy_init ();
 
-  memset (buffer, '\0', buffer_len);
+        if (curl == NULL)
+        {
+                ERROR ("curl plugin: curl_easy_init failed.");
+                return (-1);
+        }
 
-  status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
-  if ((status < 1) || (status >= buffer_len))
-    return (-1);
-  offset = status;
+        struct curl_slist *headers=NULL;
 
-  return (0);
-} /* int value_list_to_timestamp */
+        curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
 
-static int value_list_to_metric_name (char *buffer, int buffer_len,
-    const data_set_t *ds, const value_list_t *vl)
-{
-  int offset = 0;
-  int status;
-
-  assert (0 == strcmp (ds->type, vl->type));
-
-  /* hostname */
-  status = ssnprintf (buffer + offset, buffer_len - offset,
-      "%s", vl->host);
-  if ((status < 1) || (status >= buffer_len - offset))
-    return (-1);
-  offset += status;
-
-  /* plugin */
-  status = ssnprintf (buffer + offset, buffer_len - offset,
-      ",%s", vl->plugin);
-  if ((status < 1) || (status >= buffer_len - offset))
-    return (-1);
-  offset += status;
-
-  /* plugin_instance */
-  if (strlen (vl->plugin_instance) > 0)
-  {
-    status = ssnprintf (buffer + offset, buffer_len - offset,
-        ",%s", vl->plugin_instance);
-    if ((status < 1) || (status >= buffer_len - offset))
-      return (-1);
-    offset += status;
-  }
-
-  /* type (if its the same as plugin, don't bother repeating it */
-  if (0 != strcmp (vl->type, vl->plugin)) 
-  {
-    status = ssnprintf (buffer + offset, buffer_len - offset,
-        ",%s", vl->type);
-    if ((status < 1) || (status >= buffer_len - offset))
-      return (-1);
-    offset += status;
-  }
-
-  /* type_instance */
-  if (strlen (vl->type_instance) > 0)
-  {
-    status = ssnprintf (buffer + offset, buffer_len - offset,
-        ",%s", vl->type_instance);
-    if ((status < 1) || (status >= buffer_len - offset))
-      return (-1);
-    offset += status;
-  }
-
-  return (offset);
-} /* int value_list_to_metric_name */
-
-static int http_config (const char *key, const char *value)
-{
-  if (strcasecmp ("Location", key) == 0)
-  {
-    if (location != NULL)
-      free (location);
-    location = strdup (value);
-    if (location != NULL)
-    {
-      int len = strlen (location);
-      while ((len > 0) && (location[len - 1] == '/'))
-      {
-        len--;
-        location[len] = '\0';
-      }
-      if (len <= 0)
-      {
-        free (location);
-        location = NULL;
-      }
-    }
-  }
-  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);
-  }
-  return (0);
-} /* int http_config */
-
-static int http_write (const data_set_t *ds, const value_list_t *vl,
-    user_data_t __attribute__((unused)) *user_data)
-{
-  CURL         *curl;
-  char curl_errbuf[CURL_ERROR_SIZE];
-
-  char         metric_name[512];
-  int          metric_prefix_len;
-  char         value[512];
-  char         timestamp[512];
+        headers = curl_slist_append(headers, "Accept:  */*");
+        headers = curl_slist_append(headers, "Content-Type: text/plain");
+        curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
 
-  char csv_buffer[10240];
+        curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, curl_errbuf);
+        curl_easy_setopt (curl, CURLOPT_URL, location);
 
-  int status;
-  int offset = 0;
-  int i;
+        if (user != NULL)
+        {
+                size_t credentials_size;
 
-  if (0 != strcmp (ds->type, vl->type)) {
-    ERROR ("http plugin: DS type does not match value list type");
-    return -1;
-  }
+                credentials_size = strlen (user) + 2;
+                if (pass != NULL)
+                        credentials_size += strlen (pass);
 
-  curl = curl_easy_init ();
-  if (curl == NULL)
-  {
-    ERROR ("curl plugin: curl_easy_init failed.");
-    return (-1);
-  }
+                credentials = (char *) malloc (credentials_size);
+                if (credentials == NULL)
+                {
+                        ERROR ("curl plugin: malloc failed.");
+                        return (-1);
+                }
 
-  http_init_curl(curl, curl_errbuf);
+                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);
+        }
 
-  metric_prefix_len = value_list_to_metric_name (metric_name, 
-      sizeof (metric_name), ds, vl);
-    
-  if (metric_prefix_len == -1)
-    return (-1);
+        return (0);
+} /* }}} */
 
-  DEBUG ("http plugin: http_write: metric_name = %s;", metric_name);
+static int http_value_list_to_string (char *buffer, /* {{{ */
+                size_t buffer_size,
+                const data_set_t *ds, const value_list_t *vl)
+{
+        size_t offset = 0;
+        int status;
+        int i;
 
-  if (value_list_to_timestamp (timestamp, sizeof (timestamp), ds, vl) != 0)
-    return (-1);
+        assert (0 == strcmp (ds->type, vl->type));
 
-  for (i = 0; i < ds->ds_num; i++) 
-  {
+        memset (buffer, 0, buffer_size);
 
-    if (value_list_to_string (value, sizeof (value), ds, vl, i) != 0)
-      return (-1);
+#define BUFFER_ADD(...) do { \
+        status = ssnprintf (buffer + offset, buffer_size - offset, \
+                        __VA_ARGS__); \
+        if (status < 1) \
+                return (-1); \
+        else if (((size_t) status) >= (buffer_size - offset)) \
+                return (-1); \
+        else \
+                offset += ((size_t) status); \
+} while (0)
 
-    ssnprintf(metric_name + metric_prefix_len, sizeof (metric_name) - metric_prefix_len,
-        ",%s", ds->ds[i].name); 
+        BUFFER_ADD ("%lu", (unsigned long) vl->time);
 
-    escape_string (metric_name, sizeof (metric_name));
+        for (i = 0; i < ds->ds_num; i++)
+{
+        if (ds->ds[i].type == DS_TYPE_GAUGE)
+                BUFFER_ADD (":%f", vl->values[i].gauge);
+        else if (ds->ds[i].type == DS_TYPE_COUNTER)
+                BUFFER_ADD (":%llu", vl->values[i].counter);
+        else if (ds->ds[i].type == DS_TYPE_DERIVE)
+                BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
+        else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
+                BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
+        else
+        {
+                ERROR ("http plugin: Unknown data source type: %i",
+                                ds->ds[i].type);
+                return (-1);
+        }
+} /* for ds->ds_num */
+
+#undef BUFFER_ADD
+
+return (0);
+} /* }}} int http_value_list_to_string */
+
+static int http_config (const char *key, const char *value) /* {{{ */
+{
+        if (strcasecmp ("URL", key) == 0)
+        {
+                if (location != NULL)
+                        free (location);
+                location = strdup (value);
+                if (location != NULL)
+                {
+                        int len = strlen (location);
+                        while ((len > 0) && (location[len - 1] == '/'))
+                        {
+                                len--;
+                                location[len] = '\0';
+                        }
+                        if (len <= 0)
+                        {
+                                free (location);
+                                location = NULL;
+                        }
+                }
+        }
+        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);
+        }
+        return (0);
+} /* }}} int http_config */
+
+static void http_init_buffer (void)  /* {{{ */
+{
+        memset (send_buffer, 0, sizeof (send_buffer));
+        send_buffer_free = sizeof (send_buffer);
+        send_buffer_fill = 0;
+} /* }}} http_init_buffer */
 
-    status = ssnprintf (csv_buffer + offset, sizeof (csv_buffer) - offset,
-        "\"%s\",%s,%s\n",
-        metric_name, timestamp, value);
-    offset += status;
+static int http_send_buffer (char *buffer) /* {{{ */
+{
+        int status = 0;
+
+        curl_easy_setopt (curl, CURLOPT_POSTFIELDS, buffer);
+        status = curl_easy_perform (curl);
+        if (status != 0)
+        {
+                ERROR ("http plugin: curl_easy_perform failed with staus %i: %s",
+                                status, curl_errbuf);
+        }
+        return (status);
+} /* }}} http_send_buffer */
+
+static int http_flush_buffer (void) /* {{{ */
+{
+        int status = 0;
+        DEBUG ("http plugin: flushing buffer:\n%s", send_buffer);
 
-  } /* for */
+        status = http_send_buffer (send_buffer);
+        http_init_buffer ();
 
-  printf(csv_buffer);
+        return (status);
+} /* }}} http_flush_buffer */
 
-  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);
-  }
+static int http_write_command (const data_set_t *ds, const value_list_t *vl) /* {{{ */
+{
+        char key[10*DATA_MAX_NAME_LEN];
+        char values[512];
+        char command[1024];
+        size_t command_len;
+
+        int status;
+
+        if (0 != strcmp (ds->type, vl->type)) {
+                ERROR ("http plugin: DS type does not match value list type");
+                return -1;
+        }
+
+        /* Copy the identifier to `key' and escape it. */
+        status = FORMAT_VL (key, sizeof (key), vl);
+        if (status != 0) {
+                ERROR ("http plugin: error with format_name");
+                return (status);
+        }
+        escape_string (key, sizeof (key));
+
+        /* Convert the values to an ASCII representation and put that into
+         * `values'. */
+        status = http_value_list_to_string (values, sizeof (values), ds, vl);
+        if (status != 0) {
+                ERROR ("http plugin: error with http_value_list_to_string");
+                return (status);
+        }
+
+        command_len = (size_t) ssnprintf (command, sizeof (command),
+                        "PUTVAL %s interval=%i %s\n",
+                        key, vl->interval, values);
+        if (command_len >= sizeof (command)) {
+                ERROR ("http plugin: Command buffer too small: "
+                                "Need %zu bytes.", command_len + 1);
+                return (-1);
+        }
+
+        pthread_mutex_lock (&send_lock);
+
+        /* Check if we have enough space for this command. */
+        if (command_len >= send_buffer_free)
+        {
+                status = http_flush_buffer();
+                if (status != 0)
+                {
+                        pthread_mutex_unlock (&send_lock);
+                        return status;
+                }
+        }
+        assert (command_len < send_buffer_free);
+
+        /* `command_len + 1' because `command_len' does not include the
+         * trailing null byte. Neither does `send_buffer_fill'. */
+        memcpy (send_buffer + send_buffer_fill, command, command_len + 1);
+        send_buffer_fill += command_len;
+        send_buffer_free -= command_len;
+
+        pthread_mutex_unlock (&send_lock);
+
+        return (0);
+} /* }}} int http_write_command */
+
+static int http_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
+                user_data_t __attribute__((unused)) *user_data)
+{
+        int status;
 
-  curl_easy_cleanup(curl);
+        status = http_write_command (ds, vl);
 
-  return (0);
+        return (status);
+} /* }}} int http_write */
 
-} /* int http_write */
+static int http_shutdown (void) /* {{{ */
+{
+        http_flush_buffer();
+        curl_easy_cleanup(curl);
+        return (0);
+}
 
-void module_register (void)
+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);
-} /* void module_register */
+        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);
+        plugin_register_shutdown("http", http_shutdown);
+} /* }}} void module_register */
+
+/* vim: set fdm=marker sw=8 ts=8 tw=78 et : */