summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/hpssa/parse.go
blob: 64d1c8ae92741760d911f634f060a277cc0abfd7 (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// SPDX-License-Identifier: GPL-3.0-or-later

package hpssa

import (
	"bufio"
	"bytes"
	"fmt"
	"strings"
)

type hpssaController struct {
	model                   string
	slot                    string
	serialNumber            string
	controllerStatus        string
	cacheBoardPresent       string
	cacheStatus             string
	cacheRatio              string
	batteryCapacitorCount   string
	batteryCapacitorStatus  string
	controllerTemperatureC  string
	cacheModuleTemperatureC string
	numberOfPorts           string
	driverName              string
	arrays                  map[string]*hpssaArray
	unassignedDrives        map[string]*hpssaPhysicalDrive
}

func (c *hpssaController) uniqueKey() string {
	return fmt.Sprintf("%s/%s", c.model, c.slot)
}

type hpssaArray struct {
	cntrl *hpssaController

	id            string
	interfaceType string
	unusedSpace   string
	usedSpace     string
	status        string
	arrayType     string
	logicalDrives map[string]*hpssaLogicalDrive
}

func (a *hpssaArray) uniqueKey() string {
	return fmt.Sprintf("%s/%s/%s", a.cntrl.model, a.cntrl.slot, a.id)
}

type hpssaLogicalDrive struct {
	cntrl *hpssaController
	arr   *hpssaArray

	id                string
	size              string
	status            string
	diskName          string
	uniqueIdentifier  string
	logicalDriveLabel string
	driveType         string
	physicalDrives    map[string]*hpssaPhysicalDrive
}

func (ld *hpssaLogicalDrive) uniqueKey() string {
	return fmt.Sprintf("%s/%s/%s/%s", ld.cntrl.model, ld.cntrl.slot, ld.arr.id, ld.id)
}

type hpssaPhysicalDrive struct {
	cntrl *hpssaController
	arr   *hpssaArray
	ld    *hpssaLogicalDrive

	location            string // port:box:bay
	status              string
	driveType           string
	interfaceType       string
	size                string
	serialNumber        string
	wwid                string
	model               string
	currentTemperatureC string
}

func (pd *hpssaPhysicalDrive) uniqueKey() string {
	return fmt.Sprintf("%s/%s/%s/%s/%s", pd.cntrl.model, pd.cntrl.slot, pd.arrId(), pd.ldId(), pd.location)
}

func (pd *hpssaPhysicalDrive) arrId() string {
	if pd.arr == nil {
		return "na"
	}
	return pd.arr.id
}

func (pd *hpssaPhysicalDrive) ldId() string {
	if pd.ld == nil {
		return "na"
	}
	return pd.ld.id
}

func parseSsacliControllersInfo(data []byte) (map[string]*hpssaController, error) {
	var (
		cntrl *hpssaController
		arr   *hpssaArray
		ld    *hpssaLogicalDrive
		pd    *hpssaPhysicalDrive

		line       string
		prevLine   string
		section    string
		unassigned bool
	)

	controllers := make(map[string]*hpssaController)

	sc := bufio.NewScanner(bytes.NewReader(data))

	for sc.Scan() {
		prevLine = line
		line = sc.Text()

		switch {
		case line == "":
			section = ""
			continue
		case strings.HasPrefix(line, "Smart Array"):
			section = "controller"

			v, err := parseControllerLine(line)
			if err != nil {
				return nil, err
			}

			cntrl = v
			controllers[cntrl.slot] = cntrl

			continue
		case strings.HasPrefix(line, "   Array:") && cntrl != nil:
			section = "array"
			unassigned = false

			arr = parseArrayLine(line)
			cntrl.arrays[arr.id] = arr

			continue
		case strings.HasPrefix(line, "      Logical Drive:") && cntrl != nil && arr != nil:
			section = "logical drive"

			ld = parseLogicalDriveLine(line)
			arr.logicalDrives[arr.id] = ld

			continue
		case strings.HasPrefix(line, "      physicaldrive") && prevLine == "":
			section = "physical drive"

			if unassigned && cntrl == nil {
				return nil, fmt.Errorf("unassigned drive but controller is nil (line '%s')", line)
			}
			if !unassigned && ld == nil {
				return nil, fmt.Errorf("assigned drive but logical device is nil (line '%s')", line)
			}

			v, err := parsePhysicalDriveLine(line)
			if err != nil {
				return nil, err
			}

			pd = v
			if unassigned {
				cntrl.unassignedDrives[pd.location] = pd
			} else {
				ld.physicalDrives[pd.location] = pd
			}

			continue
		case strings.HasPrefix(line, "   Unassigned"):
			unassigned = true
			continue
		}

		switch section {
		case "controller":
			parseControllerSectionLine(line, cntrl)
		case "array":
			parseArraySectionLine(line, arr)
		case "logical drive":
			parseLogicalDriveSectionLine(line, ld)
		case "physical drive":
			parsePhysicalDriveSectionLine(line, pd)
		}
	}

	if len(controllers) == 0 {
		return nil, fmt.Errorf("no controllers found")
	}

	updateHpssaHierarchy(controllers)

	return controllers, nil
}

func updateHpssaHierarchy(controllers map[string]*hpssaController) {
	for _, cntrl := range controllers {
		for _, pd := range cntrl.unassignedDrives {
			pd.cntrl = cntrl
		}
		for _, arr := range cntrl.arrays {
			arr.cntrl = cntrl
			for _, ld := range arr.logicalDrives {
				ld.cntrl = cntrl
				ld.arr = arr
				for _, pd := range ld.physicalDrives {
					pd.cntrl = cntrl
					pd.arr = arr
					pd.ld = ld
				}
			}
		}
	}
}

func parseControllerLine(line string) (*hpssaController, error) {
	parts := strings.Fields(strings.TrimPrefix(line, "Smart Array "))
	if len(parts) < 4 {
		return nil, fmt.Errorf("malformed Smart Array line: '%s'", line)
	}

	cntrl := &hpssaController{
		model:            parts[0],
		slot:             parts[3],
		arrays:           make(map[string]*hpssaArray),
		unassignedDrives: make(map[string]*hpssaPhysicalDrive),
	}

	return cntrl, nil
}

func parseArrayLine(line string) *hpssaArray {
	arr := &hpssaArray{
		id:            getColonSepValue(line),
		logicalDrives: make(map[string]*hpssaLogicalDrive),
	}

	return arr
}

func parseLogicalDriveLine(line string) *hpssaLogicalDrive {
	ld := &hpssaLogicalDrive{
		id:             getColonSepValue(line),
		physicalDrives: make(map[string]*hpssaPhysicalDrive),
	}

	return ld
}

func parsePhysicalDriveLine(line string) (*hpssaPhysicalDrive, error) {
	parts := strings.Fields(strings.TrimSpace(line))
	if len(parts) != 2 {
		return nil, fmt.Errorf("malformed physicaldrive line: '%s'", line)
	}

	pd := &hpssaPhysicalDrive{
		location: parts[1],
	}

	return pd, nil
}

func parseControllerSectionLine(line string, cntrl *hpssaController) {
	indent := strings.Repeat(" ", 3)

	switch {
	case strings.HasPrefix(line, indent+"Serial Number:"):
		cntrl.serialNumber = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Controller Status:"):
		cntrl.controllerStatus = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Cache Board Present:"):
		cntrl.cacheBoardPresent = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Cache Status:"):
		cntrl.cacheStatus = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Cache Ratio:"):
		cntrl.cacheRatio = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Controller Temperature (C):"):
		cntrl.controllerTemperatureC = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Cache Module Temperature (C):"):
		cntrl.cacheModuleTemperatureC = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Number of Ports:"):
		cntrl.numberOfPorts = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Driver Name:"):
		cntrl.driverName = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Battery/Capacitor Count:"):
		cntrl.batteryCapacitorCount = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Battery/Capacitor Status:"):
		cntrl.batteryCapacitorStatus = getColonSepValue(line)
	}
}

func parseArraySectionLine(line string, arr *hpssaArray) {
	indent := strings.Repeat(" ", 6)

	switch {
	case strings.HasPrefix(line, indent+"Interface Type:"):
		arr.interfaceType = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Unused Space:"):
		arr.unusedSpace = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Used Space:"):
		arr.usedSpace = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Status:"):
		arr.status = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Array Type:"):
		arr.arrayType = getColonSepValue(line)
	}
}

func parseLogicalDriveSectionLine(line string, ld *hpssaLogicalDrive) {
	indent := strings.Repeat(" ", 9)

	switch {
	case strings.HasPrefix(line, indent+"Size:"):
		ld.size = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Status:"):
		ld.status = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Disk Name:"):
		ld.diskName = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Unique Identifier:"):
		ld.uniqueIdentifier = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Logical Drive Label:"):
		ld.logicalDriveLabel = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Drive Type:"):
		ld.driveType = getColonSepValue(line)
	}
}

func parsePhysicalDriveSectionLine(line string, pd *hpssaPhysicalDrive) {
	indent := strings.Repeat(" ", 9)

	switch {
	case strings.HasPrefix(line, indent+"Status:"):
		pd.status = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Drive Type:"):
		pd.driveType = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Interface Type:"):
		pd.interfaceType = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Size:"):
		pd.size = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Serial Number:"):
		pd.serialNumber = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"WWID:"):
		pd.wwid = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Model:"):
		pd.model = getColonSepValue(line)
	case strings.HasPrefix(line, indent+"Current Temperature (C):"):
		pd.currentTemperatureC = getColonSepValue(line)
	}
}

func getColonSepValue(line string) string {
	i := strings.IndexByte(line, ':')
	if i == -1 {
		return ""
	}
	return strings.TrimSpace(line[i+1:])
}