Created
January 21, 2010 08:11
-
-
Save walf443/282658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use IO::Socket::INET; | |
use Getopt::Long; | |
my $cmd = join q{ }, @ARGV; | |
my $port = 60000; | |
my $host = '127.0.0.1'; | |
my $timeout = 10; | |
GetOptions( | |
'port=i' => \$port, | |
'host=s' => \$host, | |
'timeout=i' => \$timeout, | |
); | |
my $interval_sec = 3; | |
my $ip_of = {}; | |
while (1) { | |
print "\033[2J"; # flush screen | |
my $sock = IO::Socket::INET->new( | |
PeerAddr => $host, | |
PeerPort => $port, | |
Timeout => $timeout, | |
) or die "cannot connect to $host:$port"; | |
$sock->autoflush(1); | |
$sock->write($cmd . "\r\nquit\r\n"); | |
my $out = ''; | |
while ( my $line = <$sock> ) { | |
if ( $line =~ /(\d+\.\d+\.\d+\.\d+):\d+\s+(\d+)/ ) { | |
my $ip = $1; | |
my $count = $2; | |
$ip_of->{$ip} ||= [0,0]; | |
chomp $line; | |
$line = "$line (+ @{[ $count - ( $ip_of->{$ip}->[0] || 0 ) ]})\n"; | |
pop @{$ip_of->{$ip}}; | |
unshift @{$ip_of->{$ip}}, $count; | |
} | |
$out .= $line; | |
} | |
my $sum = 0; | |
my $num_of_server = 0; | |
for my $ip ( keys %{ $ip_of } ) { | |
$sum += ( $ip_of->{$ip}->[0] - $ip_of->{$ip}->[1] ); | |
$num_of_server++; | |
} | |
$out .= sprintf("avg: %.2f\n", $sum / $num_of_server); | |
print $out; | |
$sock->close; | |
sleep($interval_sec); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment