-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UDP prospector type with plain harvester
event looks as following ``` { "@timestamp": "2017-05-26T11:57:17.68711727+02:00", "beat": { "hostname": "ruflin", "name": "ruflin", "version": "6.0.0-alpha2" }, "message": "Hello, World!99", "prospector": { "harvester": "plain", "type": "udp" } } ```
- Loading branch information
Showing
9 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package udp | ||
|
||
import ( | ||
"github.com/elastic/beats/filebeat/harvester" | ||
) | ||
|
||
var defaultConfig = config{ | ||
ForwarderConfig: harvester.ForwarderConfig{ | ||
Type: "udp", | ||
}, | ||
MaxMessageSize: 10240, | ||
// TODO: What should be default port? | ||
Host: "localhost:8080", | ||
} | ||
|
||
type config struct { | ||
harvester.ForwarderConfig `config:",inline"` | ||
Host string `config:"host"` | ||
MaxMessageSize int `config:"max_message_size"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package udp | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/elastic/beats/filebeat/harvester" | ||
"github.com/elastic/beats/filebeat/util" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
type Harvester struct { | ||
forwarder *harvester.Forwarder | ||
done chan struct{} | ||
cfg *common.Config | ||
listener net.PacketConn | ||
} | ||
|
||
func NewHarvester(forwarder *harvester.Forwarder, cfg *common.Config) *Harvester { | ||
return &Harvester{ | ||
done: make(chan struct{}), | ||
cfg: cfg, | ||
forwarder: forwarder, | ||
} | ||
} | ||
|
||
func (h *Harvester) Run() error { | ||
|
||
config := defaultConfig | ||
err := h.cfg.Unpack(&config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h.listener, err = net.ListenPacket("udp", config.Host) | ||
if err != nil { | ||
return err | ||
} | ||
defer h.listener.Close() | ||
|
||
logp.Info("Started listening for udp on: %s", config.Host) | ||
|
||
buffer := make([]byte, config.MaxMessageSize) | ||
|
||
for { | ||
select { | ||
case <-h.done: | ||
return nil | ||
default: | ||
} | ||
|
||
length, _, err := h.listener.ReadFrom(buffer) | ||
if err != nil { | ||
logp.Err("Error reading from buffer: %v", err.Error()) | ||
continue | ||
} | ||
data := util.NewData() | ||
event := common.MapStr{ | ||
"message": string(buffer[:length]), | ||
"@timestamp": time.Now(), | ||
} | ||
data.Event = event | ||
h.forwarder.Send(data) | ||
} | ||
} | ||
|
||
func (h *Harvester) Stop() { | ||
logp.Info("Stopping udp harvester") | ||
close(h.done) | ||
h.listener.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package udp | ||
|
||
import ( | ||
"github.com/elastic/beats/filebeat/channel" | ||
"github.com/elastic/beats/filebeat/harvester" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
type Prospector struct { | ||
harvester *Harvester | ||
started bool | ||
} | ||
|
||
func NewProspector(cfg *common.Config, outlet channel.Outleter) (*Prospector, error) { | ||
forwarder, err := harvester.NewForwarder(cfg, outlet) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Prospector{ | ||
harvester: NewHarvester(forwarder, cfg), | ||
started: false, | ||
}, nil | ||
} | ||
|
||
func (p *Prospector) Run() { | ||
logp.Info("Starting udp prospector") | ||
|
||
if !p.started { | ||
p.started = true | ||
go func() { | ||
err := p.harvester.Run() | ||
if err != nil { | ||
logp.Err("Error running harvester:: %v", err) | ||
} | ||
}() | ||
} | ||
} | ||
|
||
func (p *Prospector) Stop() { | ||
logp.Info("Stopping udp prospector") | ||
p.harvester.Stop() | ||
} | ||
|
||
func (p *Prospector) Wait() { | ||
p.Stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from filebeat import BaseTest | ||
import socket | ||
import time | ||
|
||
|
||
class Test(BaseTest): | ||
|
||
def test_udp(self): | ||
|
||
host = "127.0.0.1" | ||
port = 8080 | ||
prospector_raw = """ | ||
- type: udp | ||
host: "{}:{}" | ||
enabled: true | ||
""" | ||
|
||
prospector_raw = prospector_raw.format(host, port) | ||
|
||
self.render_config_template( | ||
prospector_raw=prospector_raw, | ||
prospectors=False, | ||
) | ||
|
||
filebeat = self.start_beat() | ||
|
||
self.wait_until(lambda: self.log_contains("Started listening for udp")) | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP | ||
|
||
for n in range(0, 2): | ||
sock.sendto("Hello World: " + str(n), (host, port)) | ||
|
||
self.wait_until(lambda: self.output_count(lambda x: x >= 2)) | ||
|
||
filebeat.check_kill_and_wait() | ||
|
||
output = self.read_output() | ||
|
||
assert len(output) == 2 | ||
assert output[0]["prospector.type"] == "udp" |