src/utils_complain: Changed c_release() into a macro.
[collectd.git] / src / tcpconns.c
index 74874ae..4f46e78 100644 (file)
 #elif HAVE_SYSCTLBYNAME
 # include <sys/socketvar.h>
 # include <sys/sysctl.h>
+
+/* Some includes needed for compiling on FreeBSD */
+#include <sys/time.h>
+#if HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+#if HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#if HAVE_NET_IF_H
+# include <net/if.h>
+#endif
+
 # include <net/route.h>
 # include <netinet/in.h>
 # include <netinet/in_systm.h>
@@ -124,39 +137,36 @@ static void conn_submit_port_entry (port_entry_t *pe)
   vl.time = time (NULL);
   strcpy (vl.host, hostname_g);
   strcpy (vl.plugin, "tcpconns");
+  strcpy (vl.type, "tcp_connections");
 
   if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
       || (pe->flags & PORT_COLLECT_LOCAL))
   {
-    snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
+    ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
        "%hu-local", pe->port);
-    vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
 
     for (i = 1; i <= TCP_STATE_MAX; i++)
     {
       vl.values[0].gauge = pe->count_local[i];
 
-      strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
-      vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+      sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
 
-      plugin_dispatch_values ("tcp_connections", &vl);
+      plugin_dispatch_values (&vl);
     }
   }
 
   if (pe->flags & PORT_COLLECT_REMOTE)
   {
-    snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
+    ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
        "%hu-remote", pe->port);
-    vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
 
     for (i = 1; i <= TCP_STATE_MAX; i++)
     {
       vl.values[0].gauge = pe->count_remote[i];
 
-      strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
-      vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+      sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
 
-      plugin_dispatch_values ("tcp_connections", &vl);
+      plugin_dispatch_values (&vl);
     }
   }
 } /* void conn_submit */
@@ -336,12 +346,7 @@ static int conn_read_file (const char *file)
 
   fh = fopen (file, "r");
   if (fh == NULL)
-  {
-    char errbuf[1024];
-    ERROR ("tcpconns plugin: fopen (%s) failed: %s",
-       file, sstrerror (errno, errbuf, sizeof (errbuf)));
     return (-1);
-  }
 
   while (fgets (buffer, sizeof (buffer), fh) != NULL)
   {
@@ -411,12 +416,25 @@ static int conn_init (void)
 
 static int conn_read (void)
 {
+  int errors_num = 0;
+
   conn_reset_port_entry ();
 
-  conn_read_file ("/proc/net/tcp");
-  conn_read_file ("/proc/net/tcp6");
+  if (conn_read_file ("/proc/net/tcp") != 0)
+    errors_num++;
+  if (conn_read_file ("/proc/net/tcp6") != 0)
+    errors_num++;
 
-  conn_submit_all ();
+  if (errors_num < 2)
+  {
+    conn_submit_all ();
+  }
+  else
+  {
+    ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
+       "coult be read.");
+    return (-1);
+  }
 
   return (0);
 } /* int conn_read */