-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathreader.go
209 lines (182 loc) · 5.68 KB
/
reader.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
// 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
package journalread
import (
"fmt"
"io"
"os"
"syscall"
"time"
"github.com/coreos/go-systemd/v22/sdjournal"
"github.com/urso/sderr"
"github.com/elastic/beats/v7/libbeat/common/backoff"
"github.com/elastic/beats/v7/libbeat/common/cleanup"
"github.com/elastic/elastic-agent-libs/logp"
)
// Reader implements a Journald base reader with backoff support. The reader
// will block until a new entry can be read from the journal.
type Reader struct {
log *logp.Logger
backoff backoff.Backoff
journal journal
}
type canceler interface {
Done() <-chan struct{}
Err() error
}
type journal interface {
Close() error
Next() (uint64, error)
Wait(time.Duration) int
GetEntry() (*sdjournal.JournalEntry, error)
SeekHead() error
SeekTail() error
SeekRealtimeUsec(usec uint64) error
SeekCursor(string) error
}
// LocalSystemJournalID is the ID of the local system journal.
const localSystemJournalID = "LOCAL_SYSTEM_JOURNAL"
// NewReader creates a new Reader for an already opened journal. The reader assumed to take
// ownership of the journal, and needs to be closed.
func NewReader(log *logp.Logger, journal journal, backoff backoff.Backoff) *Reader {
return &Reader{log: log, journal: journal, backoff: backoff}
}
// Open opens a journal and creates a reader for it.
// Additional settings can be applied to the journal by passing functions to with.
// Open returns an error if the journal can not be opened, or if one with-function failed.
//
// Open will opend the systems journal if the path is empty or matches LOCAL_SYSTEM_JOURNAL.
// The path can optionally point to a file or a directory.
func Open(log *logp.Logger, path string, backoff backoff.Backoff, with ...func(j *sdjournal.Journal) error) (*Reader, error) {
j, err := openJournal(path)
if err != nil {
return nil, err
}
ok := false
defer cleanup.IfNot(&ok, func() { j.Close() })
for _, w := range with {
if err := w(j); err != nil {
return nil, err
}
}
ok = true
return NewReader(log, j, backoff), nil
}
func openJournal(path string) (*sdjournal.Journal, error) {
if path == localSystemJournalID || path == "" {
j, err := sdjournal.NewJournal()
if err != nil {
err = sderr.Wrap(err, "failed to open local journal")
}
return j, err
}
stat, err := os.Stat(path)
if err != nil {
return nil, sderr.Wrap(err, "failed to read meta data for %{path}", path)
}
if stat.IsDir() {
j, err := sdjournal.NewJournalFromDir(path)
if err != nil {
err = sderr.Wrap(err, "failed to open journal directory %{path}", path)
}
return j, err
}
j, err := sdjournal.NewJournalFromFiles(path)
if err != nil {
err = sderr.Wrap(err, "failed to open journal file %{path}", path)
}
return j, err
}
// Close closes the journal.
func (r *Reader) Close() error {
return r.journal.Close()
}
// Seek moves the read pointer to a new position.
// If a cursor or SeekTail is given, Seek tries to ignore the entry at the
// given position, jumping right to the next entry.
func (r *Reader) Seek(mode SeekMode, cursor string) (err error) {
switch mode {
case SeekHead:
err = r.journal.SeekHead()
case SeekTail:
if err = r.journal.SeekTail(); err == nil {
_, err = r.journal.Next()
}
case SeekCursor:
if err = r.journal.SeekCursor(cursor); err == nil {
_, err = r.journal.Next()
}
default:
return fmt.Errorf("invalid seek mode '%v'", mode)
}
return err
}
// SeekRealtimeUsec moves the read pointer to the entry with the
// specified CLOCK_REALTIME timestamp. This corresponds to
// sd_journal_seek_realtime_usec.
func (r *Reader) SeekRealtimeUsec(usec uint64) error {
return r.journal.SeekRealtimeUsec(usec)
}
// Next reads a new journald entry from the journal. It blocks if there is
// currently no entry available in the journal, or until an error has occurred.
func (r *Reader) Next(cancel canceler) (*sdjournal.JournalEntry, error) {
for cancel.Err() == nil {
c, err := r.journal.Next()
if err != nil && err != io.EOF {
return nil, err
}
switch {
// error while reading next entry
case c < 0:
return nil, fmt.Errorf("error while reading next entry %+v", syscall.Errno(-c))
// no new entry, so wait
case c == 0:
hasNewEntry, err := r.checkForNewEvents()
if err != nil {
return nil, err
}
if !hasNewEntry {
// TODO: backoff support is currently not cancellable :(
r.backoff.Wait()
}
continue
// new entries are available
default:
}
entry, err := r.journal.GetEntry()
if err != nil {
return nil, err
}
r.backoff.Reset()
return entry, nil
}
return nil, cancel.Err()
}
func (r *Reader) checkForNewEvents() (bool, error) {
c := r.journal.Wait(100 * time.Millisecond)
switch c {
case sdjournal.SD_JOURNAL_NOP:
return false, nil
// new entries are added or the journal has changed (e.g. vacuum, rotate)
case sdjournal.SD_JOURNAL_APPEND, sdjournal.SD_JOURNAL_INVALIDATE:
return true, nil
default:
}
r.log.Errorf("Unknown return code from Wait: %d\n", c)
return false, nil
}