PowerShell/scripts/watch-host.ps1

128 lines
4.7 KiB
PowerShell
Raw Normal View History

2024-12-03 21:41:51 +01:00
<#
.SYNOPSIS
Writes a value with unit and range
.DESCRIPTION
This PowerShell script writes the given value with the unit and the value range to the console.
.PARAMETER value
Specifies the value
.EXAMPLE
PS> ./write-value.ps1 95.0 "°C" 0 10 90 100
[0--------95°C-100]
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
function GetCPUTemperature {
2024-12-04 07:47:02 +01:00
$temp = -300 # unsupported
2024-12-03 21:41:51 +01:00
if ($IsLinux) {
if (Test-Path "/sys/class/thermal/thermal_zone0/temp" -pathType leaf) {
[int]$IntTemp = Get-Content "/sys/class/thermal/thermal_zone0/temp"
$temp = [math]::round($IntTemp / 1000.0, 1)
}
} else {
$objects = Get-WmiObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
foreach ($object in $objects) {
$highPrec = $object.HighPrecisionTemperature
$temp = [math]::round($highPrec / 100.0, 1)
}
}
return $temp
}
function GetUptime {
if ($IsLinux) {
$uptime = (Get-Uptime)
} else {
$lastBootTime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$uptime = New-TimeSpan -Start $lastBootTime -End (Get-Date)
}
return $uptime.Days
}
function WriteValueInRange([float]$value, [string]$unit, [float]$redMin, [float]$yellowMin, [float]$yellowMax, [float]$redMax) {
$line = "------------------------------------------------"
$text = "[$redMin$($value)$unit $redMax]"
[float]$total = 20.0 - $text.Length
if ($value -gt $redMax) {
Write-Host "[$redMin$($line.Substring(0, $total))$redMax]" -noNewline
Write-Host "$($value)$unit " -noNewline -foregroundColor red
} elseif ($value -lt $redMin) {
Write-Host "$($value)$unit" -noNewline -foregroundColor red
Write-Host "[$redMin$($line.Substring(0, $total))$redMax] " -noNewline
} else {
[float]$leftSide = (($value - $redMin) * $total) / ($redMax - $redMin)
if ($leftSide -lt 1.0) { $leftSide = 1.0 }
if ($leftSide -gt ($total - 1.0)) { $leftSide = $total - 1.0 }
Write-Host "[$redMin$($line.Substring(0, $leftSide))" -noNewline
2024-12-03 22:46:12 +01:00
if (($value -lt $yellowMin) -or ($value -gt $yellowMax)) {
2024-12-03 21:41:51 +01:00
Write-Host "$($value)$unit" -noNewline -foregroundColor yellow
} else {
Write-Host "$($value)$unit" -noNewline -foregroundColor green
}
Write-Host "$($line.Substring(0, $total - $leftSide + 0.49))$redMax] " -noNewline
}
}
try {
do {
2024-12-04 12:00:15 +01:00
[int]$DayOfYear = (Get-Date).DayofYear
2024-12-03 22:46:12 +01:00
[int]$Time = Get-Date -format "HHmm"
2024-12-04 07:47:02 +01:00
[int]$TimeZone = Get-Date -format "zz"
2024-12-03 21:41:51 +01:00
$CPUtemp = GetCPUTemperature
2024-12-04 07:47:02 +01:00
$numCores = $env:NUMBER_OF_PROCESSORS
2024-12-03 21:41:51 +01:00
$numProcesses = (Get-Process).Count
2024-12-03 22:46:12 +01:00
$load = "{0}" -f $(Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object -ExpandProperty Average)
$DriveDetails = Get-PSDrive C
$DiskUse = [math]::round($DriveDetails.Used / 1GB)
$DiskSize = [math]::round(($DriveDetails.Used + $DriveDetails.Free) / 1GB)
2024-12-03 21:41:51 +01:00
$numDaysUp = GetUptime
2024-12-04 12:00:15 +01:00
if ($IsLinux) {
$result = $(free --mega | grep Swap:)
[int64]$total = $result.subString(5,14)
[int64]$used = $result.substring(20,13)
} else {
$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
}
}
2024-12-03 21:41:51 +01:00
Clear-Host
2024-12-04 07:47:02 +01:00
Write-Host "Host $env:COMPUTERNAME"
Write-Host "=================="
2024-12-04 12:00:15 +01:00
Write-Host "`n* DATE " -noNewline
WriteValueInRange $DayOfYear "" 0 0 366 366
2024-12-03 22:46:12 +01:00
Write-Host "`n* TIME " -noNewline
WriteValueInRange $Time "" 0 0 2400 2400
2024-12-04 07:47:02 +01:00
Write-Host "`n* ZONE " -noNewline
WriteValueInRange $TimeZone "" -23 -23 23 23
if ($CPUtemp -ne -300) {
Write-Host "`n* CPU " -noNewline
WriteValueInRange $CPUtemp "°C" 0 10 80 100
}
2024-12-03 22:46:12 +01:00
Write-Host "`n* CPU " -noNewline
2024-12-04 07:47:02 +01:00
WriteValueInRange $numCores " cores" 0 0 100 100
2024-12-03 22:46:12 +01:00
Write-Host "`n* LOAD " -noNewline
WriteValueInRange $load "%" 0 0 90 100
Write-Host "`n* PROC " -noNewline
2024-12-03 21:41:51 +01:00
WriteValueInRange $numProcesses "" 0 10 900 1000
2024-12-04 12:00:15 +01:00
Write-Host "`n* SWAP " -noNewline
2024-12-04 16:36:01 +01:00
WriteValueInRange $used "GB" 0 0 ($total - 1) $total
2024-12-03 22:46:12 +01:00
Write-Host "`n* DISK " -noNewline
2024-12-04 16:36:01 +01:00
WriteValueInRange $DiskUse "GB" 0 0 ($DiskSize - 5) $DiskSize
2024-12-03 22:46:12 +01:00
Write-Host "`n* UP " -noNewline
2024-12-04 16:36:01 +01:00
WriteValueInRange $numDaysUp " days" 0 0 1000 1000
2024-12-03 21:41:51 +01:00
2024-12-04 07:47:02 +01:00
Start-Sleep -milliseconds 5000
2024-12-03 21:41:51 +01:00
} while ($true)
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}