From e85fcbd5711999af88ed887c0c17a26ab29f2b28 Mon Sep 17 00:00:00 2001 From: Davide Madrisan Date: Wed, 7 May 2014 22:14:45 +0200 Subject: This patch will add the IP and port, or socket name, to the error message and thus simplify the problem debugging: no need to check for this information in the Nagios configuration. This function is only used by 'check_tcp.c'. Without the patch: $ ./plugins/check_tcp -H 127.0.0.1 -p 21 Connection refused $ ./plugins/check_tcp -H /var/spool/nagios/cmd/nagios.cmd Permission denied With the patch: $ ./plugins/check_tcp -H 127.0.0.1 -p 21 connect to address 127.0.0.1 and port 21: Connection refused $ ./plugins/check_tcp -H /var/spool/nagios/cmd/nagios.cmd connect to socket /var/spool/nagios/cmd/nagios.cmd: Permission denied Thanks to Davide Madrisan. --- Closes #1277 --- plugins/netutils.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'plugins/netutils.c') diff --git a/plugins/netutils.c b/plugins/netutils.c index 00440465..48042188 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -167,11 +167,13 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) char port_str[6], host[MAX_HOST_ADDRESS_LENGTH]; size_t len; int socktype, result; + bool is_socket; socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; + bool is_socket = (host_name[0] == '/'); /* as long as it doesn't start with a '/', it's assumed a host or ip */ - if(host_name[0] != '/'){ + if (!is_socket){ memset (&hints, 0, sizeof (hints)); hints.ai_family = address_family; hints.ai_protocol = proto; @@ -253,7 +255,11 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) return econn_refuse_state; break; case STATE_CRITICAL: /* user did not set econn_refuse_state */ - printf ("%s\n", strerror(errno)); + if (is_socket) + printf("connect to socket %s: %s\n", host_name, strerror(errno)); + else + printf("connect to address %s and port %d: %s\n", + host_name, port, strerror(errno)); return econn_refuse_state; break; default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ @@ -262,7 +268,11 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) } } else { - printf ("%s\n", strerror(errno)); + if (is_socket) + printf("connect to socket %s: %s\n", host_name, strerror(errno)); + else + printf("connect to address %s and port %d: %s\n", + host_name, port, strerror(errno)); return STATE_CRITICAL; } } -- cgit v1.2.3-74-g34f1 From fc2c099d58eeb32350a6b147db067d179d8debb6 Mon Sep 17 00:00:00 2001 From: abrist Date: Mon, 19 May 2014 16:16:40 -0400 Subject: netutils.c - A few more changes Changed bool to short. Removed first instance of is_socket to avoid redeclaration error. Changed 'socket' to 'file socket' for verbosity. --- plugins/netutils.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'plugins/netutils.c') diff --git a/plugins/netutils.c b/plugins/netutils.c index 48042188..83f8942f 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -167,10 +167,9 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) char port_str[6], host[MAX_HOST_ADDRESS_LENGTH]; size_t len; int socktype, result; - bool is_socket; + short is_socket = (host_name[0] == '/'); socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; - bool is_socket = (host_name[0] == '/'); /* as long as it doesn't start with a '/', it's assumed a host or ip */ if (!is_socket){ @@ -256,7 +255,7 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) break; case STATE_CRITICAL: /* user did not set econn_refuse_state */ if (is_socket) - printf("connect to socket %s: %s\n", host_name, strerror(errno)); + printf("connect to file socket %s: %s\n", host_name, strerror(errno)); else printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); @@ -269,7 +268,7 @@ np_net_connect (const char *host_name, int port, int *sd, int proto) } else { if (is_socket) - printf("connect to socket %s: %s\n", host_name, strerror(errno)); + printf("connect to file socket %s: %s\n", host_name, strerror(errno)); else printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); -- cgit v1.2.3-74-g34f1