-
Notifications
You must be signed in to change notification settings - Fork 51
/
monitor.go
141 lines (112 loc) · 3.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
// +build linux !windows
package monitor
import (
"context"
"fmt"
"go.aporeto.io/enforcerd/trireme-lib/common"
"go.aporeto.io/enforcerd/trireme-lib/monitor/config"
dockermonitor "go.aporeto.io/enforcerd/trireme-lib/monitor/internal/docker"
k8smonitor "go.aporeto.io/enforcerd/trireme-lib/monitor/internal/k8s"
linuxmonitor "go.aporeto.io/enforcerd/trireme-lib/monitor/internal/linux"
"go.aporeto.io/enforcerd/trireme-lib/monitor/registerer"
"go.aporeto.io/enforcerd/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(ctx context.Context, 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.Docker:
mon := dockermonitor.New(ctx)
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.K8s:
mon := k8smonitor.New(ctx)
mon.SetupHandlers(c.Common)
if err := mon.SetupConfig(nil, v); err != nil {
return nil, fmt.Errorf("K8s: %s", err.Error())
}
m.monitors[config.K8s] = mon
case config.LinuxProcess:
mon := linuxmonitor.New(ctx)
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.LinuxHost:
mon := linuxmonitor.New(ctx)
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
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
}