-
Notifications
You must be signed in to change notification settings - Fork 3
/
system.go
100 lines (86 loc) · 1.92 KB
/
system.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
package systemutils
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"sync"
"time"
"github.com/cloudfoundry/gosigar"
)
type system struct {
sync.Mutex
cpuUsage float64
totalTicks float64
ticks float64
}
func (s *system) start() {
go func() {
for {
idle0, total0 := getCPUSample()
time.Sleep(2 * time.Second)
idle1, total1 := getCPUSample()
idleTicks := float64(idle1 - idle0)
s.Lock()
s.totalTicks = float64(total1 - total0)
s.ticks = s.totalTicks - idleTicks
if s.totalTicks > 0 {
s.cpuUsage = 100 * s.ticks / s.totalTicks
} else {
s.cpuUsage = 0
}
s.Unlock()
}
}()
}
func getCPUSample() (idle, total uint64) {
contents, err := ioutil.ReadFile("/proc/stat")
if err != nil {
return
}
lines := strings.Split(string(contents), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) > 0 && fields[0] == "cpu" {
numFields := len(fields)
for i := 1; i < numFields; i++ {
val, err := strconv.ParseUint(fields[i], 10, 64)
if err != nil {
fmt.Println("Error: ", i, fields[i], err)
}
total += val // tally up all the numbers to get total ticks
if i == 4 { // idle is the 5th field in the cpu line
idle = val
}
}
return
}
}
return
}
func (s *system) CpuUsage() (usage float64, total float64, ticks float64) {
s.Lock()
defer s.Unlock()
return s.cpuUsage, s.ticks, s.totalTicks
}
func (s *system) MemUsage() (total, used, free uint64) {
mem := sigar.Mem{}
mem.Get()
return mem.Total, mem.ActualUsed, mem.ActualFree
}
func (s *system) Luns() map[string]Lun {
luns := make(map[string]Lun)
/*
var dev string
//XXX Temporarily disable scanning devices.
return luns
out, _ := exec.Command("/sbin/fdisk", "-l").Output()
lines := strings.Split(string(out), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "Disk /") {
luns[dev] = Lun{Capacity: 800}
}
}
*/
return luns
}