Perl and vmstat

U

usaims

Hello:

I am trying to write a small perl script that parses the "us"
column of the vmstat command and if that number is greater or equal to
3, it will notify me. The script does run, but I'm not getting any
output.

#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
my @memusage = `vmstat -n 1 | awk '{print \$14}'`;

foreach ( my $list = (@memusage) ) {
if ( $list <= $test ) {
print "Failure\n";
}
}

I can get vmstat to output to screen by using:
#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
my @memusage = system("vmstat -n 1 | awk '{print \$14}'");

foreach ( my $list = (@memusage) ) {
if ( $list <= $test ) {
print "Failure\n";
}
}

But it outputs the vmstat stats but it doesn't evaluate the foreach
statement. I have googled for "vmstat" in the perl group, but
didn't come up with anything. I have searched perldoc, didn't see
anything. Please direct me to a link or a perldoc source or suggest an
alternative. If I have missed something in FAQ, apologies in advance.
Thanks in advance.

usaims
 
G

Glenn Jackman

At 2005-04-20 03:40PM said:
Hello:

I am trying to write a small perl script that parses the "us"
column of the vmstat command and if that number is greater or equal to
3, it will notify me. The script does run, but I'm not getting any
output.

#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
my @memusage = `vmstat -n 1 | awk '{print \$14}'`;

foreach ( my $list = (@memusage) ) {
if ( $list <= $test ) {
print "Failure\n";
}
}

You should let perl do awk's work:

my $test = 3;
my @output = `vmstat -n 1`;
# the last line of the vmstat output is $output[-1]
my $us = ( split ' ', $output[-1] )[13];
print "Failure\n" if $us >= $test;
I can get vmstat to output to screen by using:

That's because system() does not capture the output like `backticks` do.
 
M

Mark Clements

usaims said:
Hello:

I am trying to write a small perl script that parses the "us"
column of the vmstat command and if that number is greater or equal to
3, it will notify me. The script does run, but I'm not getting any
output.

#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
my @memusage = `vmstat -n 1 | awk '{print \$14}'`;

foreach ( my $list = (@memusage) ) {
if ( $list <= $test ) {
print "Failure\n";
}
}

I can get vmstat to output to screen by using:
#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
my @memusage = system("vmstat -n 1 | awk '{print \$14}'");

foreach ( my $list = (@memusage) ) {


notwithstanding any other issues, your foreach is wrong. Try:

foreach my $list(@memusage){

....
 
T

Tad McClellan

usaims said:
my @memusage = system("vmstat -n 1 | awk '{print \$14}'");


Read up on the return value that you get from system().

Why do you want to save that to an array?

I have googled for "vmstat" in the perl group, but
didn't come up with anything.


Of course not, since you would have the same problem if you
were running a program that was not named "vmstat".

I have searched perldoc, didn't see
anything.


perldoc -f system
 
J

Josef Moellers

usaims said:
Hello:

I am trying to write a small perl script that parses the "us"
column of the vmstat command and if that number is greater or equal to
3, it will notify me. The script does run, but I'm not getting any
output.

#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
my @memusage = `vmstat -n 1 | awk '{print \$14}'`;

awk: cmd. line:1: {print \$14}
awk: cmd. line:1: ^ backslash not last character on line

The single quotes will prevent the \ from preventing the $ to be a
special character.

All other comments nonewithstanding,

Josef
 
J

Joe Smith

usaims said:
my @memusage = system("vmstat -n 1 | awk '{print \$14}'");

If I have missed something in FAQ, apologies in advance.

The behavior of system() is a FAQ.

unix% perldoc -q "output of a command"

-Joe
 
T

Tad McClellan

usaims said:
No reason,


Then why are you doing it?

There must be *some* reason why you thought that that was useful...



If you had read up on the return value from system() you could
answer that yourself.

Is there a better way?

Yes.



Please do not quote .sigs, it is poor netiquette.

Have you seen the Posting Guidelines that are posted here frequently?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top