簡體   English   中英

如何讀取HDD SMART屬性?

[英]How to read HDD S.M.A.R.T. attributes?

我會在我的 Windows 7 客戶端上監控智能硬盤。

我會在不使用任何 vbs 文件或現成的工具的情況下獲得 HDD 智能屬性,只看 WMI 或 PowerShell。

我將使用 ZABBIX 監控服務器(使用zabbix-sender.exe )聚合該數據。

我找到了一個或多或少的 Linux 解決方案,但我會監控 Windows 7 機器的硬盤。

有人有想法嗎?

像這樣使用 WMI API 訪問 SMART 數據,

gwmi -namespace root\wmi -class MSStorageDriver_FailurePredictStatus

還有更多的例子在網。

---前言: ---

我相信 PowerShell 解決方案可以滿足您的需求。 不幸的是,似乎沒有一種方法可以僅通過 PowerShell 從各種存儲設備獲取所有可用的 SMART 信息,因為 PowerShell 是該功能的一種相當通用的實現,並且 SMART 在其實現方面因存儲設備供應商而異。

話雖如此,下面描述的方法應該滿足用戶執行的典型 SMART 檢查的關鍵要求,包括預測壽命、重新分配和不可糾正的扇區等,盡管使用了相當通用的 PowerShell 術語(例如 lifespan = "Wear")。

---信息: ---

結合兩個 PowerShell cmdlet,我們可以輕松查看存儲設備提供的一些 SMART 數據:

獲取存儲可靠性計數器

Get-StorageReliabilityCounter cmdlet 獲取指定磁盤或物理磁盤的存儲可靠性計數器。這些計數器包括有關設備溫度、錯誤、磨損和設備使用時間長度等信息。”

這是實際返回我們尋找的 SMART 數據的 cmdlet。 但是,與您可能熟悉的許多其他 cmdlet 不同,此 cmdlet 需要通過 PowerShell 對象指向目標磁盤。 (如果您是 PowerShell 的新手,這並不像聽起來那么復雜,所以不要害怕。)

獲取磁盤

Get-Disk cmdlet 獲取操作系統可見的一個或多個磁盤對象,或可選的過濾列表。”

這是我們將用來提供所需 PowerShell 對象的 cmdlet,以便 Get-StorageReliabilityCounter 知道要查詢的磁盤。

---代碼: ---

與任何事情一樣,有多種方法可以實際執行代碼,因此在我看來,我將提供代碼以盡可能以最簡單的方式獲取所需信息。

有關所有本地磁盤上的簡單 SMART 信息(以管理員身份運行):

Get-Disk | Get-StorageReliabilityCounter

示例輸出:

PS C:\WINDOWS\system32> Get-Disk | Get-StorageReliabilityCounter

DeviceId Temperature ReadErrorsUncorrected Wear PowerOnHours
-------- ----------- --------------------- ---- ------------
1                    0                     0    5505
2                    0                     0    572
0                                          0    2799

有關所有本地磁盤上的擴展 SMART 信息(以管理員身份運行):

 Get-Disk | Get-StorageReliabilityCounter | Select-Object -Property "*"

截斷的樣本輸出:

PS C:\WINDOWS\system32> Get-Disk | Get-StorageReliabilityCounter | Select-Object -Property "*"

DeviceId                : 1
FlushLatencyMax         : 46
LoadUnloadCycleCount    :
LoadUnloadCycleCountMax :
ManufactureDate         :
PowerOnHours            : 5505
ReadErrorsCorrected     : 0
ReadErrorsTotal         : 0
ReadErrorsUncorrected   : 0
ReadLatencyMax          : 231
StartStopCycleCount     :
StartStopCycleCountMax  :
Temperature             : 27
TemperatureMax          : 0
Wear                    : 0
WriteErrorsCorrected    :
WriteErrorsTotal        :
WriteErrorsUncorrected  :
WriteLatencyMax         : 69
PSComputerName          :

如您所見,列出了一些可能允許或不允許您規避災難的理想指標。

--- tl;博士: ---

Get-Disk | Get-StorageReliabilityCounter

或者

Get-Disk | Get-StorageReliabilityCounter | Select-Object -Property "*"

作為管理員獲取最重要的 SMART 信息。

這是一個從 smartctl (smartmontools) 輸出中提取所有屬性數據的 powershell 腳本。 如果 smartctl 不在您的 %path% 中,請調整它的路徑。

它可以像這樣使用:

.\get-smart.ps1 -Drive hda -AttributeId 5,241 -Property Name,Raw -FriendlyOutput

要不就

.\get-smart.ps1 hda 5,241 Name,Raw -f

等。如果您指定 -FriendlyOutput 它將數據格式化為表格,否則它會給您一個對象。 如果您只對特定值感興趣,請使用

.\get-smart.ps1 hda 241 Raw

