Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Nodes in some load balanced web servers were taking turns going down. They were taking too long to respond to TCP connections.

I didn't have any graphing or monitoring. I wrote something quickly to tell me when a server was taking too long to respond. Then I could login and debug.

This program prints a warning when the connection does not complete in 1 second. I also added a signal handler to dump a summary of the connection statistics.

Update: The code is on github.

Sample output:
$ ./sas-monitoring ./sas-monitoring is starting as pid 27925. From another window you ma +y run: kill -USR1 27925 ...................................................................... +..................................................................... +......Could not create socket to : Connection timed out -- 8< -- output removed here -- 8< -- failures: 103 successes: 1092 failures: 79 successes: 1116 failures: 78 successes: 1117 failures: 29 successes: 1166 failures: 28 successes: 1167 failures: 6 successes: 1189 failures: 3 successes: 1192 failures: 2 successes: 1193 failures: 1 successes: 1194 failures: 1 successes: 1194 failures: 0 successes: 1195
The code:
#!/usr/bin/perl -w # # file: sas-monitoring # purpose: monitor sas web servers use strict; use Data::Dumper; use IO::Socket; my @WEB_SERVERS = qw( ); my $TIMEOUT_SECONDS = 1; my $SERVER_PORT = 80; # -- main -- my %connection_stats; # for tracking connection success/failure counts for (@WEB_SERVERS) { $connection_stats{$_} = {}; $connection_stats{$_}{success} = 0; $connection_stats{$_}{failure} = 0; } # GOAL : give the user a way to read connect stats via the USR1 signal +. # DOC : this signal will interrupt the socket connect call and count a +s a # failure $SIG{'USR1'} = sub { my @ip_list = keys %connection_stats; @ip_list = sort { $connection_stats{$b}{failure} <=> $connection_stats{$a}{fai +lure} } @ip_list; print "\n"; for my $i (@ip_list) { print sprintf("%16s", $i), ": failures: ", $connection_stats{$i}{failure}, " successes: ", $connection_stats{$i}{success}, "\n"; } }; print $0 ," is starting as pid ", $$ , ". From another window you may run:\n kill -USR1 ", $$, "\n"; while (1) { # GOAL : try to connect to the server SERVER: for my $server_ip (@WEB_SERVERS) { my $sock = new IO::Socket::INET ( PeerAddr => $server_ip, PeerPort => $SERVER_PORT, Proto => 'tcp', Timeout => $TIMEOUT_SECONDS, ); unless ($sock) { warn "Could not create socket to $server_ip : $!\n"; $connection_stats{$server_ip}{failure} ++; next SERVER; } $connection_stats{$server_ip}{success} ++; close($sock); syswrite(STDOUT, '.', 1); } # wait a second then check again #syswrite(STDOUT, "\n", 1); sleep(1); }

In reply to Quick and dirty http server connection status checker by superfrink

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (2)
    As of 2018-01-21 03:34 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (227 votes). Check out past polls.