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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



rayTX
New User

Dec 21, 2012, 7:40 AM

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

Hello,
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:


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

if ($ping eq "no")
{
next;
}
}
continue
{
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";
}


}
#continue
#{
# $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.


FishMonger
Veteran / Moderator

Dec 21, 2012, 8:10 AM

Post #2 of 3 (1100 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.
http://search.cpan.org/~smpeters/Net-Ping-2.36/lib/Net/Ping.pm

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.

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



rayTX
New User

Dec 21, 2012, 8:17 AM

Post #3 of 3 (1098 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