Code

Applied patch #660973 for tcp refusals
authorJeremy T. Bouse <undrgrid@users.sourceforge.net>
Sun, 16 Mar 2003 23:28:49 +0000 (23:28 +0000)
committerJeremy T. Bouse <undrgrid@users.sourceforge.net>
Sun, 16 Mar 2003 23:28:49 +0000 (23:28 +0000)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@426 f882894a-f735-0410-b71e-b25c423dba1c

plugins/check_tcp.c
plugins/netutils.c
plugins/netutils.h

index 7d1155136c74f95f43e81313ad6512ff54d33809..5bec2915108cb52f66a44cc5b954791cce462a63 100644 (file)
@@ -28,7 +28,7 @@ This plugin tests %s connections with the specified host.\n";
 
 const char *option_summary = "\
 -H host -p port [-w warn_time] [-c crit_time] [-s send]\n\
-       [-e expect] [-W wait] [-t to_sec] [-v]\n";
+       [-e expect] [-W wait] [-t to_sec] [-r refuse_state] [-v]\n";
 
 const char *options = "\
  -H, --hostname=ADDRESS\n\
@@ -48,6 +48,8 @@ const char *options = "\
     Response time to result in critical status (seconds)\n\
  -t, --timeout=INTEGER\n\
     Seconds before connection times out (default: %d)\n\
+ -r, --refuse=ok|warn|crit\n\
+    Accept tcp refusals with states ok, warn, crit (default: crit)\n\
  -v\n\
     Show details for command-line debugging (do not use with nagios server)\n\
  -h, --help\n\
@@ -337,9 +339,11 @@ main (int argc, char **argv)
        alarm (0);
 
        printf
-               ("%s %s - %.3f second response time on port %d",
+               ("%s %s%s - %.3f second response time on port %d",
                 SERVICE,
-                state_text (result), elapsed_time, server_port);
+                state_text (result),
+                (was_refused) ? " (refused)" : "",
+                elapsed_time, server_port);
 
        if (status && strlen(status) > 0)
                printf (" [%s]", status);
@@ -375,6 +379,7 @@ process_arguments (int argc, char **argv)
                {"expect", required_argument, 0, 'e'},
                {"quit", required_argument, 0, 'q'},
                {"delay", required_argument, 0, 'd'},
+               {"refuse", required_argument, 0, 'r'},
                {"verbose", no_argument, 0, 'v'},
                {"version", no_argument, 0, 'V'},
                {"help", no_argument, 0, 'h'},
@@ -402,7 +407,7 @@ process_arguments (int argc, char **argv)
        }
 
        while (1) {
-               c = getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S", long_options,
+               c = getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:Sr:", long_options,
                                                                         &option_index);
 
                if (c == -1 || c == EOF || c == 1)
@@ -471,6 +476,16 @@ process_arguments (int argc, char **argv)
                case 'q':
                        server_quit = optarg;
                        break;
+               case 'r':
+                       if (!strncmp(optarg,"ok",2))
+                               econn_refuse_state = STATE_OK;
+                       else if (!strncmp(optarg,"warn",4))
+                               econn_refuse_state = STATE_WARNING;
+                       else if (!strncmp(optarg,"crit",4))
+                               econn_refuse_state = STATE_CRITICAL;
+                       else
+                               usage ("Refuse mut be one of ok, warn, crit\n");
+                       break;
                case 'd':
                        if (is_intpos (optarg))
                                delay = atoi (optarg);
@@ -541,7 +556,7 @@ connect_SSL (void)
   time (&start_time);
 
   /* Make TCP connection */
-  if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK)
+  if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
     {
     /* Do the SSL handshake */
       if ((ssl = SSL_new (ctx)) != NULL)
index 4234c4364d333f1cb00b39ad2954a60fed09da62..b3348d6ede0ad5c38e27f75697a0dedba5dc0c2d 100644 (file)
@@ -32,6 +32,8 @@
 #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 +277,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 +302,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;
index 6c8eed36bf0e5be451367802840e585af399c02c..22542a77ac16cf15c2f032d3d3108d946c6d1786 100644 (file)
@@ -60,3 +60,5 @@ int is_inet6_addr (char *);
 int is_hostname (char *);
 
 extern int socket_timeout;
+extern int econn_refuse_state;
+extern int was_refused;