Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitoring my server cpu usage

Status
Not open for further replies.

peterneve

Programmer
Jan 24, 2002
50
GB
I am looking for a piece of code that will let me look at the current cpu usage of my server remotely. I am running apache on a windows machine. On a unix machine I would run ps from the command line and go through the results, but is there anything similar for windows?

Thanks in advance
 
Win32::perfLib is what you want; but is it trivial to use? No.....

Documented here file://C:\Perl\html\site\lib\Win32\PerfLib.html on my PC.

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
I found on the net:

use Win32::perfLib;
$processor = 238;
$proctime = 6;
while (6 ne 9){
if(my $perflib = new Win32::perfLib('srv-48')){
$proc_ref0 = {};
$proc_ref1 = {};
$perflib->GetObjectList($processor, $proc_ref0);
sleep 3;
$perflib->GetObjectList($processor, $proc_ref1);
sleep 3;
$perflib->Close($proc_ref0);
$perflib->Close($proc_ref1);
$instance_ref0 = $proc_ref0->{Objects}->{$processor}->{Instances};
$instance_ref1 = $proc_ref1->{Objects}->{$processor}->{Instances};
for $p (keys %{$instance_ref0}) {
$counter_ref0 = $instance_ref0->{$p}->{Counters};
$counter_ref1 = $instance_ref1->{$p}->{Counters};
for $i (keys %{$counter_ref0}) {
next if ($instance_ref0->{$p}->{Name} eq "_Total");
if($counter_ref0->{$i}->{CounterNameTitleIndex} == $proctime){
$Numerator0 = $counter_ref0->{$i}->{Counter};
$Denominator0 = $proc_ref0->{PerfTime100nSec};
$Numerator1 = $counter_ref1->{$i}->{Counter};
$Denominator1 = $proc_ref1->{PerfTime100nSec};
$proc_time{$p} = (1- (($Numerator1 - $Numerator0) /
($Denominator1 - $Denominator0 ))) * 100;
if ($highproc < $proc_time{$p}){
$highproc = $proc_time{$p};
}
}
}
}
print $highproc."\n";
$highproc=0;
}
}



Let's run the script:

C:\tm>\Perl\bin\perl cpu.pl
19.5
62.5
41.4507577867798
39.7905638112966
52.6041666666667
45.595836881521
25
41.8848050700341
4.14509679185638
5.12821135568664
^C
C:\tm>


 
Thanks for the code guys, it looks like what I need.

dmazzini, when I run you code I get:

Can't call method "GetObjectList" on an undefined value at c:\PROGRA~1\EASYPH~1\cgi-bin\cpu.pl line 19.

This is refering to the line:$perflib->GetObjectList($processor, $proc_ref0);

Is there a library that need to be loaded for this to work?

Any ideas, thanks.
 
Peter,

What server name are you specifying when you call new()?

If you want to know about the machine that the script is running on you don't need to set server name at all.

Check the example in the documentation.

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top