-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.go
245 lines (193 loc) · 5.44 KB
/
disk.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
/**
* Disk Usage
*/
import (
"fmt"
"log"
"sort"
"syscall"
"time"
linuxproc "github.com/c9s/goprocinfo/linux"
set "github.com/fatih/set"
ui "github.com/gizak/termui"
)
////////////////////////////////////////////
// Utility: Disk Usage
////////////////////////////////////////////
type DiskUsage struct {
MountPoint string
FSType string
TotalSizeInBytes uint64
AvailableSizeInBytes uint64
FreePercentage float64
InodesInUse uint64
TotalInodes uint64
FreeInodesPercentage float64
}
var IgnoreFilesystemTypes = buildIgnoreSet()
func buildIgnoreSet() set.Interface {
ignore := set.New(set.NonThreadSafe)
ignore.Add("sysfs", "proc", "udev", "devpts", "tmpfs", "cgroup", "systemd-1",
"mqueue", "debugfs", "hugetlbfs", "fusectl", "tracefs", "binfmt_misc",
"devtmpfs", "securityfs", "pstore", "autofs", "fuse.jetbrains-toolbox",
"fuse.gvfsd-fuse", "fuse.lxcfs", "fuse.objfsd", "fuse.srcfsd", "fuse.x20fsd",
"fuse.binfs", "sunrpc", "efivarfs")
return ignore
}
func loadDiskUsage() map[string]DiskUsage {
diskUsageData := make(map[string]DiskUsage, 0)
// Load mount points
mounts, mountsErr := linuxproc.ReadMounts("/proc/mounts")
if mountsErr != nil {
log.Printf("Error loading mounts: %v", mountsErr)
} else {
for _, mnt := range mounts.Mounts {
if IgnoreFilesystemTypes.Has(mnt.FSType) {
// Skip it
continue
}
// Also skip these docker fs, since it's a dup of root
if "/var/lib/docker/aufs" == mnt.MountPoint || "/var/lib/docker/devicemapper" == mnt.MountPoint {
// Skip it
continue
}
statfs := syscall.Statfs_t{}
statErr := syscall.Statfs(mnt.MountPoint, &statfs)
if statErr != nil {
log.Printf("Error statfs-ing mount: %v", mnt.MountPoint)
} else {
var totalBytes uint64 = 0
var availBytes uint64 = 0
var bytesFreePercent float64 = 0
var totalInodes uint64 = 0
var freeInodes uint64 = 0
var inodesFreePercent float64 = 0
var blocksize uint64 = 0
if statfs.Bsize > 0 {
blocksize = uint64(statfs.Bsize)
} else {
// Skip it
continue
}
totalBytes = statfs.Blocks * blocksize
availBytes = statfs.Bavail * blocksize
if totalBytes > 0 {
bytesFreePercent = float64(availBytes) / float64(totalBytes)
} else {
// Skip it
continue
}
totalInodes = statfs.Files
freeInodes = statfs.Ffree
if totalInodes > 0 {
inodesFreePercent = float64(freeInodes) / float64(totalInodes)
} else {
log.Printf("Bad total inodes: %v", totalInodes)
}
usage := DiskUsage{
MountPoint: mnt.MountPoint,
FSType: mnt.FSType,
TotalSizeInBytes: totalBytes,
AvailableSizeInBytes: availBytes,
FreePercentage: bytesFreePercent,
TotalInodes: totalInodes,
InodesInUse: totalInodes - freeInodes,
FreeInodesPercentage: inodesFreePercent,
}
diskUsageData[mnt.MountPoint] = usage
}
}
}
return diskUsageData
}
const DiskUsageUpdateInterval = 30 * time.Second
type CachedDiskUsage struct {
LastUsage map[string]DiskUsage
lastUpdated *time.Time
}
func (w *CachedDiskUsage) getUpdateInterval() time.Duration {
return DiskUsageUpdateInterval
}
func (w *CachedDiskUsage) getLastUpdated() *time.Time {
return w.lastUpdated
}
func (w *CachedDiskUsage) setLastUpdated(t time.Time) {
w.lastUpdated = &t
}
func (w *CachedDiskUsage) update() {
if shouldUpdate(w) {
w.LastUsage = loadDiskUsage()
}
}
func NewCachedDiskUsage() *CachedDiskUsage {
// Build it
w := &CachedDiskUsage{}
w.update()
return w
}
var cachedDiskUsage = NewCachedDiskUsage()
////////////////////////////////////////////
// Widget: Disk
////////////////////////////////////////////
const DiskHeaderText = "--- Disks ---"
type DiskColumn struct {
column *ui.Row
header *ui.Paragraph
widgets []*ui.Gauge
}
func NewDiskColumn(span int, offset int) *DiskColumn {
c := ui.NewCol(span, offset)
h := ui.NewParagraph(DiskHeaderText)
h.Border = false
h.TextFgColor = ui.ColorGreen
h.Height = 1
column := &DiskColumn{
column: c,
header: h,
widgets: make([]*ui.Gauge, 0),
}
column.update()
return column
}
func (w *DiskColumn) getGridWidget() ui.GridBufferer {
return w.column
}
func (w *DiskColumn) getColumn() *ui.Row {
return w.column
}
func (w *DiskColumn) update() {
w.header.Text = centerString(w.header.Width, DiskHeaderText)
//w.header.Text = DiskHeaderText
gauges := make([]*ui.Gauge, 0)
for _, d := range cachedDiskUsage.LastUsage {
gauges = append(gauges, NewDiskGauge(d))
}
sort.Sort(ByMountPoint(gauges))
w.column.Cols = []*ui.Row{}
ir := w.column
for _, widget := range gauges {
nr := &ui.Row{Span: 12, Widget: widget}
ir.Cols = []*ui.Row{nr}
ir = nr
}
}
func (w *DiskColumn) resize() {
// Do nothing
}
func NewDiskGauge(usage DiskUsage) *ui.Gauge {
free := int(100 * usage.FreePercentage)
g := ui.NewGauge()
g.BorderLabel = usage.MountPoint
g.Height = 3
g.Percent = free
g.Label = fmt.Sprintf("Free: %s/%s (%d%%)",
prettyPrintBytes(usage.AvailableSizeInBytes), prettyPrintBytes(usage.TotalSizeInBytes), free)
g.PercentColor = ui.ColorWhite | ui.AttrBold
g.BarColor = percentToAttribute(free, 0, 100, false)
return g
}
type ByMountPoint []*ui.Gauge
func (a ByMountPoint) Len() int { return len(a) }
func (a ByMountPoint) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByMountPoint) Less(i, j int) bool { return a[i].BorderLabel < a[j].BorderLabel }