#!/usr/local/bin/perl -w
# -*- perl -*-
#
# $Id: pingomatic,v 1.13 2006/05/02 21:20:10 eserte Exp $
# Author: Slaven Rezic
#
# Copyright (C) 1999-2006 Slaven Rezic. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: slaven@rezic.de
# WWW: http://www.sourceforge.net/projects/srezic
#
use Event;
use IO::Pipe;
use Getopt::Long;
use Term::ReadKey;
use Term::ReadLine;
use Sys::Hostname;
use strict;
use vars qw($VERSION @nameserver %pipe);
$VERSION = sprintf("%d.%03d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/);
{
package
IO::Pipe::End;
sub pid {
my $me = shift;
${*$me}{'io_pipe_pid'};
}
}
######################################################################
{
package PingDef;
use constant MAXTIME => 20000;
use vars qw(%try_later);
sub new {
my $self = { @_[1..$#_] };
$self->{Val} = [];
bless $self, $_[0];
}
sub host { $_[0]->{Host} }
sub type { $_[0]->{Type} }
sub active { !$_[0]->{Canceled} }
sub time {
my($self, $n, $as_string) = @_;
my $timesum = 0;
my $count = 0;
my $start = $#{$self->{Val}}-$n+1;
$start = 0 if ($start < 0) ;
for(my $i=$start; $i<=$#{$self->{Val}}; $i++) {
$timesum+=$self->{Val}[$i]{Time};
$count++;
}
my $time = ($count ? $timesum/$count : undef);
if ($as_string && !defined $time) {
"?"
} elsif ($as_string && $time == MAXTIME) {
"unreach."
} else {
$time;
}
}
sub add {
my($self, $line) = @_;
if ($line =~ /icmp_seq=([\d.]+).*time=([\d.]+)/) {
my($seq, $time) = ($1, $2);
## noch nicht korrekt...
# my $last_line = $self->{Val}[$#{$self->{Val}}];
# if (defined $last_line && defined $last_line->{Seq}) {
# foreach ($last_line->{Seq} .. $seq-1) { # verlorene Pakete
# push @{ $self->{Val} }, {Time => MAXTIME};
# }
# }
push @{ $self->{Val} }, {Seq => $seq,
Time => $time};
$self->{Canceled} = 0;
} elsif ($line =~ /ret=-1/) {
push @{ $self->{Val} }, {Time => MAXTIME}; #XXX
$self->{Canceled} = 1;
} else {
warn "Can't parse $line";
}
shift @{ $self->{Val} } if (@{ $self->{Val} } > 10);
}
sub addempty {
my($self) = @_;
push @{ $self->{Val} }, {Time => MAXTIME}; #XXX
shift @{ $self->{Val} } if (@{ $self->{Val} } > 10);
=1= |