CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Win32 Programming Help:
calculating CPU Utilization on Windows and unix machine

 



Amitk
Novice

Feb 16, 2013, 1:51 AM

Post #1 of 8 (13408 views)
calculating CPU Utilization on Windows and unix machine Can't Post

Dear All,

I am new to perl programming, i want to create the script for cpu utilization of local system.

I serached through the internet and found some useful information.

Copy pasting the scripts for the reference

use Win32::SystemInfo::CpuUsage;
my $intvl = 1000; # in milliseconds
my $usage = Win32::SystemInfo::CpuUsage::getCpuUsage($intvl);

my $i = 0;
while($i < 5){ #query 5 times
$i++;
$usage = Win32::SystemInfo::CpuUsage::getCpuUsage($intvl);
print "$i: cpu usage $usage\n";
}

while running this script I am getting below errors

C:\Perl\bin>perl CpuUsage1.pl
Can't locate Win32/API/Prototype.pm in @INC (@INC contains: C:/Perl/site/lib C:/
Perl/lib .) at CpuUsage1.pl line 3.
BEGIN failed--compilation aborted at CpuUsage1.pl line 3.

There is another script i found as below


use strict;
use Win32::API::Prototype;
ApiLink( 'kernel32', q[ BOOL GetSystemTimes( LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime ) ]) or die $^E; sub SystemTimes { my( $idleTicks, $kernelTicks, $userTicks ) =( chr( 0 ) x 8 ) x 3;
GetSystemTimes( $idleTicks, $kernelTicks, $userTicks ) or die $^E; return map{ my( $lo, $hi ) = unpack 'VV', $_;
( $hi * 2**32 + $lo ); } $idleTicks, $kernelTicks, $userTicks;}
use constant { IDLE=>0, KERNEL=>1, USER=>2 };$|=1; my @last = SystemTimes;
while( sleep 1 )
{ my @now = SystemTimes; my @deltas = map{ $now[ $_ ] - $last[ $_ ] } IDLE, KERNEL, USER; my $busy = $deltas[ KERNEL ] + $deltas[ USER ]; my $pcUsage = ( $busy - $deltas[ IDLE ] )* 100 /( $deltas[ IDLE ] || 100e5 );
printf "\rCPU usage(%%): %6.3f ", $pcUsage; @last = @now;}

for this i am getting below error

C:\Perl\bin>perl CpuUsage1.pl
Can't locate Win32/API/Prototype.pm in @INC (@INC contains: C:/Perl/site/lib C:/
Perl/lib .) at CpuUsage1.pl line 3.
BEGIN failed--compilation aborted at CpuUsage1.pl line 3.



Can some one help me to sort this out or of there is any easy to find out the cpuutilization please suggest.

Thanks

Amit
Attachments: Win32-SystemInfo-CpuUsage-0.02.tar.gz (16.3 KB)


FishMonger
Veteran / Moderator

Feb 16, 2013, 6:05 AM

Post #2 of 8 (13401 views)
Re: [Amitk] calculating CPU Utilization on Windows and unix machine [In reply to] Can't Post

The error message is telling you that you need to install the Win32::API::Prototype module.


Amitk
Novice

Feb 16, 2013, 7:07 AM

Post #3 of 8 (13398 views)
Re: [FishMonger] calculating CPU Utilization on Windows and unix machine [In reply to] Can't Post

Thanks for the reply!!

Can you please help me to get those missing files.

Regards

Amit


FishMonger
Veteran / Moderator

Feb 16, 2013, 7:31 AM

Post #4 of 8 (13395 views)
Re: [Amitk] calculating CPU Utilization on Windows and unix machine [In reply to] Can't Post

If you're using Activestate perl, you normally use the ppm utility to install modules (via its gui interface or command line interface).

example:

Quote
c:\>ppm install Win32::API::Prototype

Since Activestate doesn't have the module in its repository, you'll first need to add the bribes repository ppm before installing the module.


If you're using strawberry perl, then you normally use the cpan command to install modules.

example:

Quote
c:\>cpan Win32::API::Prototype



Amitk
Novice

Feb 16, 2013, 9:38 PM

Post #5 of 8 (13380 views)
Re: [FishMonger] calculating CPU Utilization on Windows and unix machine [In reply to] Can't Post

hi

I installed all WIN32 packages but still I am getting the same prototype.pm missing error.

Regards

Amit


Amitk
Novice

Feb 16, 2013, 11:00 PM

Post #6 of 8 (13378 views)
Re: [Amitk] calculating CPU Utilization on Windows and unix machine [In reply to] Can't Post

hi

Thanks for your help.

I downloaded just prototype.pm file from internet and that script worked.

just for info while seraching i even got one script for calculating cpu load but for this i installed sysinfo package and even that also worked out.

Thanks so much

Amit


Amitk
Novice

Feb 17, 2013, 8:16 AM

Post #7 of 8 (13370 views)
Re: [Amitk] calculating CPU Utilization on Windows and unix machine [In reply to] Can't Post

hi All,

Can someone please help to find out cpu utilization of unix server as well.

This is little urgent, looking for help apperciate if someone help quickly on this

Amit


Amitk
Novice

Feb 18, 2013, 3:07 AM

Post #8 of 8 (13353 views)
Re: [Amitk] calculating CPU Utilization on Windows and unix machine [In reply to] Can't Post

hi All,

SAR is command very useful for getting CPU utilization.

I used the samd created the script with samples and intervals.

Hopefully this will be useful to all .

#!/usr/bin/perl
use FileHandle;
sub collect_sar
{
my($samples,$interval,$outf)=@_;
open(SARF,">$outf");
autoflush SARF;
@fargs=("/usr/sbin/sar",$interval,$samples);
open(SAR,"@fargs|");
autoflush SAR;
while(<SAR>) {
continue if ( /%user %nice %system/);
if ( /^\d\d:\d\d:\d\d\s+\d+\s+\d+\s+\d+\s+\d+/ ) {
my @values = split(' ', $_);
my $idle = $values[9];
if ( $idle < 80 ) {
print "Cpu Utilization is increasing Please do the needful";
} elsif ( $idle < 60 )
{


open (MYFILE, '>>data.txt');

print " Cpu Utilization Crossed 40%";

close (MYFILE);



}
undef (@values);
}
}
close(SAR);
close(SARF);
&collect_sar(15,4,"sar.out");



Thanks

Amit

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives