forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
248 lines (208 loc) · 6.97 KB
/
input.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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.
//go:build linux && cgo && withjournald
// +build linux,cgo,withjournald
package journald
import (
"time"
"github.com/coreos/go-systemd/v22/sdjournal"
"github.com/urso/sderr"
"github.com/elastic/beats/v7/filebeat/input/journald/pkg/journalfield"
"github.com/elastic/beats/v7/filebeat/input/journald/pkg/journalread"
input "github.com/elastic/beats/v7/filebeat/input/v2"
cursor "github.com/elastic/beats/v7/filebeat/input/v2/input-cursor"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/backoff"
"github.com/elastic/beats/v7/libbeat/feature"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/reader"
"github.com/elastic/beats/v7/libbeat/reader/parser"
)
type journald struct {
Backoff time.Duration
MaxBackoff time.Duration
Seek journalread.SeekMode
CursorSeekFallback journalread.SeekMode
Matches []journalfield.Matcher
SaveRemoteHostname bool
Parsers parser.Config
}
type checkpoint struct {
Version int
Position string
RealtimeTimestamp uint64
MonotonicTimestamp uint64
}
// LocalSystemJournalID is the ID of the local system journal.
const localSystemJournalID = "LOCAL_SYSTEM_JOURNAL"
const pluginName = "journald"
// Plugin creates a new journald input plugin for creating a stateful input.
func Plugin(log *logp.Logger, store cursor.StateStore) input.Plugin {
return input.Plugin{
Name: pluginName,
Stability: feature.Experimental,
Deprecated: false,
Info: "journald input",
Doc: "The journald input collects logs from the local journald service",
Manager: &cursor.InputManager{
Logger: log,
StateStore: store,
Type: pluginName,
Configure: configure,
},
}
}
type pathSource string
var cursorVersion = 1
func (p pathSource) Name() string { return string(p) }
func configure(cfg *common.Config) ([]cursor.Source, cursor.Input, error) {
config := defaultConfig()
if err := cfg.Unpack(&config); err != nil {
return nil, nil, err
}
paths := config.Paths
if len(paths) == 0 {
paths = []string{localSystemJournalID}
}
sources := make([]cursor.Source, len(paths))
for i, p := range paths {
sources[i] = pathSource(p)
}
return sources, &journald{
Backoff: config.Backoff,
MaxBackoff: config.MaxBackoff,
Seek: config.Seek,
CursorSeekFallback: config.CursorSeekFallback,
Matches: config.Matches,
SaveRemoteHostname: config.SaveRemoteHostname,
Parsers: config.Parsers,
}, nil
}
func (inp *journald) Name() string { return pluginName }
func (inp *journald) Test(src cursor.Source, ctx input.TestContext) error {
reader, err := inp.open(ctx.Logger, ctx.Cancelation, src)
if err != nil {
return err
}
return reader.Close()
}
func (inp *journald) Run(
ctx input.Context,
src cursor.Source,
cursor cursor.Cursor,
publisher cursor.Publisher,
) error {
log := ctx.Logger.With("path", src.Name())
currentCheckpoint := initCheckpoint(log, cursor)
reader, err := inp.open(ctx.Logger, ctx.Cancelation, src)
if err != nil {
return err
}
defer reader.Close()
if err := reader.Seek(seekBy(ctx.Logger, currentCheckpoint, inp.Seek, inp.CursorSeekFallback)); err != nil {
log.Error("Continue from current position. Seek failed with: %v", err)
}
parser := inp.Parsers.Create(&readerAdapter{r: reader, canceler: ctx.Cancelation})
for {
entry, err := parser.Next()
if err != nil {
return err
}
event := entry.ToEvent()
if err := publisher.Publish(event, event.Private); err != nil {
return err
}
}
}
func (inp *journald) open(log *logp.Logger, canceler input.Canceler, src cursor.Source) (*journalread.Reader, error) {
backoff := backoff.NewExpBackoff(canceler.Done(), inp.Backoff, inp.MaxBackoff)
reader, err := journalread.Open(log, src.Name(), backoff, withFilters(inp.Matches))
if err != nil {
return nil, sderr.Wrap(err, "failed to create reader for %{path} journal", src.Name())
}
return reader, nil
}
func initCheckpoint(log *logp.Logger, c cursor.Cursor) checkpoint {
if c.IsNew() {
return checkpoint{Version: cursorVersion}
}
var cp checkpoint
err := c.Unpack(&cp)
if err != nil {
log.Errorf("Reset journald position. Failed to read checkpoint from registry: %v", err)
return checkpoint{Version: cursorVersion}
}
if cp.Version != cursorVersion {
log.Error("Reset journald position. invalid journald position entry.")
return checkpoint{Version: cursorVersion}
}
return cp
}
func withFilters(filters []journalfield.Matcher) func(*sdjournal.Journal) error {
return func(j *sdjournal.Journal) error {
return journalfield.ApplyMatchersOr(j, filters)
}
}
// seekBy tries to find the last known position in the journal, so we can continue collecting
// from the last known position.
// The checkpoint is ignored if the user has configured the input to always
// seek to the head/tail of the journal on startup.
func seekBy(log *logp.Logger, cp checkpoint, seek, defaultSeek journalread.SeekMode) (journalread.SeekMode, string) {
mode := seek
if mode == journalread.SeekCursor && cp.Position == "" {
mode = defaultSeek
if mode != journalread.SeekHead && mode != journalread.SeekTail {
log.Error("Invalid option for cursor_seek_fallback")
mode = journalread.SeekHead
}
}
return mode, cp.Position
}
// readerAdapter is an adapter so journalread.Reader can
// behave like reader.Reader
type readerAdapter struct {
r *journalread.Reader
canceler input.Canceler
}
func (r *readerAdapter) Close() error {
return r.r.Close()
}
func (r *readerAdapter) Next() (reader.Message, error) {
data, err := r.r.Next(r.canceler)
if err != nil {
return reader.Message{}, err
}
content := []byte(data.Fields["MESSAGE"])
delete(data.Fields, "MESSAGE")
fields := make(map[string]interface{}, len(data.Fields))
for k, v := range data.Fields {
fields[k] = v
}
m := reader.Message{
Ts: time.UnixMicro(int64(data.RealtimeTimestamp)),
Content: content,
Bytes: len(content),
Fields: fields,
Private: checkpoint{
Version: cursorVersion,
RealtimeTimestamp: data.RealtimeTimestamp,
MonotonicTimestamp: data.MonotonicTimestamp,
Position: data.Cursor,
},
}
return m, nil
}