diff --git a/app/daemon.go b/app/daemon.go index 84035d6..8066fc7 100644 --- a/app/daemon.go +++ b/app/daemon.go @@ -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()} } } diff --git a/app/main.go b/app/main.go index 1d69c26..9ffe8b1 100644 --- a/app/main.go +++ b/app/main.go @@ -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 diff --git a/app/startup.go b/app/startup.go index 1bc6403..7bcf4a0 100644 --- a/app/startup.go +++ b/app/startup.go @@ -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 diff --git a/app/types.go b/app/types.go index 450a1d6..9918d9d 100644 --- a/app/types.go +++ b/app/types.go @@ -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 diff --git a/config/test.jsonnet b/config/test.jsonnet index f9ff43b..503eb63 100644 --- a/config/test.jsonnet +++ b/config/test.jsonnet @@ -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 $'], - retry: 1, + regex: [ + '^found _$', + '^found _$', + ], + retry: 2, retryperiod: '30s', actions: { damn: {