logstash-input-mongoprofile 0.1.15 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/logstash/inputs/mongoprofile.rb +7 -5
- data/lib/logstash/test.rb +176 -3
- data/logstash-input-mongoprofile.gemspec +1 -1
- data/spec/inputs/mongoprofile_spec.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91179ba0a1df249cae35aa208dbe2439272eb878
|
4
|
+
data.tar.gz: d5df9210fd7cf7f60331f7d0c0a229ff670a4e69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b18b777e1f8d2862b563ce5bf698415ab6983de2712e35753657c838c577573031236b4e9ceca02c4f58e08ce63516ecdf04cdb8dd50b5f5856d69862422c3a2
|
7
|
+
data.tar.gz: cf27b8bae36e7aad52b6f1a03ec117cecc9644a51350369314f773533b97f5bf14ab56f8036aea7ff737f79b376379b0841032b226ad196546fa4f378f99bb2e
|
@@ -75,11 +75,11 @@ class MongoAccessor
|
|
75
75
|
end
|
76
76
|
|
77
77
|
def get_documents_by_ts(date, limit)
|
78
|
-
@collection.find({:ts => {:$gt => DateTime.parse(date)}, :client => {:$ne => @client_host}}).limit(limit).sort(:ts =>
|
78
|
+
@collection.find({:ts => {:$gt => DateTime.parse(date) + 0.00002}, :client => {:$ne => @client_host}}).limit(limit).sort(:ts => 1)
|
79
79
|
end
|
80
80
|
|
81
81
|
def get_documents(limit)
|
82
|
-
@collection.find({:client => {:$ne => @client_host}}).limit(limit).sort(:ts =>
|
82
|
+
@collection.find({:client => {:$ne => @client_host}}).limit(limit).sort(:ts => 1)
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -103,10 +103,12 @@ class ProfileCollection
|
|
103
103
|
|
104
104
|
yield @parser.parse(document)
|
105
105
|
end
|
106
|
+
|
107
|
+
@documents = []
|
106
108
|
end
|
107
109
|
|
108
110
|
def get_last_document_date
|
109
|
-
if @documents and @documents[-1] != nil
|
111
|
+
if @documents != nil and @documents[-1] != nil
|
110
112
|
@documents[-1]['ts']
|
111
113
|
else
|
112
114
|
nil
|
@@ -183,9 +185,9 @@ class Controller
|
|
183
185
|
|
184
186
|
if profile_collection.get_last_document_date != nil
|
185
187
|
@last_value_store.save_last_value(profile_collection.get_last_document_date)
|
186
|
-
end
|
187
|
-
@logger.info('Nothing to get...')
|
188
188
|
else
|
189
|
+
@logger.info('Nothing to get...')
|
190
|
+
end
|
189
191
|
|
190
192
|
profile_collection
|
191
193
|
end
|
data/lib/logstash/test.rb
CHANGED
@@ -1,3 +1,176 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
1
|
+
require 'mongo'
|
2
|
+
|
3
|
+
class Test
|
4
|
+
def each
|
5
|
+
arr = [1, 2, 3]
|
6
|
+
|
7
|
+
arr.each do |i|
|
8
|
+
yield i
|
9
|
+
end
|
10
|
+
|
11
|
+
puts 'end each'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
test = Test.new
|
16
|
+
|
17
|
+
test.each do |i|
|
18
|
+
puts i
|
19
|
+
end
|
20
|
+
|
21
|
+
class EventMock
|
22
|
+
def set(arg)
|
23
|
+
puts arg
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class LoggerMock
|
28
|
+
def info(arg)
|
29
|
+
puts arg
|
30
|
+
end
|
31
|
+
|
32
|
+
def debug(arg)
|
33
|
+
puts arg
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
class MongoAccessor
|
39
|
+
def initialize(url, collection, client_host)
|
40
|
+
connection = Mongo::Client.new(url)
|
41
|
+
|
42
|
+
@mongodb = connection.database
|
43
|
+
@collection = @mongodb.collection(collection)
|
44
|
+
@client_host = client_host
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_documents_by_ts(date, limit)
|
48
|
+
@collection.find({:ts => {:$gt => DateTime.parse(date) + 0.00002}, :client => {:$ne => @client_host}}).limit(limit).sort(:ts => 1)
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_documents(limit)
|
52
|
+
@collection.find({:client => {:$ne => @client_host}}).limit(limit).sort(:ts => 1)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class ProfileCollection
|
57
|
+
def initialize(documents, parser, generate_id)
|
58
|
+
@generate_id = generate_id
|
59
|
+
@documents = []
|
60
|
+
|
61
|
+
documents.each do |document|
|
62
|
+
@documents.push(document)
|
63
|
+
puts document['ts']
|
64
|
+
end
|
65
|
+
|
66
|
+
@parser = parser
|
67
|
+
end
|
68
|
+
|
69
|
+
def each
|
70
|
+
@documents.each do |document|
|
71
|
+
if @generate_id
|
72
|
+
document['_id'] = generate_id.to_s
|
73
|
+
end
|
74
|
+
|
75
|
+
yield @parser.parse(document)
|
76
|
+
end
|
77
|
+
|
78
|
+
@documents = []
|
79
|
+
end
|
80
|
+
|
81
|
+
def get_last_document_date
|
82
|
+
if @documents != nil and @documents[-1] != nil
|
83
|
+
@documents[-1]['ts']
|
84
|
+
else
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def length
|
90
|
+
@documents.length
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
def generate_id
|
95
|
+
# noinspection RubyArgCount
|
96
|
+
BSON::ObjectId.new
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class DocumentParser
|
101
|
+
def initialize(host, logger)
|
102
|
+
@host = host
|
103
|
+
@logger = logger
|
104
|
+
end
|
105
|
+
|
106
|
+
def parse(document)
|
107
|
+
@logger.info('Start documents parsing')
|
108
|
+
event = LogStash::Event.new('host' => @host)
|
109
|
+
|
110
|
+
document.each do |key, value|
|
111
|
+
@logger.debug("Try set event field key: #{key} value: #{value}")
|
112
|
+
event.set(key, value)
|
113
|
+
end
|
114
|
+
|
115
|
+
event
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class LastValueStore
|
120
|
+
def initialize(path, name)
|
121
|
+
@file_full_name = "#{path}/#{name}"
|
122
|
+
end
|
123
|
+
|
124
|
+
def save_last_value(value)
|
125
|
+
file = File.open(@file_full_name, 'a+')
|
126
|
+
|
127
|
+
file.truncate(0)
|
128
|
+
file.puts(value)
|
129
|
+
|
130
|
+
file.close
|
131
|
+
end
|
132
|
+
|
133
|
+
def get_last_value
|
134
|
+
File.read(@file_full_name)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class Controller
|
139
|
+
def initialize(event, url, collection, limit, path, client_host, logger, generate_id)
|
140
|
+
@mongo_accessor = MongoAccessor.new(url, collection, client_host)
|
141
|
+
@last_value_store = LastValueStore.new(path, collection)
|
142
|
+
@document_parser = DocumentParser.new(event, logger)
|
143
|
+
@generate_id = generate_id
|
144
|
+
@limit = limit
|
145
|
+
@logger = logger
|
146
|
+
end
|
147
|
+
|
148
|
+
def get_next_events
|
149
|
+
last_date_value = @last_value_store.get_last_value
|
150
|
+
|
151
|
+
if last_date_value == ''
|
152
|
+
@logger.info('Getting documents from mongo first time')
|
153
|
+
documents = @mongo_accessor.get_documents(@limit)
|
154
|
+
else
|
155
|
+
@logger.info("Getting documents from mongo start at #{last_date_value}")
|
156
|
+
documents = @mongo_accessor.get_documents_by_ts(last_date_value, @limit)
|
157
|
+
end
|
158
|
+
|
159
|
+
profile_collection = ProfileCollection.new(documents, @document_parser, @generate_id)
|
160
|
+
|
161
|
+
if profile_collection.get_last_document_date != nil
|
162
|
+
@last_value_store.save_last_value(profile_collection.get_last_document_date)
|
163
|
+
else
|
164
|
+
@logger.info('Nothing to get...')
|
165
|
+
end
|
166
|
+
|
167
|
+
profile_collection
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
controller = Controller.new(EventMock.new, 'mongodb://192.168.1.37/eleet-v2-dev', 'system.profile', 10, '/home/artem/', '192.168.1.35', LoggerMock.new, false)
|
172
|
+
|
173
|
+
while true
|
174
|
+
events = controller.get_next_events
|
175
|
+
puts events.length
|
176
|
+
end
|
@@ -5,7 +5,7 @@ require "logstash/inputs/mongoprofile"
|
|
5
5
|
describe LogStash::Inputs::Mongoprofile do
|
6
6
|
|
7
7
|
it_behaves_like "an interruptible input plugin" do
|
8
|
-
let(:config) { { "interval" => 100 } }
|
8
|
+
let(:config) { { "interval" => 100, "url" => "mongodb://192.168.1.37/eleet-v2-dev", "path" => "/home/artem"} }
|
9
9
|
end
|
10
10
|
|
11
11
|
end
|