forked from Meituan-Dianping/cat-go
/
monitor.go
133 lines (108 loc) · 2.9 KB
/
monitor.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
package cat
import (
"bytes"
"encoding/xml"
"runtime"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/Meituan-Dianping/cat-go/message"
)
type catMonitor struct {
scheduleMixin
collectors []Collector
}
func (m *catMonitor) GetName() string {
return "Monitor"
}
func sleep2NextMinute() *time.Timer {
var delta = 60 - time.Now().Second()
return time.NewTimer(time.Duration(delta) * time.Second)
}
func (m *catMonitor) afterStart() {
LogEvent(typeSystem, nameReboot)
m.collectAndSend()
}
func (m *catMonitor) process() {
timer := sleep2NextMinute()
defer timer.Stop()
select {
case sig := <-m.signals:
m.handle(sig)
case <-timer.C:
m.collectAndSend()
}
}
func (m *catMonitor) buildXml() *bytes.Buffer {
type ExtensionDetail struct {
Id string `xml:"id,attr"`
Value string `xml:"value,attr"`
}
type Extension struct {
Id string `xml:"id,attr"`
Desc string `xml:"description"`
Details []ExtensionDetail `xml:"extensionDetail"`
}
type CustomInfo struct {
Key string `xml:"key,attr"`
Value string `xml:"value,attr"`
}
type Status struct {
XMLName xml.Name `xml:"status"`
Extensions []Extension `xml:"extension"`
CustomInfos []CustomInfo `xml:"customInfo"`
}
status := Status{
Extensions: make([]Extension, 0, len(m.collectors)),
CustomInfos: make([]CustomInfo, 0, 3),
}
for _, collector := range m.collectors {
extension := Extension{
Id: collector.GetId(),
Desc: collector.GetDesc(),
Details: make([]ExtensionDetail, 0),
}
for k, v := range collector.GetProperties() {
detail := ExtensionDetail{
Id: k,
Value: v,
}
extension.Details = append(extension.Details, detail)
}
status.Extensions = append(status.Extensions, extension)
}
// add custom information.
status.CustomInfos = append(status.CustomInfos, CustomInfo{"gocat-version", GoCatVersion})
status.CustomInfos = append(status.CustomInfos, CustomInfo{"go-version", runtime.Version()})
buf := bytes.NewBuffer([]byte{})
encoder := xml.NewEncoder(buf)
encoder.Indent("", "\t")
if err := encoder.Encode(status); err != nil {
buf.Reset()
buf.WriteString(err.Error())
return buf
}
return buf
}
func (m *catMonitor) collectAndSend() {
var trans = message.NewTransaction(typeSystem, "Status", manager.flush)
defer trans.Complete()
trans.LogEvent("Cat_golang_Client_Version", GoCatVersion)
// NOTE type & name is useless while sending a heartbeat
heartbeat := message.NewHeartbeat("Heartbeat", config.ip, nil)
heartbeat.SetData(m.buildXml().String())
heartbeat.Complete()
trans.AddChild(heartbeat)
}
var monitor = catMonitor{
scheduleMixin: makeScheduleMixedIn(signalMonitorExit),
collectors: []Collector{
&memStatsCollector{},
&cpuInfoCollector{
lastTime: &cpu.TimesStat{},
lastCPUTime: 0,
},
},
}
func AddMonitorCollector(collector Collector) {
monitor.collectors = append(monitor.collectors, collector)
}