For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2006 > cpu idle measurement: sar/vmstat









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author cpu idle measurement: sar/vmstat
inetquestion

2006-10-23, 6:59 pm

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

Charles DeRykus

2006-10-30, 7:09 pm

inetquestion wrote:
> 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,
--
Charles DeRykus
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com