github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/internal/linux/monitor.go (about)

     1  package linuxmonitor
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"regexp"
     7  
     8  	"go.aporeto.io/enforcerd/trireme-lib/common"
     9  	"go.aporeto.io/enforcerd/trireme-lib/monitor/config"
    10  	"go.aporeto.io/enforcerd/trireme-lib/monitor/registerer"
    11  	"go.aporeto.io/enforcerd/trireme-lib/utils/cgnetcls"
    12  )
    13  
    14  // LinuxMonitor captures all the monitor processor information
    15  // It implements the EventProcessor interface of the rpc monitor
    16  type LinuxMonitor struct {
    17  	proc *linuxProcessor
    18  }
    19  
    20  // New returns a new implmentation of a monitor implmentation
    21  func New(context.Context) *LinuxMonitor {
    22  
    23  	return &LinuxMonitor{
    24  		proc: &linuxProcessor{},
    25  	}
    26  }
    27  
    28  // Run implements Implementation interface
    29  func (l *LinuxMonitor) Run(ctx context.Context) error {
    30  
    31  	if err := l.proc.config.IsComplete(); err != nil {
    32  		return fmt.Errorf("linux %t: %s", l.proc.host, err)
    33  	}
    34  
    35  	return l.Resync(ctx)
    36  }
    37  
    38  // SetupConfig provides a configuration to implmentations. Every implmentation
    39  // can have its own config type.
    40  func (l *LinuxMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
    41  
    42  	if cfg == nil {
    43  		cfg = DefaultConfig(false)
    44  	}
    45  
    46  	linuxConfig, ok := cfg.(*Config)
    47  	if !ok {
    48  		return fmt.Errorf("Invalid configuration specified")
    49  	}
    50  
    51  	if registerer != nil {
    52  		if err := registerer.RegisterProcessor(common.HostNetworkPU, l.proc); err != nil {
    53  			return err
    54  		}
    55  		if err := registerer.RegisterProcessor(common.HostPU, l.proc); err != nil {
    56  			return err
    57  		}
    58  		if err := registerer.RegisterProcessor(common.LinuxProcessPU, l.proc); err != nil {
    59  			return err
    60  		}
    61  	}
    62  
    63  	// Setup defaults
    64  	linuxConfig = SetupDefaultConfig(linuxConfig)
    65  
    66  	// Setup config
    67  	l.proc.host = linuxConfig.Host
    68  
    69  	l.proc.netcls = cgnetcls.NewCgroupNetController(common.TriremeCgroupPath, linuxConfig.ReleasePath)
    70  
    71  	l.proc.regStart = regexp.MustCompile("^[a-zA-Z0-9_]{1,11}$")
    72  	l.proc.regStop = regexp.MustCompile("^/trireme/[a-zA-Z0-9_]{1,11}$")
    73  
    74  	l.proc.metadataExtractor = linuxConfig.EventMetadataExtractor
    75  	if l.proc.metadataExtractor == nil {
    76  		return fmt.Errorf("Unable to setup a metadata extractor")
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  // SetupHandlers sets up handlers for monitors to invoke for various events such as
    83  // processing unit events and synchronization events. This will be called before Start()
    84  // by the consumer of the monitor
    85  func (l *LinuxMonitor) SetupHandlers(m *config.ProcessorConfig) {
    86  
    87  	l.proc.config = m
    88  }
    89  
    90  // Resync instructs the monitor to do a resync.
    91  func (l *LinuxMonitor) Resync(ctx context.Context) error {
    92  	return l.proc.Resync(ctx, nil)
    93  }