close #6
This commit is contained in:
parent
5dcbdd865e
commit
a637d63826
@ -31,7 +31,7 @@ type Filter struct {
|
||||
compiledRegex []regexp.Regexp
|
||||
patternName, patternWithBraces string
|
||||
|
||||
Retry uint
|
||||
Retry int
|
||||
RetryPeriod string `yaml:"retry-period"`
|
||||
retryDuration time.Duration
|
||||
|
||||
@ -64,6 +64,7 @@ func (c *Conf) setup() {
|
||||
filter := stream.Filters[filterName]
|
||||
filter.stream = stream
|
||||
filter.name = filterName
|
||||
filter.matches = make(map[string][]time.Time)
|
||||
|
||||
// Parse Duration
|
||||
retryDuration, err := time.ParseDuration(filter.RetryPeriod)
|
||||
|
@ -3,6 +3,7 @@ package app
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
|
||||
// "fmt"
|
||||
"log"
|
||||
"os"
|
||||
@ -80,15 +81,33 @@ func (a *Action) exec(match string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Filter) cleanOldMatches(match string) {
|
||||
now := time.Now()
|
||||
newMatches := make([]time.Time, 0, len(f.matches[match]))
|
||||
for _, old := range f.matches[match] {
|
||||
if old.Add(f.retryDuration).After(now) {
|
||||
newMatches = append(newMatches, old)
|
||||
}
|
||||
}
|
||||
f.matches[match] = newMatches
|
||||
}
|
||||
|
||||
func (f *Filter) handle() chan *string {
|
||||
lines := make(chan *string)
|
||||
|
||||
go func() {
|
||||
for line := range lines {
|
||||
if match := f.match(line); match != "" {
|
||||
|
||||
f.cleanOldMatches(match)
|
||||
|
||||
f.matches[match] = append(f.matches[match], time.Now())
|
||||
|
||||
if len(f.matches[match]) >= f.Retry {
|
||||
f.execActions(match)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return lines
|
||||
|
@ -8,13 +8,13 @@ patterns:
|
||||
|
||||
streams:
|
||||
tailDown:
|
||||
cmd: [ "tail", "/home/ao/DOWN" ]
|
||||
cmd: [ "sh", "-c", "echo 'found 1.1.1.1' && sleep 2s && echo 'found 1.1.1.2' && sleep 2s && echo 'found 1.1.1.1' && sleep 1s" ]
|
||||
filters:
|
||||
findIP:
|
||||
regex:
|
||||
- found <ip>
|
||||
# retry: 1
|
||||
retry-period: 1s
|
||||
retry: 2
|
||||
retry-period: 5s
|
||||
actions:
|
||||
damn:
|
||||
cmd: [ "echo", "<ip>" ]
|
||||
|
Loading…
Reference in New Issue
Block a user