From 85d3137b14725171d76d42cf1c9319ff6aa0c2c0 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Sat, 20 Mar 2021 15:51:03 +0100 Subject: [PATCH] Add check-health.ps1 --- Data/scripts.csv | 3 +- README.md | 3 +- ...dns-cache.ps1 => check-dns-resolution.ps1} | 8 ++--- Scripts/check-drive-space.ps1 | 12 +++---- Scripts/check-health.ps1 | 36 +++++++++++++++++++ 5 files changed, 50 insertions(+), 12 deletions(-) rename Scripts/{train-dns-cache.ps1 => check-dns-resolution.ps1} (69%) create mode 100755 Scripts/check-health.ps1 diff --git a/Data/scripts.csv b/Data/scripts.csv index b6a4456a..c8968064 100644 --- a/Data/scripts.csv +++ b/Data/scripts.csv @@ -1,6 +1,8 @@ Script,Description add-firewall-rules.ps1, adds firewall rules to the given executables (requires admin rights) +check-dns-resolution.ps1, checks the DNS resolution with frequently used domain names check-drive-space.ps1, checks the given drive for free space left +check-health.ps1, checks the system health check-ipv4-address.ps1, checks the given IPv4 address for validity check-ipv6-address.ps1, checks the given IPv6 address for validity check-mac-address.ps1, checks the given MAC address for validity @@ -127,7 +129,6 @@ switch-branch.ps1, switches the branch in the current/given Git repository (incl switch-shelly1.ps1, switches a Shelly1 device in the local network take-screenshot.ps1, takes a single screenshot take-screenshots.ps1, takes multiple screenshots -train-dns-cache.ps1, trains the DNS cache with frequently used domain names translate-text.ps1, translates the given text into other languages turn-volume-up.ps1, turns the audio volume up (+10% by default) turn-volume-down.ps1, turns the audio volume down (-10% by default) diff --git a/README.md b/README.md index dd4160ae..d1f06ddb 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,9 @@ Collection of PowerShell Scripts ⚙️ Scripts for Computer Management --------------------------------- * [add-firewall-rules.ps1](Scripts/add-firewall-rules.ps1) - adds firewall rules for the given executables (requires admin rights) +* [check-dns-resolution.ps1](Scripts/check-dns-resolution.ps1) - checks the DNS resolution with frequently used domain names * [check-drive-space.ps1](Scripts/check-drive-space.ps1) - checks the given drive for free space left +* [check-health.ps1](Scripts/check-health.ps1) - checks the system health * [check-swap-space.ps1](Scripts/check-swap-space.ps1) - checks the swap space for free space left * [check-windows-system-files.ps1](Scripts/check-windows-system-files.ps1) - checks the validity of the Windows system files (requires admin rights) * [enable-crash-dumps.ps1](Scripts/enable-crash-dumps.ps1) - enables the writing of crash dumps @@ -158,7 +160,6 @@ Collection of PowerShell Scripts * [simulate-matrix.ps1](Scripts/simulate-matrix.ps1) - simulates the Matrix (fun) * [simulate-presence.ps1](Scripts/simulate-presence.ps1) - simulates the human presence against burglars * [switch-shelly1.ps1](Scripts/switch-shelly1.ps1) - switches a Shelly1 device in the local network -* [train-dns-cache.ps1](Scripts/train-dns-cache.ps1) - trains the DNS cache with frequently used domain names * [translate-text.ps1](Scripts/translate-text.ps1) - translates the given text into other languages * [weather.ps1](Scripts/weather.ps1) - prints the current weather forecast * [weather-alert.ps1](Scripts/weather-alert.ps1) - checks the current weather for critical values diff --git a/Scripts/train-dns-cache.ps1 b/Scripts/check-dns-resolution.ps1 similarity index 69% rename from Scripts/train-dns-cache.ps1 rename to Scripts/check-dns-resolution.ps1 index 10b93150..e682e98a 100755 --- a/Scripts/train-dns-cache.ps1 +++ b/Scripts/check-dns-resolution.ps1 @@ -1,7 +1,7 @@ #!/bin/powershell <# -.SYNTAX ./train-dns-cache.ps1 -.DESCRIPTION trains the DNS cache with frequently used domain names +.SYNTAX ./check-dns-resolution.ps1 +.DESCRIPTION checks the DNS resolution with frequently used domain names .LINK https://github.com/fleschutz/PowerShell .NOTES Author: Markus Fleschutz / License: CC0 #> @@ -18,14 +18,14 @@ try { foreach($Row in $Table) { $Domain = $Row.Domain - write-progress "Training DNS cache with $Domain..." + write-progress "Resolving $Domain..." $Ignore = nslookup $Domain } $Count = $Table.Length $StopTime = Get-Date $TimeInterval = New-Timespan -start $StartTime -end $StopTime - write-host -foregroundColor green "Done - DNS cache trained with $Count domain names in $TimeInterval seconds" + write-host -foregroundColor green "OK - resolved $Count domain names in $TimeInterval seconds" exit 0 } catch { write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" diff --git a/Scripts/check-drive-space.ps1 b/Scripts/check-drive-space.ps1 index c0c6381d..034ecd00 100755 --- a/Scripts/check-drive-space.ps1 +++ b/Scripts/check-drive-space.ps1 @@ -14,15 +14,15 @@ if ($Drive -eq "" ) { try { $DriveDetails = (get-psdrive $Drive) - [int]$FreeSpace = (($DriveDetails.Free / 1024) / 1024) / 1024 - [int]$InUse = (($DriveDetails.Used / 1024) / 1024) / 1024 - [int]$Total = ($InUse + $FreeSpace) + [int]$Free = (($DriveDetails.Free / 1024) / 1024) / 1024 + [int]$Used = (($DriveDetails.Used / 1024) / 1024) / 1024 + [int]$Total = ($Used + $Free) - if ($FreeSpace -lt $MinLevel) { - write-warning "Drive $Drive has only $FreeSpace GB left to use! ($InUse GB out of $Total GB in use, minimum is $MinLevel GB)" + if ($Free -lt $MinLevel) { + write-warning "Drive $Drive has only $Free GB left to use! ($Used GB out of $Total GB in use, minimum is $MinLevel GB)" exit 1 } - write-host -foregroundColor green "OK - drive $Drive has $FreeSpace GB left to use ($InUse GB out of $Total GB in use, minimum is $MinLevel GB)" + write-host -foregroundColor green "OK - $Free GB free at drive $Drive ($Used GB used out of $Total GB, $MinLevel GB is minimum)" exit 0 } catch { write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" diff --git a/Scripts/check-health.ps1 b/Scripts/check-health.ps1 new file mode 100755 index 00000000..4867b74b --- /dev/null +++ b/Scripts/check-health.ps1 @@ -0,0 +1,36 @@ +#!/bin/powershell +<# +.SYNTAX ./check-health.ps1 +.DESCRIPTION checks the system health +.LINK https://github.com/fleschutz/PowerShell +.NOTES Author: Markus Fleschutz / License: CC0 +#> + +try { + $Hostname = $(hostname) + "Checking health of $Hostname ..." + + & check-swap-space.ps1 + if ($lastExitCode -ne "0") { throw "check-swap-space.ps1 failed" } + + if ($IsLinux) { + & check-drive-space.ps1 / + } else { + & check-drive-space.ps1 C + } + if ($lastExitCode -ne "0") { throw "check-drive-space.ps1 failed" } + + if (-not($IsLinux)) { + & check-windows-system-files.ps1 + if ($lastExitCode -ne "0") { throw "check-windows-system-files.ps1 failed" } + } + + & check-dns-resolution.ps1 + if ($lastExitCode -ne "0") { throw "check-dns-resolution.ps1 failed" } + + write-host -foregroundColor green "OK - $Hostname is healthy" + exit 0 +} catch { + write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +}