Skip to content

Instantly share code, notes, and snippets.

@walf443 walf443/perlbal-top
Created Jan 21, 2010

Embed
What would you like to do?
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
You can’t perform that action at this time.