forked from Meituan-Dianping/cat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_collector.go
160 lines (131 loc) · 3.82 KB
/
monitor_collector.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
package cat
import (
"fmt"
"runtime"
"strconv"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
)
type Collector interface {
GetId() string
GetDesc() string
GetProperties() map[string]string
}
func b2kbstr(b uint64) string {
return strconv.Itoa(int(b / 1024))
}
func f642str(b float64) string {
return fmt.Sprintf("%f", b)
}
type memStatsCollector struct {
m runtime.MemStats
alloc,
mallocs,
lookups,
frees uint64
}
func (c *memStatsCollector) GetId() string {
return "mem.runtime"
}
func (c *memStatsCollector) GetDesc() string {
return "mem.runtime"
}
func (c *memStatsCollector) GetProperties() map[string]string {
runtime.ReadMemStats(&c.m)
m := map[string]string{
"mem.sys": b2kbstr(c.m.Sys),
// heap
"mem.heap.alloc": b2kbstr(c.m.HeapAlloc),
"mem.heap.sys": b2kbstr(c.m.HeapSys),
"mem.heap.idle": b2kbstr(c.m.HeapIdle),
"mem.heap.inuse": b2kbstr(c.m.HeapInuse),
"mem.heap.released": b2kbstr(c.m.HeapReleased),
"mem.heap.objects": strconv.Itoa(int(c.m.HeapObjects)),
// stack
"mem.stack.inuse": b2kbstr(c.m.StackInuse),
"mem.stack.sys": b2kbstr(c.m.StackSys),
}
if c.alloc > 0 {
m["mem.alloc"] = b2kbstr(c.m.TotalAlloc - c.alloc)
m["mem.mallocs"] = strconv.Itoa(int(c.m.Mallocs - c.mallocs))
m["mem.lookups"] = strconv.Itoa(int(c.m.Lookups - c.lookups))
m["mem.frees"] = strconv.Itoa(int(c.m.Frees - c.frees))
}
c.alloc = c.m.TotalAlloc
c.mallocs = c.m.Mallocs
c.lookups = c.m.Lookups
c.frees = c.m.Frees
return m
}
type cpuInfoCollector struct {
lastTime *cpu.TimesStat
lastCPUTime float64
}
func (c *cpuInfoCollector) GetId() string {
return "cpu"
}
func (c *cpuInfoCollector) GetDesc() string {
return "cpu"
}
func (c *cpuInfoCollector) GetProperties() map[string]string {
m := make(map[string]string)
if avg, err := load.Avg(); err == nil {
m["load.1min"] = f642str(avg.Load1)
m["load.5min"] = f642str(avg.Load5)
m["load.15min"] = f642str(avg.Load15)
m["system.load.average"] = m["load.1min"]
}
if times, err := cpu.Times(false); err == nil {
if len(times) > 0 {
currentTime := times[0]
currentCpuTime := 0.0 +
currentTime.User +
currentTime.System +
currentTime.Idle +
currentTime.Nice +
currentTime.Iowait +
currentTime.Irq +
currentTime.Softirq +
currentTime.Steal +
currentTime.Guest +
currentTime.GuestNice
if c.lastCPUTime > 0 {
cpuTime := currentCpuTime - c.lastCPUTime
if cpuTime > 0.0 {
user := currentTime.User - c.lastTime.User
system := currentTime.System - c.lastTime.System
nice := currentTime.Nice - c.lastTime.Nice
idle := currentTime.Idle - c.lastTime.Idle
iowait := currentTime.Iowait - c.lastTime.Iowait
softirq := currentTime.Softirq - c.lastTime.Softirq
irq := currentTime.Irq - c.lastTime.Irq
steal := currentTime.Steal - c.lastTime.Steal
m["cpu.user"] = f642str(user)
m["cpu.sys"] = f642str(system)
m["cpu.nice"] = f642str(nice)
m["cpu.idle"] = f642str(idle)
m["cpu.iowait"] = f642str(iowait)
m["cpu.softirq"] = f642str(softirq)
m["cpu.irq"] = f642str(irq)
m["cpu.steal"] = f642str(steal)
m["cpu.user.percent"] = f642str(user / cpuTime * 100)
m["cpu.sys.percent"] = f642str(system / cpuTime * 100)
m["cpu.nice.percent"] = f642str(nice / cpuTime * 100)
m["cpu.idle.percent"] = f642str(idle / cpuTime * 100)
m["cpu.iowait.percent"] = f642str(iowait / cpuTime * 100)
m["cpu.softirq.percent"] = f642str(softirq / cpuTime * 100)
m["cpu.irq.percent"] = f642str(irq / cpuTime * 100)
m["cpu.steal.percent"] = f642str(steal / cpuTime * 100)
}
}
c.lastCPUTime = currentCpuTime
c.lastTime = ¤tTime
}
}
// TODO process status
// if processes, err := process.Processes(); err == nil {
// for _, p := range processes {
// }
// }
return m
}