logstash-input-gcs 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9fbbed9d2b231da600fae778d3cd1ad6c360b6a0
4
+ data.tar.gz: 6f19cd166e7e4394276cd29d68b183887e9be5c2
5
+ SHA512:
6
+ metadata.gz: cfa7f099a26a2b90ddb41d4043c51bc62e52043ba385df212286fea9fd0bfae593b8a4c8b9b912e501134b45756a71f4551801ca158cafac1c50c198a7b13b5f
7
+ data.tar.gz: 73e091f464661bb725254e1cca29d6b83debbd44c020367e8d50d37f7e0dbd1b0d5d59f6b13d5d296138334af76e21df240e038ed6b238a21ccf39c911342be9
@@ -0,0 +1,9 @@
1
+ ## 2.0.4
2
+ - Add encoding: utf-8 line to spec file. This can prevent issues with tests.
3
+ ## 2.0.1
4
+ - Simplify the shutdown implementation a bit for easier understanding
5
+ ## 2.0.0
6
+ - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
7
+ instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
8
+ - Dependency on logstash-core update to 2.0
9
+
@@ -0,0 +1,2 @@
1
+ # logstash-input-example
2
+ Example input plugin. This should help bootstrap your effort to write your own input plugin!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+ gem
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
@@ -0,0 +1,89 @@
1
+ # Logstash Plugin
2
+
3
+ [![Build
4
+ Status](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Inputs/job/logstash-plugin-input-example-unit/badge/icon)](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Inputs/job/logstash-plugin-input-example-unit/)
5
+
6
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
7
+
8
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
9
+
10
+ ## Documentation
11
+
12
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
13
+
14
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
15
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
16
+
17
+ ## Need Help?
18
+
19
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
20
+
21
+ ## Developing
22
+
23
+ ### 1. Plugin Developement and Testing
24
+
25
+ #### Code
26
+ - To get started, you'll need JRuby with the Bundler gem installed.
27
+
28
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
29
+
30
+ - Install dependencies
31
+ ```sh
32
+ bundle install
33
+ ```
34
+
35
+ #### Test
36
+
37
+ - Update your dependencies
38
+
39
+ ```sh
40
+ bundle install
41
+ ```
42
+
43
+ - Run tests
44
+
45
+ ```sh
46
+ bundle exec rspec
47
+ ```
48
+
49
+ ### 2. Running your unpublished Plugin in Logstash
50
+
51
+ #### 2.1 Run in a local Logstash clone
52
+
53
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
54
+ ```ruby
55
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
56
+ ```
57
+ - Install plugin
58
+ ```sh
59
+ bin/plugin install --no-verify
60
+ ```
61
+ - Run Logstash with your plugin
62
+ ```sh
63
+ bin/logstash -e 'filter {awesome {}}'
64
+ ```
65
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
66
+
67
+ #### 2.2 Run in an installed Logstash
68
+
69
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
70
+
71
+ - Build your plugin gem
72
+ ```sh
73
+ gem build logstash-filter-awesome.gemspec
74
+ ```
75
+ - Install the plugin from the Logstash home
76
+ ```sh
77
+ bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
78
+ ```
79
+ - Start Logstash and proceed to test the plugin
80
+
81
+ ## Contributing
82
+
83
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
84
+
85
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
86
+
87
+ It is more important to the community that you are able to contribute.
88
+
89
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,314 @@
1
+ # encoding: utf-8
2
+ require 'gcloud'
3
+
4
+ require 'faraday'
5
+ module Faraday
6
+ class Adapter
7
+ class NetHttp < Faraday::Adapter
8
+ def ssl_verify_mode(ssl)
9
+ OpenSSL::SSL::VERIFY_NONE
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ require "logstash/inputs/base"
16
+ require "logstash/namespace"
17
+ require "time"
18
+ require "tmpdir"
19
+ require "stud/interval"
20
+ require "stud/temporary"
21
+
22
+ # Stream events from files from a S3 bucket.
23
+ #
24
+ # Each line from each file generates an event.
25
+ # Files ending in `.gz` are handled as gzip'ed files.
26
+ class LogStash::Inputs::GCS < LogStash::Inputs::Base
27
+ config_name "gcs"
28
+
29
+ default :codec, "plain"
30
+
31
+ config :project
32
+
33
+ # Path to JSON file containing the Service Account credentials (not needed when running inside GCE)
34
+ config :keyfile
35
+
36
+ # The name of the GCS bucket.
37
+ config :bucket, :validate => :string, :required => true
38
+
39
+ # If specified, the prefix of filenames in the bucket must match (not a regexp)
40
+ config :prefix, :validate => :string, :default => nil
41
+
42
+ # Where to write the since database (keeps track of the date
43
+ # the last handled file was added to S3). The default will write
44
+ # sincedb files to some path matching "$HOME/.sincedb*"
45
+ # Should be a path with filename not just a directory.
46
+ config :sincedb_path, :validate => :string, :default => nil
47
+
48
+ # Name of a S3 bucket to backup processed files to.
49
+ config :backup_to_bucket, :validate => :string, :default => nil
50
+
51
+ # Append a prefix to the key (full path including file name in s3) after processing.
52
+ # If backing up to another (or the same) bucket, this effectively lets you
53
+ # choose a new 'folder' to place the files in
54
+ config :backup_add_prefix, :validate => :string, :default => nil
55
+
56
+ # Path of a local directory to backup processed files to.
57
+ config :backup_to_dir, :validate => :string, :default => nil
58
+
59
+ # Whether to delete processed files from the original bucket.
60
+ config :delete, :validate => :boolean, :default => false
61
+
62
+ # Interval to wait between to check the file list again after a run is finished.
63
+ # Value is in seconds.
64
+ config :interval, :validate => :number, :default => 60
65
+
66
+ # Ruby style regexp of keys to exclude from the bucket
67
+ config :exclude_pattern, :validate => :string, :default => nil
68
+
69
+ # Set the directory where logstash will store the tmp files before processing them.
70
+ # default to the current OS temporary directory in linux /tmp/logstash
71
+ config :temporary_directory, :validate => :string, :default => File.join(Dir.tmpdir, "logstash")
72
+
73
+ public
74
+ def register
75
+ require "fileutils"
76
+ require "digest/md5"
77
+
78
+ @logger.info("Registering GCS input", :bucket => @bucket, :project => @project, :keyfile => @keyfile)
79
+
80
+ @gcs = Gcloud.new(project=@project, keyfile=@keyfile).storage
81
+ @gcsbucket = @gcs.bucket @bucket
82
+
83
+ unless @backup_to_bucket.nil?
84
+ @backup_bucket = @gcs.bucket @backup_to_bucket
85
+ unless @backup_bucket
86
+ @gcs.create_bucket(@backup_to_bucket)
87
+ end
88
+ end
89
+
90
+ unless @backup_to_dir.nil?
91
+ Dir.mkdir(@backup_to_dir, 0700) unless File.exists?(@backup_to_dir)
92
+ end
93
+
94
+ FileUtils.mkdir_p(@temporary_directory) unless Dir.exist?(@temporary_directory)
95
+ end # def register
96
+
97
+ public
98
+ def run(queue)
99
+ @current_thread = Thread.current
100
+ Stud.interval(@interval, sleep_then_run: false) do
101
+ process_files(queue)
102
+ end
103
+ end # def run
104
+
105
+ public
106
+ def list_new_files
107
+ @logger.debug("GCS input: Polling")
108
+
109
+ objects = {}
110
+ @gcsbucket.files({prefix: @prefix}).each do |file|
111
+ @logger.debug("GCS input: Found file", :name => file.name)
112
+
113
+ unless ignore_filename?(file.name)
114
+ if sincedb.newer?(file.updated_at())
115
+ objects[file.name] = file.updated_at()
116
+ @logger.debug("GCS input: Adding to objects[]", :name => file.name)
117
+ end
118
+ end
119
+ end
120
+ return objects.keys.sort {|a,b| objects[a] <=> objects[b]}
121
+ end # def fetch_new_files
122
+
123
+ public
124
+ def backup_to_bucket(object, key)
125
+ # TODO (barak)
126
+ unless @backup_to_bucket.nil?
127
+ backup_key = "#{@backup_add_prefix}#{key}"
128
+ if @delete
129
+ object.move_to(backup_key, :bucket => @backup_bucket)
130
+ else
131
+ object.copy_to(backup_key, :bucket => @backup_bucket)
132
+ end
133
+ end
134
+ end
135
+
136
+ public
137
+ def backup_to_dir(filename)
138
+ unless @backup_to_dir.nil?
139
+ FileUtils.cp(filename, @backup_to_dir)
140
+ end
141
+ end
142
+
143
+ public
144
+ def process_files(queue)
145
+ objects = list_new_files
146
+
147
+ objects.each do |file|
148
+ if stop?
149
+ break
150
+ else
151
+ @logger.debug("GCS input processing", :bucket => @bucket, :file => file)
152
+ process_log(queue, file)
153
+ end
154
+ end
155
+ end # def process_files
156
+
157
+ public
158
+ def stop
159
+ # @current_thread is initialized in the `#run` method,
160
+ # this variable is needed because the `#stop` is a called in another thread
161
+ # than the `#run` method and requiring us to call stop! with a explicit thread.
162
+ Stud.stop!(@current_thread)
163
+ end
164
+
165
+ private
166
+
167
+ # Read the content of the local file
168
+ #
169
+ # @param [Queue] Where to push the event
170
+ # @param [String] Which file to read from
171
+ # @return [Boolean] True if the file was completely read, false otherwise.
172
+ def process_local_log(queue, filename)
173
+ @logger.debug('Processing file', :filename => filename)
174
+
175
+ metadata = {}
176
+ # Currently codecs operates on bytes instead of stream.
177
+ # So all IO stuff: decompression, reading need to be done in the actual
178
+ # input and send as bytes to the codecs.
179
+ read_file(filename) do |line|
180
+ if stop?
181
+ @logger.warn("Logstash GCS input, stop reading in the middle of the file, we will read it again when logstash is started")
182
+ return false
183
+ end
184
+
185
+ @codec.decode(line) do |event|
186
+ decorate(event)
187
+ queue << event
188
+ end
189
+ end
190
+
191
+ return true
192
+ end # def process_local_log
193
+
194
+ private
195
+ def read_file(filename, &block)
196
+ if gzip?(filename)
197
+ read_gzip_file(filename, block)
198
+ else
199
+ read_plain_file(filename, block)
200
+ end
201
+ end
202
+
203
+ def read_plain_file(filename, block)
204
+ File.open(filename, 'rb') do |file|
205
+ file.each(&block)
206
+ end
207
+ end
208
+
209
+ private
210
+ def read_gzip_file(filename, block)
211
+ begin
212
+ Zlib::GzipReader.open(filename) do |decoder|
213
+ decoder.each_line { |line| block.call(line) }
214
+ end
215
+ rescue Zlib::Error, Zlib::GzipFile::Error => e
216
+ @logger.error("Gzip codec: We cannot uncompress the gzip file", :filename => filename)
217
+ raise e
218
+ end
219
+ end
220
+
221
+ private
222
+ def gzip?(filename)
223
+ filename.end_with?('.gz')
224
+ end
225
+
226
+ private
227
+ def sincedb
228
+ @sincedb ||= if @sincedb_path.nil?
229
+ @logger.info("Using default generated file for the sincedb", :filename => sincedb_file)
230
+ SinceDB::File.new(sincedb_file)
231
+ else
232
+ @logger.info("Using the provided sincedb_path",
233
+ :sincedb_path => @sincedb_path)
234
+ SinceDB::File.new(@sincedb_path)
235
+ end
236
+ end
237
+
238
+ private
239
+ def sincedb_file
240
+ File.join(ENV["HOME"], ".sincedb_" + Digest::MD5.hexdigest("#{@bucket}+#{@prefix}"))
241
+ end
242
+
243
+ private
244
+ def ignore_filename?(filename)
245
+ if @prefix == filename
246
+ return true
247
+ elsif (@backup_add_prefix && @backup_to_bucket == @bucket && filename =~ /^#{backup_add_prefix}/)
248
+ return true
249
+ elsif @exclude_pattern.nil?
250
+ return false
251
+ elsif filename =~ Regexp.new(@exclude_pattern)
252
+ return true
253
+ else
254
+ return false
255
+ end
256
+ end
257
+
258
+ private
259
+ def process_log(queue, key)
260
+ object = @gcsbucket.file key
261
+
262
+ filename = File.join(temporary_directory, File.basename(key))
263
+
264
+ @logger.debug("GCS input: Download remote file", :remote_key => object.name, :local_filename => filename)
265
+ object.download filename
266
+
267
+ if process_local_log(queue, filename)
268
+ backup_to_bucket(object, key)
269
+ backup_to_dir(filename)
270
+ delete_file_from_bucket(object)
271
+ FileUtils.remove_entry_secure(filename, true)
272
+ lastmod = object.updated_at()
273
+ sincedb.write(lastmod)
274
+ else
275
+ FileUtils.remove_entry_secure(filename, true)
276
+ end
277
+ end
278
+
279
+ private
280
+ def delete_file_from_bucket(object)
281
+ if @delete and @backup_to_bucket.nil?
282
+ object.delete()
283
+ end
284
+ end
285
+
286
+
287
+ private
288
+ module SinceDB
289
+ class File
290
+ def initialize(file)
291
+ @sincedb_path = file
292
+ end
293
+
294
+ def newer?(date)
295
+ date > read
296
+ end
297
+
298
+ def read
299
+ if ::File.exists?(@sincedb_path)
300
+ content = ::File.read(@sincedb_path).chomp.strip
301
+ # If the file was created but we didn't have the time to write to it
302
+ return content.empty? ? Time.new(0) : Time.parse(content)
303
+ else
304
+ return Time.new(0)
305
+ end
306
+ end
307
+
308
+ def write(since = nil)
309
+ since = Time.now() if since.nil?
310
+ ::File.open(@sincedb_path, 'w') { |file| file.write(since.to_s) }
311
+ end
312
+ end
313
+ end
314
+ end # class LogStash::Inputs::GCS
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-gcs'
3
+ s.version = '2.0.5'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "This example input streams a string at a definable interval."
6
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
7
+ s.authors = ["Elastic"]
8
+ s.email = 'info@elastic.co'
9
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "input" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core", ">= 2.0.0", "< 3.0.0"
22
+ s.add_runtime_dependency 'logstash-codec-plain'
23
+ s.add_runtime_dependency 'gcloud'
24
+ s.add_runtime_dependency 'stud', '>= 0.0.22'
25
+ s.add_development_dependency 'logstash-devutils', '>= 0.0.16'
26
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/inputs/example"
4
+
5
+ describe LogStash::Inputs::Example do
6
+
7
+ it_behaves_like "an interruptible input plugin" do
8
+ let(:config) { { "interval" => 100 } }
9
+ end
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-gcs
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: logstash-codec-plain
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: gcloud
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: stud
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 0.0.22
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: 0.0.22
75
+ - !ruby/object:Gem::Dependency
76
+ name: logstash-devutils
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: 0.0.16
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: 0.0.16
89
+ description: This gem is a logstash plugin required to be installed on top of the
90
+ Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not
91
+ a stand-alone program
92
+ email: info@elastic.co
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - CHANGELOG.md
98
+ - DEVELOPER.md
99
+ - Gemfile
100
+ - LICENSE
101
+ - NOTICE.TXT
102
+ - README.md
103
+ - lib/logstash/inputs/gcs.rb
104
+ - logstash-input-gce.gemspec
105
+ - spec/inputs/example_spec.rb
106
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
107
+ licenses:
108
+ - Apache License (2.0)
109
+ metadata:
110
+ logstash_plugin: 'true'
111
+ logstash_group: input
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.2.2
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: This example input streams a string at a definable interval.
132
+ test_files:
133
+ - spec/inputs/example_spec.rb