-
Notifications
You must be signed in to change notification settings - Fork 2
/
rule.go
81 lines (70 loc) · 1.8 KB
/
rule.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
package main
import (
"errors"
"fmt"
"plugin"
"github.com/patrobinson/go-fish/output"
"github.com/patrobinson/go-fish/state"
log "github.com/sirupsen/logrus"
)
// Rule is an interface for rule implementations
// Rules implement this interface as a plugin
type Rule interface {
Init(...interface{}) error
Process(interface{}) interface{}
String() string
WindowInterval() int
Window() ([]output.OutputEvent, error)
Close() error
}
type ruleConfig struct {
Source string `json:"source"`
State string `json:"state,omitempty"`
Plugin string `json:"plugin"`
Sink string `json:"sink,omitempty"`
}
func testRule(ruleFile string) error {
plug, err := plugin.Open(ruleFile)
if err != nil {
return fmt.Errorf("Unable to load plugin: %s", err)
}
symRule, err := plug.Lookup("Rule")
if err != nil {
return fmt.Errorf("Rule has no Rule symbol: %v", err)
}
_ = symRule.(Rule)
return nil
}
func newRule(config ruleConfig, s state.State) (Rule, error) {
plug, err := plugin.Open(config.Plugin)
if err != nil {
return nil, fmt.Errorf("Unable to load plugin %s: %s", config.Plugin, err)
}
symRule, err := plug.Lookup("Rule")
if err != nil {
return nil, fmt.Errorf("Rule has no Rule symbol: %v", err)
}
rule, ok := symRule.(Rule)
if !ok {
return nil, errors.New("Rule is not a rule type")
}
if err := rule.Init(s); err != nil {
return nil, err
}
return rule, nil
}
func startRule(rule Rule, output *chan interface{}, windower *windowManager) *chan interface{} {
input := make(chan interface{})
log.Debugf("Starting %v\n", rule.String())
go func(input *chan interface{}, output *chan interface{}, r Rule) {
defer r.Close()
for str := range *input {
res := r.Process(str)
*output <- res
}
}(&input, output, rule)
if rule.WindowInterval() > 0 {
windower.start()
}
return &input
}