From 35c1fad517294c178072e2a92d0763726fc432b2 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 9 Nov 2020 10:21:26 +0100 Subject: [PATCH] server+healthcheck: rename function, add absolute disk space function With this commit we rename the existing AvailableDiskSpace function to its correct name AvailableDiskSpaceRatio as it only returns a ratio. We then go ahead and add a new function that returns the actual number of free bytes available on a file system. This also fixes some comments and always returns an error instead of panicking. --- healthcheck/diskcheck.go | 19 +++++++++++++++++-- healthcheck/diskcheck_netbsd.go | 18 +++++++++++++++--- healthcheck/diskcheck_openbsd.go | 18 +++++++++++++++--- healthcheck/diskcheck_solaris.go | 18 +++++++++++++++--- healthcheck/diskcheck_windows.go | 22 ++++++++++++++++++---- server.go | 4 +++- 6 files changed, 83 insertions(+), 16 deletions(-) diff --git a/healthcheck/diskcheck.go b/healthcheck/diskcheck.go index ed230d275..7f30b1546 100644 --- a/healthcheck/diskcheck.go +++ b/healthcheck/diskcheck.go @@ -4,8 +4,9 @@ package healthcheck import "syscall" -// AvailableDiskSpace returns ratio of available disk space to total capacity. -func AvailableDiskSpace(path string) (float64, error) { +// AvailableDiskSpaceRatio returns ratio of available disk space to total +// capacity. +func AvailableDiskSpaceRatio(path string) (float64, error) { s := syscall.Statfs_t{} err := syscall.Statfs(path, &s) if err != nil { @@ -16,3 +17,17 @@ func AvailableDiskSpace(path string) (float64, error) { // free blocks. return float64(s.Bfree) / float64(s.Blocks), nil } + +// AvailableDiskSpace returns the available disk space in bytes of the given +// file system. +func AvailableDiskSpace(path string) (uint64, error) { + s := syscall.Statfs_t{} + err := syscall.Statfs(path, &s) + if err != nil { + return 0, err + } + + // Some OSes have s.Bavail defined as int64, others as uint64, so we + // need the explicit type conversion here. + return uint64(s.Bavail) * uint64(s.Bsize), nil // nolint:unconvert +} diff --git a/healthcheck/diskcheck_netbsd.go b/healthcheck/diskcheck_netbsd.go index d44330b79..a70e9c355 100644 --- a/healthcheck/diskcheck_netbsd.go +++ b/healthcheck/diskcheck_netbsd.go @@ -2,9 +2,9 @@ package healthcheck import "golang.org/x/sys/unix" -// AvailableDiskSpace returns ratio of available disk space to total capacity -// for solaris. -func AvailableDiskSpace(path string) (float64, error) { +// AvailableDiskSpaceRatio returns ratio of available disk space to total +// capacity for netbsd. +func AvailableDiskSpaceRatio(path string) (float64, error) { s := unix.Statvfs_t{} err := unix.Statvfs(path, &s) if err != nil { @@ -15,3 +15,15 @@ func AvailableDiskSpace(path string) (float64, error) { // free blocks. return float64(s.Bfree) / float64(s.Blocks), nil } + +// AvailableDiskSpace returns the available disk space in bytes of the given +// file system for netbsd. +func AvailableDiskSpace(path string) (uint64, error) { + s := unix.Statvfs_t{} + err := unix.Statvfs(path, &s) + if err != nil { + return 0, err + } + + return s.Bavail * uint64(s.Bsize), nil +} diff --git a/healthcheck/diskcheck_openbsd.go b/healthcheck/diskcheck_openbsd.go index 4738db9ac..b7538ff90 100644 --- a/healthcheck/diskcheck_openbsd.go +++ b/healthcheck/diskcheck_openbsd.go @@ -2,9 +2,9 @@ package healthcheck import "golang.org/x/sys/unix" -// AvailableDiskSpace returns ratio of available disk space to total capacity -// for solaris. -func AvailableDiskSpace(path string) (float64, error) { +// AvailableDiskSpaceRatio returns ratio of available disk space to total +// capacity for openbsd. +func AvailableDiskSpaceRatio(path string) (float64, error) { s := unix.Statfs_t{} err := unix.Statfs(path, &s) if err != nil { @@ -15,3 +15,15 @@ func AvailableDiskSpace(path string) (float64, error) { // free blocks. return float64(s.F_bfree) / float64(s.F_blocks), nil } + +// AvailableDiskSpace returns the available disk space in bytes of the given +// file system for openbsd. +func AvailableDiskSpace(path string) (uint64, error) { + s := unix.Statfs_t{} + err := unix.Statfs(path, &s) + if err != nil { + return 0, err + } + + return uint64(s.F_bavail) * uint64(s.F_bsize), nil +} diff --git a/healthcheck/diskcheck_solaris.go b/healthcheck/diskcheck_solaris.go index d44330b79..32d7992f2 100644 --- a/healthcheck/diskcheck_solaris.go +++ b/healthcheck/diskcheck_solaris.go @@ -2,9 +2,9 @@ package healthcheck import "golang.org/x/sys/unix" -// AvailableDiskSpace returns ratio of available disk space to total capacity -// for solaris. -func AvailableDiskSpace(path string) (float64, error) { +// AvailableDiskSpaceRatio returns ratio of available disk space to total +// capacity for solaris. +func AvailableDiskSpaceRatio(path string) (float64, error) { s := unix.Statvfs_t{} err := unix.Statvfs(path, &s) if err != nil { @@ -15,3 +15,15 @@ func AvailableDiskSpace(path string) (float64, error) { // free blocks. return float64(s.Bfree) / float64(s.Blocks), nil } + +// AvailableDiskSpace returns the available disk space in bytes of the given +// file system for solaris. +func AvailableDiskSpace(path string) (uint64, error) { + s := unix.Statvfs_t{} + err := unix.Statvfs(path, &s) + if err != nil { + return 0, err + } + + return s.Bavail * uint64(s.Bsize), nil +} diff --git a/healthcheck/diskcheck_windows.go b/healthcheck/diskcheck_windows.go index 7fed088b9..a999cf11e 100644 --- a/healthcheck/diskcheck_windows.go +++ b/healthcheck/diskcheck_windows.go @@ -2,16 +2,30 @@ package healthcheck import "golang.org/x/sys/windows" -// AvailableDiskSpace returns ratio of available disk space to total capacity -// for windows. -func AvailableDiskSpace(path string) (float64, error) { +// AvailableDiskSpaceRatio returns ratio of available disk space to total +// capacity for windows. +func AvailableDiskSpaceRatio(path string) (float64, error) { var free, total, avail uint64 pathPtr, err := windows.UTF16PtrFromString(path) if err != nil { - panic(err) + return 0, err } err = windows.GetDiskFreeSpaceEx(pathPtr, &free, &total, &avail) return float64(avail) / float64(total), nil } + +// AvailableDiskSpace returns the available disk space in bytes of the given +// file system for windows. +func AvailableDiskSpace(path string) (uint64, error) { + var free, total, avail uint64 + + pathPtr, err := windows.UTF16PtrFromString(path) + if err != nil { + return 0, err + } + err = windows.GetDiskFreeSpaceEx(pathPtr, &free, &total, &avail) + + return avail, nil +} diff --git a/server.go b/server.go index a20d6cf58..5858909c3 100644 --- a/server.go +++ b/server.go @@ -1302,7 +1302,9 @@ func newServer(cfg *Config, listenAddrs []net.Addr, diskCheck := healthcheck.NewObservation( "disk space", func() error { - free, err := healthcheck.AvailableDiskSpace(cfg.LndDir) + free, err := healthcheck.AvailableDiskSpaceRatio( + cfg.LndDir, + ) if err != nil { return err }