-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdisk.go
More file actions
308 lines (290 loc) · 8.88 KB
/
Copy pathdisk.go
File metadata and controls
308 lines (290 loc) · 8.88 KB
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
/*
* Copyright (c) 2023 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Project: Curve-Manager
* Created Date: 2023-02-15
* Author: wanghai (SeanHai)
*/
package common
import (
"fmt"
"strconv"
"strings"
comm "github.com/opencurve/curve-manager/internal/common"
)
const (
// disk info
NODE_DISK_INFO = "node_disk_info"
NODE_DISK_READ_COMPLETED_TOTAL = "node_disk_reads_completed_total"
NODE_DISK_WRITE_COMPLETED_TOTAL = "node_disk_writes_completed_total"
NODE_DISK_READ_BYTES_TOTAL = "node_disk_read_bytes_total"
NODE_DISK_WRITTEN_BYTES_TOTAL = "node_disk_written_bytes_total"
// filesystem
NODE_FILESYSTEM_SIZE_TOTAL = "node_filesystem_size_bytes"
NODE_FILESYSTEM_SIZE_AVAIL = "node_filesystem_avail_bytes"
// disk
NODE_DISK_ROTATION_RATE = "node_disk_ata_rotation_rate_rpm"
NODE_DISK_WRITE_CACHE_ENABLE = "node_disk_ata_write_cache_enabled"
SSD_TYPE = "SSD"
HDD_TYPE = "HDD"
// disk modle
MODLE = "model"
)
// @return map[string][]Performance, key: device, value: performance at different timestamp
func GetDiskPerformance(instance string, start, end, interval uint64) (interface{}, error) {
performance := make(map[string][]Performance)
retMap := make(map[string]map[float64]*Performance)
// writeIOPS, writeBPS, readIOPS, readBPS
requestSize := 4
results := make(chan MetricResult, requestSize)
writeIOPSName := fmt.Sprintf("%s&start=%d&end=%d&step=%ds",
GetNodeDiskPerformanceName(NODE_DISK_WRITE_COMPLETED_TOTAL, instance, interval), start, end, interval)
writeBPSName := fmt.Sprintf("%s&start=%d&end=%d&step=%ds",
GetNodeDiskPerformanceName(NODE_DISK_WRITTEN_BYTES_TOTAL, instance, interval), start, end, interval)
readIOPSName := fmt.Sprintf("%s&start=%d&end=%d&step=%ds",
GetNodeDiskPerformanceName(NODE_DISK_READ_COMPLETED_TOTAL, instance, interval), start, end, interval)
readBPSName := fmt.Sprintf("%s&start=%d&end=%d&step=%ds",
GetNodeDiskPerformanceName(NODE_DISK_READ_BYTES_TOTAL, instance, interval), start, end, interval)
go QueryRangeMetric(writeIOPSName,start,end,interval, &results)
go QueryRangeMetric(writeBPSName,start,end,interval, &results)
go QueryRangeMetric(readIOPSName,start,end,interval, &results)
go QueryRangeMetric(readBPSName,start,end,interval, &results)
count := 0
for res := range results {
if res.Err != nil {
return nil, res.Err
}
ret := ParseMatrixMetric(res.Result.(*QueryResponseOfMatrix), DEVICE)
for device, v := range ret {
for _, data := range v {
if _, ok := retMap[device]; ok {
if p, ok := retMap[device][data.Timestamp]; ok {
switch res.Key.(string) {
case writeIOPSName:
p.WriteIOPS = data.Value
case writeBPSName:
p.WriteBPS = data.Value
case readIOPSName:
p.ReadIOPS = data.Value
case readBPSName:
p.ReadBPS = data.Value
}
} else {
switch res.Key.(string) {
case writeIOPSName:
retMap[device][data.Timestamp] = &Performance{
Timestamp: data.Timestamp,
WriteIOPS: data.Value,
}
case writeBPSName:
retMap[device][data.Timestamp] = &Performance{
Timestamp: data.Timestamp,
WriteBPS: data.Value,
}
case readIOPSName:
retMap[device][data.Timestamp] = &Performance{
Timestamp: data.Timestamp,
ReadIOPS: data.Value,
}
case readBPSName:
retMap[device][data.Timestamp] = &Performance{
Timestamp: data.Timestamp,
ReadBPS: data.Value,
}
}
}
} else {
retMap[device] = make(map[float64]*Performance)
}
}
}
count += 1
if count >= requestSize {
break
}
}
for dev, v := range retMap {
var tmparr []Performance
for _, p := range v {
tmparr = append(tmparr, *p)
}
performance[dev] = tmparr
}
return performance, nil
}
// @return map[instance][]map[key]value
func ListDiskInfo(instance string) (interface{}, error) {
disks := make(map[string][]map[string]string)
requestSize := 1
results := make(chan MetricResult, requestSize)
metricName := NODE_DISK_INFO
if instance != "" {
metricName = fmt.Sprintf("%s{instance=%q}", metricName, instance)
}
QueryInstantMetric(metricName, &results)
count := 0
for res := range results {
if res.Err != nil {
return nil, res.Err
}
ret := res.Result.(*QueryResponseOfVector)
for _, item := range ret.Data.Result {
if _, ok := item.Metric[MODLE]; ok {
disks[item.Metric[INSTANCE]] = append(disks[item.Metric[INSTANCE]], item.Metric)
}
}
count += 1
if count >= requestSize {
break
}
}
return disks, nil
}
// /dev/sda -> sda
func getShortDeviceName(name string) string {
prefix := "/dev/"
if !strings.HasPrefix(name, prefix) {
return name
}
return strings.ReplaceAll(name, prefix, "")
}
// @return ap[instance]map[device]FileSystemInfo
func GetDiskFileSystemInfo(instance string) (interface{}, error) {
fileSystemInfos := make(map[string]map[string]FileSystemInfo)
// totalSpace, freeSpace
requestSize := 2
results := make(chan MetricResult, requestSize)
totalName := NODE_FILESYSTEM_SIZE_TOTAL
availName := NODE_FILESYSTEM_SIZE_AVAIL
if instance != "" {
totalName = fmt.Sprintf("%s{instance=%q}", totalName, instance)
availName = fmt.Sprintf("%s{instance=%q}", availName, instance)
}
go QueryInstantMetric(totalName, &results)
go QueryInstantMetric(availName, &results)
count := 0
for res := range results {
if res.Err != nil {
return nil, res.Err
}
ret := res.Result.(*QueryResponseOfVector)
for _, item := range ret.Data.Result {
inst := item.Metric[INSTANCE]
// ListDisk return the short device name, and this info will combine with disk info at uplayer
dev := getShortDeviceName(item.Metric[DEVICE])
if _, ok := fileSystemInfos[inst]; !ok {
fileSystemInfos[inst] = make(map[string]FileSystemInfo)
}
space, _ := strconv.ParseUint(item.Value[1].(string), 10, 64)
if info, ok := fileSystemInfos[inst][dev]; ok {
switch res.Key.(string) {
case totalName:
info.SpaceTotal = space / comm.GiB
case availName:
info.SpaceAvail = space / comm.GiB
}
fileSystemInfos[inst][dev] = info
} else {
info := FileSystemInfo{}
info.Device = dev
info.FsType = item.Metric[FSTYPE]
info.MountPoint = item.Metric[MOUNTPOINT]
switch res.Key.(string) {
case totalName:
info.SpaceTotal = space / comm.GiB
case availName:
info.SpaceAvail = space / comm.GiB
}
fileSystemInfos[inst][dev] = info
}
}
count += 1
if count >= requestSize {
break
}
}
return fileSystemInfos, nil
}
func getDiskTypeByRotationRate(rate string) string {
switch rate {
case "0":
return SSD_TYPE
default:
return HDD_TYPE
}
}
// @return map[instance]map[device]type
func GetDiskType(instance string) (interface{}, error) {
diskTypes := make(map[string]map[string]string)
requestSize := 1
results := make(chan MetricResult, requestSize)
metricName := NODE_DISK_ROTATION_RATE
if instance != "" {
metricName = fmt.Sprintf("%s{instance=%q}", metricName, instance)
}
go QueryInstantMetric(metricName, &results)
count := 0
for res := range results {
if res.Err != nil {
return nil, res.Err
}
ret := res.Result.(*QueryResponseOfVector)
for _, item := range ret.Data.Result {
inst := item.Metric[INSTANCE]
dev := item.Metric[DEVICE]
if _, ok := diskTypes[inst]; !ok {
diskTypes[inst] = make(map[string]string)
}
diskTypes[inst][dev] = getDiskTypeByRotationRate(item.Value[1].(string))
}
count += 1
if count >= requestSize {
break
}
}
return diskTypes, nil
}
// @return map[instance]map[device]writeCacheEnable
func GetDiskWriteCacheEnableFlag(instance string) (interface{}, error) {
diskWriteCaches := make(map[string]map[string]string)
requestSize := 1
results := make(chan MetricResult, requestSize)
metricName := NODE_DISK_WRITE_CACHE_ENABLE
if instance != "" {
metricName = fmt.Sprintf("%s{instance=%q}", metricName, instance)
}
go QueryInstantMetric(metricName, &results)
count := 0
for res := range results {
if res.Err != nil {
return nil, res.Err
}
ret := res.Result.(*QueryResponseOfVector)
for _, item := range ret.Data.Result {
inst := item.Metric[INSTANCE]
dev := item.Metric[DEVICE]
if _, ok := diskWriteCaches[inst]; !ok {
diskWriteCaches[inst] = make(map[string]string)
}
diskWriteCaches[inst][dev] = item.Value[1].(string)
}
count += 1
if count >= requestSize {
break
}
}
return diskWriteCaches, nil
}