Skip to content

Commit be01291

Browse files
committed
Adds new constructor NewFromEndOfFile
Ignores OOMs that occurred before the parser was created.
1 parent a1160cf commit be01291

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

utils/oomparser/oomexample/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ import (
2525
// demonstrates how to run oomparser.OomParser to get OomInstance information
2626
func main() {
2727
klog.InitFlags(nil)
28+
ignoreOldOOMs := flag.Bool("ignore_old", false, "Read only new OOM events, ignoring old. ")
2829
flag.Parse()
2930
// out is a user-provided channel from which the user can read incoming
3031
// OomInstance objects
3132
outStream := make(chan *oomparser.OomInstance)
32-
oomLog, err := oomparser.New()
33+
34+
var oomLog *oomparser.OomParser
35+
var err error
36+
if *ignoreOldOOMs {
37+
oomLog, err = oomparser.NewFromEndOfFile()
38+
} else {
39+
oomLog, err = oomparser.New()
40+
}
3341
if err != nil {
3442
klog.Infof("Couldn't make a new oomparser. %v", err)
3543
} else {

utils/oomparser/oomparser.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ func (p *OomParser) StreamOoms(outStream chan<- *OomInstance) {
149149
klog.Errorf("exiting analyzeLines. OOM events will not be reported.")
150150
}
151151

152+
// NewFromEndOfFile initializes an OomParser object that starts reading from the end of the kernel log.
153+
// This will ignore any OOM events that occurred before the parser was created.
154+
func NewFromEndOfFile() (*OomParser, error) {
155+
parser, err := New()
156+
if err != nil {
157+
return nil, err
158+
}
159+
160+
// seek to and set the offset at the end of /dev/kmsg to avoid reporting old OOM events
161+
if err := parser.parser.SeekEnd(); err != nil {
162+
return nil, err
163+
}
164+
return parser, nil
165+
}
166+
152167
// initializes an OomParser object. Returns an OomParser object and an error.
153168
func New() (*OomParser, error) {
154169
parser, err := kmsgparser.NewParser()

0 commit comments

Comments
 (0)