Solved

Require Perl script to get system load and uptime on remote UNIX hosts

Posted on 2010-08-13
10
1,759 Views
Last Modified: 2013-12-27
I need help in executing the uptime on remote unix servers mainly linux and solaris as well as sun containers. I am able to get the uptime on local system but when I execute the code with arguments  I am unable to get the logic right. I need to pass arguments with perl script which i have to integrate with a monitoring tool to set the threshold.


#!/usr/bin/perl -w



 # my $MYHOME=$ENV($MYHOME);

 # $MYHOME="/home/anil" if (not $MYHOME);

 open(LOG, ">/tmp/uptime.pl.log") or die "Can't open file: $!";

 my $ScriptDir="/home/anil";

 print LOG  "Arg1=$ARGV[0], Arg2=$ARGV[1], Arg3=$ARGV[2], Arg4=$ARGV[3]\n";



 exit if not ($ARGV[0] eq "-stat" || $ARGV[0] eq "-config");



  my $ip=$ARGV[3];

  exit 1 if (not $ip);

  

  my $user="root";

  $user=$ARGV[4] if ($ARGV[4]);

  #my @result;

  my $keyfile="/root/.ssh/id_rsa";

	sub query {

	open (RESULT, "ssh 2>/dev/null -l $user -i $keyfile -oStrictHostKeyChecking=no -oBatchMode=yes $ip $cmd |") || die "can't connect to $ip";

	@result = <RESULT>;

	close RESULT;

}       

	$os = `uname -s`;



 	if ($os =~ /Linux/)

 {	

     $uptime=`ps -eo pid,comm,etime | grep init`;

 }

 else 

 {

     $uptime=`ps -p 1 -o etime | tail -1`;

 }



 if ($uptime =~ /[0-9]+\-/) 

 {

     ($Days, $Hours, $Minutes, $Seconds) = $uptime =~ /([0-9]*)\-?([0-9]+):([0-9]+):([0-9]+)/;

 } 

 elsif  

     ($uptime =~ /[0-9]+:[0-9]+:[0-9]+/) 

 {

     ($Hours, $Minutes, $Seconds) = $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;

      $Days = 0;

 } 

 else 

 {

     ($Minutes, $Seconds) = $uptime =~ /([0-9]+):([0-9]+)/;

      $Days = 0;

      $Hours = 0;

 }

  query();

    for each $_ (@result) {

     $t_uptime = eval($Days*24*60 + $Hours*60 + $Minutes);

    } 

     print ("uptime: $t_uptime \n");

Open in new window

0
Comment
Question by:amk2121
10 Comments
 

Author Comment

by:amk2121
ID: 33434887
I require a solution to the above. I have the script which runs when the remote execution code from the perl script is removed.. I need to execute the script on a remote Unix server (Linux /Solaris) using ssh and secure RSA key to first get the uptime and convert the uptime into total minutes.

Please let me know if you require more information
0
 
LVL 28

Expert Comment

by:Jan Springer
ID: 33436486
Please give an example of some arguments.  If I'm understanding the question properly, you may need to "shift" in the beginning of your script.
0
 

Author Comment

by:amk2121
ID: 33439250
the script will remotely log into a target server and report the following data by printing it to stdout

Response Time=NNN  (in seconds)
Uptime=NNN  (in minutes)
System Load=NN.mm (float number)
AVAILABILITY=   (0 or 100)

The script will have input argument specifying desired timeout in seconds
If operation can not be completed in that time then AVAILABILITY reported should be set to 0
The script must finish after TimeOut seconds and should not be hanging around waiting for connection (hint: use timeout parameter to ssh command).


The script will be called with these arguments:

ScriptName Arg1 Arg2 TimeOut TargetServer UserName
 
You can ignore Arg1 and Arg2 but they will be there!
0
 
LVL 10

Expert Comment

by:APNFSSC
ID: 33460183
have a look at Net::SSH to run the remote commands... also I'm not sure if this is true on all nix systems but /proc/uptime contains the uptime in seconds if that is any help.

Also have a look at Getopt::Long to help with handling the arguments that your passing through.

As for the timeout I'm not sure how you can handle that... i think the ssh will just time out by itself if it cannot connect.
0
 
LVL 3

Expert Comment

by:ckhsu1977
ID: 33490535
a bit curious about your script.
Regardless of what user you set $user, you'll still use /root/.ssh/id_rsa as the private key. Seems odd.
How are you passing and what commands are you passing for $cmd in the subroutine query?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:amk2121
ID: 33518340
I got the script working and yes there was a wrong logic expressed in my code snippet above.

Thanks for all your support
0
 
LVL 10

Expert Comment

by:APNFSSC
ID: 33518455
What did you end out altering that made it work?
0
 

Accepted Solution

by:
amk2121 earned 0 total points
ID: 33541096
I changed the script and used a logic pertaining to my requirement.

$user = "root";
$ips=$ARGV[0];                    ## this holds the command needs to run on remote host.
@ipary= split(/\,/,$ips);       ## split if there are more then one host
for $ipary(@ipary){

print ("\n");
print "connecting ......$ipary \n";
$CMD="ps -eo pid,comm,etime | grep init > xyz.dat";
$IDENT="/root/.ssh/id_rsa";
system("ssh $user\@$ipary -i $IDENT -o ConnectTimeout=$ARGV[1]  $CMD");    ## execute ssh command
$uptime=`cat xyz.dat | tail -1`;
0
 

Author Comment

by:amk2121
ID: 34497438
all went well and my script works good
0
 

Author Closing Comment

by:amk2121
ID: 34860165
I got help from others and made changes to the script which ultimately worked
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now