2021-09-27 10:38:12 +02: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
|
|
|
|
|
Specifies the minimum level in GB (10 GB by default)
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2023-08-04 12:59:10 +02:00
|
|
|
|
PS> ./check-swap-space.ps1
|
2023-10-13 08:06:24 +02:00
|
|
|
|
✅ Swap space uses 42%, 748MB free of 1GB
|
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
|
|
|
|
|
2022-11-06 22:06:55 +01:00
|
|
|
|
function MB2String { param([int64]$Bytes)
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)MB" }
|
|
|
|
|
$Bytes /= 1000
|
2022-11-06 21:55:00 +01:00
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)GB" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)TB" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)PB" }
|
|
|
|
|
$Bytes /= 1000
|
|
|
|
|
if ($Bytes -lt 1000) { return "$($Bytes)EB" }
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 16:41:26 +01:00
|
|
|
|
try {
|
2023-08-21 23:11:36 +02:00
|
|
|
|
[int64]$Total = [int64]$Used = [int64]$Free = 0
|
2021-03-20 15:15:57 +01:00
|
|
|
|
if ($IsLinux) {
|
|
|
|
|
$Result = $(free --mega | grep Swap:)
|
2023-08-21 23:11:36 +02: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 {
|
2022-10-16 11:06:11 +02:00
|
|
|
|
$Items = Get-WmiObject -class "Win32_PageFileUsage" -namespace "root\CIMV2" -computername localhost
|
2021-03-20 15:15:57 +01:00
|
|
|
|
foreach ($Item in $Items) {
|
2023-08-21 23:11:36 +02:00
|
|
|
|
$Total += $Item.AllocatedBaseSize
|
|
|
|
|
$Used += $Item.CurrentUsage
|
|
|
|
|
$Free += ($Total - $Used)
|
2021-03-20 15:15:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-25 15:08:56 +02:00
|
|
|
|
if ($Total -eq 0) {
|
2023-08-04 12:59:10 +02:00
|
|
|
|
Write-Output "⚠️ No swap space configured"
|
2022-12-19 19:33:32 +01:00
|
|
|
|
} elseif ($Free -eq 0) {
|
2023-08-21 23:11:36 +02:00
|
|
|
|
Write-Output "⚠️ Swap space of $(MB2String $Total) is full"
|
2023-08-16 08:12:18 +02:00
|
|
|
|
} elseif ($Free -lt $minLevel) {
|
2023-08-21 23:11:36 +02:00
|
|
|
|
Write-Output "⚠️ Swap space of $(MB2String $Total) is nearly full, only $(MB2String $Free) free"
|
2022-11-02 21:57:42 +01:00
|
|
|
|
} elseif ($Used -eq 0) {
|
2023-08-21 23:11:36 +02:00
|
|
|
|
Write-Output "✅ Swap space of $(MB2String $Total) reserved"
|
2022-10-16 11:14:41 +02:00
|
|
|
|
} else {
|
2023-08-05 16:19:35 +02:00
|
|
|
|
[int]$Percent = ($Used * 100) / $Total
|
2023-10-13 08:06:24 +02:00
|
|
|
|
Write-Output "✅ Swap space uses $Percent%, $(MB2String $Free) free of $(MB2String $Total)"
|
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
|
|
|
|
}
|