-
Notifications
You must be signed in to change notification settings - Fork 51
/
monitor.go
176 lines (143 loc) · 4.24 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
// +build linux !windows
package monitor
import (
"context"
"fmt"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/monitor/config"
cnimonitor "go.aporeto.io/trireme-lib/monitor/internal/cni"
dockermonitor "go.aporeto.io/trireme-lib/monitor/internal/docker"
kubernetesmonitor "go.aporeto.io/trireme-lib/monitor/internal/kubernetes"
linuxmonitor "go.aporeto.io/trireme-lib/monitor/internal/linux"
podmonitor "go.aporeto.io/trireme-lib/monitor/internal/pod"
uidmonitor "go.aporeto.io/trireme-lib/monitor/internal/uid"
"go.aporeto.io/trireme-lib/monitor/registerer"
"go.aporeto.io/trireme-lib/monitor/remoteapi/server"
"go.uber.org/zap"
)
type monitors struct {
config *config.MonitorConfig
monitors map[config.Type]Implementation
registerer registerer.Registerer
server server.APIServer
}
// NewMonitors instantiates all/any combination of monitors supported.
func NewMonitors(opts ...Options) (Monitor, error) {
var err error
c := &config.MonitorConfig{
MergeTags: []string{},
Common: &config.ProcessorConfig{},
Monitors: map[config.Type]interface{}{},
}
for _, opt := range opts {
opt(c)
}
if err = c.Common.IsComplete(); err != nil {
return nil, err
}
m := &monitors{
config: c,
monitors: make(map[config.Type]Implementation),
}
m.registerer = registerer.New()
m.server, err = server.NewEventServer(common.TriremeSocket, m.registerer)
if err != nil {
return nil, err
}
for k, v := range c.Monitors {
switch k {
case config.CNI:
mon := cnimonitor.New()
mon.SetupHandlers(c.Common)
if err := mon.SetupConfig(m.registerer, v); err != nil {
return nil, fmt.Errorf("CNI: %s", err.Error())
}
m.monitors[config.CNI] = mon
case config.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[config.Docker] = mon
case config.Kubernetes:
mon := kubernetesmonitor.New()
mon.SetupHandlers(c.Common)
if err := mon.SetupConfig(nil, v); err != nil {
return nil, fmt.Errorf("kubernetes: %s", err.Error())
}
m.monitors[config.Kubernetes] = mon
case config.Pod:
mon := podmonitor.New()
mon.SetupHandlers(c.Common)
if err := mon.SetupConfig(nil, v); err != nil {
return nil, fmt.Errorf("pod: %s", err.Error())
}
m.monitors[config.Pod] = mon
case config.LinuxProcess:
mon := linuxmonitor.New()
mon.SetupHandlers(c.Common)
if err := mon.SetupConfig(m.registerer, v); err != nil {
return nil, fmt.Errorf("Process: %s", err.Error())
}
m.monitors[config.LinuxProcess] = mon
case config.SSH:
mon := linuxmonitor.New()
mon.SetupHandlers(c.Common)
if err := mon.SetupConfig(m.registerer, v); err != nil {
return nil, fmt.Errorf("Process: %s", err.Error())
}
m.monitors[config.SSH] = mon
case config.LinuxHost:
mon := linuxmonitor.New()
mon.SetupHandlers(c.Common)
if err := mon.SetupConfig(m.registerer, v); err != nil {
return nil, fmt.Errorf("Host: %s", err.Error())
}
m.monitors[config.LinuxHost] = mon
case config.UID:
mon := uidmonitor.New()
mon.SetupHandlers(c.Common)
if err := mon.SetupConfig(m.registerer, v); err != nil {
return nil, fmt.Errorf("UID: %s", err.Error())
}
m.monitors[config.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) Run(ctx context.Context) (err error) {
if err = m.server.Run(ctx); err != nil {
return err
}
for _, v := range m.monitors {
if err = v.Run(ctx); err != nil {
return err
}
}
return nil
}
// UpdateConfiguration updates the configuration of the monitors.
func (m *monitors) UpdateConfiguration(ctx context.Context, config *config.MonitorConfig) error {
// Monitor configuration cannot change at this time.
// TODO:
return nil
}
// Resync resyncs the monitor
func (m *monitors) Resync(ctx context.Context) error {
failure := false
var errs string
for _, i := range m.monitors {
if err := i.Resync(ctx); err != nil {
errs = errs + err.Error()
failure = true
}
}
if failure {
return fmt.Errorf("Monitor resync failed: %s", errs)
}
return nil
}