master
go 100 lines 2.17 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package adaptecraid
4
5 import (
6 "bufio"
7 "bytes"
8 "errors"
9 "fmt"
10 "strings"
11 )
12
13 type logicalDevice struct {
14 number string
15 name string
16 raidLevel string
17 status string
18 failedStripes string
19 }
20
21 func (c *Collector) collectLogicalDevices(mx map[string]int64) error {
22 bs, err := c.exec.logicalDevicesInfo()
23 if err != nil {
24 return err
25 }
26
27 devices, err := parseLogicDevInfo(bs)
28 if err != nil {
29 return err
30 }
31
32 if len(devices) == 0 {
33 return errors.New("no logical devices found")
34 }
35
36 for _, ld := range devices {
37 if !c.lds[ld.number] {
38 c.lds[ld.number] = true
39 c.addLogicalDeviceCharts(ld)
40 }
41
42 px := fmt.Sprintf("ld_%s_", ld.number)
43
44 // Unfortunately, all available states are unknown.
45 mx[px+"health_state_ok"] = 0
46 mx[px+"health_state_critical"] = 0
47 if isOkLDStatus(ld) {
48 mx[px+"health_state_ok"] = 1
49 } else {
50 mx[px+"health_state_critical"] = 1
51 }
52 }
53
54 return nil
55 }
56
57 func isOkLDStatus(ld *logicalDevice) bool {
58 // https://github.com/thomas-krenn/check_adaptec_raid/blob/a104fd88deede87df4f07403b44394bffb30c5c3/check_adaptec_raid#L340
59 return ld.status == "Optimal"
60 }
61
62 func parseLogicDevInfo(bs []byte) (map[string]*logicalDevice, error) {
63 devices := make(map[string]*logicalDevice)
64
65 var ld *logicalDevice
66
67 sc := bufio.NewScanner(bytes.NewReader(bs))
68
69 for sc.Scan() {
70 line := strings.TrimSpace(sc.Text())
71
72 if strings.HasPrefix(line, "Logical device number") ||
73 strings.HasPrefix(line, "Logical Device number") {
74 parts := strings.Fields(line)
75 num := parts[len(parts)-1]
76 ld = &logicalDevice{number: num}
77 devices[num] = ld
78 continue
79 }
80
81 if ld == nil {
82 continue
83 }
84
85 switch {
86 case strings.HasPrefix(line, "Logical device name"),
87 strings.HasPrefix(line, "Logical Device name"):
88 ld.name = getColonSepValue(line)
89 case strings.HasPrefix(line, "RAID level"):
90 ld.raidLevel = getColonSepValue(line)
91 case strings.HasPrefix(line, "Status of logical device"),
92 strings.HasPrefix(line, "Status of Logical Device"):
93 ld.status = getColonSepValue(line)
94 case strings.HasPrefix(line, "Failed stripes"):
95 ld.failedStripes = getColonSepValue(line)
96 }
97 }
98
99 return devices, nil
100 }