Page 1 of 1

Free Physical Memory using powershell

Posted: Fri Dec 24, 2021 4:42 am
by lanxessinfy
Hi,

Currently we are using the below powershell script to get the Top 5 CPU consuming process when the CPU load is meeting the Threshold.


Try {
$Threshold1 = 90
$Threshold2 = 95
$CpuLoad = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average ).Average
IF($CpuLoad -gt $Threshold1) {
Write-Output (Get-Process | Sort -Descending CPU | Select -First 5)
}elseif($CpuLoad -gt $Threshold2) {
Write-Output (Get-Process | Sort -Descending CPU | Select -First 5)
} Else {
Write-Host "CPU load is" $CpuLoad "%"
}
}
Catch {
Write-Host "Script Check Failed"
}




Now we have a new requirement. i.e., When the Physical memory is like 90 or 95 we need to get top 5 memory consuming process.
could you please help with the requirement.

Thanks in advance.

Re: Free Physical Memory using powershell

Posted: Mon Dec 27, 2021 3:13 pm
by ssax
See here:

https://stackoverflow.com/questions/265 ... mory-usage

Could try something like this:

Code: Select all

Try {
$Threshold1 = 90
$Threshold2 = 95
$CpuLoad = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average ).Average
IF($CpuLoad -gt $Threshold1) {
Write-Output (Get-Process | Sort -Descending CPU | Select -First 5)
Write-Output (Get-Process | Sort -Descending WorkingSet| Select -First 5)
}elseif($CpuLoad -gt $Threshold2) {
Write-Output (Get-Process | Sort -Descending CPU | Select -First 5)
Write-Output (Get-Process | Sort -Descending WorkingSet | Select -First 5)
} Else {
Write-Host "CPU load is" $CpuLoad "%"
}
}
Catch {
Write-Host "Script Check Failed"
}

Re: Free Physical Memory using powershell

Posted: Tue Dec 28, 2021 8:07 am
by lanxessinfy
Hi,

We need to calculate the physical memory load if physical memory load is 90% or 95% then it should generate top 5 memory consuming process.

The above one is calculating CPU load. we require physical memory load.

Please provide the suitable solution.

Thanks!

Re: Free Physical Memory using powershell

Posted: Tue Dec 28, 2021 12:23 pm
by ssax
You can use this one:
- It can do top CPU or the top WorkingSet (used physical memory by process)

Code: Select all

# check_windows_top.ps1
# .\check_windows_top.ps1 CPU 5 90 95
# .\check_windows_top.ps1 WorkingSet 5 95 99

param (
    [string]$mode = "CPU",
    [int]$number = 5,
    [int]$warning = 90,
    [int]$critical = 95
)

