X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftcpconns.c;h=2e1159229da7c3d67b32a18133839e9aea019be2;hb=5cdafc6cb9228b93cf9b6956c92df61d00bd2a9e;hp=74874ae36382080ebd90af4dcfdb0079965e3d10;hpb=054651e4798cc8b9170c17fc7d8884b1bb1d7a87;p=collectd.git diff --git a/src/tcpconns.c b/src/tcpconns.c index 74874ae3..2e115922 100644 --- a/src/tcpconns.c +++ b/src/tcpconns.c @@ -33,6 +33,19 @@ #elif HAVE_SYSCTLBYNAME # include # include + +/* Some includes needed for compiling on FreeBSD */ +#include +#if HAVE_SYS_TYPES_H +# include +#endif +#if HAVE_SYS_SOCKET_H +# include +#endif +#if HAVE_NET_IF_H +# include +#endif + # include # include # include @@ -122,14 +135,14 @@ static void conn_submit_port_entry (port_entry_t *pe) vl.values = values; vl.values_len = 1; vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "tcpconns"); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin)); if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING)) || (pe->flags & PORT_COLLECT_LOCAL)) { snprintf (vl.plugin_instance, sizeof (vl.plugin_instance), - "%hu-local", pe->port); + "%"PRIu16"-local", pe->port); vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0'; for (i = 1; i <= TCP_STATE_MAX; i++) @@ -146,7 +159,7 @@ static void conn_submit_port_entry (port_entry_t *pe) if (pe->flags & PORT_COLLECT_REMOTE) { snprintf (vl.plugin_instance, sizeof (vl.plugin_instance), - "%hu-remote", pe->port); + "%"PRIu16"-remote", pe->port); vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0'; for (i = 1; i <= TCP_STATE_MAX; i++) @@ -214,7 +227,7 @@ static void conn_reset_port_entry (void) port_entry_t *next = pe->next; DEBUG ("tcpconns plugin: Removing temporary entry " - "for listening port %hu", pe->port); + "for listening port %"PRIu16, pe->port); if (prev == NULL) port_list_head = next; @@ -245,8 +258,8 @@ static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t #endif ) { - NOTICE ("tcpconns plugin: Ignoring connection with unknown state 0x%02x.", - state); + NOTICE ("tcpconns plugin: Ignoring connection with " + "unknown state 0x%02"PRIx8".", state); return (-1); } @@ -258,7 +271,7 @@ static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t pe->flags |= PORT_IS_LISTENING; } - DEBUG ("tcpconns plugin: Connection %hu <-> %hu (%s)", + DEBUG ("tcpconns plugin: Connection %"PRIu16" <-> %"PRIu16" (%s)", port_local, port_remote, tcp_state[state]); pe = conn_get_port_entry (port_local, 0 /* no create */); @@ -336,12 +349,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 +419,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 */