Monitoring Windows CPU and RAM Utilization Using Perl via WMI
Monitoring windows CPU and RAM Utilization of a system becomes essential when we need to narrow down and identify the time span or application when the available resources in the system are fully used. Windows Operating System provides an in build mechanism for this called WMI (Windows Management Instruction) service through which we will be able to monitor the CPU and RAM utilization in a system.
WMI Query
The WMI Services are available in OLE (Object Linking and Embedding) Standards using which other application will be able to connect to the WMI Service and query the required data.
Here in this blog we are going to connect to the WMI Service using the Win32::OLE object of the Perl and monitor the CPU and RAM Utilization percentage of a windows machine.
The WMI Queries are similar to that of a standard SQL query, which we use in Database to retrieve data. The difference between WMI Queries and SQL queries are that we connect to an Object Class in WMI queries whereas we connect to a table or view in SQL queries.
We should be aware of the Object Class that provide us the necessary data from a windows operating system when we work with WMI Queries.
For our requirement to monitor windows CPU and RAM utilization using WMI the necessary object classes are
- Win32_PerfRawData_PerfOS_Processor
- Win32_OperatingSystem
Win32_PerfRawData_PerfOS_Processor
provides us the RAW CPU utilization value using which we need to apply a calculation logic to get the correct CPU percentage utilization value.
Win32_OperatingSystem
provides us the RAM utilization of a windows operating system. Using Win32_OperatingSystem we can also retrieve details like system name, OS Build Number, Total RAM Installed and so on. The full list of details that can be retrieved is available in the link https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx
Monitoring Windows CPU and RAM Utilization percentage
The steps that has to be followed to monitor the Windows CPU and RAM Utilization are given below. The steps are applicable to all programming and script languages, which uses WMI service to monitor Windows CPU and RAM Utilization.
- Include the Win32::OLE library to the script
- Connect to WMI Service and get the object instance of it
- Using the Win32_PerfRawData_PerfOS_Processor Object Class retrieve the values of PercentProcessorTime and TimeStamp_Sys100NS for two times with a sleep interval of 1 second
- Apply the CPU % utilization formula to get the CPU utilization percentage
- Using the Win32_OperatingSystem Object Class retrieve the values for TotalVisibleMemorySize and FreePhysicalMemory.
- Apply the RAM % utilization formula to get the RAM utilization percentage.
- Display the values or store it for further use based on the requirement.
Monitoring Windows CPU and RAM Utilization Code - Perl
###################################### ##### www.craftedforeveryone.com ##### ###################################### #### OS : Windows 10 64 Bit ########## #### Perl : v5.26.1 Strawberry Perl ## ###################################### use strict; use warnings; use Win32::OLE('in'); my $statsQuery; my $ramQuery; my $objWMIService = Win32::OLE->GetObject("winmgmts://./root/cimv2") or die "WMI connection failed.n"; sub main() { $statsQuery="select * from Win32_PerfRawData_PerfOS_Processor where Name='_Total'"; $ramQuery="select * from Win32_OperatingSystem"; GetUtilizationStats(); } sub GetUtilizationStats() { my $processingTime=0; my $timeStamp=0; my $cpu=0; my $ram; my $name; while(sleep 1) { my $cols = $objWMIService->ExecQuery($statsQuery); foreach my $Data( in $cols){ $name=$Data->{Name}; $cpu = (1 - GetCorrectCPUData($processingTime,$Data->{PercentProcessorTime},$timeStamp,$Data->{TimeStamp_Sys100NS}))*100; $processingTime=$Data->{PercentProcessorTime}; $timeStamp=$Data->{TimeStamp_Sys100NS}; } $cols=$objWMIService->ExecQuery($ramQuery); foreach my $Data( in $cols){ my $totalMemory=$Data->{TotalVisibleMemorySize}; my $freeMemory=$Data->{FreePhysicalMemory}; $ram=(($totalMemory-$freeMemory)/$totalMemory)*100; } print "Name :".$name.", CPU:".sprintf("%.2f",$cpu)."% RAM:".sprintf("%.2f",$ram)."%n"; } } sub GetCorrectCPUData() { #CPU utilization calculation logic. my $oldProcessingData = shift; my $newProcessingData = shift; my $oldProcessingTime = shift; my $newProcessingTime = shift; my $diffProcessingData = ($newProcessingData - $oldProcessingData); my $diffProcessingTime = ($newProcessingTime - $oldProcessingTime); my $utilization=($diffProcessingData/$diffProcessingTime); return $utilization; } main();
Monitoring Windows CPU and RAM Utilization Perl Code Output
In the below WMI Query to retrieve the Windows CPU utilization data, we are filtering out the data using the where clause of Name and value as _Total. This indicates we are querying the overall windows CPU utilization data.
"select * from Win32_PerfRawData_PerfOS_Processor where Name='_Total'"
Monitoring Windows CPU and RAM Utilization of an Application
If you are looking out to monitor the windows CPU and RAM Utilization of an individual application, then we need to use the Win32_PerfRawData_PerfProc_Process Object Class rather than using Win32_PerfRawData_PerfOS_Processor
The Win32_PerfRawData_PerfProc_Process provides details and values specific to a Process or an Application, whereas Win32_PerfRawData_PerfOS_Processor provides details specific to a Processor.
You can refer to our post at http://www.craftedforeveryone.com/cpu-and-ram-utilization-of-an-application-using-perl-via-wmi/ for more details on monitoring Windows CPU and RAM Utilization of an individual application. The application can be monitored either using Process ID or Process Name.
Total CPU % utilization formula
p1 refers to the PercentProcessorTime value retrieved while executing the Win32_PerfRawData_PerfOS_Processor query for the first time, and p2 refers to the data retrieved while querying for the second time. Whereas t1 and t2 refers to TimeStamp_Sys100NS values.