forked from toolkits/nux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfstat.go
180 lines (157 loc) · 3.95 KB
/
dfstat.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package nux
import (
"bufio"
"bytes"
"fmt"
"github.com/toolkits/file"
"io"
"io/ioutil"
"strings"
"syscall"
)
var FSSPEC_IGNORE = map[string]struct{}{
"none": struct{}{},
"nodev": struct{}{},
"tmpfs": struct{}{},
}
var FSTYPE_IGNORE = map[string]struct{}{
"cgroup": struct{}{},
"debugfs": struct{}{},
"devtmpfs": struct{}{},
"rpc_pipefs": struct{}{},
"rootfs": struct{}{},
}
var FSFILE_PREFIX_IGNORE = []string{
"/dev",
"/sys",
"/net",
"/misc",
"/proc",
"/lib",
}
func IgnoreFsFile(fs_file string) bool {
for _, prefix := range FSFILE_PREFIX_IGNORE {
if strings.HasPrefix(fs_file, prefix) {
return true
}
}
return false
}
// return: [][$fs_spec, $fs_file, $fs_vfstype]
func ListMountPoint() ([][3]string, error) {
contents, err := ioutil.ReadFile("/proc/mounts")
if err != nil {
return nil, err
}
ret := make([][3]string, 0)
reader := bufio.NewReader(bytes.NewBuffer(contents))
for {
line, err := file.ReadLine(reader)
if err == io.EOF {
break
}
fields := strings.Fields(string(line))
// Docs come from the fstab(5)
// fs_spec # Mounted block special device or remote filesystem e.g. /dev/sda1
// fs_file # Mount point e.g. /data
// fs_vfstype # File system type e.g. ext4
// fs_mntops # Mount options
// fs_freq # Dump(8) utility flags
// fs_passno # Order in which filesystem checks are done at reboot time
fs_spec := fields[0]
fs_file := fields[1]
fs_vfstype := fields[2]
if _, exist := FSSPEC_IGNORE[fs_spec]; exist {
continue
}
if _, exist := FSTYPE_IGNORE[fs_vfstype]; exist {
continue
}
if strings.HasPrefix(fs_vfstype, "fuse") {
continue
}
if IgnoreFsFile(fs_file) {
continue
}
// keep /dev/xxx device with shorter fs_file (remove mount binds)
if strings.HasPrefix(fs_spec, "/dev") {
deviceFound := false
for idx := range ret {
if ret[idx][0] == fs_spec {
deviceFound = true
if len(fs_file) < len(ret[idx][1]) {
ret[idx][1] = fs_file
}
break
}
}
if !deviceFound {
ret = append(ret, [3]string{fs_spec, fs_file, fs_vfstype})
}
} else {
ret = append(ret, [3]string{fs_spec, fs_file, fs_vfstype})
}
}
return ret, nil
}
type DeviceUsage struct {
FsSpec string
FsFile string
FsVfstype string
BlocksAll uint64
BlocksUsed uint64
BlocksFree uint64
BlocksUsedPercent float64
BlocksFreePercent float64
InodesAll uint64
InodesUsed uint64
InodesFree uint64
InodesUsedPercent float64
InodesFreePercent float64
}
func (this *DeviceUsage) String() string {
return fmt.Sprintf("<FsSpec:%s, FsFile:%s, FsVfstype:%s, BAll:%d, BUsed:%d, BFree:%d, BPUsed:%f, BPFree:%f, IAll:%d, IUsed:%d, IFree:%d, IPUsed:%f, IPFree:%f>",
this.FsSpec,
this.FsFile,
this.FsVfstype,
this.BlocksAll,
this.BlocksUsed,
this.BlocksFree,
this.BlocksUsedPercent,
this.BlocksFreePercent,
this.InodesAll,
this.InodesUsed,
this.InodesFree,
this.InodesUsedPercent,
this.InodesFreePercent)
}
func BuildDeviceUsage(_fsSpec, _fsFile, _fsVfstype string) (*DeviceUsage, error) {
ret := &DeviceUsage{FsSpec: _fsSpec, FsFile: _fsFile, FsVfstype: _fsVfstype}
fs := syscall.Statfs_t{}
err := syscall.Statfs(_fsFile, &fs)
if err != nil {
return nil, err
}
// blocks
used := fs.Blocks - fs.Bfree
ret.BlocksAll = uint64(fs.Frsize) * fs.Blocks
ret.BlocksUsed = uint64(fs.Frsize) * used
ret.BlocksFree = uint64(fs.Frsize) * fs.Bavail
if fs.Blocks == 0 {
ret.BlocksUsedPercent = 100.0
} else {
ret.BlocksUsedPercent = float64(used) * 100.0 / float64(used+fs.Bavail)
}
ret.BlocksFreePercent = 100.0 - ret.BlocksUsedPercent
// inodes
ret.InodesAll = fs.Files
ret.InodesFree = fs.Ffree
ret.InodesUsed = fs.Files - fs.Ffree
if fs.Files == 0 {
ret.InodesUsedPercent = 100.0
} else {
ret.InodesUsedPercent = float64(ret.InodesUsed) * 100.0 / float64(ret.InodesAll)
}
ret.InodesFreePercent = 100.0 - ret.InodesUsedPercent
return ret, nil
}