in reply to Trying to get alarm to work...
Thanks for the suggestions. I got it to work. Final solution is below.
Thanks!
-fiddler42
use Mail::Mailer; use Term::Cap; use POSIX; my $TermIOs = new POSIX::Termios; $TermIOs->getattr; my $OSpeed = $TermIOs->getospeed; my $t = Tgetent Term::Cap { TERM => undef, OSPEED => $OSpeed }; (${norm}, ${bold}, ${under}) = map { $t->Tputs($_,1) } qw/me md us/; $Timeout = 5; $Home = "/path/to/my/home"; my @MasterMachineIndex = qw (host1 host host3); foreach $Host (@MasterMachineIndex) { &ping; $UptimeCheck = "rsh $Host uptime"; my $UptimeResult = ""; eval { local $SIG{ALRM} = sub { die "Alarm!\n" }; alarm $Timeout; $UptimeResult = `$UptimeCheck`; alarm 0; }; if ($@) { ($@ eq "Alarm!\n") or die ("Unexpected error...\n"); print ("${bold}Problems accessing $Host.${norm} Skipping...\n +"); next; } else { $CheckHome = ("rsh $Host \"cd $Home\""); $HomeStatus = `$CheckHome 2>&1`; # This captures STDERR and + STDOUT outputs that may or may not manifest... if ($HomeStatus =~ /No such/) { # May need to add more +keywords here. Not sure all OSs echo same nomenclature... print ("${bold}Home directory not mounted on $Host!${norm} + Skipping...\n"); } else { # e.g. auto-e +mail admin? $CPUScan = "rsh $Host \"~/perl/CPUScan.pl >> ~/cpu.stats\" +"; $CHECK = `$CPUScan`; print ("$Host:\t$UptimeResult"); } } } sub ping { my $os = `uname -a`; if ($os =~ /SunOS/) { # For the line below, bytes transmitted will be 56+8=64 and 1 += # of transactions my $p = `ping -s $Host 56 1 | grep 'packet loss'`; unless ($p =~ /(\d+)% packet loss/) { print ("${bold}Bad Response from $Host.${norm} Skipping.. +.\n"); next; } } elsif ($os =~ /Linux/) { my $p = `ping -c 1 -t 1 $Host | grep 'packet loss'`; unless ($p =~ /(\d+)% packet loss/) { print ("${bold}Bad Response from $Host.${norm} Skipping.. +.\n"); next; } } my $loss = $1; if ($loss > 50) { print ("${bold}High packet loss from $Host.${norm} Skipping.. +.\n"); next; } }
|
|---|