forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prospector.go
124 lines (104 loc) · 2.81 KB
/
prospector.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
package redis
import (
"time"
rd "github.com/garyburd/redigo/redis"
"github.com/elastic/beats/filebeat/channel"
"github.com/elastic/beats/filebeat/harvester"
"github.com/elastic/beats/filebeat/input/file"
"github.com/elastic/beats/filebeat/prospector"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/libbeat/logp"
)
func init() {
err := prospector.Register("redis", NewProspector)
if err != nil {
panic(err)
}
}
// Prospector is a prospector for redis
type Prospector struct {
started bool
outlet channel.Outleter
config config
cfg *common.Config
registry *harvester.Registry
}
// NewProspector creates a new redis prospector
func NewProspector(cfg *common.Config, outletFactory channel.Factory, context prospector.Context) (prospector.Prospectorer, error) {
cfgwarn.Experimental("Redis slowlog prospector is enabled.")
config := defaultConfig
err := cfg.Unpack(&config)
if err != nil {
return nil, err
}
outlet, err := outletFactory(cfg, context.DynamicFields)
if err != nil {
return nil, err
}
p := &Prospector{
started: false,
outlet: outlet,
config: config,
cfg: cfg,
registry: harvester.NewRegistry(),
}
return p, nil
}
// LoadStates loads the states
func (p *Prospector) LoadStates(states []file.State) error {
return nil
}
// Run runs the prospector
func (p *Prospector) Run() {
logp.Debug("redis", "Run redis prospector with hosts: %+v", p.config.Hosts)
if len(p.config.Hosts) == 0 {
logp.Err("No redis hosts configured")
return
}
forwarder := harvester.NewForwarder(p.outlet)
for _, host := range p.config.Hosts {
pool := CreatePool(host, p.config.Password, p.config.Network,
p.config.MaxConn, p.config.IdleTimeout, p.config.IdleTimeout)
h := NewHarvester(pool.Get())
h.forwarder = forwarder
if err := p.registry.Start(h); err != nil {
logp.Err("Harvester start failed: %s", err)
}
}
}
// Stop stopps the prospector and all its harvesters
func (p *Prospector) Stop() {
p.registry.Stop()
p.outlet.Close()
}
// Wait waits for the propsector to be completed. Not implemented.
func (p *Prospector) Wait() {}
// CreatePool creates a redis connection pool
// NOTE: This code is copied from the redis pool handling in metricbeat
func CreatePool(
host, password, network string,
maxConn int,
idleTimeout, connTimeout time.Duration,
) *rd.Pool {
return &rd.Pool{
MaxIdle: maxConn,
IdleTimeout: idleTimeout,
Dial: func() (rd.Conn, error) {
c, err := rd.Dial(network, host,
rd.DialConnectTimeout(connTimeout),
rd.DialReadTimeout(connTimeout),
rd.DialWriteTimeout(connTimeout))
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
}
}