Skip to content

Commit f238ab6

Browse files
committed
Add --revert-pattern for logcounter
1 parent e9eddcc commit f238ab6

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

cmd/logcounter/options/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type LogCounterOptions struct {
3434
Lookback string
3535
Delay string
3636
Pattern string
37+
RevertPattern string
3738
Count int
3839
}
3940

@@ -46,6 +47,8 @@ func (fedo *LogCounterOptions) AddFlags(fs *pflag.FlagSet) {
4647
"The time duration log watcher delays after node boot time. This is useful when log watcher needs to wait for some time until the node is stable.")
4748
fs.StringVar(&fedo.Pattern, "pattern", "",
4849
"The regular expression to match the problem in log. The pattern must match to the end of the line.")
50+
fs.StringVar(&fedo.RevertPattern, "revert-pattern", "",
51+
"Similar to --pattern but conversely it decreases count value for every match. This is useful to discount a log when another log occurs.")
4952
fs.IntVar(&fedo.Count, "count", 1,
5053
"The number of times the pattern must be found to trigger the condition")
5154
}

pkg/logcounter/log_counter.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ const (
4040
)
4141

4242
type logCounter struct {
43-
logCh <-chan *systemtypes.Log
44-
buffer systemlogmonitor.LogBuffer
45-
pattern string
46-
clock clock.Clock
43+
logCh <-chan *systemtypes.Log
44+
buffer systemlogmonitor.LogBuffer
45+
pattern string
46+
revertPattern string
47+
clock clock.Clock
4748
}
4849

4950
func NewJournaldLogCounter(options *options.LogCounterOptions) (types.LogCounter, error) {
@@ -59,10 +60,11 @@ func NewJournaldLogCounter(options *options.LogCounterOptions) (types.LogCounter
5960
return nil, fmt.Errorf("error watching journald: %v", err)
6061
}
6162
return &logCounter{
62-
logCh: logCh,
63-
buffer: systemlogmonitor.NewLogBuffer(bufferSize),
64-
pattern: options.Pattern,
65-
clock: clock.RealClock{},
63+
logCh: logCh,
64+
buffer: systemlogmonitor.NewLogBuffer(bufferSize),
65+
pattern: options.Pattern,
66+
revertPattern: options.RevertPattern,
67+
clock: clock.RealClock{},
6668
}, nil
6769
}
6870

@@ -84,6 +86,9 @@ func (e *logCounter) Count() (count int, err error) {
8486
if len(e.buffer.Match(e.pattern)) != 0 {
8587
count++
8688
}
89+
if len(e.buffer.Match(e.revertPattern)) != 0 {
90+
count--
91+
}
8792
case <-e.clock.After(timeout):
8893
// Don't block forever if we do not get any new messages
8994
return

0 commit comments

Comments
 (0)