Skip to content

Commit 6170b0c

Browse files
committed
Add multiple log monitoring support.
1 parent 92e67b8 commit 6170b0c

File tree

6 files changed

+57
-15
lines changed

6 files changed

+57
-15
lines changed

Dockerfile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ RUN test -h /etc/localtime && rm -f /etc/localtime && cp /usr/share/zoneinfo/UTC
2020

2121
ADD ./bin/node-problem-detector /node-problem-detector
2222
ADD config /config
23-
ENTRYPOINT ["/node-problem-detector", "--system-log-monitor=/config/kernel-monitor.json"]
23+
ENTRYPOINT ["/node-problem-detector", "--system-log-monitors=/config/kernel-monitor.json"]

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ List of supported problem daemons:
5454
# Usage
5555
## Flags
5656
* `--version`: Print current version of node-problem-detector.
57-
* `--system-log-monitor`: The configuration used by the system log monitor, e.g.
57+
* `--system-log-monitors`: List of paths to system log monitor configuration files, comma separated, e.g.
5858
[config/kernel-monitor.json](https://github.com/kubernetes/node-problem-detector/blob/master/config/kernel-monitor.json).
59+
Node problem detector will start a separate log monitor for each configuration. You can
60+
use different log monitors to monitor different system log.
5961
* `--apiserver-override`: A URI parameter used to customize how node-problem-detector
6062
connects the apiserver. The format is same as the
6163
[`source`](https://github.com/kubernetes/heapster/blob/master/docs/source-configuration.md#kubernetes)

cmd/node_problem_detector.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ func main() {
6767
os.Exit(0)
6868
}
6969

70-
l := systemlogmonitor.NewLogMonitorOrDie(npdo.SystemLogMonitorConfigPath)
70+
monitors := make(map[string]systemlogmonitor.LogMonitor)
71+
for _, config := range npdo.SystemLogMonitorConfigPaths {
72+
if _, ok := monitors[config]; ok {
73+
// Skip the config if it's duplictaed.
74+
glog.Warningf("Duplicated log monitor configuration %q", config)
75+
continue
76+
}
77+
monitors[config] = systemlogmonitor.NewLogMonitorOrDie(config)
78+
}
7179
c := problemclient.NewClientOrDie(npdo)
72-
p := problemdetector.NewProblemDetector(l, c)
80+
p := problemdetector.NewProblemDetector(monitors, c)
7381

7482
// Start http server.
7583
if npdo.ServerPort > 0 {

pkg/options/options.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ import (
3030
type NodeProblemDetectorOptions struct {
3131
// command line options
3232

33-
// SystemLogMonitorConfigPath specifies the path to system log monitor configuration file.
34-
SystemLogMonitorConfigPath string
33+
// SystemLogMonitorConfigPaths specifies the list of paths to system log monitor configuration
34+
// files.
35+
SystemLogMonitorConfigPaths []string
3536
// ApiServerOverride is the custom URI used to connect to Kubernetes ApiServer.
3637
ApiServerOverride string
3738
// PrintVersion is the flag determining whether version information is printed.
@@ -55,8 +56,8 @@ func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {
5556

5657
// AddFlags adds node problem detector command line options to pflag.
5758
func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
58-
fs.StringVar(&npdo.SystemLogMonitorConfigPath, "system-log-monitor",
59-
"/config/kernel-monitor.json", "The path to the system log monitor config file")
59+
fs.StringSliceVar(&npdo.SystemLogMonitorConfigPaths, "system-log-monitors",
60+
[]string{}, "List of paths to system log monitor config files, comma separated.")
6061
fs.StringVar(&npdo.ApiServerOverride, "apiserver-override",
6162
"", "Custom URI used to connect to Kubernetes ApiServer")
6263
fs.BoolVar(&npdo.PrintVersion, "version", false, "Print version information and quit")

pkg/problemdetector/problem_detector.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package problemdetector
1818

1919
import (
20+
"fmt"
2021
"net/http"
2122

2223
"github.com/golang/glog"
@@ -26,6 +27,7 @@ import (
2627
"k8s.io/node-problem-detector/pkg/condition"
2728
"k8s.io/node-problem-detector/pkg/problemclient"
2829
"k8s.io/node-problem-detector/pkg/systemlogmonitor"
30+
"k8s.io/node-problem-detector/pkg/types"
2931
"k8s.io/node-problem-detector/pkg/util"
3032
)
3133

@@ -38,28 +40,39 @@ type ProblemDetector interface {
3840
type problemDetector struct {
3941
client problemclient.Client
4042
conditionManager condition.ConditionManager
41-
// TODO(random-liu): Use slices of problem daemons if multiple monitors are needed in the future
42-
monitor systemlogmonitor.LogMonitor
43+
monitors map[string]systemlogmonitor.LogMonitor
4344
}
4445

4546
// NewProblemDetector creates the problem detector. Currently we just directly passed in the problem daemons, but
4647
// in the future we may want to let the problem daemons register themselves.
47-
func NewProblemDetector(monitor systemlogmonitor.LogMonitor, client problemclient.Client) ProblemDetector {
48+
func NewProblemDetector(monitors map[string]systemlogmonitor.LogMonitor, client problemclient.Client) ProblemDetector {
4849
return &problemDetector{
4950
client: client,
5051
conditionManager: condition.NewConditionManager(client, clock.RealClock{}),
51-
monitor: monitor,
52+
monitors: monitors,
5253
}
5354
}
5455

5556
// Run starts the problem detector.
5657
func (p *problemDetector) Run() error {
5758
p.conditionManager.Start()
58-
ch, err := p.monitor.Start()
59-
if err != nil {
60-
return err
59+
// Start the log monitors one by one.
60+
var chans []<-chan *types.Status
61+
for cfg, m := range p.monitors {
62+
ch, err := m.Start()
63+
if err != nil {
64+
// Do not return error and keep on trying the following config files.
65+
glog.Errorf("Failed to start log monitor %q: %v", cfg, err)
66+
continue
67+
}
68+
chans = append(chans, ch)
69+
}
70+
if len(chans) == 0 {
71+
return fmt.Errorf("no log montior is successfully setup")
6172
}
73+
ch := groupChannel(chans)
6274
glog.Info("Problem detector started")
75+
6376
for {
6477
select {
6578
case status := <-ch:
@@ -80,3 +93,15 @@ func (p *problemDetector) RegisterHTTPHandlers() {
8093
util.ReturnHTTPJson(w, p.conditionManager.GetConditions())
8194
})
8295
}
96+
97+
func groupChannel(chans []<-chan *types.Status) <-chan *types.Status {
98+
statuses := make(chan *types.Status)
99+
for _, ch := range chans {
100+
go func(c <-chan *types.Status) {
101+
for status := range c {
102+
statuses <- status
103+
}
104+
}(ch)
105+
}
106+
return statuses
107+
}

pkg/systemlogmonitor/logwatchers/journald/log_watcher.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package journald
2020

2121
import (
2222
"fmt"
23+
"os"
2324
"strings"
2425
"time"
2526

@@ -134,6 +135,11 @@ func getJournal(cfg types.WatcherConfig) (*sdjournal.Journal, error) {
134135
if err != nil {
135136
return nil, fmt.Errorf("failed to parse lookback duration %q: %v", cfg.Lookback, err)
136137
}
138+
// If the path doesn't present, NewJournalFromDir will create it instead of
139+
// returning error. So check the path existence ourselves.
140+
if _, err := os.Stat(path); err != nil {
141+
return nil, fmt.Errorf("failed to stat the log path %q: %v", path, err)
142+
}
137143
// Get journal client from the log path.
138144
journal, err := sdjournal.NewJournalFromDir(path)
139145
if err != nil {

0 commit comments

Comments
 (0)