Fix match logic

This commit is contained in:
ppom 2024-04-09 12:00:00 +02:00
commit a2be0c2ac6
5 changed files with 41 additions and 36 deletions

View file

@ -2,6 +2,7 @@ package app
import (
"bufio"
"fmt"
"os"
"os/exec"
"os/signal"
@ -78,38 +79,34 @@ func (p *Pattern) notAnIgnore(match *string) bool {
// Whether one of the filter's regexes is matched on a line
func (f *Filter) match(line *string) string {
var result string
for _, regex := range f.compiledRegex {
if matches := regex.FindStringSubmatch(*line); matches != nil {
var pnames []string
for _, p := range f.pattern {
pnames = append(pnames, p.name)
}
for _, p := range f.pattern {
match := matches[regex.SubexpIndex(p.name)]
if p.notAnIgnore(&match) {
logger.Printf(logger.INFO, "%s.%s: match [%v]\n", f.stream.name, f.name, match)
if len(result) == 0 {
result = match
} else {
result = strings.Join([]string{result, match}, "\x00")
if f.pattern != nil {
var result []string
for _, p := range f.pattern {
match := matches[regex.SubexpIndex(p.name)]
if p.notAnIgnore(&match) {
result = append(result, match)
}
}
}
if f.pattern == nil {
if len(result) == len(f.pattern) {
var b strings.Builder
fmt.Fprintf(&b, "%s.%s: match ", f.stream.name, f.name)
for _, match := range result {
fmt.Fprintf(&b, "[%s]", match)
}
logger.Printf(logger.INFO, b.String())
return strings.Join(result, "\x00")
}
} else {
logger.Printf(logger.INFO, "%s.%s: match [.]\n", f.stream.name, f.name)
// No pattern, so this match will never actually be used
return ""
return "."
}
}
}
if len(strings.Split(result, "\x00")) == len(f.pattern) {
return result
} else {
// Incomplete match = no match
return ""
}
return ""
}
func (f *Filter) sendActions(match string, at time.Time) {
@ -335,7 +332,7 @@ func StreamManager(s *Stream, endedSignal chan *Stream) {
return
}
for _, filter := range s.Filters {
if match := filter.match(line); len(match) > 0 {
if match := filter.match(line); match != "" {
matchesC <- PFT{match, filter, time.Now()}
}
}

View file

@ -100,8 +100,8 @@ func basicUsage() {
-l/--limit STREAM[.FILTER] # only show items related to this STREAM (or STREAM.FILTER)
-p/--pattern PATTERN # only show items matching the PATTERN regex
` + bold + `reaction flush` + reset + ` TARGET
# remove currently active matches and run currently pending actions for the specified TARGET
` + bold + `reaction flush` + reset + ` TARGET [TARGET...]
# remove currently active matches and run currently pending actions for the specified TARGET(s)
# (then show flushed matches and actions)
# e.g. reaction flush 192.168.1.1 root

View file

@ -116,7 +116,7 @@ func (c *Conf) setup() {
}
for actionName := range filter.Actions {
action := filter.Actions[actionName]
action := filter.Actions[actionName]
action.filter = filter
action.name = actionName

View file

@ -42,7 +42,7 @@ type Filter struct {
Regex []string `json:"regex"`
compiledRegex []regexp.Regexp `json:"-"`
pattern []*Pattern `json:"-"`
pattern []*Pattern `json:"-"`
Retry int `json:"retry"`
RetryPeriod string `json:"retryperiod"`
@ -89,12 +89,12 @@ type ActionsMap map[PA]map[time.Time]struct{}
// Helper structs made to carry information
// Stream, Filter
type SF struct{ s, f string }
// Pattern, Stream, Filter
type PSF struct{
p string
s string
f string
type PSF struct {
p, s, f string
}
type PF struct {
p string
f *Filter

View file

@ -3,17 +3,25 @@
num: {
regex: '[0-9]+',
ignore: ['1'],
ignoreregex: ['2.?'],
// ignoreregex: ['2.?'],
},
letter: {
regex: '[a-z]+',
ignore: ['b'],
// ignoreregex: ['b.?'],
},
},
streams: {
tailDown1: {
cmd: ['sh', '-c', "echo 1 2 3 4 5 11 12 21 22 33 | tr ' ' '\n' | while read i; do sleep 1; echo found $i; done"],
cmd: ['sh', '-c', "echo 1_a 2_a 3_a a_1 a_2 a_3 | tr ' ' '\n' | while read i; do sleep 1; echo found $i; done"],
filters: {
findIP: {
regex: ['^found <num>$'],
retry: 1,
regex: [
'^found <num>_<letter>$',
'^found <letter>_<num>$',
],
retry: 2,
retryperiod: '30s',
actions: {
damn: {