From 51351f91b267ff95ce71e1c372baf87efbbec71b Mon Sep 17 00:00:00 2001 From: Random-Liu Date: Tue, 30 May 2017 15:58:45 -0700 Subject: [PATCH] Cleanup kmsg log wather. --- pkg/systemlogmonitor/README.md | 4 ++-- .../logwatchers/kmsg/log_watcher.go | 15 +++++++-------- .../logwatchers/kmsg/log_watcher_test.go | 19 ------------------- 3 files changed, 9 insertions(+), 29 deletions(-) diff --git a/pkg/systemlogmonitor/README.md b/pkg/systemlogmonitor/README.md index 56cf4a335..0849b433b 100644 --- a/pkg/systemlogmonitor/README.md +++ b/pkg/systemlogmonitor/README.md @@ -47,7 +47,7 @@ System log monitor supports different log management tools with different log watchers: * [filelog](./logwatchers/filelog): Log watcher for arbitrary file based log. -* [journald](.//logwatchers/journald): Log watcher for +* [journald](.//logwatchers/journald): Log watcher for journald. * [kmsg](./logwatchers/kmsg): Log watcher for the kernel ring buffer device, /dev/kmsg. Set `plugin` in the configuration file to specify log watcher. @@ -67,7 +67,7 @@ Log watcher specific configurations are configured in `pluginConfig`. * timestampFormat: The format of the timestamp. The format string is the time `2006-01-02T15:04:05Z07:00` in the expected format. (See [golang timestamp format](https://golang.org/pkg/time/#pkg-constants)) -* **kmsg** +* **kmsg**: No configuration for now. ### Change Log Path diff --git a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher.go b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher.go index f5d6f4e73..6d392d2e6 100644 --- a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher.go +++ b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher.go @@ -17,7 +17,6 @@ limitations under the License. package kmsg import ( - "bufio" "fmt" "strings" "time" @@ -32,10 +31,9 @@ import ( ) type kernelLogWatcher struct { - cfg types.WatcherConfig - logCh chan *logtypes.Log - tomb *util.Tomb - reader *bufio.Reader + cfg types.WatcherConfig + logCh chan *logtypes.Log + tomb *util.Tomb kmsgParser kmsgparser.Parser clock utilclock.Clock @@ -43,7 +41,6 @@ type kernelLogWatcher struct { // NewKmsgWatcher creates a watcher which will read messages from /dev/kmsg func NewKmsgWatcher(cfg types.WatcherConfig) types.LogWatcher { - kmsgparser.NewParser() return &kernelLogWatcher{ cfg: cfg, tomb: util.NewTomb(), @@ -92,7 +89,9 @@ func (k *kernelLogWatcher) watchLoop(lookback time.Duration) { select { case <-k.tomb.Stopping(): glog.Infof("Stop watching kernel log") - k.kmsgParser.Close() + if err := k.kmsgParser.Close(); err != nil { + glog.Errorf("Failed to close kmsg parser: %v", err) + } return case msg := <-kmsgs: glog.V(5).Infof("got kernel message: %+v", msg) @@ -102,7 +101,7 @@ func (k *kernelLogWatcher) watchLoop(lookback time.Duration) { // Discard too old messages if k.clock.Since(msg.Timestamp) > lookback { - glog.V(5).Infof("throwing away msg %v for being too old: %v > %v", msg.Message, msg.Timestamp.String(), lookback.String()) + glog.V(5).Infof("Throwing away msg %v for being too old: %v > %v", msg.Message, msg.Timestamp.String(), lookback.String()) continue } diff --git a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_test.go b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_test.go index f34037c58..80665a860 100644 --- a/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_test.go +++ b/pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_test.go @@ -17,7 +17,6 @@ limitations under the License. package kmsg import ( - "io" "testing" "code.cloudfoundry.org/clock/fakeclock" @@ -140,21 +139,3 @@ func TestWatch(t *testing.T) { } } } - -type fakeKmsgReader struct { - logLines []string -} - -func (r *fakeKmsgReader) Read(data []byte) (int, error) { - if len(r.logLines) == 0 { - return 0, io.EOF - } - l := r.logLines[0] - r.logLines = r.logLines[1:] - copy(data, []byte(l)) - return len(l), nil -} - -func (r *fakeKmsgReader) Close() error { - return nil -}