/
sources.go
129 lines (98 loc) · 2.15 KB
/
sources.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
package main
import (
"errors"
"fmt"
"os"
)
type source interface {
initialize(r *rule) error
matches() (chan *match, error)
}
type fileSource struct {
rule *rule
path string
}
func (s *fileSource) initialize(r *rule) error {
s.rule = r
if len(r.Source) < 2 {
return errors.New("missing path parameter")
}
s.path = r.Source[1]
if fi, err := os.Stat(s.path); err == nil && fi.IsDir() {
return fmt.Errorf(`"%s" is a directory`, s.path)
}
if len(r.Source) > 2 {
return errors.New("superfluous parameter(s)")
}
return nil
}
func (s *fileSource) matches() (chan *match, error) {
return s.rule.processScanner("tail", "-n", "0", "-F", s.path)
}
type systemdSource struct {
rule *rule
service string
}
func (s *systemdSource) initialize(r *rule) error {
s.rule = r
if len(r.Source) < 2 {
return errors.New("missing service parameter")
}
s.service = r.Source[1]
if len(r.Source) > 2 {
return errors.New("superfluous parameter(s)")
}
return nil
}
func (s *systemdSource) matches() (chan *match, error) {
return s.rule.processScanner("journalctl", "-n", "0", "-f", "-u", s.service)
}
type kernelSource struct {
rule *rule
}
func (k *kernelSource) initialize(r *rule) error {
k.rule = r
if len(r.Source) > 1 {
return errors.New("superfluous parameter(s)")
}
return nil
}
func (k *kernelSource) matches() (chan *match, error) {
return k.rule.processScanner("journalctl", "-kf", "-n", "0")
}
type testSource struct {
rule *rule
matchesErr error
processPath string
}
func (s *testSource) initialize(r *rule) error {
s.rule = r
return nil
}
func (s *testSource) matches() (chan *match, error) {
if s.matchesErr != nil {
return nil, s.matchesErr
}
p := "test/producer"
if s.processPath != "" {
p = s.processPath
}
return s.rule.processScanner(p)
}
type processSource struct {
rule *rule
name string
args []string
}
func (s *processSource) initialize(r *rule) error {
s.rule = r
if len(r.Source) < 2 {
return errors.New("missing process name")
}
s.name = r.Source[1]
s.args = r.Source[2:]
return nil
}
func (s *processSource) matches() (chan *match, error) {
return s.rule.processScanner(s.name, s.args...)
}