25 lines
528 B
Go
25 lines
528 B
Go
//go:build unix
|
|
|
|
package sysstats
|
|
|
|
import "syscall"
|
|
|
|
func diskUsage(path string) (Disk, error) {
|
|
var st syscall.Statfs_t
|
|
if err := syscall.Statfs(path, &st); err != nil {
|
|
return Disk{}, err
|
|
}
|
|
bsize := uint64(st.Bsize)
|
|
total := uint64(st.Blocks) * bsize
|
|
free := uint64(st.Bavail) * bsize // space available to non-root users
|
|
used := total - uint64(st.Bfree)*bsize
|
|
|
|
return Disk{
|
|
Path: path,
|
|
TotalBytes: total,
|
|
UsedBytes: used,
|
|
FreeBytes: free,
|
|
UsagePercent: percent(used, used+free),
|
|
}, nil
|
|
}
|