Files
komari-agent/monitoring/unit/gpu_linux.go
kdwycz 5304a68d5d 新增Linux GPU监控功能
参考了nezha-agent。实现了Linux服务器下NVIDIA显卡的监控。
通过 --gpu 参数启用显卡监控功能。
支持多显卡,显卡使用率,显存使用率监控
实现了AMD显卡的监控,但是未经过测试
2025-09-12 16:09:55 +08:00

56 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//go:build linux
// +build linux
package monitoring
import (
"os/exec"
"strings"
)
func GpuName() string {
// 调整优先级:专用显卡厂商优先,避免只识别集成显卡
accept := []string{"nvidia", "amd", "radeon", "vga", "3d"}
out, err := exec.Command("lspci").Output()
if err == nil {
lines := strings.Split(string(out), "\n")
// 首先尝试找专用显卡
for _, line := range lines {
lower := strings.ToLower(line)
// 跳过集成显卡和管理控制器
if strings.Contains(lower, "aspeed") ||
strings.Contains(lower, "matrox") ||
strings.Contains(lower, "management") {
continue
}
// 优先匹配专用显卡厂商
for _, a := range accept {
if strings.Contains(lower, a) {
parts := strings.SplitN(line, ":", 4)
if len(parts) >= 4 {
return strings.TrimSpace(parts[3])
} else if len(parts) == 3 {
return strings.TrimSpace(parts[2])
} else if len(parts) == 2 {
return strings.TrimSpace(parts[1])
}
}
}
}
// 如果没有找到专用显卡返回第一个VGA设备作为兜底
for _, line := range lines {
if strings.Contains(strings.ToLower(line), "vga") {
parts := strings.SplitN(line, ":", 4)
if len(parts) >= 3 {
return strings.TrimSpace(parts[2])
}
}
}
}
return "None"
}