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

     1  // +build windows
     2  
     3  package windowsmonitor
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"regexp"
     9  
    10  	"go.aporeto.io/enforcerd/trireme-lib/common"
    11  	"go.aporeto.io/enforcerd/trireme-lib/monitor/config"
    12  	"go.aporeto.io/enforcerd/trireme-lib/monitor/registerer"
    13  )
    14  
    15  // WindowsMonitor hold state for the windows monitor
    16  type WindowsMonitor struct {
    17  	proc *windowsProcessor
    18  }
    19  
    20  // New returns a new implmentation of a monitor implmentation
    21  func New(context.Context) *WindowsMonitor {
    22  	return &WindowsMonitor{
    23  		proc: &windowsProcessor{},
    24  	}
    25  }
    26  
    27  // Run implements Implementation interface
    28  func (w *WindowsMonitor) Run(ctx context.Context) error {
    29  	if err := w.proc.config.IsComplete(); err != nil {
    30  		return fmt.Errorf("windows %t: %s", w.proc.host, err)
    31  	}
    32  
    33  	return w.Resync(ctx)
    34  
    35  }
    36  
    37  // SetupHandlers sets up handlers for monitors to invoke for various events such as
    38  // processing unit events and synchronization events. This will be called before Start()
    39  // by the consumer of the monitor
    40  func (w *WindowsMonitor) SetupHandlers(m *config.ProcessorConfig) {
    41  	w.proc.config = m
    42  }
    43  
    44  // SetupConfig sets up the config for the monitor
    45  func (w *WindowsMonitor) SetupConfig(registerer registerer.Registerer, cfg interface{}) error {
    46  	if cfg == nil {
    47  		cfg = DefaultConfig(false)
    48  	}
    49  	windowsConfig, ok := cfg.(*Config)
    50  	if !ok {
    51  		return fmt.Errorf("Invalid configuration specified")
    52  	}
    53  	if registerer != nil {
    54  		if err := registerer.RegisterProcessor(common.HostPU, w.proc); err != nil {
    55  			return err
    56  		}
    57  		if err := registerer.RegisterProcessor(common.HostNetworkPU, w.proc); err != nil {
    58  			return err
    59  		}
    60  		if err := registerer.RegisterProcessor(common.WindowsProcessPU, w.proc); err != nil {
    61  			return err
    62  		}
    63  	}
    64  	windowsConfig = SetupDefaultConfig(windowsConfig)
    65  	w.proc.host = windowsConfig.Host
    66  	w.proc.regStart = regexp.MustCompile("^[a-zA-Z0-9_]{1,11}$")
    67  	w.proc.metadataExtractor = windowsConfig.EventMetadataExtractor
    68  	if w.proc.metadataExtractor == nil {
    69  		return fmt.Errorf("Unable to setup a metadata extractor")
    70  	}
    71  	return nil
    72  }
    73  
    74  // Resync instructs the monitor to do a resync.
    75  func (w *WindowsMonitor) Resync(ctx context.Context) error {
    76  	return w.proc.Resync(ctx, nil)
    77  }