forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_info.go
executable file
·84 lines (77 loc) · 2.24 KB
/
disk_info.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
package hostInfo
import (
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/utils"
"github.com/shirou/gopsutil/disk"
"math"
"strings"
)
func (d DiskCollector) convertUnits(number uint64) float64 {
// return in MB
return math.Floor(float64(number/d.Unit*1000)) / 1000.0
}
func (d DiskCollector) getDockerStorageInfo(infoData model.InfoData) map[string]interface{} {
data := map[string]interface{}{}
info := infoData.Info
for _, item := range info.DriverStatus {
data[item[0]] = item[1]
}
return data
}
func (d DiskCollector) includeInFilesystem(infoData model.InfoData, device string) bool {
include := true
if infoData.Info.Driver == "devicemapper" {
pool := d.getDockerStorageInfo(infoData)
poolName, ok := pool["Pool Name"]
if !ok {
poolName = "/dev/mapper/docker-"
}
if strings.HasSuffix(utils.InterfaceToString(poolName), "-pool") {
poolName := utils.InterfaceToString(poolName)
poolName = poolName[len(poolName)-5 : len(poolName)]
}
if strings.Contains(device, utils.InterfaceToString(poolName)) {
include = false
}
}
return include
}
func (d DiskCollector) getMountPoints() (map[string]interface{}, error) {
data := map[string]interface{}{}
partitions, err := disk.Partitions(false)
if err != nil {
return data, err
}
for _, partition := range partitions {
usage, err := disk.Usage(partition.Mountpoint)
if err != nil {
return map[string]interface{}{}, err
}
data[partition.Device] = map[string]interface{}{
"free": d.convertUnits(usage.Free),
"total": d.convertUnits(usage.Total),
"used": d.convertUnits(usage.Used),
"percentage": usage.UsedPercent,
}
}
return data, nil
}
func (d DiskCollector) getMachineFilesystems(infoData model.InfoData) (map[string]interface{}, error) {
data := map[string]interface{}{}
partitions, err := disk.Partitions(false)
if err != nil {
return data, err
}
for _, partition := range partitions {
if d.includeInFilesystem(infoData, partition.Device) {
usage, err := disk.Usage(partition.Mountpoint)
if err != nil {
return map[string]interface{}{}, err
}
data[utils.InterfaceToString(partition.Device)] = map[string]interface{}{
"capacity": d.convertUnits(usage.Total),
}
}
}
return data, nil
}