-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
filebeat.go
123 lines (91 loc) · 2.49 KB
/
filebeat.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
package beat
import (
"fmt"
"os"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/logp"
cfg "github.com/elastic/beats/filebeat/config"
. "github.com/elastic/beats/filebeat/crawler"
. "github.com/elastic/beats/filebeat/input"
)
// Beater object. Contains all objects needed to run the beat
type Filebeat struct {
FbConfig *cfg.Config
// Channel from harvesters to spooler
publisherChan chan []*FileEvent
Spooler *Spooler
registrar *Registrar
}
func New() *Filebeat {
return &Filebeat{}
}
// Config setups up the filebeat configuration by fetch all additional config files
func (fb *Filebeat) Config(b *beat.Beat) error {
// Load Base config
err := cfgfile.Read(&fb.FbConfig, "")
if err != nil {
return fmt.Errorf("Error reading config file: %v", err)
}
// Check if optional config_dir is set to fetch additional prospector config files
fb.FbConfig.FetchConfigs()
return nil
}
func (fb *Filebeat) Setup(b *beat.Beat) error {
return nil
}
func (fb *Filebeat) Run(b *beat.Beat) error {
defer func() {
p := recover()
if p == nil {
return
}
fmt.Printf("recovered panic: %v", p)
os.Exit(1)
}()
var err error
// Init channels
fb.publisherChan = make(chan []*FileEvent, 1)
// Setup registrar to persist state
fb.registrar, err = NewRegistrar(fb.FbConfig.Filebeat.RegistryFile)
if err != nil {
logp.Err("Could not init registrar: %v", err)
return err
}
crawl := &Crawler{
Registrar: fb.registrar,
}
// Load the previous log file locations now, for use in prospector
fb.registrar.LoadState()
// Init and Start spooler: Harvesters dump events into the spooler.
fb.Spooler = NewSpooler(fb)
err = fb.Spooler.Config()
if err != nil {
logp.Err("Could not init spooler: %v", err)
return err
}
// Start up spooler
go fb.Spooler.Run()
crawl.Start(fb.FbConfig.Filebeat.Prospectors, fb.Spooler.Channel)
// Publishes event to output
pub := newPublisher(fb.FbConfig.Filebeat.PublishAsync,
fb.publisherChan, fb.registrar.Channel, b.Events)
pub.Start()
// registrar records last acknowledged positions in all files.
fb.registrar.Run()
return nil
}
func (fb *Filebeat) Cleanup(b *beat.Beat) error {
return nil
}
// Stop is called on exit for cleanup
func (fb *Filebeat) Stop() {
// Stop harvesters
// Stop prospectors
// Stopping spooler will flush items
fb.Spooler.Stop()
// Stopping registrar will write last state
fb.registrar.Stop()
// Close channels
//close(fb.publisherChan)
}