-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiskspace_linux.go
125 lines (100 loc) · 2.9 KB
/
diskspace_linux.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
// +build linux
package main
import (
"fmt"
"golang.org/x/sys/unix" //see https://godoc.org/golang.org/x/sys/unix
"io/ioutil"
"log"
"path/filepath"
"strings"
)
func (m *DiskspaceInputModule) GetMetrics() (*ModuleMetrics, error) {
metrics := make([]Metric, 0, 50)
filesystems, err := GetFileSystems(m)
if err != nil {
log.Printf("Error retrieving filesystems: %v", err)
return nil, err
}
stats, err := GetFilesystemStats(filesystems)
if err != nil {
log.Printf("Error retrieving filesystem stats: %v", err)
return nil, err
}
for _, stat := range stats {
used := NewMetric(fmt.Sprintf("%s.used", stat.DeviceName), stat.Used)
free := NewMetric(fmt.Sprintf("%s.free", stat.DeviceName), stat.Free)
reserved := NewMetric(fmt.Sprintf("%s.reserved", stat.DeviceName), stat.Reserved)
available := NewMetric(fmt.Sprintf("%s.available", stat.DeviceName), stat.Available)
metrics = append(metrics, used)
metrics = append(metrics, free)
metrics = append(metrics, reserved)
metrics = append(metrics, available)
}
return &ModuleMetrics{Module: m.Name(), Metrics: metrics}, nil
}
func GetFileSystems(m *DiskspaceInputModule) ([]Filesystem, error) {
b, err := ioutil.ReadFile("/proc/mounts")
if err != nil {
return nil, err
}
content := string(b)
lines := strings.Split(content, "\n")
filesystems := make([]Filesystem, 0, len(lines))
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
device := fields[0]
mount := fields[1]
fsType := fields[2]
if !m.CheckTypes.Contains(fsType) {
continue
}
mountPrefix := strings.Split(mount, "/")[1]
if mountPrefix == "proc" || mountPrefix == "dev" || mountPrefix == "sys" {
continue
}
deviceSym, err := filepath.EvalSymlinks(device)
if err == nil {
device = deviceSym
}
stat := unix.Stat_t{}
err = unix.Stat(mount, &stat)
if err != nil {
// filesystem likely not mounted
continue
}
fs := Filesystem{
Device: device,
Mount: mount,
Type: fsType,
}
filesystems = append(filesystems, fs)
}
return filesystems, nil
}
func GetFilesystemStats(filesystems []Filesystem) ([]FilesystemStats, error) {
stats := make([]FilesystemStats, 0, len(filesystems))
for _, fs := range filesystems {
stat := unix.Statfs_t{}
err := unix.Statfs(fs.Mount, &stat)
if err != nil {
continue
}
// change /dev/sda1 to sda1
var name string
if fs.Device[:1] == "/" {
_, name = filepath.Split(fs.Device)
}
if name == "" {
name = strings.Replace(fs.Mount[1:], "/", "_", -1)
}
used := float64(stat.Bsize) * float64(stat.Blocks-stat.Bfree)
free := float64(stat.Bsize) * float64(stat.Bfree)
reserved := float64(stat.Bsize) * float64(stat.Bfree-stat.Bavail)
available := float64(stat.Bsize) * float64(stat.Bavail)
stats = append(stats, FilesystemStats{DeviceName: name, Used: used, Free: free, Reserved: reserved, Available: available})
}
return stats, nil
}