-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysical_drives.go
237 lines (217 loc) · 6.08 KB
/
physical_drives.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
225
226
227
228
229
230
231
232
233
234
235
236
237
package hpmib
import (
"strconv"
)
// MediaType describes a physical drive's media type in the HP MIB.
type MediaType int
// PhysicalDriveStatus describes the state of a physical drive.
type PhysicalDriveStatus int
// SMARTStatus describes the state of a physical disk using S.M.A.R.T. semantics.
type SMARTStatus int
// Media types for physical drives defined by the HP MIB.
const (
MediaTypeUnknown MediaType = -1
MediaTypeOther MediaType = 1
MediaTypeRotatingPlatter MediaType = 2
MediaTypeSolidState MediaType = 3
MediaTypeSMR MediaType = 4
)
// Statuses for physical drives defined by the HP MIB.
const (
PhysicalDriveStatusUnknown PhysicalDriveStatus = -1
PhysicalDriveStatusOther PhysicalDriveStatus = 1
PhysicalDriveStatusOK PhysicalDriveStatus = 2
PhysicalDriveStatusFailed PhysicalDriveStatus = 3
PhysicalDriveStatusPredictiveFailure PhysicalDriveStatus = 4
PhysicalDriveStatusErasing PhysicalDriveStatus = 5
PhysicalDriveStatusEraseDone PhysicalDriveStatus = 6
PhysicalDriveStatusEraseQueued PhysicalDriveStatus = 7
PhysicalDriveStatusSSDWearOut PhysicalDriveStatus = 8
PhysicalDriveStatusNotAuthenticated PhysicalDriveStatus = 9
)
// S.M.A.R.T. statuses for physical drives defined by the HP MIB.
const (
SMARTStatusUnknown SMARTStatus = -1
SMARTStatusOther SMARTStatus = 1
SMARTStatusOK SMARTStatus = 2
SMARTStatusReplaceDrive SMARTStatus = 3
)
// Table defined by the HP MIB that contains the status of each physical drive.
const (
cpqDaPhyDrvCntlrIndex OID = "1.3.6.1.4.1.232.3.2.5.1.1.1"
cpqDaPhyDrvIndex OID = "1.3.6.1.4.1.232.3.2.5.1.1.2"
cpqDaPhyDrvModel OID = "1.3.6.1.4.1.232.3.2.5.1.1.3"
cpqDaPhyDrvStatus OID = "1.3.6.1.4.1.232.3.2.5.1.1.6"
cpqDaPhyDrvSize OID = "1.3.6.1.4.1.232.3.2.5.1.1.45"
cpqDaPhyDrvSerialNum OID = "1.3.6.1.4.1.232.3.2.5.1.1.51"
cpqDaPhyDrvSmartStatus OID = "1.3.6.1.4.1.232.3.2.5.1.1.57"
cpqDaPhyDrvLocation OID = "1.3.6.1.4.1.232.3.2.5.1.1.64"
cpqDaPhyDrvMediaType OID = "1.3.6.1.4.1.232.3.2.5.1.1.69"
)
// PhysicalDrive models a physical drive in the HP MIB.
type PhysicalDrive struct {
ID int
ControllerID int
CapacityMB int
MediaType MediaType
Location string
Model string
SerialNo string
SMARTStatus SMARTStatus
Status PhysicalDriveStatus
}
// PhysicalDrives returns a list of Physical Drives. Returns a non-nil error of the list of Physical
// Drives could not be determined.
func (m *MIB) PhysicalDrives() ([]PhysicalDrive, error) {
physicalDrives := []PhysicalDrive{}
columns := OIDList{
cpqDaPhyDrvCntlrIndex,
cpqDaPhyDrvIndex,
cpqDaPhyDrvModel,
cpqDaPhyDrvStatus,
cpqDaPhyDrvSize,
cpqDaPhyDrvSerialNum,
cpqDaPhyDrvSmartStatus,
cpqDaPhyDrvLocation,
cpqDaPhyDrvMediaType,
}
table, err := traverseTable(m.snmpClient, columns)
if err != nil {
return []PhysicalDrive{}, err
}
for _, row := range table {
cntlrIndex, err := strconv.Atoi(row[0])
if err != nil {
return []PhysicalDrive{}, err
}
index, err := strconv.Atoi(row[1])
if err != nil {
return []PhysicalDrive{}, err
}
model := prettifyString(row[2])
status := parsePhysicalDriveStatus(row[3])
size, err := strconv.Atoi(row[4])
if err != nil {
return []PhysicalDrive{}, err
}
serialNo := prettifyString(row[5])
smartStatus := parseSMARTStatus(row[6])
location := prettifyString(row[7])
mediaType := parseMediaType(row[8])
physicalDrives = append(physicalDrives, PhysicalDrive{
ControllerID: cntlrIndex,
ID: index,
Model: model,
Status: status,
CapacityMB: size,
SerialNo: serialNo,
SMARTStatus: smartStatus,
Location: location,
MediaType: mediaType,
})
}
return physicalDrives, nil
}
func parsePhysicalDriveStatus(s string) PhysicalDriveStatus {
switch s {
case "1":
return PhysicalDriveStatusOther
case "2":
return PhysicalDriveStatusOK
case "3":
return PhysicalDriveStatusFailed
case "4":
return PhysicalDriveStatusPredictiveFailure
case "5":
return PhysicalDriveStatusErasing
case "6":
return PhysicalDriveStatusEraseDone
case "7":
return PhysicalDriveStatusEraseQueued
case "8":
return PhysicalDriveStatusSSDWearOut
case "9":
return PhysicalDriveStatusNotAuthenticated
default:
return PhysicalDriveStatusUnknown
}
}
// String converts the PhysicalDriveStatus to a human readable string.
func (p *PhysicalDriveStatus) String() string {
switch *p {
case PhysicalDriveStatusOther:
return "Other"
case PhysicalDriveStatusOK:
return "OK"
case PhysicalDriveStatusFailed:
return "Failed"
case PhysicalDriveStatusPredictiveFailure:
return "Predictive Failure"
case PhysicalDriveStatusErasing:
return "Erasing"
case PhysicalDriveStatusEraseDone:
return "Erase Done"
case PhysicalDriveStatusEraseQueued:
return "Erase Queued"
case PhysicalDriveStatusSSDWearOut:
return "SSD Wear Out"
case PhysicalDriveStatusNotAuthenticated:
return "Not Authenticated"
default:
return "Unknown"
}
}
func parseMediaType(s string) MediaType {
switch s {
case "1":
return MediaTypeOther
case "2":
return MediaTypeRotatingPlatter
case "3":
return MediaTypeSolidState
case "4":
return MediaTypeSMR
default:
return MediaTypeUnknown
}
}
// String converts the MediaType to a human readable string.
func (m *MediaType) String() string {
switch *m {
case MediaTypeOther:
return "Other"
case MediaTypeRotatingPlatter:
return "Rotating Platter"
case MediaTypeSolidState:
return "Solid State"
case MediaTypeSMR:
return "SMR"
default:
return "Unknown"
}
}
func parseSMARTStatus(s string) SMARTStatus {
switch s {
case "1":
return SMARTStatusOther
case "2":
return SMARTStatusOK
case "3":
return SMARTStatusReplaceDrive
default:
return SMARTStatusUnknown
}
}
// String converts the SMARTStatus to a human readable string.
func (s *SMARTStatus) String() string {
switch *s {
case SMARTStatusOther:
return "Other"
case SMARTStatusOK:
return "OK"
case SMARTStatusReplaceDrive:
return "Replace Drive"
default:
return "Unknown"
}
}