Code

Reference to web site to get NSClient
[nagiosplug.git] / plugins / netutils.c
index 4234c4364d333f1cb00b39ad2954a60fed09da62..ce8b428b2ec8009680c8d36f980bb8e3e2a9d425 100644 (file)
 *
 ****************************************************************************/
 
+#include "common.h"
 #include "netutils.h"
 
 int socket_timeout = DEFAULT_SOCKET_TIMEOUT; 
+int econn_refuse_state = STATE_CRITICAL;
+int was_refused = FALSE;
 
 /* handles socket timeouts */
 void
@@ -275,8 +278,22 @@ my_connect (char *host_name, int port, int *sd, int proto)
                        /* attempt to open a connection */
                        result = connect (*sd, res->ai_addr, res->ai_addrlen);
 
-                       if (result == 0)
+                       if (result == 0) {
+                               was_refused = FALSE;
                                break;
+                       }
+
+                       if (result < 0) {
+                               switch (errno) {
+                                       case ECONNREFUSED:
+                                               switch (econn_refuse_state) {
+                                                       case STATE_OK:
+                                                       case STATE_WARNING:
+                                                               was_refused = TRUE;
+                                               }
+                                               break;
+                               }
+                       }
 
                        close (*sd);
                        res = res->ai_next;
@@ -286,6 +303,8 @@ my_connect (char *host_name, int port, int *sd, int proto)
 
        if (result == 0)
                return STATE_OK;
+       else if (was_refused)
+               return econn_refuse_state;
        else {
                printf ("%s\n", strerror(errno));
                return STATE_CRITICAL;