if ($mode -eq 'CPU') {
	$value = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average ).Average
	$metric = 'load_avg'
} else {
	# Output is in bytes
	$totalRAM = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum
	# Output is in bytes
	$availRAM = (((Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue) * 1048576)
	$value = [math]::Round((($availRAM / $totalRAM) * 100))
	$metric = 'memory_used_percent'
	
	# Debug
	#Write-Host $totalRAM
	#Write-Host $availRAM
	#Write-Host $utilization
}

$procs = (Get-Process | Sort-Object $mode -desc | Select-Object -first $number | Format-Table | Out-String)

if (@($procs).Count -ge 0) {
	if ($value -ge $critical) {
		Write-Host "CRITICAL - $mode $value% exceeded critcal threshold ($warning%/$critical%)! | $metric=$value%;$warning;$critical;"
		Write-Host $procs
		exit 2
	} elseif ($value -ge $warning) {
		Write-Host "WARNING - $mode $value% exceeded warning threshold ($warning%/$critical%)! | $metric=$value%;$warning;$critical;"
		Write-Host $procs 
		exit 1
	} else {
		Write-Host "OK - $mode $value% - No thresholds exceeded ($warning%/$critical%) | $metric=$value%;$warning;$critical;"
		# Uncomment the next line if you want it to show top procs on OK
		#Write-Host $procs
		exit 0
	}
} else {
    Write-Host "UNKNOWN - Something went wrong!"
    exit 3
}

Re: Free Physical Memory using powershell

Posted: Thu Dec 30, 2021 9:33 am
by lanxessinfy
Hi,

We have run the command on the target server.
I'm getting the correct output on PowerShell whereas in Nagios am getting weird output. Please check.
Script:

param (
[String]$mode = "WorkingSet",
$number = 5,
$warning = 10,
$critical = 35
)

if ($mode -eq 'CPU') {
$value = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average ).Average
$metric = 'load_avg'
} else {
# Output is in bytes
$totalRAM = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum
# Output is in bytes
$availRAM = (((Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue) * 1048576)
$value = [math]::Round(((($totalRAM-$availRAM) / $totalRAM) * 100))
$metric = 'memory_used_percent'


# Debug
#Write-Host $totalRAM
#Write-Host $availRAM
#Write-Host $utilization
}


$procs = (Get-Process | Sort-Object $mode -desc | Select-Object -first 5 | Format-Table | Out-String)

if (@($procs).Count -ge 0) {
if ($value -ge $critical) {
Write-Host "CRITICAL - $mode $value% exceeded critcal threshold ($warning%/$critical%)! | $metric=$value%;$warning;$critical;"
Write-Host $procs
exit 2
} elseif ($value -ge $warning) {
Write-Host "WARNING - $mode $value% exceeded warning threshold ($warning%/$critical%)! | $metric=$value%;$warning;$critical;"
Write-Host $procs
exit 1
} else {
Write-Host "OK - $mode $value% - No thresholds exceeded ($warning%/$critical%) | $metric=$value%;$warning;$critical;"
# Uncomment the next line if you want it to show top procs on OK
#Write-Host $procs
exit 0


Powershell output.
Powershell output.PNG

Nagios Output:
Nagios Output.PNG

Re: Free Physical Memory using powershell

Posted: Mon Jan 03, 2022 12:17 pm
by ssax
Try this one:

Code: Select all

# check_windows_top.ps1
# .\check_windows_top.ps1 CPU 5 90 95
# .\check_windows_top.ps1 WorkingSet 5 95 99

param (
	[string]$mode = "WorkingSet",
	[int]$number = 5,
	[int]$warning = 10,
	[int]$critical = 35
)

if ($mode -eq 'CPU') {
   $value = (Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average ).Average
   $metric = 'load_avg'
} else {
   # Output is in bytes
   $totalRAM = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum
   # Output is in bytes
   $availRAM = (((Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue) * 1048576)
   $value = [math]::Round((($availRAM / $totalRAM) * 100))
   $metric = 'memory_used_percent'
   
   # Debug
   #Write-Host $totalRAM
   #Write-Host $availRAM
   #Write-Host $utilization
}

$procs = (Get-Process | Sort-Object $mode -desc | Select-Object -first $number | Format-Table | Out-String)

if (@($procs).Count -ge 0) {
   if ($value -ge $critical) {
	  Write-Host "CRITICAL - $mode $value% exceeded critcal threshold ($warning%/$critical%)! | $metric=$value%;$warning;$critical;"
	  Write-Host $procs
	  exit 2
   } elseif ($value -ge $warning) {
	  Write-Host "WARNING - $mode $value% exceeded warning threshold ($warning%/$critical%)! | $metric=$value%;$warning;$critical;"
	  Write-Host $procs
	  exit 1
   } else {
	  Write-Host "OK - $mode $value% - No thresholds exceeded ($warning%/$critical%) | $metric=$value%;$warning;$critical;"
	  # Uncomment the next line if you want it to show top procs on OK
	  #Write-Host $procs
	  exit 0
   }
} else {
	Write-Host "UNKNOWN - Something went wrong!"
	exit 3
}