$update_w->interval($update);
}
}
}
}
sub add_host {
my @stopped;
foreach (Event::all_running()) {
$_->stop;
push @stopped, $_;
}
ReadMode 0;
my $OUT = $term->OUT || \*STDOUT;
print $OUT "\n";
my $line = $term->readline("add host: "); # Can't use "\n" in prompt
if (defined $line and $line !~ /^\s*$/) {
my($host, $type) = split /[\t:]/, $line;
new_ping([$host, $type]);
}
ReadMode 3;
$_->start foreach @stopped;
}
sub new_ping {
my $host = shift;
my $type;
if (ref $host eq 'ARRAY') {
($host, $type) = @$host;
$type = undef if defined $type && $type =~ /^\s*$/;
}
my $host_type = $host . (defined $type ? ":$type" : "");
my $pipe = $pipe{$host_type} = new IO::Pipe;
if (!defined $type || $type eq 'ping') {
my @cmd;
if ($^O eq 'solaris') {
@cmd = ('ping', '-s', $host);
} else {
@cmd = ('ping', $host);
}
$pipe{$host_type}->reader(@cmd);
} else {
my $pid = fork;
if (!$pid) {
$pipe->writer();
$pipe->autoflush(1);
require Net::Ping;
my($port, $protocol) = $type =~ m{^(.*)/(.*)$};
if (!defined $port) {
$port = $type;
}
if (!defined $protocol) {
$protocol = "tcp";
}
my $p = Net::Ping->new($protocol);
if ($type =~ /^\d+/) {
$p->{port_num} = $port;
} else {
$p->{port_num} = getservbyname($port, $protocol);
}
$p->hires();
my $seq = 0;
while(1) {
my($ret, $duration, $ip) = $p->ping($host, 10);
if ($ret) {
$duration = sprintf "%.3f ms", 1000 * $duration;
print $pipe <<EOF;
answer from $ip: icmp_seq=$seq ttl=??? time=$duration
EOF
}
sleep(1);
}
die "Never reached";
}
$pipe->reader;
}
my $pingdef = new PingDef Host => $host, Type => $type;
$pingdefs{$host_type} = $pingdef;
Event->io(
fd => $pipe,
poll => 'r',
timeout => 5,
repeat => 1,
desc => $host_type,
cb => \&get_ping_line,
);
}
sub pingomatic_host {
my $line = shift;
if (my($host, $type) = split /[\t:]/, $line) {
[$host, $type];
} else {
$line;
=5= |