Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Call vmstat remotely

by e123 (Initiate)
on Sep 25, 2014 at 22:53 UTC ( [id://1102057]=perlquestion: print w/replies, xml ) Need Help??

e123 has asked for the wisdom of the Perl Monks concerning the following question:

I have specific requirement to call vmstat remotely on linux machine and get the output in format so that each of cpu usage can be extracted from the command. I have to run this 3 times and get average result. What is the best way to implement it. The main thing here is i need to do this so that it is exected remotely and results are available in a variable so that it can be appropriately displayed.

Replies are listed 'Best First'.
Re: Call vmstat remotely
by thanos1983 (Parson) on Sep 25, 2014 at 23:46 UTC

    Hello e123,

    Welcome to the community.

    If I understand correctly you want to execute a command remotely to a linux host and get the output. A possible solution would be ssh with Net::OpenSSH module.

    Update: Sorry I forgot completely the part that you need to make it 3 times and take the average.

    Update 2: I thought a bit about it and I came up with a more programmable solution. I am updating the code, I hope you are still following your question. From my point of view this a more correct answer because you are applying 3 times the same command and you force the system to process the command 3 times.

    Updated code: Sample of code with command running on my OS

    #!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Data::Dumper; my $host = "127.0.0.1"; # remote IP change my $port = 22; # ssh default port my $passwd = "password"; my $user = "username"; my %opts = ( passwd => $passwd, port => $port, user => $user ); sub loop { my $ssh = Net::OpenSSH->new( $host , %opts ); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; my @final = (); for (1..3) { $ssh->system("vmstat") or die "remote command failed: " . $ssh->error; my @output = $ssh->capture("vmstat"); $ssh->error and die "remote ls command failed: " . $ssh->error; push (@final,@output); } chomp(@final); return @final; } # end sub loop my @array = loop(); print Dumper(\@array); __END__ procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 1 0 341104 448888 57136 1158016 5 23 85 85 560 1618 23 + 5 71 1 0 procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 2 0 341104 448504 57136 1158016 5 23 85 85 560 1618 23 + 5 71 1 0 procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 1 0 341104 459728 57136 1147676 5 23 85 85 560 1618 23 + 5 71 1 0 $VAR1 = [ 'procs -----------memory---------- ---swap-- -----io---- -sy +stem-- ------cpu-----', ' r b swpd free buff cache si so bi bo i +n cs us sy id wa st', ' 2 0 341104 448472 57136 1158016 5 23 85 85 5 +60 1618 23 5 71 1 0', 'procs -----------memory---------- ---swap-- -----io---- -sy +stem-- ------cpu-----', ' r b swpd free buff cache si so bi bo i +n cs us sy id wa st', ' 1 0 341104 459312 57136 1147676 5 23 85 85 5 +60 1618 23 5 71 1 0', 'procs -----------memory---------- ---swap-- -----io---- -sy +stem-- ------cpu-----', ' r b swpd free buff cache si so bi bo i +n cs us sy id wa st', ' 1 0 341104 459568 57136 1147676 5 23 85 85 5 +60 1618 23 5 71 1 0' ];

    I am not calculating the average for you I left this part so you can play around and practice a bit. ;)

    I execute the code on my localhost so I see double the output because the actual command is executed on my terminal. Which will happen the same on the remote terminal.

    Hope this helps, let me know how it worked for you.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1102057]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found