This commit is contained in:
ppom 2023-03-25 19:12:11 +01:00
parent 5dcbdd865e
commit a637d63826
3 changed files with 25 additions and 5 deletions

View File

@ -31,7 +31,7 @@ type Filter struct {
compiledRegex []regexp.Regexp compiledRegex []regexp.Regexp
patternName, patternWithBraces string patternName, patternWithBraces string
Retry uint Retry int
RetryPeriod string `yaml:"retry-period"` RetryPeriod string `yaml:"retry-period"`
retryDuration time.Duration retryDuration time.Duration
@ -64,6 +64,7 @@ func (c *Conf) setup() {
filter := stream.Filters[filterName] filter := stream.Filters[filterName]
filter.stream = stream filter.stream = stream
filter.name = filterName filter.name = filterName
filter.matches = make(map[string][]time.Time)
// Parse Duration // Parse Duration
retryDuration, err := time.ParseDuration(filter.RetryPeriod) retryDuration, err := time.ParseDuration(filter.RetryPeriod)

View File

@ -3,6 +3,7 @@ package app
import ( import (
"bufio" "bufio"
"flag" "flag"
// "fmt" // "fmt"
"log" "log"
"os" "os"
@ -80,13 +81,31 @@ 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 { func (f *Filter) handle() chan *string {
lines := make(chan *string) lines := make(chan *string)
go func() { go func() {
for line := range lines { for line := range lines {
if match := f.match(line); match != "" { if match := f.match(line); match != "" {
f.execActions(match)
f.cleanOldMatches(match)
f.matches[match] = append(f.matches[match], time.Now())
if len(f.matches[match]) >= f.Retry {
f.execActions(match)
}
} }
} }
}() }()

View File

@ -8,13 +8,13 @@ patterns:
streams: streams:
tailDown: 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: filters:
findIP: findIP:
regex: regex:
- found <ip> - found <ip>
# retry: 1 retry: 2
retry-period: 1s retry-period: 5s
actions: actions:
damn: damn:
cmd: [ "echo", "<ip>" ] cmd: [ "echo", "<ip>" ]