Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup kmsg log watcher #112

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Cleanup kmsg log wather.
  • Loading branch information
Random-Liu committed May 30, 2017
commit 51351f91b267ff95ce71e1c372baf87efbbec71b
4 changes: 2 additions & 2 deletions pkg/systemlogmonitor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down
15 changes: 7 additions & 8 deletions pkg/systemlogmonitor/logwatchers/kmsg/log_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package kmsg

import (
"bufio"
"fmt"
"strings"
"time"
Expand All @@ -32,18 +31,16 @@ 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
}

// 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(),
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
19 changes: 0 additions & 19 deletions pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package kmsg

import (
"io"
"testing"

"code.cloudfoundry.org/clock/fakeclock"
Expand Down Expand Up @@ -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
}