-
Notifications
You must be signed in to change notification settings - Fork 51
/
monitor.go
183 lines (150 loc) · 4.35 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package monitor
import (
"fmt"
"strings"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance/cni"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance/docker"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance/linux"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance/uid"
"github.com/aporeto-inc/trireme-lib/internal/monitor/rpc"
"github.com/aporeto-inc/trireme-lib/internal/monitor/rpc/registerer"
"github.com/aporeto-inc/trireme-lib/rpc/processor"
"go.uber.org/zap"
)
// Type specifies the type of monitors supported.
type Type int
// Types supported.
const (
CNI Type = iota + 1
Docker
LinuxProcess
LinuxHost
UID
)
// Config specifies the configs for monitors.
type Config struct {
Common processor.Config
MergeTags []string
Monitors map[Type]interface{}
}
func (c *Config) String() string {
buf := fmt.Sprintf("MergeTags:[%s] ", strings.Join(c.MergeTags, ","))
buf += fmt.Sprintf("Common:%+v ", c.Common)
buf += fmt.Sprintf("Monitors:{")
for k, v := range c.Monitors {
buf += fmt.Sprintf("{%d:%+v},", k, v)
}
buf += fmt.Sprintf("}")
return buf
}
type monitors struct {
config *Config
monitors map[Type]monitorinstance.Implementation
userRPCListener rpcmonitor.Listener
userRegisterer registerer.Registerer
rootRPCListener rpcmonitor.Listener
rootRegisterer registerer.Registerer
syncHandler processor.SynchronizationHandler
}
// NewMonitors instantiates all/any combination of monitors supported.
func NewMonitors(collector collector.EventCollector, puhandler processor.ProcessingUnitsHandler, c *Config) (Monitor, error) {
var err error
c.Common.Collector = collector
c.Common.PUHandler = puhandler
if err = c.Common.IsComplete(); err != nil {
return nil, err
}
m := &monitors{
config: c,
monitors: make(map[Type]monitorinstance.Implementation),
syncHandler: c.Common.SyncHandler,
}
if m.userRPCListener, m.userRegisterer, err = rpcmonitor.New(
rpcmonitor.DefaultRPCAddress,
false,
); err != nil {
return nil, fmt.Errorf("Unable to create user RPC Listener %s", err.Error())
}
if m.rootRPCListener, m.rootRegisterer, err = rpcmonitor.New(
rpcmonitor.DefaultRootRPCAddress,
true,
); err != nil {
return nil, fmt.Errorf("Unable to create user RPC Listener %s", err.Error())
}
for k, v := range c.Monitors {
switch k {
case CNI:
mon := cnimonitor.New()
mon.SetupHandlers(&c.Common)
if err := mon.SetupConfig(m.userRegisterer, v); err != nil {
return nil, fmt.Errorf("CNI: %s", err.Error())
}
m.monitors[CNI] = mon
case Docker:
mon := dockermonitor.New()
mon.SetupHandlers(&c.Common)
if err := mon.SetupConfig(nil, v); err != nil {
return nil, fmt.Errorf("Docker: %s", err.Error())
}
m.monitors[Docker] = mon
case LinuxProcess:
mon := linuxmonitor.New()
mon.SetupHandlers(&c.Common)
if err := mon.SetupConfig(m.userRegisterer, v); err != nil {
return nil, fmt.Errorf("Process: %s", err.Error())
}
m.monitors[LinuxProcess] = mon
case LinuxHost:
mon := linuxmonitor.New()
mon.SetupHandlers(&c.Common)
if err := mon.SetupConfig(m.rootRegisterer, v); err != nil {
return nil, fmt.Errorf("Host: %s", err.Error())
}
m.monitors[LinuxHost] = mon
case UID:
mon := uidmonitor.New()
mon.SetupHandlers(&c.Common)
if err := mon.SetupConfig(m.userRegisterer, v); err != nil {
return nil, fmt.Errorf("UID: %s", err.Error())
}
m.monitors[UID] = mon
default:
return nil, fmt.Errorf("Unsupported type %d", k)
}
}
zap.L().Debug("Monitor configuration", zap.String("conf", m.config.String()))
return m, nil
}
func (m *monitors) Start() (err error) {
if err = m.userRPCListener.Start(); err != nil {
return err
}
if err = m.rootRPCListener.Start(); err != nil {
return err
}
for _, v := range m.monitors {
if err = v.Start(); err != nil {
return err
}
}
if m.syncHandler != nil {
m.syncHandler.HandleSynchronizationComplete(processor.SynchronizationTypeInitial)
}
return nil
}
func (m *monitors) Stop() error {
for _, v := range m.monitors {
if err := v.Stop(); err != nil {
return err
}
}
if err := m.userRPCListener.Stop(); err != nil {
return err
}
if err := m.rootRPCListener.Stop(); err != nil {
return err
}
return nil
}