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.

76 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
*
@author: sre
@date: 2023/11/19 0019
@desc: todo
*
*/
package main
import (
"strings"
)
// getMemoryInfo 返回总内存 可用内存
func getMemoryInfo(ip, port string) (float64, float64, error) {
var memTotal, memAvailable float64
data, err := getMetrics(ip, port)
if err != nil {
return 0, 0, err
}
// 解析metrics数据
lines := strings.Split(data, "\n")
for _, line := range lines {
//memTotal
if strings.HasPrefix(line, "node_memory_MemTotal_bytes ") {
fields := strings.Fields(line)
if len(fields) == 2 {
memTotal, err = extractMetricValue(fields[1])
if err != nil {
return 0, 0, err
}
}
}
//memAvailable
if strings.HasPrefix(line, "node_memory_MemAvailable_bytes ") {
fields := strings.Fields(line)
if len(fields) == 2 {
memAvailable, err = extractMetricValue(fields[1])
if err != nil {
return 0, 0, err
}
}
}
}
return memTotal, memAvailable, nil
}
//// 解析Node Exporter的响应内容提取内存使用率指标
//func parseMemoryUsage(response string) float64 {
// // 在响应中找到相关的指标行,例如 "node_memory_MemTotal: 167772160000 149510252888 149510252888"
// lines := strings.Split(response, "\n")
// for _, line := range lines {
// if strings.Contains(line, "node_memory_MemTotal") {
// fields := strings.Fields(line)
// if len(fields) >= 4 {
// // 从指标行中提取内存使用率指标,并将其转换为百分比形式
// memoryUsage := strings.TrimSuffix(fields[2], " ") + "%"
// return getPercentage(memoryUsage)
// }
// }
// }
//
// // 如果找不到相关的指标行则返回0%
// return 0.0
//}
//
//// 从字符串中提取百分比值,并将其转换为浮点数形式
//func getPercentage(percentage string) float64 {
// // 去掉百分号,并将其转换为浮点数形式
// percentage = strings.ReplaceAll(percentage, "%", "")
// return parseFloat(percentage) * (100.0 / 100) // 将字符串转换为浮点数并乘以100转换为百分比形式
//}