-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.go
224 lines (186 loc) · 5.55 KB
/
disk.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"bufio"
"fmt"
"os/exec"
"strings"
"syscall"
hierr "github.com/reconquest/hierr-go"
)
func hasBit(n int64, pos uint) bool {
val := n & (1 << pos)
return (val > 0)
}
func errorFromBitMask(diskName string, err error) error {
var exitCodeSmart = []string{
"Bit 0: Command line did not parse",
`Bit 1: Device open failed, device did not return an IDENTIFY
DEVICE structure, or device is in a low-power mode (see '-n' option above)`,
`Bit 2: Some SMART command to the disk failed, or there was a
checksum error in a SMART data structure (see '-b' option above)`,
"Bit 3: SMART status check returned DISK FAILING",
"Bit 4: We found prefail Attributes <= threshold",
`Bit 5: SMART status check returned DISK OK but we found that
some (usage or prefail) Attributes have been <= threshold
at some time in the past`,
"Bit 6: The device error log contains records of errors",
`Bit 7: The device self-test log contains records of errors.
[ATA only] Failed self-tests outdated by a newer successful extended
self-test are ignored`,
}
var errorSmart string
var waitStatus syscall.WaitStatus
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
bitMaskInt64 := int64(waitStatus.ExitStatus())
for i := uint(0); i < uint(len(exitCodeSmart)); i++ {
if hasBit(bitMaskInt64, i) {
errorSmart = fmt.Sprintf("%s%s\n",
errorSmart,
exitCodeSmart[i],
)
if i <= 1 {
hierr.Fatalf(
fmt.Errorf(strings.TrimRight(errorSmart, "\n")),
"can't get stats for %s disk",
diskName,
)
}
}
}
return hierr.Errorf(
fmt.Errorf(strings.TrimRight(errorSmart, "\n")),
"smartctl return not null exit code for %s disk",
diskName,
)
}
return hierr.Errorf(
fmt.Errorf("can't get abnormal exit code"),
"smartctl return not null exit code for %s disk",
diskName,
)
}
func getDiskStats(diskInfo map[string]string) (map[string]string, error) {
args := []string{smartctl, "-a"}
args = append(args, strings.Split(diskInfo["name"], " ")...)
out, err := exec.Command(sudo, args...).CombinedOutput()
var exitMsg error
if err != nil {
exitMsg = errorFromBitMask(diskInfo["name"], err)
}
if diskInfo["interface"] == SAS {
return getSASStats(diskInfo, string(out)), exitMsg
}
return getSATAStats(diskInfo, string(out)), exitMsg
}
func getSASStats(
diskInfo map[string]string,
rawStats string,
) map[string]string {
diskStats := make(map[string]string)
scanner := bufio.NewScanner(strings.NewReader(rawStats))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Vendor:") {
metric := strings.Split(line, ":")[1]
diskStats["diskIDSAS"] = strings.TrimSpace(metric)
}
if strings.Contains(line, "Product:") {
metric := strings.Split(line, ":")[1]
diskStats["diskIDSAS"] = fmt.Sprintf(
"%s_%s",
diskStats["diskIDSAS"],
strings.TrimSpace(metric),
)
}
if strings.Contains(line, "Serial number:") {
metric := strings.Split(line, ":")[1]
diskStats["diskIDSAS"] = fmt.Sprintf(
"%s-%s",
diskStats["diskIDSAS"],
strings.TrimSpace(metric),
)
}
if strings.Contains(line, "Health Status") {
metric := strings.Split(line, ":")[1]
diskStats["healthStatus"] = strings.TrimSpace(metric)
}
if strings.Contains(line, "Current Drive Temperature") {
metric := strings.Replace(strings.Split(line, ":")[1], "C", "", -1)
diskStats["currentDriveTemp"] = strings.TrimSpace(metric)
}
if strings.Contains(line, "Elements in grown defect list") {
metric := strings.Split(line, ":")[1]
diskStats["elementsInGrownDefectList"] = strings.TrimSpace(metric)
}
if strings.Contains(line, "Non-medium error count") {
metric := strings.Split(line, ":")[1]
diskStats["non-mediumErrorCount"] = strings.TrimSpace(metric)
}
if strings.Contains(line, "read:") {
metric := strings.Fields(line)[7]
diskStats["uncorrectedErrorsRead"] = strings.TrimSpace(metric)
}
if strings.Contains(line, "write:") {
metric := strings.Fields(line)[7]
diskStats["uncorrectedErrorsWrite"] = strings.TrimSpace(metric)
}
if strings.Contains(line, "verify:") {
metric := strings.Fields(line)[7]
diskStats["uncorrectedErrorsVerify"] = strings.TrimSpace(metric)
}
}
return diskStats
}
func getSATAStats(
diskInfo map[string]string,
rawStats string,
) map[string]string {
diskStats := make(map[string]string)
scanner := bufio.NewScanner(strings.NewReader(rawStats))
for scanner.Scan() {
line := scanner.Text()
attribute := strings.Fields(line)
if strings.Contains(line, "Device Model:") {
metric := strings.Split(line, ":")[1]
diskStats["diskIDSATA"] = strings.Replace(
strings.TrimSpace(metric),
" ",
"_",
-1,
)
}
if strings.Contains(line, "Serial Number:") {
metric := strings.Split(line, ":")[1]
diskStats["diskIDSATA"] = fmt.Sprintf(
"%s-%s",
diskStats["diskIDSATA"],
strings.TrimSpace(metric),
)
}
if len(attribute) >= 9 {
if attribute[0] == "5" {
diskStats["reallocatedSectorCount"] = attribute[9]
}
if attribute[0] == "184" {
diskStats["endToEndError"] = attribute[9]
}
if attribute[0] == "187" {
diskStats["reportedUncorrectableErrors"] = attribute[9]
}
if attribute[0] == "194" {
diskStats["temperature"] = attribute[9]
}
if attribute[0] == "197" {
diskStats["currentPendingSectorCount"] = attribute[9]
}
if attribute[0] == "199" {
diskStats["CRCErrorCount"] = attribute[9]
}
if attribute[0] == "233" {
diskStats["mediaWearoutIndicator"] = attribute[3]
}
}
}
return diskStats
}