-
Notifications
You must be signed in to change notification settings - Fork 15
/
smartctl-storage-discovery.ps1
129 lines (113 loc) · 4.27 KB
/
smartctl-storage-discovery.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<#
.VERSION
0.7
.DESCRIPTION
Author: Nikitin Maksim
Github: https://github.com/nikimaxim/zbx-smartmonitor.git
Note: Zabbix lld for smartctl
.TESTING
PowerShell: 5.1 and later
Smartmontools: 7.1 and later
OS: Windows 7, Windows 8, Windows 10
#>
$CTL = "C:\Program Files\smartmontools\bin\smartctl.exe"
if ((Get-Command $CTL -ErrorAction SilentlyContinue) -eq $null) {
Write-Host "Could not find path: $CTL"
exit
}
$idx = 0
$json = ""
$disk_sn_all = ""
$smart_scan = & $CTL "--scan-open"
Write-Host "{"
Write-Host " `"data`":["
foreach ($device in $smart_scan) {
# Remove non-working disks
# Example: "# /dev/sdb -d scsi"
if (!$device.StartsWith("# ")) {
$storage_sn = ""
$storage_model = ""
$storage_name = ""
$storage_args = ""
$storage_smart = 0
$storage_type = 0
# Extract and concatenate args
if ($device -match '(-d) ([A-Za-z0-9/,\+]+)') {
$storage_args = $matches[1] + $matches[2]
}
# Get device name
$storage_name = $device.Substring(0, $device.IndexOf(" "))
$info = & $CTL "-i" $storage_name $storage_args
# Get device SN
$sn = ($info | Select-String "serial number:") -ireplace "serial number:"
# Check empty SN
if ($sn) {
# Number disks
$idx++
$storage_sn = $sn.Trim()
# Check duplicate storage
if (!$disk_sn_all.Contains($storage_sn)) {
if ($disk_sn_all) {
$disk_sn_all += "," + $storage_sn
} else {
$disk_sn_all = $storage_sn
}
# Device SMART
if ($info | Select-String "SMART.+Enabled$") {
$storage_smart = 1
}
# Get device model(For different types of devices)
$d = (($info | Select-String "Device Model:") -replace "Device Model:")
if ($d) {
$storage_model = $d.Trim()
} else {
$m = (($info | Select-String "Model Number:") -replace "Model Number:")
if ($m) {
$storage_model = $m.Trim()
} else {
$p = (($info | Select-String "Product:") -replace "Product:")
if ($p) {
$storage_model = $p.Trim()
} else {
$v = (($info | Select-String "Vendor:") -replace "Vendor:")
if ($v) {
$storage_model = $v.Trim()
} else {
$storage_model = "Not find!"
}
}
}
}
# Get device type:
# - 0 is for HDD
# - 1 is for SSD
# - 2 is for NVMe
$rotation_rate = $info | Select-String "Rotation Rate:"
if ($rotation_rate -like "*rpm*") {
$storage_type = 0
} elseif ($rotation_rate -like "*Solid State Device*") {
$storage_type = 1
} elseif ($storage_args -like "*nvme*" -or $storage_name -like "*nvme*") {
# Device NVMe and SMART
$storage_type = 2
$storage_smart = 1
}
# Adding a split sign when multiple disks are detected
if ($idx -ne 1) {
$json += ",`n"
}
$json += "`t {`n " +
"`t`t`"{#STORAGE.SN}`":`"" + $storage_sn + "`"" + ",`n" +
"`t`t`"{#STORAGE.MODEL}`":`"" + $storage_model + "`"" + ",`n" +
"`t`t`"{#STORAGE.NAME}`":`"" + $storage_name + "`"" + ",`n" +
"`t`t`"{#STORAGE.CMD}`":`"" + $storage_name + " " + $storage_args + "`"" + ",`n" +
"`t`t`"{#STORAGE.SMART}`":`"" + $storage_smart + "`"" + ",`n" +
"`t`t`"{#STORAGE.TYPE}`":`"" + $storage_type + "`"" + "`n" +
"`t }"
}
}
}
}
Write-Host $json
Write-Host " ]"
Write-Host "}"