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;