Created
April 3, 2013 09:37
-
-
Save morsik/5299783 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
#!/usr/bin/perl -w | |
use strict; | |
use warnings; | |
use Net::Ping; | |
my @hosts = ( | |
'localhost.localdomain', | |
); | |
my @states = (); | |
sub printxy($$$) | |
{ | |
my ($x, $y, $txt) = @_; | |
print "\033[".$x.";".$y."H\033[0m$txt\n"; | |
} | |
sub updatestatus($$) | |
{ | |
my ($id, $status) = @_; | |
printxy($id + 1, 3, $states[$id]->{'hostname'}); | |
if (defined($status) and $status == 1) | |
{ | |
printxy($id + 1, 24, "\033[1;32m UP \033[0m"); | |
if ($states[$id]->{'status'} != 1) | |
{ | |
$states[$id]->{'status'} = 1; | |
printxy($id + 1, 32, `ssh -oBatchMode=yes root\@$states[$id]->{'hostname'} "[ -e ] && cat /etc/gentoo-release || cat /etc/centos-release || cat /etc/debian-release || cat /etc/linux-release" 2>/dev/null`); | |
} | |
} | |
else | |
{ | |
printxy($id + 1, 24, "\033[1;31m DOWN \033[0m"); | |
if ($states[$id]->{'status'} != 0) | |
{ | |
$states[$id]->{'status'} = 0; | |
printxy($id + 1, 32, "\033[0K"); | |
} | |
} | |
} | |
# prepare states | |
foreach my $host (@hosts) | |
{ | |
my %state = ( | |
'hostname' => $host, | |
'status' => -1, | |
); | |
push(@states, \%state); | |
} | |
print "\033[2J\033[H\033[?25l"; | |
while (1) | |
{ | |
for my $i (0 .. $#states) | |
{ | |
my $p = Net::Ping->new(); | |
my $status = $p->ping($states[$i]->{'hostname'}, 0.05); | |
$p->close(); | |
updatestatus($i, $status); | |
} | |
sleep 5; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment