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.
Free Physical Memory using powershell
Re: Free Physical Memory using powershell
See here:
https://stackoverflow.com/questions/265 ... mory-usage
Could try something like this:
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"
}-
lanxessinfy
- Posts: 68
- Joined: Tue Nov 24, 2020 5:55 am
Re: Free Physical Memory using powershell
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!
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
You can use this one:
- It can do top CPU or the top WorkingSet (used physical memory by process)
- 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
}-
lanxessinfy
- Posts: 68
- Joined: Tue Nov 24, 2020 5:55 am
Re: Free Physical Memory using powershell
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.
Nagios Output:
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.
Nagios Output:
You do not have the required permissions to view the files attached to this post.
Re: Free Physical Memory using powershell
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
}