2023-10-31 12:12:58 +01:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
2023-10-13 08:06:24 +02:00
|
|
|
|
Checks the swap space
|
2021-10-04 21:29:23 +02:00
|
|
|
|
.DESCRIPTION
|
2023-10-13 08:06:24 +02:00
|
|
|
|
This PowerShell script queries the current status of the swap space and prints it.
|
2023-08-16 08:12:18 +02:00
|
|
|
|
.PARAMETER minLevel
|
2024-07-11 14:35:05 +02:00
|
|
|
|
Specifies the minimum level in MB (10 MB by default)
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2023-08-04 12:59:10 +02:00
|
|
|
|
PS> ./check-swap-space.ps1
|
2024-06-15 11:55:22 +02:00
|
|
|
|
✅ Swap space uses 21% of 1GB - 1005MB free
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
2022-09-06 21:42:04 +02:00
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-03-15 16:41:26 +01:00
|
|
|
|
#>
|
|
|
|
|
|
2023-08-16 08:12:18 +02:00
|
|
|
|
param([int]$minLevel = 10)
|
2021-03-15 16:41:26 +01:00
|
|
|
|
|
2024-02-07 18:39:57 +01:00
|
|
|
|
function MB2String { param([int64]$bytes)
|
2024-06-15 11:55:22 +02:00
|
|
|
|
if ($bytes -lt 1024) { return "$($bytes)MB" }
|
|
|
|
|
$bytes /= 1024
|
|
|
|
|
if ($bytes -lt 1024) { return "$($bytes)GB" }
|
|
|
|
|
$bytes /= 1024
|
|
|
|
|
if ($bytes -lt 1024) { return "$($bytes)TB" }
|
|
|
|
|
$bytes /= 1024
|
|
|
|
|
if ($bytes -lt 1024) { return "$($bytes)PB" }
|
|
|
|
|
$bytes /= 1024
|
|
|
|
|
if ($bytes -lt 1024) { return "$($bytes)EB" }
|
2022-11-06 21:55:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 16:41:26 +01:00
|
|
|
|
try {
|
2024-02-07 18:39:57 +01:00
|
|
|
|
|
2021-03-20 15:15:57 +01:00
|
|
|
|
if ($IsLinux) {
|
|
|
|
|
$Result = $(free --mega | grep Swap:)
|
2024-02-07 18:39:57 +01:00
|
|
|
|
[int64]$total = $Result.subString(5,14)
|
|
|
|
|
[int64]$used = $Result.substring(20,13)
|
|
|
|
|
[int64]$free = $Result.substring(32,11)
|
2021-03-20 15:15:57 +01:00
|
|
|
|
} else {
|
2024-02-07 18:39:57 +01:00
|
|
|
|
$items = Get-WmiObject -class "Win32_PageFileUsage" -namespace "root\CIMV2" -computername localhost
|
|
|
|
|
[int64]$total = [int64]$used = 0
|
|
|
|
|
foreach ($item in $items) {
|
|
|
|
|
$total += $item.AllocatedBaseSize
|
|
|
|
|
$used += $item.CurrentUsage
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
[int64]$free = ($total - $used)
|
2021-03-20 15:15:57 +01:00
|
|
|
|
}
|
2024-02-07 18:39:57 +01:00
|
|
|
|
if ($total -eq 0) {
|
2023-08-04 12:59:10 +02:00
|
|
|
|
Write-Output "⚠️ No swap space configured"
|
2024-02-07 18:39:57 +01:00
|
|
|
|
} elseif ($free -eq 0) {
|
|
|
|
|
Write-Output "⚠️ Swap space of $(MB2String $total) is full"
|
|
|
|
|
} elseif ($free -lt $minLevel) {
|
2024-03-13 16:52:55 +01:00
|
|
|
|
Write-Output "⚠️ Swap space has only $(MB2String $free) of $(MB2String $total) free"
|
2024-07-11 14:35:05 +02:00
|
|
|
|
} elseif ($used -lt 3) {
|
|
|
|
|
Write-Output "✅ Swap space unused - full $(MB2String $free) free"
|
2022-10-16 11:14:41 +02:00
|
|
|
|
} else {
|
2024-03-16 11:36:02 +01:00
|
|
|
|
[int64]$percent = ($used * 100) / $total
|
2024-07-11 14:35:05 +02:00
|
|
|
|
Write-Output "✅ Swap space at $percent% of $(MB2String $total) - $(MB2String $free) free"
|
2021-03-15 16:58:14 +01:00
|
|
|
|
}
|
2021-09-27 10:09:45 +02:00
|
|
|
|
exit 0 # success
|
2021-03-15 16:41:26 +01:00
|
|
|
|
} catch {
|
2022-04-13 12:06:32 +02:00
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2021-03-15 16:41:26 +01:00
|
|
|
|
exit 1
|
2023-08-04 12:59:10 +02:00
|
|
|
|
}
|