/
hdd.go
69 lines (55 loc) · 1.28 KB
/
hdd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package checkers
import (
"errors"
"fmt"
"syscall"
"github.com/efureev/health-checker"
)
func CheckingHDDFn(path string, size uint64) checker.CmdFn {
return func(result *checker.Result, log checker.ILogger) {
disk := DiskUsage(path)
result.Info.Text = disk.String()
if !disk.HasFree(size) {
result.AddError(errors.New(`lack of disk space`))
return
}
return
}
}
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
type DiskStatus struct {
All uint64
Used uint64
Free uint64
}
func (d DiskStatus) AllString() string {
return fmt.Sprintf("All: %.2f GB", float64(d.All)/float64(GB))
}
func (d DiskStatus) UsedString() string {
return fmt.Sprintf("Used: %.2f GB", float64(d.Used)/float64(GB))
}
func (d DiskStatus) FreeString() string {
return fmt.Sprintf("Free: %.2f GB", float64(d.Free)/float64(GB))
}
func (d DiskStatus) HasFree(size uint64) bool {
return d.Free > size
}
func (d DiskStatus) String() string {
return fmt.Sprintf("%s\n%s\n%s\n", d.AllString(), d.UsedString(), d.FreeString())
}
func DiskUsage(path string) (disk DiskStatus) {
fs := syscall.Statfs_t{}
err := syscall.Statfs(path, &fs)
if err != nil {
return
}
disk.All = fs.Blocks * uint64(fs.Bsize)
disk.Free = fs.Bfree * uint64(fs.Bsize)
disk.Used = disk.All - disk.Free
return
}