cpu idle measurement: sar/vmstat

I

inetquestion

I can't seem to get rid of the error message if sar doesn't exist in
the path. The redirection of stderr from within the backticks isn't
working via perl. Is there another way to allieviate these errors from
showing up in the console? Any suggestions on how to get cpuidle which
will work on unix or windows?

$CpuAvg=`sar -u 15 2>/dev/null | tail -1 | awk '{print \$5}'`;
if ( $CpuAvg=~/[0-9]*/ ) {
$CpuAvg=`vmstat 15 2 | tail -1 | awk '{print \$22}'`;
}
chomp($CpuAvg);

-Inet
 
C

Charles DeRykus

inetquestion said:
I can't seem to get rid of the error message if sar doesn't exist in
the path. The redirection of stderr from within the backticks isn't
working via perl. Is there another way to allieviate these errors from
showing up in the console? Any suggestions on how to get cpuidle which
will work on unix or windows?

$CpuAvg=`sar -u 15 2>/dev/null | tail -1 | awk '{print \$5}'`;
if ( $CpuAvg=~/[0-9]*/ ) {
$CpuAvg=`vmstat 15 2 | tail -1 | awk '{print \$22}'`;
}
chomp($CpuAvg);

On Unix, STDERR can be redirected but you'd have to pry out any shell
errors because the output would be interleaved. You could also exec
2>/dev/null but I can't imagine that being helpful.

$CpuAvg = `exec 2>&1; sar -u 15 | tail -1 | awk '{print \$22}'`;

IPC::Open3 is another Unix possibility although I don't know how
well it works on other OS's such as Win32:


use IPC::Open3;
# make errors available from /path/to/errfile
open(my $e, ">","/path/to/errfile") or die $!;

$p = open3($w, $r, ">&" . fileno($e), "sar -u 15...." );
....


hth,
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top