-
Notifications
You must be signed in to change notification settings - Fork 15
/
generator.rb
96 lines (85 loc) · 2.42 KB
/
generator.rb
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
# encoding: utf-8
require "logstash/inputs/threadable"
require "logstash/namespace"
require "socket" # for Socket.gethostname
# Generate random log events.
#
# The general intention of this is to test performance of plugins.
#
# An event is generated first
class LogStash::Inputs::Generator < LogStash::Inputs::Threadable
config_name "generator"
default :codec, "plain"
# The message string to use in the event.
#
# If you set this to `stdin` then this plugin will read a single line from
# stdin and use that as the message string for every event.
#
# Otherwise, this value will be used verbatim as the event message.
config :message, :validate => :string, :default => "Hello world!"
# The lines to emit, in order. This option cannot be used with the 'message'
# setting.
#
# Example:
# [source,ruby]
# input {
# generator {
# lines => [
# "line 1",
# "line 2",
# "line 3"
# ]
# # Emit all lines 3 times.
# count => 3
# }
# }
#
# The above will emit `line 1` then `line 2` then `line`, then `line 1`, etc...
config :lines, :validate => :array
# Set how many messages should be generated.
#
# The default, `0`, means generate an unlimited number of events.
config :count, :validate => :number, :default => 0
public
def register
@host = Socket.gethostname
@count = Array(@count).first
end # def register
def run(queue)
number = 0
if @message == "stdin"
@logger.info("Generator plugin reading a line from stdin")
@message = $stdin.readline
@logger.debug("Generator line read complete", :message => @message)
end
@lines = [@message] if @lines.nil?
while !stop? && (@count <= 0 || number < @count)
@lines.each do |line|
@codec.decode(line.clone) do |event|
decorate(event)
event.set("host", @host)
event.set("sequence", number)
queue << event
end
end
number += 1
end # loop
if @codec.respond_to?(:flush)
@codec.flush do |event|
decorate(event)
event.set("host", @host)
queue << event
end
end
end # def run
public
def close
if @codec.respond_to?(:flush)
@codec.flush do |event|
decorate(event)
event.set("host", @host)
queue << event
end
end
end # def close
end # class LogStash::Inputs::Generator