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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package megacli
import (
"bufio"
"bytes"
"fmt"
"strings"
)
type (
megaAdapter struct {
number string
name string
state string
physDrives map[string]*megaPhysDrive
}
megaPhysDrive struct {
adapterNumber string
number string
wwn string
slotNumber string
drivePosition string
pdType string
mediaErrorCount string
predictiveFailureCount string
}
)
var adapterStates = []string{
"optimal",
"degraded",
"partially_degraded",
"failed",
}
func (m *MegaCli) collectPhysDrives(mx map[string]int64) error {
bs, err := m.exec.physDrivesInfo()
if err != nil {
return err
}
adapters, err := parsePhysDrivesInfo(bs)
if err != nil {
return err
}
for _, ad := range adapters {
if !m.adapters[ad.number] {
m.adapters[ad.number] = true
m.addAdapterCharts(ad)
}
px := fmt.Sprintf("adapter_%s_health_state_", ad.number)
for _, st := range adapterStates {
mx[px+st] = 0
}
st := strings.ReplaceAll(strings.ToLower(ad.state), " ", "_")
mx[px+st] = 1
for _, pd := range ad.physDrives {
if !m.adapters[pd.wwn] {
m.adapters[pd.wwn] = true
m.addPhysDriveCharts(pd)
}
px := fmt.Sprintf("phys_drive_%s_", pd.wwn)
writeInt(mx, px+"media_error_count", pd.mediaErrorCount)
writeInt(mx, px+"predictive_failure_count", pd.predictiveFailureCount)
}
}
return nil
}
func parsePhysDrivesInfo(bs []byte) (map[string]*megaAdapter, error) {
adapters := make(map[string]*megaAdapter)
var ad *megaAdapter
var pd *megaPhysDrive
sc := bufio.NewScanner(bytes.NewReader(bs))
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
switch {
case strings.HasPrefix(line, "Adapter #"):
idx := strings.TrimPrefix(line, "Adapter #")
ad = &megaAdapter{number: idx, physDrives: make(map[string]*megaPhysDrive)}
adapters[idx] = ad
case strings.HasPrefix(line, "Name") && ad != nil:
ad.name = getColonSepValue(line)
case strings.HasPrefix(line, "State") && ad != nil:
ad.state = getColonSepValue(line)
case strings.HasPrefix(line, "PD:") && ad != nil:
if parts := strings.Fields(line); len(parts) == 3 {
idx := parts[1]
pd = &megaPhysDrive{number: idx, adapterNumber: ad.number}
ad.physDrives[idx] = pd
}
case strings.HasPrefix(line, "Slot Number:") && pd != nil:
pd.slotNumber = getColonSepValue(line)
case strings.HasPrefix(line, "Drive's position:") && pd != nil:
pd.drivePosition = getColonSepValue(line)
case strings.HasPrefix(line, "WWN:") && pd != nil:
pd.wwn = getColonSepValue(line)
case strings.HasPrefix(line, "PD Type:") && pd != nil:
pd.pdType = getColonSepValue(line)
case strings.HasPrefix(line, "Media Error Count:") && pd != nil:
pd.mediaErrorCount = getColonSepNumValue(line)
case strings.HasPrefix(line, "Predictive Failure Count:") && pd != nil:
pd.predictiveFailureCount = getColonSepNumValue(line)
}
}
return adapters, nil
}
|