Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / gmond.c
index 336fbb9..2ac9f25 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/gmond.c
- * Copyright (C) 2009,2010  Florian octo Forster
+ * Copyright (C) 2009-2015  Florian octo Forster
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -291,8 +291,14 @@ static int create_sockets (socket_entry_t **ret_sockets, /* {{{ */
     {
       int yes = 1;
 
-      setsockopt (sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
+      status = setsockopt (sockets[sockets_num].fd, SOL_SOCKET, SO_REUSEADDR,
           (void *) &yes, sizeof (yes));
+      if (status != 0)
+      {
+        char errbuf[1024];
+        WARNING ("gmond plugin: setsockopt(2) failed: %s",
+                 sstrerror (errno, errbuf, sizeof (errbuf)));
+      }
     }
 
     status = bind (sockets[sockets_num].fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
@@ -410,10 +416,19 @@ static int request_meta_data (const char *host, const char *name) /* {{{ */
 
   pthread_mutex_lock (&mc_send_sockets_lock);
   for (i = 0; i < mc_send_sockets_num; i++)
-    sendto (mc_send_sockets[i].fd, buffer, (size_t) buffer_size,
+  {
+    ssize_t status = sendto (mc_send_sockets[i].fd, buffer, (size_t) buffer_size,
         /* flags = */ 0,
         (struct sockaddr *) &mc_send_sockets[i].addr,
         mc_send_sockets[i].addrlen);
+    if (status == -1)
+    {
+      char errbuf[1024];
+      ERROR ("gmond plugin: sendto(2) failed: %s",
+             sstrerror (errno, errbuf, sizeof (errbuf)));
+      continue;
+    }
+  }
   pthread_mutex_unlock (&mc_send_sockets_lock);
 
   sfree (msg.Ganglia_metadata_msg_u.grequest.metric_id.host);
@@ -479,36 +494,6 @@ static staging_entry_t *staging_entry_get (const char *host, /* {{{ */
   return (se);
 } /* }}} staging_entry_t *staging_entry_get */
 
-static int staging_entry_submit (const char *host, const char *name, /* {{{ */
-    staging_entry_t *se)
-{
-  value_list_t vl;
-  value_t values[se->vl.values_len];
-
-  if (se->vl.interval == 0)
-  {
-    /* No meta data has been received for this metric yet. */
-    se->flags = 0;
-    pthread_mutex_unlock (&staging_lock);
-    request_meta_data (host, name);
-    return (0);
-  }
-
-  se->flags = 0;
-
-  memcpy (values, se->vl.values, sizeof (values));
-  memcpy (&vl, &se->vl, sizeof (vl));
-
-  /* Unlock before calling `plugin_dispatch_values'.. */
-  pthread_mutex_unlock (&staging_lock);
-
-  vl.values = values;
-
-  plugin_dispatch_values (&vl);
-
-  return (0);
-} /* }}} int staging_entry_submit */
-
 static int staging_entry_update (const char *host, const char *name, /* {{{ */
     const char *type, const char *type_instance,
     int ds_index, int ds_type, value_t value)
@@ -558,17 +543,30 @@ static int staging_entry_update (const char *host, const char *name, /* {{{ */
 
   se->flags |= (0x01 << ds_index);
 
-  /* Check if all values have been set and submit if so. */
-  if (se->flags == ((0x01 << se->vl.values_len) - 1))
+  /* Check if all data sources have been set. If not, return here. */
+  if (se->flags != ((0x01 << se->vl.values_len) - 1))
   {
-    /* `staging_lock' is unlocked in `staging_entry_submit'. */
-    staging_entry_submit (host, name, se);
+    pthread_mutex_unlock (&staging_lock);
+    return (0);
   }
-  else
+
+  /* Check if the interval of this metric is known. If not, request meta data
+   * and return. */
+  if (se->vl.interval == 0)
   {
+    /* No meta data has been received for this metric yet. */
+    se->flags = 0;
     pthread_mutex_unlock (&staging_lock);
+
+    request_meta_data (host, name);
+    return (0);
   }
 
+  plugin_dispatch_values (&se->vl);
+
+  se->flags = 0;
+  pthread_mutex_unlock (&staging_lock);
+
   return (0);
 } /* }}} int staging_entry_update */