You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1011 B
43 lines
1011 B
/*
|
|
*
|
|
@author: sre
|
|
@date: 2023/11/19 0019
|
|
@desc: todo
|
|
*
|
|
*/
|
|
package main
|
|
|
|
import "strings"
|
|
|
|
// getCPUInfo 获取cpu核心 使用率
|
|
func getCPUInfo(ip, port string) (int, float64, error) {
|
|
var cpuTotal, cpuIdle float64
|
|
var cpuCount int
|
|
data, err := getMetrics(ip, port)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
|
|
}
|
|
// 解析metrics数据
|
|
lines := strings.Split(data, "\n")
|
|
for _, line := range lines {
|
|
//有两个版本的Metrics node_cpu_seconds_total and node_cpu
|
|
if strings.HasPrefix(line, "node_cpu{cpu=") || strings.HasPrefix(line, "node_cpu_seconds_total") || strings.HasPrefix(line, "windows_cpu_time_total") {
|
|
fields := strings.Fields(line)
|
|
if len(fields) == 2 {
|
|
cpuData, err := extractMetricValue(fields[1])
|
|
if err != nil {
|
|
//fmt.Println("无法提取指标值: ", err)
|
|
return 0, 0, err
|
|
}
|
|
cpuTotal += cpuData
|
|
if strings.Contains(line, "mode=\"idle\"") {
|
|
cpuIdle += cpuData
|
|
cpuCount += 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return cpuCount, (1 - cpuIdle/cpuTotal), nil
|
|
}
|