-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdisk.go
More file actions
186 lines (166 loc) · 4.67 KB
/
disk.go
File metadata and controls
186 lines (166 loc) · 4.67 KB
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
181
182
183
184
185
186
package systats
import (
"strconv"
"strings"
"time"
"github.com/dhamith93/systats/exec"
)
// Disk holds information on single disk
type Disk struct {
FileSystem string
Type string
MountedOn string
Usage DiskUsage
Inodes InodeUsage
Time int64
}
// DiskUsage holds information on single disk usage information
type DiskUsage struct {
Size uint64
Used uint64
Available uint64
Usage string
Unit string
}
// InodeUsage holds information on single disk inode usage
type InodeUsage struct {
Inodes uint64
Available uint64
Used uint64
Usage string
}
func getDisks() ([]Disk, error) {
output := []Disk{}
dfOutput := getDiskInfo()
dfOutputInode := getDiskInodeInfo()
for i, disk := range dfOutput {
diskArr := strings.Fields(disk)
diskInodeArr := strings.Fields(dfOutputInode[i])
if len(diskArr) < 6 || len(diskInodeArr) < 6 {
continue
}
newDisk := Disk{}
newDisk.FileSystem = diskArr[0]
newDisk.Type = diskArr[1]
newDisk.MountedOn = diskArr[6]
newDisk.Usage = DiskUsage{}
newDisk.Inodes = InodeUsage{}
val, err := strconv.ParseUint(diskArr[2], 10, 64)
if err != nil {
return output, err
}
newDisk.Usage.Size = val
val, err = strconv.ParseUint(diskArr[3], 10, 64)
if err != nil {
return output, err
}
newDisk.Usage.Used = val
val, err = strconv.ParseUint(diskArr[4], 10, 64)
if err != nil {
return output, err
}
newDisk.Usage.Available = val
newDisk.Usage.Usage = diskArr[5]
newDisk.Usage.Unit = Byte
val, err = strconv.ParseUint(diskInodeArr[2], 10, 64)
if err != nil {
return output, err
}
newDisk.Inodes.Inodes = val
val, err = strconv.ParseUint(diskInodeArr[3], 10, 64)
if err != nil {
return output, err
}
newDisk.Inodes.Used = val
val, err = strconv.ParseUint(diskInodeArr[4], 10, 64)
if err != nil {
return output, err
}
newDisk.Inodes.Available = uint64(val)
newDisk.Inodes.Usage = diskInodeArr[5]
newDisk.Time = time.Now().Unix()
output = append(output, newDisk)
}
return output, nil
}
func getDiskInfo() []string {
// Filesystem Type 1K-blocks Used Available Use% Mounted on
result := exec.Execute("df", "-T", "-B1", "--exclude-type=tmpfs", "--exclude-type=devtmpfs", "--exclude-type=udev")
return strings.Split(result, "\n")[1:]
}
func getDiskInodeInfo() []string {
// Filesystem Type Inodes IUsed IFree IUse% Mounted on
result := exec.Execute("df", "-T", "-B1", "-i", "--exclude-type=tmpfs", "--exclude-type=devtmpfs", "--exclude-type=udev")
return strings.Split(result, "\n")[1:]
}
func (d *Disk) Convert(unit string) {
if d.Usage.Unit == Byte {
if unit == Kilobyte {
d.Usage.Size = d.Usage.Size / 1024
d.Usage.Used = d.Usage.Used / 1024
d.Usage.Available = d.Usage.Available / 1024
}
if unit == Megabyte {
d.Usage.Size = d.Usage.Size / 1024 / 1024
d.Usage.Used = d.Usage.Used / 1024 / 1024
d.Usage.Available = d.Usage.Available / 1024 / 1024
}
if unit == Gigabyte {
d.Usage.Size = d.Usage.Size / 1024 / 1024 / 1024
d.Usage.Used = d.Usage.Used / 1024 / 1024 / 1024
d.Usage.Available = d.Usage.Available / 1024 / 1024 / 1024
}
}
if d.Usage.Unit == Kilobyte {
if unit == Byte {
d.Usage.Size = d.Usage.Size * 1024
d.Usage.Used = d.Usage.Used * 1024
d.Usage.Available = d.Usage.Available * 1024
}
if unit == Megabyte {
d.Usage.Size = d.Usage.Size / 1024
d.Usage.Used = d.Usage.Used / 1024
d.Usage.Available = d.Usage.Available / 1024
}
if unit == Gigabyte {
d.Usage.Size = d.Usage.Size / 1024 / 1024
d.Usage.Used = d.Usage.Used / 1024 / 1024
d.Usage.Available = d.Usage.Available / 1024 / 1024
}
}
if d.Usage.Unit == Megabyte {
if unit == Byte {
d.Usage.Size = d.Usage.Size * 1024 * 1024
d.Usage.Used = d.Usage.Used * 1024 * 1024
d.Usage.Available = d.Usage.Available * 1024 * 1024
}
if unit == Kilobyte {
d.Usage.Size = d.Usage.Size * 1024
d.Usage.Used = d.Usage.Used * 1024
d.Usage.Available = d.Usage.Available * 1024
}
if unit == Gigabyte {
d.Usage.Size = d.Usage.Size / 1024
d.Usage.Used = d.Usage.Used / 1024
d.Usage.Available = d.Usage.Available / 1024
}
}
if d.Usage.Unit == Gigabyte {
if unit == Byte {
d.Usage.Size = d.Usage.Size * 1024 * 1024 * 1024
d.Usage.Used = d.Usage.Used * 1024 * 1024 * 1024
d.Usage.Available = d.Usage.Available * 1024 * 1024 * 1024
}
if unit == Kilobyte {
d.Usage.Size = d.Usage.Size * 1024 * 1024
d.Usage.Used = d.Usage.Used * 1024 * 1024
d.Usage.Available = d.Usage.Available * 1024 * 1024
}
if unit == Megabyte {
d.Usage.Size = d.Usage.Size * 1024
d.Usage.Used = d.Usage.Used * 1024
d.Usage.Available = d.Usage.Available * 1024
}
}
d.Usage.Unit = unit
}