OrangePi3588Media/agent/internal/metrics/metrics_linux.go
2026-01-17 16:57:51 +08:00

34 lines
601 B
Go

//go:build linux
package metrics
import (
"strings"
"syscall"
)
func ReadDiskUsage(path string) (DiskUsage, error) {
p := strings.TrimSpace(path)
if p == "" {
p = "/"
}
var st syscall.Statfs_t
if err := syscall.Statfs(p, &st); err != nil {
return DiskUsage{Path: p}, err
}
total := st.Blocks * uint64(st.Bsize)
free := st.Bavail * uint64(st.Bsize)
used := total - free
usedPct := 0.0
if total > 0 {
usedPct = float64(used) / float64(total) * 100
}
return DiskUsage{
Path: p,
TotalBytes: total,
FreeBytes: free,
UsedBytes: used,
UsedPct: usedPct,
}, nil
}