/
usage.go
55 lines (46 loc) · 1.48 KB
/
usage.go
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
package metrics
import (
"context"
"fmt"
"github.com/bytedance/Elkeid/server/manager/internal/monitor"
"strings"
)
const (
MonitorServiceUsageLow = "low"
MonitorServiceUsageMiddle = "middle"
MonitorServiceUsageHigh = "high"
MonitorServiceUsageUnavailable = "unavailable"
)
func UsageToStatus(usage int) string {
if usage < 50 {
return MonitorServiceUsageLow
} else if usage < 80 {
return MonitorServiceUsageMiddle
} else {
return MonitorServiceUsageHigh
}
}
func GetAvgCpuByHosts(ctx context.Context, hosts []*monitor.HostInfo) float64 {
count := len(hosts)
if count == 0 {
return 0
}
items := make([]string, 0)
for _, host := range hosts {
items = append(items, fmt.Sprintf("%s:.*", host.IP))
}
return PromQueryJsonPathWithRetFloat(ctx, fmt.Sprintf("sum(rate(node_cpu_seconds_total{mode!='idle',instance=~'%s'}[1m]))/sum(rate(node_cpu_seconds_total{instance=~'%s'}[1m]))",
strings.Join(items, "|"), strings.Join(items, "|")), "$.data.result[0].value.[1]")
}
func GetAvgMemByHosts(ctx context.Context, hosts []*monitor.HostInfo) float64 {
count := len(hosts)
if count == 0 {
return 0
}
items := make([]string, 0)
for _, host := range hosts {
items = append(items, fmt.Sprintf("%s:.*", host.IP))
}
return PromQueryJsonPathWithRetFloat(ctx, fmt.Sprintf("1-(sum(node_memory_MemAvailable_bytes{instance=~'%s'})/sum(node_memory_MemTotal_bytes{instance=~'%s'}))",
strings.Join(items, "|"), strings.Join(items, "|")), "$.data.result[0].value.[1]")
}