PowerShell/scripts/check-smart-devices.ps1

102 lines
3.1 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2021-10-03 21:32:17 +02:00
.SYNOPSIS
2023-04-11 10:48:59 +02:00
Checks the SMART device status
2021-10-03 21:32:17 +02:00
.DESCRIPTION
2023-04-11 10:48:59 +02:00
This PowerShell script queries the status of the SSD/HDD devices (supporting S.M.A.R.T.) and prints it.
2021-10-03 21:32:17 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-smart-devices.ps1
2024-12-19 19:15:04 +01:00
1TB Samsung SSD 970 EVO 1TB via NVMe (35°C, 6142h, 770x on/off, 34TB read, 64TB written, v2B2QEXE7, test passed)
2021-10-03 21:32:17 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-10-03 21:32:17 +02:00
#>
2024-04-28 15:29:23 +02:00
function Bytes2String([int64]$bytes) {
if ($bytes -lt 1000) { return "$bytes bytes" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)KB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)MB" }
$bytes /= 1000
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" }
2022-10-27 16:55:32 +02:00
}
2021-10-03 21:32:17 +02:00
try {
2024-04-28 15:29:23 +02:00
$result = (smartctl --version)
2021-10-03 21:32:17 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'smartctl' - make sure smartmontools are installed" }
2022-10-16 10:39:31 +02:00
if ($IsLinux) {
2024-04-28 15:29:23 +02:00
$devices = $(sudo smartctl --scan-open)
2022-10-16 10:39:31 +02:00
} else {
2024-04-28 15:29:23 +02:00
$devices = $(smartctl --scan-open)
2022-10-16 10:39:31 +02:00
}
2024-04-28 15:29:23 +02:00
foreach($device in $devices) {
$array = $device.split(" ")
$dev = $array[0]
if ("$dev" -eq "#") {
2022-10-17 09:15:12 +02:00
continue
} elseif ($IsLinux) {
2024-04-28 15:29:23 +02:00
$details = (sudo smartctl --all --json $dev) | ConvertFrom-Json
2024-04-28 15:56:17 +02:00
$null = (sudo smartctl --test=conveyance $dev)
2022-10-16 10:39:31 +02:00
} else {
2024-04-28 15:29:23 +02:00
$details = (smartctl --all --json $dev) | ConvertFrom-Json
2024-04-28 15:56:17 +02:00
$null = (smartctl --test=conveyance $dev)
2022-10-16 10:39:31 +02:00
}
2024-04-28 15:29:23 +02:00
$status = ""
$modelName = $details.model_name
$protocol = $details.device.protocol
[int64]$bytes = $details.user_capacity.bytes
if ($bytes -gt 0) {
$capacity = "$(Bytes2String $bytes) "
2022-10-21 16:55:41 +02:00
} else {
2024-04-28 15:29:23 +02:00
$capacity = ""
2022-10-21 16:55:41 +02:00
}
2024-12-11 13:33:51 +01:00
$infos = ""
2024-04-28 15:29:23 +02:00
if ($details.temperature.current -gt 50) {
2024-12-11 13:33:51 +01:00
$infos = "$($details.temperature.current)°C TOO HOT"
$status = "⚠️"
} elseif ($details.temperature.current -lt 0) {
2024-12-11 13:33:51 +01:00
$infos = "$($details.temperature.current)°C TOO COLD"
2024-04-28 15:29:23 +02:00
$status = "⚠️"
} else {
2024-12-11 13:33:51 +01:00
$infos = "$($details.temperature.current)°C"
2024-04-28 15:29:23 +02:00
}
if ($details.power_on_time.hours -gt 87600) { # 10 years
2024-12-11 13:33:51 +01:00
$infos += ", $($details.power_on_time.hours)h (!)"
2024-04-28 15:29:23 +02:00
$status = "⚠️"
} else {
2024-12-11 13:33:51 +01:00
$infos += ", $($details.power_on_time.hours)h"
}
2024-04-28 15:29:23 +02:00
if ($details.power_cycle_count -gt 100000) {
2024-12-11 13:33:51 +01:00
$infos += ", $($details.power_cycle_count)x on/off (!)"
2024-04-28 15:29:23 +02:00
$status = "⚠️"
} else {
2024-12-11 13:33:51 +01:00
$infos += ", $($details.power_cycle_count)x on/off"
}
2024-12-19 19:15:04 +01:00
if ($details.nvme_smart_health_information_log.host_reads) {
$infos += ", $(Bytes2String ($details.nvme_smart_health_information_log.data_units_read * 512 * 1000)) read"
$infos += ", $(Bytes2String ($details.nvme_smart_health_information_log.data_units_written * 512 * 1000)) written"
}
2024-12-11 13:33:51 +01:00
$infos += ", v$($details.firmware_version)"
2024-04-28 15:29:23 +02:00
if ($details.smart_status.passed) {
2024-12-11 13:33:51 +01:00
$infos += ", test passed"
2024-04-28 15:29:23 +02:00
} else {
2024-12-11 13:33:51 +01:00
$infos += ", test FAILED"
2024-04-28 15:29:23 +02:00
$status = "⚠️"
}
2024-12-11 13:33:51 +01:00
Write-Host "$status $capacity$modelName via $protocol ($infos)"
2021-10-03 21:32:17 +02:00
}
exit 0 # success
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-10-03 21:32:17 +02:00
exit 1
2022-11-01 16:43:00 +01:00
}