For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > January 2006 > WMI script to display also the virtual memory









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 WMI script to display also the virtual memory
MoshiachNow

2006-01-31, 7:57 am

HI,

I need the following great script by David Roth to also display the
virtual memory for every process - what should I change for this ?
========================================
==============

use Win32::OLE qw( in );

$Machine = "." unless( $Machine = shift @ARGV );
$Machine =~ s#^[\\/]+## if( $ARGV[0] =~ m#^[\\/]{2}# );

# This is the WMI moniker that will connect to a machine's
# CIM (Common Information Model) repository
$CLASS = " WinMgmts:{impersonationLevel=impersonate
}!//$Machine";

# Get the WMI (Microsoft's implementation of WBEM) interface
$WMI = Win32::OLE->GetObject( $CLASS )
|| die "Unable to connect to \\$Machine:" . Win32::OLE->LastError();

# Get the collection of Win32_Process objects
$ProcList = $WMI->InstancesOf( "Win32_Process" );

$~ = PROCESS_HEADER;
write;
$~ = PROCESS_INFO;

# Cycle through each Win32_Process object
# and write out its details...
foreach $Proc ( sort( SortProcs ( in( $ProcList ) ) ) )
{
write;
}

sub SortProcs
{
lc $a->{Name} cmp lc $b->{Name};
}

sub FormatNumber
{
my( $Number ) = @_;
my( $Suffix ) = "";
my $K = 1024;
my $M = 1024 * $K;

if( $M <= $Number )
{
$Suffix = "M";
$Number /= $M;
}
elsif( $K <= $Number )
{
$Suffix = "K";
$Number /= $K;
}

$Number =~ s/(\.\d{0,2})\d*$/$1/;

{} while ($Number =~ s/^(-?\d+)(\d{3})/$1,$2/);

return( $Number . $Suffix );
}

sub FormatDate
{
my( $Date ) = @_;
$Date =~ s/ (\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2
}).*/$1.$2.$3
$4:$5:$6/;
return( $Date );
}

format PROCESS_HEADER =
@||| @||||| @|||||||||||||||| @|||| @|||||| @|||||||
@||||||||||||||||||
PID, Parent, "Process Name", "Thrds", "Memory", "Mem Peak", "Created"
---- ------ ----------------- ----- ------- --------
-------------------
..

format PROCESS_INFO =
@||| @||||| @<<<<<<<<<<<<<<<< @>>>> @>>>>>> @>>>>>>>
@>>>>>>>>>>>>>>>>>>
$Proc->{'ProcessID'}, $Proc->{'ParentProcessID'}, $Proc->{Name},
$Proc->{'ThreadCount'}, FormatNumber( $Proc->{'WorkingSetSize'} ),
FormatNumber( $Proc->{'PeakWorkingSetSize'} ), FormatDate(
$Proc->{'CreationDate'} )
..

Anno Siegel

2006-01-31, 7:57 am

MoshiachNow <lev.weissman@creo.com> wrote in comp.lang.perl.misc:
> HI,
>
> I need the following great script by David Roth to also display the
> virtual memory for every process - what should I change for this ?
> ========================================
==============


What have you tried?

We can help you with programming problems. If you want a program
changed to specification, hire someone.

[script snipped]

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Sponsored Links







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

Copyright 2008 codecomments.com