summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/storcli/collect_drives.go
blob: 95965d572e076831e3eb074c14f375aea0ff04b2 (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
// SPDX-License-Identifier: GPL-3.0-or-later

package storcli

import (
	"encoding/json"
	"errors"
	"fmt"
	"strconv"
	"strings"
)

type drivesInfoResponse struct {
	Controllers []struct {
		CommandStatus struct {
			Controller int    `json:"Controller"`
			Status     string `json:"Status"`
		} `json:"Command Status"`
		ResponseData map[string]json.RawMessage `json:"Response Data"`
	} `json:"Controllers"`
}

type (
	driveInfo struct {
		EIDSlt string `json:"EID:Slt"`
		//DID    int    `json:"DID"`
		//State  string `json:"State"`
		//DG     int    `json:"DG"` // FIX: can be integer or "-"
		//Size   string `json:"Size"`
		//Intf   string `json:"Intf"`
		Med string `json:"Med"`
		//SED    string `json:"SED"`
		//PI     string `json:"PI"`
		//SeSz   string `json:"SeSz"`
		//Model  string `json:"Model"`
		//Sp     string `json:"Sp"`
		//Type   string `json:"Type"`
	}
	driveState struct {
		MediaErrorCount        storNumber `json:"Media Error Count"`
		OtherErrorCount        storNumber `json:"Other Error Count"`
		DriveTemperature       string     `json:"Drive Temperature"`
		PredictiveFailureCount storNumber `json:"Predictive Failure Count"`
		SmartAlertFlagged      string     `json:"S.M.A.R.T alert flagged by drive"`
	}
	driveAttrs struct {
		WWN         string `json:"WWN"`
		DeviceSpeed string `json:"Device Speed"`
		LinkSpeed   string `json:"Link Speed"`
	}
)

type storNumber string // some int values can be 'N/A'

func (n *storNumber) UnmarshalJSON(b []byte) error { *n = storNumber(b); return nil }

func (s *StorCli) collectMegaRaidDrives(mx map[string]int64, resp *drivesInfoResponse) error {
	if resp == nil {
		return nil
	}

	for _, cntrl := range resp.Controllers {
		var ids []string
		for k := range cntrl.ResponseData {
			if !strings.HasSuffix(k, "Detailed Information") {
				continue
			}
			parts := strings.Fields(k) // Drive /c0/e252/s0 - Detailed Information
			if len(parts) < 2 {
				continue
			}
			id := parts[1]
			if strings.IndexByte(id, '/') == -1 {
				continue
			}
			ids = append(ids, id)
		}

		cntrlIdx := cntrl.CommandStatus.Controller

		for _, id := range ids {
			info, err := getDriveInfo(cntrl.ResponseData, id)
			if err != nil {
				return err
			}
			data, err := getDriveDetailedInfo(cntrl.ResponseData, id)
			if err != nil {
				return err
			}
			state, err := getDriveState(data, id)
			if err != nil {
				return err
			}
			attrs, err := getDriveAttrs(data, id)
			if err != nil {
				return err
			}

			if attrs.WWN == "" {
				continue
			}

			if !s.drives[attrs.WWN] {
				s.drives[attrs.WWN] = true
				s.addPhysDriveCharts(cntrlIdx, info, state, attrs)
			}

			px := fmt.Sprintf("phys_drive_%s_cntrl_%d_", attrs.WWN, cntrlIdx)

			if v, ok := parseInt(string(state.MediaErrorCount)); ok {
				mx[px+"media_error_count"] = v
			}
			if v, ok := parseInt(string(state.OtherErrorCount)); ok {
				mx[px+"other_error_count"] = v
			}
			if v, ok := parseInt(string(state.PredictiveFailureCount)); ok {
				mx[px+"predictive_failure_count"] = v
			}
			if v, ok := parseInt(getTemperature(state.DriveTemperature)); ok {
				mx[px+"temperature"] = v
			}
			for _, st := range []string{"active", "inactive"} {
				mx[px+"smart_alert_status_"+st] = 0
			}
			if state.SmartAlertFlagged == "Yes" {
				mx[px+"smart_alert_status_active"] = 1
			} else {
				mx[px+"smart_alert_status_inactive"] = 1
			}
		}
	}

	return nil
}

func (s *StorCli) queryDrivesInfo() (*drivesInfoResponse, error) {
	bs, err := s.exec.drivesInfo()
	if err != nil {
		return nil, err
	}

	if len(bs) == 0 {
		return nil, errors.New("empty response")
	}

	var resp drivesInfoResponse
	if err := json.Unmarshal(bs, &resp); err != nil {
		return nil, err
	}

	if len(resp.Controllers) == 0 {
		return nil, errors.New("no controllers found")
	}
	if st := resp.Controllers[0].CommandStatus.Status; st != "Success" {
		return nil, fmt.Errorf("command status error: %s", st)
	}

	return &resp, nil
}

func getDriveInfo(respData map[string]json.RawMessage, id string) (*driveInfo, error) {
	k := fmt.Sprintf("Drive %s", id)
	raw, ok := respData[k]
	if !ok {
		return nil, fmt.Errorf("drive info not found for '%s'", id)
	}

	var drive []driveInfo
	if err := json.Unmarshal(raw, &drive); err != nil {
		return nil, err
	}

	if len(drive) == 0 {
		return nil, fmt.Errorf("drive info not found for '%s'", id)
	}

	return &drive[0], nil
}

func getDriveDetailedInfo(respData map[string]json.RawMessage, id string) (map[string]json.RawMessage, error) {
	k := fmt.Sprintf("Drive %s - Detailed Information", id)
	raw, ok := respData[k]
	if !ok {
		return nil, fmt.Errorf("drive detailed info not found for '%s'", id)
	}

	var info map[string]json.RawMessage
	if err := json.Unmarshal(raw, &info); err != nil {
		return nil, err
	}

	return info, nil
}

func getDriveState(driveDetailedInfo map[string]json.RawMessage, id string) (*driveState, error) {
	k := fmt.Sprintf("Drive %s State", id)
	raw, ok := driveDetailedInfo[k]
	if !ok {
		return nil, fmt.Errorf("drive detailed info state not found for '%s'", id)
	}

	var state driveState
	if err := json.Unmarshal(raw, &state); err != nil {
		return nil, err
	}

	return &state, nil
}

func getDriveAttrs(driveDetailedInfo map[string]json.RawMessage, id string) (*driveAttrs, error) {
	k := fmt.Sprintf("Drive %s Device attributes", id)
	raw, ok := driveDetailedInfo[k]
	if !ok {
		return nil, fmt.Errorf("drive detailed info state not found for '%s'", id)
	}

	var state driveAttrs
	if err := json.Unmarshal(raw, &state); err != nil {
		return nil, err
	}

	return &state, nil
}

func getTemperature(temp string) string {
	// ' 28C (82.40 F)' (drive) or '33C' (bbu)
	i := strings.IndexByte(temp, 'C')
	if i == -1 {
		return ""
	}
	return strings.TrimSpace(temp[:i])
}

func parseInt(s string) (int64, bool) {
	i, err := strconv.ParseInt(s, 10, 64)
	return i, err == nil
}