forked from kubernetes/node-problem-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_monitor.go
194 lines (177 loc) · 5.61 KB
/
log_monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package systemlogmonitor
import (
"encoding/json"
"io/ioutil"
"time"
"github.com/golang/glog"
"k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers"
watchertypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/types"
logtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
systemlogtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
"k8s.io/node-problem-detector/pkg/types"
"k8s.io/node-problem-detector/pkg/util"
"k8s.io/node-problem-detector/pkg/util/tomb"
)
type logMonitor struct {
watcher watchertypes.LogWatcher
buffer LogBuffer
config MonitorConfig
conditions []types.Condition
logCh <-chan *logtypes.Log
output chan *types.Status
tomb *tomb.Tomb
}
// NewLogMonitorOrDie create a new LogMonitor, panic if error occurs.
func NewLogMonitorOrDie(configPath string) types.Monitor {
l := &logMonitor{
tomb: tomb.NewTomb(),
}
f, err := ioutil.ReadFile(configPath)
if err != nil {
glog.Fatalf("Failed to read configuration file %q: %v", configPath, err)
}
err = json.Unmarshal(f, &l.config)
if err != nil {
glog.Fatalf("Failed to unmarshal configuration file %q: %v", configPath, err)
}
// Apply default configurations
(&l.config).ApplyDefaultConfiguration()
err = l.config.ValidateRules()
if err != nil {
glog.Fatalf("Failed to validate matching rules %+v: %v", l.config.Rules, err)
}
glog.Infof("Finish parsing log monitor config file: %+v", l.config)
l.watcher = logwatchers.GetLogWatcherOrDie(l.config.WatcherConfig)
l.buffer = NewLogBuffer(l.config.BufferSize)
// A 1000 size channel should be big enough.
l.output = make(chan *types.Status, 1000)
return l
}
func (l *logMonitor) Start() (<-chan *types.Status, error) {
glog.Info("Start log monitor")
var err error
l.logCh, err = l.watcher.Watch()
if err != nil {
return nil, err
}
go l.monitorLoop()
return l.output, nil
}
func (l *logMonitor) Stop() {
glog.Info("Stop log monitor")
l.tomb.Stop()
}
// monitorLoop is the main loop of log monitor.
func (l *logMonitor) monitorLoop() {
defer l.tomb.Done()
l.initializeStatus()
for {
select {
case log := <-l.logCh:
l.parseLog(log)
case <-l.tomb.Stopping():
l.watcher.Stop()
glog.Infof("Log monitor stopped")
return
}
}
}
// parseLog parses one log line.
func (l *logMonitor) parseLog(log *logtypes.Log) {
// Once there is new log, log monitor will push it into the log buffer and try
// to match each rule. If any rule is matched, log monitor will report a status.
l.buffer.Push(log)
for _, rule := range l.config.Rules {
matched := l.buffer.Match(rule.Pattern)
if len(matched) == 0 {
continue
}
status := l.generateStatus(matched, rule)
glog.Infof("New status generated: %+v", status)
l.output <- status
}
}
// generateStatus generates status from the logs.
func (l *logMonitor) generateStatus(logs []*logtypes.Log, rule systemlogtypes.Rule) *types.Status {
// We use the timestamp of the first log line as the timestamp of the status.
timestamp := logs[0].Timestamp
message := generateMessage(logs)
var events []types.Event
if rule.Type == types.Temp {
// For temporary error only generate event
events = append(events, types.Event{
Severity: types.Warn,
Timestamp: timestamp,
Reason: rule.Reason,
Message: message,
})
} else {
// For permanent error changes the condition
for i := range l.conditions {
condition := &l.conditions[i]
if condition.Type == rule.Condition {
// Update transition timestamp and message when the condition
// changes. Condition is considered to be changed only when
// status or reason changes.
if condition.Status == types.False || condition.Reason != rule.Reason {
condition.Transition = timestamp
condition.Message = message
events = append(events, util.GenerateConditionChangeEvent(
condition.Type,
types.True,
rule.Reason,
timestamp,
))
}
condition.Status = types.True
condition.Reason = rule.Reason
break
}
}
}
return &types.Status{
Source: l.config.Source,
// TODO(random-liu): Aggregate events and conditions and then do periodically report.
Events: events,
Conditions: l.conditions,
}
}
// initializeStatus initializes the internal condition and also reports it to the node problem detector.
func (l *logMonitor) initializeStatus() {
// Initialize the default node conditions
l.conditions = initialConditions(l.config.DefaultConditions)
glog.Infof("Initialize condition generated: %+v", l.conditions)
// Update the initial status
l.output <- &types.Status{
Source: l.config.Source,
Conditions: l.conditions,
}
}
func initialConditions(defaults []types.Condition) []types.Condition {
conditions := make([]types.Condition, len(defaults))
copy(conditions, defaults)
for i := range conditions {
conditions[i].Status = types.False
conditions[i].Transition = time.Now()
}
return conditions
}
func generateMessage(logs []*logtypes.Log) string {
messages := []string{}
for _, log := range logs {
messages = append(messages, log.Message)
}
return concatLogs(messages)
}