CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

Dec 21, 2012, 7:40 AM

Post #1 of 3 (2602 views)
Script hanging due to unresponsive server Can't Post

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

Post #2 of 3 (2599 views)
Re: [rayTX] Script hanging due to unresponsive server [In reply to] Can't Post

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

Post #3 of 3 (2597 views)
Re: [FishMonger] Script hanging due to unresponsive server [In reply to] Can't Post

Great thanks I will look into this. :)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives