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.
This commit is contained in:
Oliver Gugger 2020-11-09 10:21:26 +01:00
parent f8907fdb47
commit 35c1fad517
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
6 changed files with 83 additions and 16 deletions

@ -4,8 +4,9 @@ package healthcheck
import "syscall" import "syscall"
// AvailableDiskSpace returns ratio of available disk space to total capacity. // AvailableDiskSpaceRatio returns ratio of available disk space to total
func AvailableDiskSpace(path string) (float64, error) { // capacity.
func AvailableDiskSpaceRatio(path string) (float64, error) {
s := syscall.Statfs_t{} s := syscall.Statfs_t{}
err := syscall.Statfs(path, &s) err := syscall.Statfs(path, &s)
if err != nil { if err != nil {
@ -16,3 +17,17 @@ func AvailableDiskSpace(path string) (float64, error) {
// free blocks. // free blocks.
return float64(s.Bfree) / float64(s.Blocks), nil 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
}

@ -2,9 +2,9 @@ package healthcheck
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"
// AvailableDiskSpace returns ratio of available disk space to total capacity // AvailableDiskSpaceRatio returns ratio of available disk space to total
// for solaris. // capacity for netbsd.
func AvailableDiskSpace(path string) (float64, error) { func AvailableDiskSpaceRatio(path string) (float64, error) {
s := unix.Statvfs_t{} s := unix.Statvfs_t{}
err := unix.Statvfs(path, &s) err := unix.Statvfs(path, &s)
if err != nil { if err != nil {
@ -15,3 +15,15 @@ func AvailableDiskSpace(path string) (float64, error) {
// free blocks. // free blocks.
return float64(s.Bfree) / float64(s.Blocks), nil 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
}

@ -2,9 +2,9 @@ package healthcheck
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"
// AvailableDiskSpace returns ratio of available disk space to total capacity // AvailableDiskSpaceRatio returns ratio of available disk space to total
// for solaris. // capacity for openbsd.
func AvailableDiskSpace(path string) (float64, error) { func AvailableDiskSpaceRatio(path string) (float64, error) {
s := unix.Statfs_t{} s := unix.Statfs_t{}
err := unix.Statfs(path, &s) err := unix.Statfs(path, &s)
if err != nil { if err != nil {
@ -15,3 +15,15 @@ func AvailableDiskSpace(path string) (float64, error) {
// free blocks. // free blocks.
return float64(s.F_bfree) / float64(s.F_blocks), nil 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
}

@ -2,9 +2,9 @@ package healthcheck
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"
// AvailableDiskSpace returns ratio of available disk space to total capacity // AvailableDiskSpaceRatio returns ratio of available disk space to total
// for solaris. // capacity for solaris.
func AvailableDiskSpace(path string) (float64, error) { func AvailableDiskSpaceRatio(path string) (float64, error) {
s := unix.Statvfs_t{} s := unix.Statvfs_t{}
err := unix.Statvfs(path, &s) err := unix.Statvfs(path, &s)
if err != nil { if err != nil {
@ -15,3 +15,15 @@ func AvailableDiskSpace(path string) (float64, error) {
// free blocks. // free blocks.
return float64(s.Bfree) / float64(s.Blocks), nil 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
}

@ -2,16 +2,30 @@ package healthcheck
import "golang.org/x/sys/windows" import "golang.org/x/sys/windows"
// AvailableDiskSpace returns ratio of available disk space to total capacity // AvailableDiskSpaceRatio returns ratio of available disk space to total
// for windows. // capacity for windows.
func AvailableDiskSpace(path string) (float64, error) { func AvailableDiskSpaceRatio(path string) (float64, error) {
var free, total, avail uint64 var free, total, avail uint64
pathPtr, err := windows.UTF16PtrFromString(path) pathPtr, err := windows.UTF16PtrFromString(path)
if err != nil { if err != nil {
panic(err) return 0, err
} }
err = windows.GetDiskFreeSpaceEx(pathPtr, &free, &total, &avail) err = windows.GetDiskFreeSpaceEx(pathPtr, &free, &total, &avail)
return float64(avail) / float64(total), nil 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
}

@ -1302,7 +1302,9 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
diskCheck := healthcheck.NewObservation( diskCheck := healthcheck.NewObservation(
"disk space", "disk space",
func() error { func() error {
free, err := healthcheck.AvailableDiskSpace(cfg.LndDir) free, err := healthcheck.AvailableDiskSpaceRatio(
cfg.LndDir,
)
if err != nil { if err != nil {
return err return err
} }