/
monitor.go
148 lines (115 loc) · 3.88 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
package linuxmonitor
import (
"fmt"
"regexp"
"github.com/aporeto-inc/trireme-lib/constants"
"github.com/aporeto-inc/trireme-lib/internal/monitor/instance"
"github.com/aporeto-inc/trireme-lib/internal/monitor/rpc/registerer"
"github.com/aporeto-inc/trireme-lib/rpc/events"
"github.com/aporeto-inc/trireme-lib/rpc/processor"
"github.com/aporeto-inc/trireme-lib/utils/cgnetcls"
"github.com/aporeto-inc/trireme-lib/utils/contextstore"
)
// Config is the configuration options to start a CNI monitor
type Config struct {
EventMetadataExtractor events.EventMetadataExtractor
StoredPath string
ReleasePath string
Host bool
}
// DefaultConfig provides a default configuration
func DefaultConfig(host bool) *Config {
if host {
return &Config{
EventMetadataExtractor: events.DefaultHostMetadataExtractor,
StoredPath: "/var/run/trireme/host",
ReleasePath: "/var/lib/aporeto/cleaner",
Host: host,
}
}
return &Config{
EventMetadataExtractor: events.DefaultHostMetadataExtractor,
StoredPath: "/var/run/trireme/linux",
ReleasePath: "/var/lib/aporeto/cleaner",
Host: host,
}
}
// SetupDefaultConfig adds defaults to a partial configuration
func SetupDefaultConfig(linuxConfig *Config) *Config {
defaultConfig := DefaultConfig(linuxConfig.Host)
if linuxConfig.ReleasePath == "" {
linuxConfig.ReleasePath = defaultConfig.ReleasePath
}
if linuxConfig.StoredPath == "" {
linuxConfig.StoredPath = defaultConfig.StoredPath
}
if linuxConfig.EventMetadataExtractor == nil {
linuxConfig.EventMetadataExtractor = defaultConfig.EventMetadataExtractor
}
return linuxConfig
}
// linuxMonitor captures all the monitor processor information
// It implements the EventProcessor interface of the rpc monitor
type linuxMonitor struct {
proc *linuxProcessor
}
// New returns a new implmentation of a monitor implmentation
func New() monitorinstance.Implementation {
return &linuxMonitor{
proc: &linuxProcessor{},
}
}
// Start implements Implementation interface
func (l *linuxMonitor) Start() error {
if err := l.proc.config.IsComplete(); err != nil {
return fmt.Errorf("linux %t: %s", l.proc.host, err)
}
if err := l.ReSync(); err != nil {
return err
}
return nil
}
// Stop implements Implementation interface
func (l *linuxMonitor) Stop() error {
return nil
}
// SetupConfig provides a configuration to implmentations. Every implmentation
// can have its own config type.
func (l *linuxMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
defaultConfig := DefaultConfig(false)
if cfg == nil {
cfg = defaultConfig
}
linuxConfig, ok := cfg.(*Config)
if !ok {
return fmt.Errorf("Invalid configuration specified")
}
if registerer != nil {
if err := registerer.RegisterProcessor(constants.LinuxProcessPU, l.proc); err != nil {
return err
}
}
// Setup defaults
linuxConfig = SetupDefaultConfig(linuxConfig)
// Setup config
l.proc.host = linuxConfig.Host
l.proc.netcls = cgnetcls.NewCgroupNetController(linuxConfig.ReleasePath)
l.proc.contextStore = contextstore.NewFileContextStore(linuxConfig.StoredPath, l.proc.RemapData)
l.proc.storePath = linuxConfig.StoredPath
l.proc.regStart = regexp.MustCompile("^[a-zA-Z0-9_].{0,11}$")
l.proc.regStop = regexp.MustCompile("^/trireme/[a-zA-Z0-9_].{0,11}$")
l.proc.metadataExtractor = linuxConfig.EventMetadataExtractor
if l.proc.metadataExtractor == nil {
return fmt.Errorf("Unable to setup a metadata extractor")
}
return nil
}
// SetupHandlers sets up handlers for monitors to invoke for various events such as
// processing unit events and synchronization events. This will be called before Start()
// by the consumer of the monitor
func (l *linuxMonitor) SetupHandlers(m *processor.Config) {
l.proc.config = m
}
func (l *linuxMonitor) ReSync() error {
return l.proc.ReSync(nil)
}