-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhost.go
More file actions
218 lines (197 loc) · 5.98 KB
/
Copy pathhost.go
File metadata and controls
218 lines (197 loc) · 5.98 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
/*
* 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"
"github.com/opencurve/curve-manager/internal/common"
)
const (
// node info
NODE_UNAME_INFO = "node_uname_info"
NODE_UNAME_NODE_NAME = "nodename"
NODE_UNAME_MACHINE = "machine"
NODE_UNAME_RELEASE = "release"
NODE_UNAME_SYSNAME = "sysname"
NODE_UNAME_VERSION = "version"
// cpu info
NODE_CPU_INFO = "node_cpu_info"
NODE_CPU_MODLE = "model_name"
NODE_CPU_UTILIZATION = "node_cpu_seconds_total"
NODE_CPU_IDLE = "idle"
// memory info
NODE_MEMORY_TOTAL_BYTES = "node_memory_MemTotal_bytes"
// network info
NODE_NETWORK_DEVICE_FILTER = "tap.*|veth.*|br.*|docker.*|virbr*|lo*"
NODE_NETWORK_RECEIVE_BYTES_TOTAL = "node_network_receive_bytes_total"
NODE_NETWORK_TRANSMIT_BYTES_TOTAL = "node_network_transmit_bytes_total"
)
type HostInfo struct {
HostName string `json:"hostname"`
IP string `json:"ip"`
Machine string `json:"machine"`
Release string `json:"kernel-release"`
Version string `json:"kernel-version"`
System string `json:"operating-system"`
}
type CPUInfo struct {
TotalNum uint32 `json:"totalNum"`
Models map[string]uint32 `json:"cpuModles"`
}
type NetworkTraffic struct {
Receive map[string][]RangeMetricItem
Transmit map[string][]RangeMetricItem
}
func GetHostInfo(instance string) (interface{}, error) {
hostsInfo := make(map[string]HostInfo)
requestSize := 1
results := make(chan MetricResult, requestSize)
metricName := NODE_UNAME_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 := ParseVectorMetric(res.Result.(*QueryResponseOfVector), false)
for k, v := range ret {
info := HostInfo{}
info.HostName = v[NODE_UNAME_NODE_NAME]
info.IP, _ = common.GetIPFromEndpoint(v[INSTANCE])
info.Machine = v[NODE_UNAME_MACHINE]
info.Release = v[NODE_UNAME_RELEASE]
info.System = v[NODE_UNAME_SYSNAME]
info.Version = v[NODE_UNAME_VERSION]
hostsInfo[k] = info
}
count += 1
if count >= requestSize {
break
}
}
return hostsInfo, nil
}
func GetHostCPUInfo(instance string) (interface{}, error) {
cpuInfoMap := make(map[string]CPUInfo)
requestSize := 1
results := make(chan MetricResult, requestSize)
metricName := NODE_CPU_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
}
info := res.Result.(*QueryResponseOfVector)
for _, item := range info.Data.Result {
instance := item.Metric[INSTANCE]
model := item.Metric[NODE_CPU_MODLE]
if cpu, ok := cpuInfoMap[instance]; ok {
cpu.TotalNum++
cpu.Models[model]++
cpuInfoMap[instance] = cpu
} else {
cpu := CPUInfo{}
cpu.TotalNum = 1
cpu.Models = make(map[string]uint32)
cpu.Models[model] = 1
cpuInfoMap[instance] = cpu
}
}
count += 1
if count >= requestSize {
break
}
}
return cpuInfoMap, nil
}
func GetHostMemoryInfo(instance string) (interface{}, error) {
memoryInfo := make(map[string]uint64)
requestSize := 1
results := make(chan MetricResult, requestSize)
metricName := NODE_MEMORY_TOTAL_BYTES
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 := ParseVectorMetric(res.Result.(*QueryResponseOfVector), true)
for k, v := range ret {
memoryInfo[k], _ = strconv.ParseUint(v["value"], 10, 64)
memoryInfo[k] /= common.GiB
}
count += 1
if count >= requestSize {
break
}
}
return memoryInfo, nil
}
/*
* return: reveive, transmit, error
* map[string][]RangeMetricItem: key: network device, value: performance in different timestamp
*/
func GetNetWorkTraffic(instance string, start, end, interval uint64) (interface{}, error) {
networkTraffic := NetworkTraffic{}
// receive, transmit
requestSize := 2
results := make(chan MetricResult, requestSize)
receiveName := fmt.Sprintf("%s&start=%d&end=%d&step=%ds",
GetNodeNetWorkReveiveName(NODE_NETWORK_RECEIVE_BYTES_TOTAL, instance, interval), start, end, interval)
transmitName := fmt.Sprintf("%s&start=%d&end=%d&step=%ds",
GetNodeNetWorkReveiveName(NODE_NETWORK_TRANSMIT_BYTES_TOTAL, instance, interval), start, end, interval)
go QueryRangeMetric(receiveName,start,end,interval, &results)
go QueryRangeMetric(transmitName,start,end,interval, &results)
count := 0
for res := range results {
if res.Err != nil {
return networkTraffic, res.Err
}
ret := ParseMatrixMetric(res.Result.(*QueryResponseOfMatrix), DEVICE)
switch res.Key.(string) {
case receiveName:
networkTraffic.Receive = ret
case transmitName:
networkTraffic.Transmit = ret
}
count += 1
if count >= requestSize {
break
}
}
return networkTraffic, nil
}
func GetHostCPUUtilization(instance string, start, end, interval uint64) (interface{}, error) {
return GetUtilization(GetNodeCPUUtilizationName(instance, interval), start, end, interval)
}
func GetHostMemUtilization(instance string, start, end, interval uint64) (interface{}, error) {
return GetUtilization(GetNodeMemUtilizationName(instance), start, end, interval)
}