-
Notifications
You must be signed in to change notification settings - Fork 51
/
monitor.go
100 lines (79 loc) · 2.58 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
package linuxmonitor
import (
"context"
"fmt"
"regexp"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/monitor/config"
"go.aporeto.io/trireme-lib/monitor/registerer"
"go.aporeto.io/trireme-lib/utils/cgnetcls"
)
// 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() *LinuxMonitor {
return &LinuxMonitor{
proc: &linuxProcessor{},
}
}
// Run implements Implementation interface
func (l *LinuxMonitor) Run(ctx context.Context) error {
if err := l.proc.config.IsComplete(); err != nil {
return fmt.Errorf("linux %t: %s", l.proc.host, err)
}
return l.Resync(ctx)
}
// SetupConfig provides a configuration to implmentations. Every implmentation
// can have its own config type.
func (l *LinuxMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
if cfg == nil {
cfg = DefaultConfig(false, false)
}
linuxConfig, ok := cfg.(*Config)
if !ok {
return fmt.Errorf("Invalid configuration specified")
}
if registerer != nil {
if linuxConfig.SSH {
if err := registerer.RegisterProcessor(common.SSHSessionPU, l.proc); err != nil {
return err
}
} else {
if err := registerer.RegisterProcessor(common.HostNetworkPU, l.proc); err != nil {
return err
}
if err := registerer.RegisterProcessor(common.HostPU, l.proc); err != nil {
return err
}
if err := registerer.RegisterProcessor(common.LinuxProcessPU, l.proc); err != nil {
return err
}
}
}
// Setup defaults
linuxConfig = SetupDefaultConfig(linuxConfig)
// Setup config
l.proc.host = linuxConfig.Host
l.proc.ssh = linuxConfig.SSH
l.proc.netcls = cgnetcls.NewCgroupNetController(common.TriremeCgroupPath, linuxConfig.ReleasePath)
l.proc.regStart = regexp.MustCompile("^[a-zA-Z0-9_]{1,11}$")
l.proc.regStop = regexp.MustCompile("^/trireme/[a-zA-Z0-9_]{1,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 *config.ProcessorConfig) {
l.proc.config = m
}
// Resync instructs the monitor to do a resync.
func (l *LinuxMonitor) Resync(ctx context.Context) error {
return l.proc.Resync(ctx, nil)
}