-
Notifications
You must be signed in to change notification settings - Fork 14
/
linefile.go
214 lines (190 loc) · 5.87 KB
/
linefile.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
/*
* skogul, linefile receiver
*
* Copyright (c) 2019 Telenor Norge AS
* Author(s):
* - Kristian Lyngstøl <kly@kly.no>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
package receiver
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/telenornms/skogul"
)
var lfLog = skogul.Logger("receiver", "linefile")
// LineFile will keep reading File over and over again, assuming one
// collection per line. Best suited for pointing at a FIFO, which will
// allow you to 'cat' stuff to Skogul.
type LineFile struct {
File string `doc:"Path to the fifo or file from which to read from repeatedly."`
Handler skogul.HandlerRef `doc:"Handler used to parse and transform and send data."`
Delay skogul.Duration `doc:"Delay before re-opening the file, if any."`
}
// Common routine for both fifo and stdin
func (lf *LineFile) read() error {
f, err := os.Open(lf.File)
if err != nil {
return fmt.Errorf("unable to open file %s: %w", lf.File, err)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
bytes := scanner.Bytes()
if err := lf.Handler.H.Handle(bytes); err != nil {
lfLog.WithError(err).Error("Failed to send metric")
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("unable to scan file: %w", err)
}
return nil
}
// Start never returns.
func (lf *LineFile) Start() error {
for {
if err := lf.read(); err != nil {
lfLog.WithError(err).Error("Unable to read file")
}
if lf.Delay.Duration != 0 {
time.Sleep(lf.Delay.Duration)
}
}
}
// File reads from a FILE, a single JSON object per line, and
// exits at EOF.
type File struct {
File string `doc:"Path to the file to read from once."`
Handler skogul.HandlerRef `doc:"Handler used to parse, transform and send data."`
lf LineFile
}
// Start reads a file once, then returns.
func (s *File) Start() error {
s.lf.File = s.File
s.lf.Handler = s.Handler
return s.lf.read()
}
// Stdin reads from /dev/stdin
type Stdin struct {
Handler skogul.HandlerRef `doc:"Handler used to parse, transform and send data."`
lf LineFile
}
// Start reads from stdin until EOF, then returns
func (s *Stdin) Start() error {
s.lf.File = "/dev/stdin"
s.lf.Handler = s.Handler
return s.lf.read()
}
// WholeFile reads the whole file and parses it as a single container
type WholeFile struct {
File string `doc:"Path to the file to read from."`
Handler skogul.HandlerRef `doc:"Handler used to parse, transform and send data."`
Frequency skogul.Duration `doc:"How often to re-read the same file. Leave blank or set to a negative value to only read once."`
}
func (wf *WholeFile) read() error {
b, err := os.ReadFile(wf.File)
if err != nil {
return fmt.Errorf("unable to open file from %s: %w", wf.File, err)
}
err = wf.Handler.H.Handle(b)
if err != nil {
return fmt.Errorf("unable to handle content: %w", err)
}
return nil
}
// Start never returns
func (wf *WholeFile) Start() error {
freq := wf.Frequency.Duration
sleep := freq >= time.Nanosecond
for {
err := wf.read()
if err != nil {
lfLog.WithError(err).Errorf("whole file reader %s", skogul.Identity[wf])
}
if sleep {
time.Sleep(freq)
} else {
for {
time.Sleep(time.Hour)
}
}
}
}
type LineFileAdvanced struct {
File string `doc:"Path to the fifo or file from which to read from repeatedly."`
NewFile string `doc:"Path to the fifo or file that 'File'will be moved to."`
Handler skogul.HandlerRef `doc:"Handler used to parse and transform and send data."`
Delay skogul.Duration `doc:"Delay before re-opening the file, if any."`
Pre string `doc:"Command to run AFTER moving the file, but BEFORE reading it. E.g.: Sighup/reload."`
Post string `doc:"Shell command to execute after reading file is finished."`
Shell string `doc:"Shell used to execute post command. Default: /bin/sh -c"`
}
func (lf *LineFileAdvanced) runCmd(cmd string) error {
if cmd == "" {
return nil
}
rcmd := strings.Split(lf.Shell, " ")
rcmd = append(rcmd, cmd)
c := exec.Command(rcmd[0], rcmd[1:]...)
_, err := c.Output()
if err != nil {
return fmt.Errorf("failed to execute command %s: %w", cmd, err)
}
return nil
}
func (lf *LineFileAdvanced) read() error {
err := os.Rename(lf.File, lf.NewFile)
if err != nil {
return fmt.Errorf("could not move file from %s to %s: %w", lf.File, lf.NewFile, err)
}
err = lf.runCmd(lf.Pre)
if err != nil {
lfLog.WithError(err).Error("unable to run pre-command, but continuing anyway.")
}
f, err := os.Open(lf.NewFile)
if err != nil {
return fmt.Errorf("unable to open file %s: %w", lf.NewFile, err)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
bytes := scanner.Bytes()
if err := lf.Handler.H.Handle(bytes); err != nil {
lfLog.WithError(err).Error("Failed to send metric")
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("unable to scan file: %w", err)
}
return lf.runCmd(lf.Post)
}
// Start never returns.
func (lf *LineFileAdvanced) Start() error {
if lf.Shell == "" {
lf.Shell = "/bin/sh -c"
}
for {
if err := lf.read(); err != nil {
lfLog.WithError(err).Error("Unable to handle file")
}
if lf.Delay.Duration != 0 {
time.Sleep(lf.Delay.Duration)
}
}
}