30 lines
779 B
Go
30 lines
779 B
Go
package main
|
|
|
|
import "syscall"
|
|
|
|
type StorageMetrics struct {
|
|
Mountpoint string `json:"mountpoint"`
|
|
TotalBytes uint64 `json:"totalBytes"`
|
|
UsedBytes uint64 `json:"usedBytes"`
|
|
FreeBytes uint64 `json:"freeBytes"`
|
|
UsagePercent float64 `json:"usagePercent"`
|
|
}
|
|
|
|
func readStorageMetrics(path string) (StorageMetrics, error) {
|
|
var stat syscall.Statfs_t
|
|
if err := syscall.Statfs(path, &stat); err != nil {
|
|
return StorageMetrics{}, err
|
|
}
|
|
total := stat.Blocks * uint64(stat.Bsize)
|
|
free := stat.Bavail * uint64(stat.Bsize)
|
|
used := total - free
|
|
var percent float64
|
|
if total > 0 {
|
|
percent = float64(used) / float64(total) * 100
|
|
}
|
|
return StorageMetrics{
|
|
Mountpoint: path, TotalBytes: total, UsedBytes: used,
|
|
FreeBytes: free, UsagePercent: percent,
|
|
}, nil
|
|
}
|