check_disk_size.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "runtime"
  6. "syscall"
  7. )
  8. var (
  9. dir = flag.String("dir", ".", "the directory which uses a disk")
  10. )
  11. func main() {
  12. flag.Parse()
  13. fillInDiskStatus(*dir)
  14. fmt.Printf("OS: %v\n", runtime.GOOS)
  15. fmt.Printf("Arch: %v\n", runtime.GOARCH)
  16. }
  17. func fillInDiskStatus(dir string) {
  18. fs := syscall.Statfs_t{}
  19. err := syscall.Statfs(dir, &fs)
  20. if err != nil {
  21. fmt.Printf("failed to statfs on %s: %v\n", dir, err)
  22. return
  23. }
  24. fmt.Printf("statfs: %+v\n", fs)
  25. fmt.Println()
  26. total := fs.Blocks * uint64(fs.Bsize)
  27. free := fs.Bfree * uint64(fs.Bsize)
  28. fmt.Printf("Total: %d blocks x %d block size = %d bytes\n", fs.Blocks, uint64(fs.Bsize), total)
  29. fmt.Printf("Free : %d blocks x %d block size = %d bytes\n", fs.Bfree, uint64(fs.Bsize), free)
  30. fmt.Printf("Used : %d blocks x %d block size = %d bytes\n", fs.Blocks-fs.Bfree, uint64(fs.Bsize), total-free)
  31. fmt.Printf("Free Percentage : %.2f%%\n", float32((float64(free)/float64(total))*100))
  32. fmt.Printf("Used Percentage : %.2f%%\n", float32((float64(total-free)/float64(total))*100))
  33. return
  34. }