@@ -17,8 +17,6 @@ limitations under the License.
17
17
package kernelmonitor
18
18
19
19
import (
20
- "bufio"
21
- "fmt"
22
20
"os"
23
21
"time"
24
22
@@ -97,19 +95,11 @@ func (k *kernelLogWatcher) Watch() (<-chan *types.KernelLog, error) {
97
95
glog .Infof ("kernel log %q is not found, kernel monitor doesn't support the os distro" , path )
98
96
return nil , nil
99
97
}
100
- start , err := k .getStartPoint (path )
101
- if err != nil {
102
- return nil , err
103
- }
104
- // TODO(random-liu): If the file gets recreated during this interval, the logic
105
- // will go wrong here.
106
98
// TODO(random-liu): Rate limit tail file.
107
99
// TODO(random-liu): Figure out what happens if log lines are removed.
100
+ // Notice that, kernel log watcher doesn't look back to the rolled out logs.
101
+ var err error
108
102
k .tl , err = tail .TailFile (path , tail.Config {
109
- Location : & tail.SeekInfo {
110
- Offset : start ,
111
- Whence : os .SEEK_SET ,
112
- },
113
103
Poll : true ,
114
104
ReOpen : true ,
115
105
Follow : true ,
@@ -132,6 +122,10 @@ func (k *kernelLogWatcher) watchLoop() {
132
122
close (k .logCh )
133
123
k .tomb .Done ()
134
124
}()
125
+ lookback , err := parseDuration (k .cfg .Lookback )
126
+ if err != nil {
127
+ glog .Fatalf ("failed to parse duration %q: %v" , k .cfg .Lookback , err )
128
+ }
135
129
for {
136
130
select {
137
131
case line := <- k .tl .Lines :
@@ -145,6 +139,10 @@ func (k *kernelLogWatcher) watchLoop() {
145
139
glog .Infof ("Unable to parse line: %q, %v" , line , err )
146
140
continue
147
141
}
142
+ // If the log is older than look back duration, discard it.
143
+ if k .clock .Since (log .Timestamp ) > lookback {
144
+ continue
145
+ }
148
146
k .logCh <- log
149
147
case <- k .tomb .Stopping ():
150
148
k .tl .Stop ()
@@ -154,42 +152,6 @@ func (k *kernelLogWatcher) watchLoop() {
154
152
}
155
153
}
156
154
157
- // getStartPoint finds the start point to parse the log. The start point is either
158
- // the line at (now - lookback) or the first line of kernel log.
159
- // Notice that, kernel log watcher doesn't look back to the rolled out logs.
160
- func (k * kernelLogWatcher ) getStartPoint (path string ) (int64 , error ) {
161
- f , err := os .Open (path )
162
- if err != nil {
163
- return 0 , fmt .Errorf ("failed to open file %q: %v" , path , err )
164
- }
165
- defer f .Close ()
166
- lookback , err := parseDuration (k .cfg .Lookback )
167
- if err != nil {
168
- return 0 , fmt .Errorf ("failed to parse duration %q: %v" , k .cfg .Lookback , err )
169
- }
170
- start := int64 (0 )
171
- reader := bufio .NewReader (f )
172
- done := false
173
- for ! done {
174
- line , err := reader .ReadString ('\n' )
175
- if err != nil {
176
- if len (line ) == 0 {
177
- // No need to continue parsing if nothing is read
178
- break
179
- }
180
- done = true
181
- }
182
- log , err := k .trans .Translate (line )
183
- if err != nil {
184
- glog .Infof ("unable to parse line: %q, %v" , line , err )
185
- } else if k .clock .Since (log .Timestamp ) <= lookback {
186
- break
187
- }
188
- start += int64 (len (line ))
189
- }
190
- return start , nil
191
- }
192
-
193
155
func parseDuration (s string ) (time.Duration , error ) {
194
156
// If the duration is not configured, just return 0 by default
195
157
if s == "" {
0 commit comments