Skip to content

Commit cc315c1

Browse files
authored
Merge pull request kubernetes#101 from Random-Liu/cherry-pick-#98-v0.3
Fix journald plugin to only look at the current boot.
2 parents 5422f63 + c95a0d1 commit cc315c1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

pkg/systemlogmonitor/logwatchers/journald/log_watcher.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"strings"
25+
"syscall"
2526
"time"
2627

2728
"github.com/coreos/go-systemd/sdjournal"
@@ -131,7 +132,7 @@ func getJournal(cfg types.WatcherConfig) (*sdjournal.Journal, error) {
131132
path = cfg.LogPath
132133
}
133134
// Get lookback duration.
134-
since, err := time.ParseDuration(cfg.Lookback)
135+
lookback, err := time.ParseDuration(cfg.Lookback)
135136
if err != nil {
136137
return nil, fmt.Errorf("failed to parse lookback duration %q: %v", cfg.Lookback, err)
137138
}
@@ -145,11 +146,24 @@ func getJournal(cfg types.WatcherConfig) (*sdjournal.Journal, error) {
145146
if err != nil {
146147
return nil, fmt.Errorf("failed to create journal client from path %q: %v", path, err)
147148
}
149+
// Use system uptime if lookback duration is longer than it.
150+
// Ideally, we should use monotonic timestamp + boot id in journald. However, it doesn't seem
151+
// to work with go-system/journal package.
152+
// TODO(random-liu): Use monotonic timestamp + boot id.
153+
var info syscall.Sysinfo_t
154+
if err := syscall.Sysinfo(&info); err != nil {
155+
return nil, fmt.Errorf("failed to get system info: %v", err)
156+
}
157+
uptime := time.Duration(info.Uptime) * time.Second
158+
if lookback > uptime {
159+
lookback = uptime
160+
glog.Infof("Lookback changed to system uptime: %v", lookback)
161+
}
148162
// Seek journal client based on the lookback duration.
149-
start := time.Now().Add(-since)
163+
start := time.Now().Add(-lookback)
150164
err = journal.SeekRealtimeUsec(uint64(start.UnixNano() / 1000))
151165
if err != nil {
152-
return nil, fmt.Errorf("failed to lookback %q: %v", since, err)
166+
return nil, fmt.Errorf("failed to lookback %q: %v", lookback, err)
153167
}
154168
// Empty source is not allowed and treated as an error.
155169
source := cfg.PluginConfig[configSourceKey]

0 commit comments

Comments
 (0)