derohe-miniblock-mod/metrics/diskusage.go
2021-12-04 16:42:11 +00:00

46 lines
1.2 KiB
Go

//go:build !windows
// +build !windows
package metrics
import "syscall"
// DiskUsage contains usage data and provides user-friendly access methods
type DiskUsage struct {
stat *syscall.Statfs_t
}
// NewDiskUsages returns an object holding the disk usage of volumePath
// or nil in case of error (invalid path, etc)
func NewDiskUsage(volumePath string) *DiskUsage {
var stat syscall.Statfs_t
syscall.Statfs(volumePath, &stat)
return &DiskUsage{&stat}
}
// Free returns total free bytes on file system
func (du *DiskUsage) Free() uint64 {
return du.stat.Bfree * uint64(du.stat.Bsize)
}
// Available return total available bytes on file system to an unprivileged user
func (du *DiskUsage) Available() uint64 {
return uint64(du.stat.Bavail) * uint64(du.stat.Bsize)
}
// Size returns total size of the file system
func (du *DiskUsage) Size() uint64 {
return uint64(du.stat.Blocks) * uint64(du.stat.Bsize)
}
// Used returns total bytes used in file system
func (du *DiskUsage) Used() uint64 {
return du.Size() - du.Free()
}
// Usage returns percentage of use on the file system
func (du *DiskUsage) Usage() float32 {
return float32(du.Used()) / float32(du.Size())
}