Hi all. I wrote a script for my hosts which collects data and send it to a server. Script opens network socket and wait for a connection from monitoring application. After connection my script starts to send data to an open socket. The issue is that if remote side disconnected, script returns "Broken pipe" and die while I suppose it to wait connection again. Could someone help with it ? Here is the snippet:

#!/usr/bin/env perl use 5.010; use warnings; use strict; use Net::Ping; use Getopt::Std; use IO::Socket; my $proto; # ping protocol my $host; # remote host my $bind_addr; # local ip interface to bind my $bind_port; # local port to bind my $monitoring_node; # monitoring status node my $monitoring_name; # cantain formatted remote host name to +insert into a monitoring node my %options=(); # hash to work with options my $result; # ping probe result my $latency; # latency to remote host my $sock; # network sockect object my $agent_socket; # network sockect object my $interval; # delay between checks $|=1; # Getting options getopts("p:h::n:i:s:d:", \%options); sub CheckArgs() { # Checking for non-mandatory options if (defined ($options{p})) { $proto = $options{p}; } else { $p +roto = "tcp"; }; if (defined ($options{i})) { $bind_addr = $options{i}; } else +{ $bind_addr="0.0.0.0"; }; if (defined ($options{s})) { $bind_port = $options{s}; } else +{ $bind_port = "9906"; }; if (defined ($options{d})) { $interval = $options{d}; } else { + $interval = "5"; }; # Checking for mandatory options if (defined ($options{h}) && defined ($options{n})) { $host = $options{h}; $monitoring_node = $options{n}; &Main; } else { &PrintHelp; exit 1; } } sub PrintHelp() { print "Latency monitor v1.5.1\nOptions:\n -p Ping protocol +to use: tcp(default), udp, icmp(requires root)\n -h Destination ho +st. IP or DNS name can be specified. Mandatory option, no have defaul +t value\n -n status node to report. Mandatory option, no default v +alue\n -d Delay between checks in seconds. Default value 5\n -i + IP interface to bind. Default value 0.0.0.0\n -s Socket to bind. +Default value 9906\n"; exit; } sub Main() { # New network socket instance $sock = new IO::Socket::INET ( LocalHost => "$bind_addr", LocalPort => "$bind_port", Proto => 'tcp', Listen => 5, Type => SOCK_STREAM, Reuse => 1 ) or die "Can't create socket: $!\n"; # New ping instance my $ping = Net::Ping->new("$proto") or die "Can't initialize ping: $!\n"; $monitoring_name = $host; $monitoring_name =~ s/\./-/; $agent_socket = $sock->accept(); # Main loop while (42) { ($result,$latency) = $ping->ping($host); $latency *= 1000; $latency =~ s/\.\d+//; print $agent_socket $monitoring_node . ".lmonitor." . +$monitoring_name . ".rtt.value=" . $latency . "\n"; sleep $interval; } $sock->close; } &CheckArgs;

In reply to IO::Socket reconnect by nikita.af

Title:
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, details, 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, summary, 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.