Home: Perl Programming Help: Beginner:
Script hanging due to unresponsive server

New User

Dec 21, 2012, 7:40 AM

Views: 2929
Script hanging due to unresponsive server

I was hoping for some help with this script I am trying to get working. Basically I want the script to first ping my servers, and if the response is "no" then i want to skip that one and continue the loop.

Currently what happens is it gets to one server that is offline, and it will not skip it. I have tried a few different ways around this, but here is my current code:

$cas = 31; 
while ($cas++ <=58)
$ping = `ping cas$cas| awk '{print \$1}'`;
print "ping test=$ping\n";

if ($ping eq "no")
if ($ping ne "no")

# This gets hostname from each server
($shostname, $nl) = split(/\n/, `ssh cas$cas "hostname"`);

# This will get the CPU Utilzation
($mpstat, $nl) = split(/\n/, `ssh cas$cas "/usr/bin/mpstat | grep -A 5 "%idle" | tail -1"`);
($a,$b,$c,$d,$e,$f,$g,$h,$cpu) = split(/\s+/,$mpstat);
$cputil = 100 - $cpu;
$cputil1 = sprintf "%.2f", $cputil;
#print "$cputil1 \n";

# This uses the uptime command to get the load average
($uptime, $nl) = split(/\n/, `ssh cas$cas "uptime"`);
($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$load) = split(/\s+/,$uptime);
($loadavg, $nl) = split(/\,/, $load);
#print "$loadavg \n";

# This uses the free command to get used and total memory and calculate the utilization
($free, $nl) = split(/\n/, `ssh cas$cas "free -tm"|grep Total`);
($a,$total,$used,$free) = split(/\s+/,$free);
$mem = ($used / $total) * 100;
$memutil = sprintf "%.2f", $mem;
#print "$memutil \n";

print "$shostname,$cputil1,$loadavg,$memutil\n";

# $cas++;
print "done \n";

Any suggestions are welcome, I feel like I might be close to getting it to work, but I just am missing something.

Veteran / Moderator

Dec 21, 2012, 8:10 AM

Views: 2926
Re: [rayTX] Script hanging due to unresponsive server

The approach I'd take would be to use the Net::Ping module rather than shelling out to the ping command. This would avoid the need to use a while/continue block and instead use a simple for loop.

I'd also take each of your tasks (CPU Utilzation, uptime, etc) and turn them into subroutine calls.

Your split statements coould be cleaned up. There's no need to create those single letter vars which are never used. Instead use an array slice on the split.

my ($cpu) = (split(/\s+/,$mpstat))[8];

New User

Dec 21, 2012, 8:17 AM

Views: 2924
Re: [FishMonger] Script hanging due to unresponsive server

Great thanks I will look into this. :)