memcached plugin: Fix the default behavior, i.e. use the documented host and port.
[collectd.git] / src / memcached.c
index b8fd914..5cfd823 100644 (file)
@@ -75,7 +75,7 @@ static void memcached_free (memcached_t *st)
 
 static int memcached_query_daemon (char *buffer, int buffer_size, user_data_t *user_data)
 {
-  int fd=-1;
+  int fd;
   ssize_t status;
   int buffer_fill;
   int i = 0;
@@ -100,69 +100,59 @@ static int memcached_query_daemon (char *buffer, int buffer_size, user_data_t *u
      }
   }
   else {
-    if (st->port != NULL) {
-      const char *host;
-      const char *port;
+    const char *host;
+    const char *port;
 
-      struct addrinfo  ai_hints;
-      struct addrinfo *ai_list, *ai_ptr;
-      int              ai_return = 0;
+    struct addrinfo  ai_hints;
+    struct addrinfo *ai_list, *ai_ptr;
+    int              ai_return = 0;
 
-      memset (&ai_hints, '\0', sizeof (ai_hints));
-      ai_hints.ai_flags    = 0;
+    memset (&ai_hints, '\0', sizeof (ai_hints));
+    ai_hints.ai_flags    = 0;
 #ifdef AI_ADDRCONFIG
-    /*  ai_hints.ai_flags   |= AI_ADDRCONFIG; */
+    ai_hints.ai_flags   |= AI_ADDRCONFIG;
 #endif
-      ai_hints.ai_family   = AF_INET;
-      ai_hints.ai_socktype = SOCK_STREAM;
-      ai_hints.ai_protocol = 0;
+    ai_hints.ai_family   = AF_UNSPEC;
+    ai_hints.ai_socktype = SOCK_STREAM;
+    ai_hints.ai_protocol = 0;
 
-      host = st->host;
-      if (host == NULL) {
-        host = MEMCACHED_DEF_HOST;
-      }
-
-      port = st->port;
-      if (strlen (port) == 0) {
-        port = MEMCACHED_DEF_PORT;
-      }
+    host = (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST;
+    port = (st->port != NULL) ? st->port : MEMCACHED_DEF_PORT;
 
-      if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) {
-        char errbuf[1024];
-        ERROR ("memcached: getaddrinfo (%s, %s): %s",
+    if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) {
+      char errbuf[1024];
+      ERROR ("memcached: getaddrinfo (%s, %s): %s",
           host, port,
           (ai_return == EAI_SYSTEM)
           ? sstrerror (errno, errbuf, sizeof (errbuf))
           : gai_strerror (ai_return));
-        return -1;
+      return -1;
+    }
+
+    for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
+      /* create our socket descriptor */
+      fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
+      if (fd < 0) {
+        char errbuf[1024];
+        ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
+        continue;
       }
 
-      fd = -1;
-      for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
-        /* create our socket descriptor */
-        fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
-        if (fd < 0) {
-          char errbuf[1024];
-          ERROR ("memcached: socket: %s", sstrerror (errno, errbuf, sizeof (errbuf)));
-          continue;
-        }
-
-        /* connect to the memcached daemon */
-        status = (ssize_t) connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen);
-        if (status != 0) {
-          shutdown (fd, SHUT_RDWR);
-          close (fd);
-          fd = -1;
-          continue;
-        }
-
-        /* A socket could be opened and connecting succeeded. We're
-         * done. */
-        break;
+      /* connect to the memcached daemon */
+      status = (ssize_t) connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
+      if (status != 0) {
+        shutdown (fd, SHUT_RDWR);
+        close (fd);
+        fd = -1;
+        continue;
       }
 
-      freeaddrinfo (ai_list);
+      /* A socket could be opened and connecting succeeded. We're
+       * done. */
+      break;
     }
+
+    freeaddrinfo (ai_list);
   }
 
   if (fd < 0) {
@@ -261,32 +251,6 @@ static int memcached_query_daemon (char *buffer, int buffer_size, user_data_t *u
  *   </Instance>
  * </Plugin>
  */
-static int config_set_string (char **ret_string, oconfig_item_t *ci)
-{
-  char *string;
-
-  if ((ci->values_num != 1)
-      || (ci->values[0].type != OCONFIG_TYPE_STRING))
-  {
-    WARNING ("memcached plugin: The `%s' config option "
-        "needs exactly one string argument.", ci->key);
-    return (-1);
-  }
-
-  string = strdup (ci->values[0].value.string);
-  if (string == NULL)
-  {
-    ERROR ("memcached plugin: strdup failed.");
-    return (-1);
-  }
-
-  if (*ret_string != NULL)
-    free (*ret_string);
-  *ret_string = string;
-
-  return (0);
-}
-
 static int config_add_instance(oconfig_item_t *ci)
 {
   memcached_t *st;
@@ -314,7 +278,7 @@ static int config_add_instance(oconfig_item_t *ci)
   st->port = NULL;
   memset (st, 0, sizeof (*st));
 
-  status = config_set_string (&st->name, ci);
+  status = cf_util_get_string (ci, &st->name);
   if (status != 0)
   {
     sfree (st);
@@ -327,11 +291,11 @@ static int config_add_instance(oconfig_item_t *ci)
     oconfig_item_t *child = ci->children + i;
 
     if (strcasecmp ("Socket", child->key) == 0)
-      status = config_set_string (&st->socket, child);
+      status = cf_util_get_string (child, &st->socket);
     else if (strcasecmp ("Host", child->key) == 0)
-      status = config_set_string (&st->host, child);
+      status = cf_util_get_string (child, &st->host);
     else if (strcasecmp ("Port", child->key) == 0)
-      status = config_set_string (&st->port, child);
+      status = cf_util_get_service (child, &st->port);
     else
     {
       WARNING ("memcached plugin: Option `%s' not allowed here.",
@@ -355,10 +319,10 @@ static int config_add_instance(oconfig_item_t *ci)
     memset (callback_name, 0, sizeof (callback_name));
     ssnprintf (callback_name, sizeof (callback_name),
         "memcached/%s/%s",
-        (st->host != NULL) ? st->host : hostname_g,
-        (st->port != NULL) ? st->port : "default"),
+        (st->host != NULL) ? st->host : MEMCACHED_DEF_HOST,
+        (st->port != NULL) ? st->port : MEMCACHED_DEF_PORT)
 
-    status = plugin_register_complex_read (/* group = */ NULL,
+    status = plugin_register_complex_read (/* group = */ "memcached",
         /* name      = */ callback_name,
         /* callback  = */ memcached_read,
         /* interval  = */ NULL,
@@ -374,7 +338,7 @@ static int config_add_instance(oconfig_item_t *ci)
   return (0);
 }
 
-static int config (oconfig_item_t *ci)
+static int memcached_config (oconfig_item_t *ci)
 {
   int status = 0;
   int i;
@@ -645,5 +609,5 @@ static int memcached_read (user_data_t *user_data)
 
 void module_register (void)
 {
-  plugin_register_complex_config ("memcached", config);
+  plugin_register_complex_config ("memcached", memcached_config);
 }