請注意,如果 smartctl 未以管理員身份運行,則某些屬性(例如閾值)將不存在。

還沒有異常處理! 你被警告了!

param(
    [Parameter(Mandatory=$True)]
    [string]    $Drive,
    [int[]]     $AttributeId,
    [string[]]  $Property,
    [switch]    $FriendlyOutput)
# parses attribute table in smartctl output and builds an object
$smart = [string[]](smartctl -A $Drive)
$attributes=@()
foreach ($s in $smart) {
    if ($s -match '^\s*(\d+)\s+(\w+)\s+(\w+)\s+(\d+)\s+(\d+)\s+([\d-]+)\s+([\w-]+)\s+(\w+)\s+([\w-]+)\s+(\d+)') {
        $o = new-object -Typename PSObject
        add-member -in $o -m NoteProperty -name 'ID' -value ([int]$matches[1])
        add-member -in $o -m NoteProperty -name 'Name' -value $matches[2]
        add-member -in $o -m NoteProperty -name 'Flag' -value $matches[3]
        add-member -in $o -m NoteProperty -name 'Value' -value ([int]$matches[4])
        add-member -in $o -m NoteProperty -name 'Worst' -value ([int]$matches[5])
        add-member -in $o -m NoteProperty -name 'Threshold' -value ([int]$matches[6])
        add-member -in $o -m NoteProperty -name 'Type' -value $matches[7]
        add-member -in $o -m NoteProperty -name 'Updated' -value $matches[8]
        add-member -in $o -m NoteProperty -name 'WhenFailed' -value $matches[9]
        add-member -in $o -m NoteProperty -name 'Raw' -value ([int64]$matches[10])
        $attributes += $o
    }
}
if ($AttributeId){
    $attributes = $attributes | ? {$_.id -in $attributeid}
}
if ($Property){
    if ($property.count -gt 1 -and $attributes.count -gt -0 -and $Property -notcontains 'id'){
        # if more than one result and more than one attribute, add the ID to the output
        $property = ,'id'+$Property
    }
    $attributes = $attributes | select $Property
}
if (@($attributes).count -eq 1 -and @($attributes.psobject.properties).count -eq 1){
    # return single values directly instead of an object
    $attributes.psobject.properties.value
} elseif ($FriendlyOutput){
    $attributes | ft * -a
} else {
    $attributes
}

使用Smartmontools的 JSON 輸出模式可以輕松完成:

以下簡短的 Powershell 腳本讀取第一個磁盤(在 smartmontools 中名為/dev/sda )的 SMART 屬性並選擇 ID 為 241 的屬性(即我的 SSD 上的Total_LBAs_Written )。

最后一行轉換LBA值到TBW值(T時代YTE(S)w ^雷農)。

$json = (smartctl -A -j /dev/sda | ConvertFrom-JSON)
$lbaRaw = ($json.ata_smart_attributes.table | Where id -eq 241 | Select-Object raw)
$tbw = $lbaRaw.raw.value * 512 / 1024 / 1024 / 1024 / 1024
$tbw.toString("##.## TB")

這將獲得一個表中 SMART 值的有用視圖,因此您可以映射哪個驅動器具有哪些值。

$Disks = Get-PhysicalDisk
$DiskTable = $Disks | Select-Object -Property FriendlyName,SerialNumber,UniqueId,FirmwareVersion,
    MediaType,Size,PhysicalSectorSize,LogicalSectorSize,
    @{Name="ReadErrorsCorrected";Expression={""}},@{Name="ReadErrorsUncorrected";Expression={""}},
    @{Name="ReadLatencyMax";Expression={""}},@{Name="WriteLatencyMax";Expression={""}},
    @{Name="Temperature";Expression={""}},@{Name="PowerOnHours";Expression={""}},
    @{Name="Wear";Expression={""}}
for ( $i=0 ; $i -lt $Disks.Count ; $i++ ) {
    $DiskReliability = $Disks[$i] | Get-StorageReliabilityCounter | 
        Select-Object -Property ReadErrorsCorrected,ReadErrorsUncorrected,ReadLatencyMax,
            WriteLatencyMax,Temperature,PowerOnHours,Wear
    $DiskTable[$i].ReadErrorsCorrected = $DiskReliability.ReadErrorsCorrected
    $DiskTable[$i].ReadErrorsUncorrected = $DiskReliability.ReadErrorsUncorrected
    $DiskTable[$i].ReadLatencyMax = $DiskReliability.ReadLatencyMax
    $DiskTable[$i].WriteLatencyMax = $DiskReliability.WriteLatencyMax
    $DiskTable[$i].Temperature = $DiskReliability.Temperature
    $DiskTable[$i].PowerOnHours = $DiskReliability.PowerOnHours
    $DiskTable[$i].Wear = $DiskReliability.Wear
}
$DiskTable | Out-GridView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM