-
Notifications
You must be signed in to change notification settings - Fork 13
/
diskspace.go
47 lines (35 loc) · 904 Bytes
/
diskspace.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
package goshin
import "fmt"
import "os/exec"
import "strings"
import "strconv"
import "github.com/tjgq/broadcast"
type DiskSpace struct {
}
func (m *DiskSpace) Collect(queue chan *Metric, listener *broadcast.Listener) {
for {
<-listener.Ch
out, _ := exec.Command("sh", "-c", "df -P").Output()
lines := strings.Split(string(out), "\n")
for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
if !strings.Contains(fields[0], "/") {
continue
}
var capacity float64
fields[4] = strings.Replace(fields[4], "%", "", -1)
capacity, _ = strconv.ParseFloat(fields[4], 64)
metric := NewMetric()
metric.Service = fmt.Sprint("disk ", fields[5])
metric.Value = capacity / 100
metric.Description = fmt.Sprint(capacity, "% used")
queue <- metric
}
}
}
func NewDiskSpace() *DiskSpace {
return &DiskSpace{}
}