logstash-input-ngc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 49ac95d1d66d2769e9851953f3c924a77bb69a30b3327380bc9fdd8a270ed6d1
4
+ data.tar.gz: 99ec01bb5b0c17bd5fec1efe0780a1b1ce07189a7b4127ed16e66aecf0053896
5
+ SHA512:
6
+ metadata.gz: 3ad46f9b183926d430c7ac8154f1055b1f6977992dddfedf237e1a7ff6625f47bb92b18c6c27ff34a8956465d031dec4b6ad477d66a6f134a2d5aac0cb64d07f
7
+ data.tar.gz: 6479c6692d31cd016ae05ab80e2b1937a2b59e48bf0b316ab5459cdf424a32b749b64924a504a5c2796d3ebe2a08b15056cccf632471cf50d0dcf4b4b6460361
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ 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.
6
+
7
+ ## Documentation
8
+
9
+ 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/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - 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).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/logstash-plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ 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:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ 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.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,145 @@
1
+ # encoding: utf-8
2
+ require "logstash/inputs/base"
3
+ require "logstash/namespace"
4
+ require "socket" # for Socket.gethostname
5
+ require "stud/interval"
6
+ require "rufus/scheduler"
7
+
8
+ # Periodically run a shell command and capture the whole output as an event.
9
+ #
10
+ # Notes:
11
+ #
12
+ # * The `script` field of this event will be the python script run.
13
+ # * The `api_key` field of this event will be the key used to access NGC.
14
+ #
15
+ class LogStash::Inputs::Ngc < LogStash::Inputs::Base
16
+
17
+ config_name "ngc"
18
+
19
+ default :codec, "json"
20
+
21
+ # path to python script to run.
22
+ config :script, :validate => :string, :required => true
23
+
24
+ # ngc api key
25
+ config :api_key, :validate => :string, :required => true
26
+
27
+ # jwt token
28
+ config :jwt_token, :validate => :string
29
+
30
+ # Interval to run the command. Value is in seconds.
31
+ # Either `interval` or `schedule` option must be defined.
32
+ config :interval, :validate => :number
33
+
34
+ # Schedule of when to periodically run command, in Cron format
35
+ # For example: "* * * * *" (execute command every minute, on the minute)
36
+ # Either `interval` or `schedule` option must be defined.
37
+ config :schedule, :validate => :string
38
+
39
+ def register
40
+ @logger.info("Registering Exec Input", :type => @type, :script => @script, :interval => @interval, :schedule => @schedule)
41
+ @hostname = Socket.gethostname
42
+ @io = nil
43
+
44
+ if (@interval.nil? && @schedule.nil?) || (@interval && @schedule)
45
+ raise LogStash::ConfigurationError, "jdbc input: either 'interval' or 'schedule' option must be defined."
46
+ end
47
+ end # def register
48
+
49
+ def run(queue)
50
+ if @schedule
51
+ @scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
52
+ @scheduler.cron @schedule do
53
+ execute(queue)
54
+ end
55
+ @scheduler.join
56
+ else
57
+ while !stop?
58
+ duration = execute(queue)
59
+ wait_until_end_of_interval(duration)
60
+ end # loop
61
+ end
62
+ end # def run
63
+
64
+ def stop
65
+ close_io()
66
+ @scheduler.shutdown(:wait) if @scheduler
67
+ end
68
+
69
+ # Execute a given command
70
+ # @param [String] A command string
71
+ # @param [Array or Queue] A queue to append events to
72
+ def execute(queue)
73
+ start = Time.now
74
+ output = exit_status = nil
75
+ begin
76
+ output, exit_status = run_script()
77
+ rescue StandardError => e
78
+ @logger.error("Error while running script",
79
+ :script => @script, :e => e, :backtrace => e.backtrace)
80
+ rescue Exception => e
81
+ @logger.error("Exception while running script",
82
+ :script => @script, :e => e, :backtrace => e.backtrace)
83
+ end
84
+ duration = Time.now - start
85
+ @logger.debug? && @logger.debug("Command completed", :script => @script, :duration => duration)
86
+ if output
87
+ @codec.decode(output) do |decoded|
88
+
89
+ @jwt_token = decoded.get("token")
90
+
91
+ decoded.get("values").each do |val|
92
+ event = LogStash::Event.new(val)
93
+ decorate(event)
94
+ event.set("[@metadata][host]", @hostname)
95
+ event.set("[@metadata][duration]", duration)
96
+ event.set("[@metadata][exit_status]", exit_status)
97
+ queue << event
98
+ end
99
+ end
100
+ end
101
+ duration
102
+ end
103
+
104
+ private
105
+
106
+ def run_script
107
+ command = "python3 #{@script} -k #{@api_key}"
108
+ if @jwt_token
109
+ command << " -t #{@jwt_token}"
110
+ end
111
+
112
+ @logger.debug? && @logger.debug("Running exec", :command => command)
113
+
114
+ @io = IO.popen(command)
115
+ output = @io.read
116
+ @io.close # required in order to read $?
117
+ exit_status = $?.exitstatus # should be threadsafe as per rb_thread_save_context
118
+ [output, exit_status]
119
+ ensure
120
+ close_io()
121
+ end
122
+
123
+ # Close @io
124
+ def close_io
125
+ return if @io.nil? || @io.closed?
126
+ @io.close
127
+ @io = nil
128
+ end
129
+
130
+ # Wait until the end of the interval
131
+ # @param [Integer] the duration of the last command executed
132
+ def wait_until_end_of_interval(duration)
133
+ # Sleep for the remainder of the interval, or 0 if the duration ran
134
+ # longer than the interval.
135
+ sleeptime = [0, @interval - duration].max
136
+ if sleeptime > 0
137
+ Stud.stoppable_sleep(sleeptime) { stop? }
138
+ else
139
+ @logger.warn("Execution ran longer than the interval. Skipping sleep.",
140
+ :script => @script, :duration => duration, :interval => @interval)
141
+ end
142
+ end
143
+
144
+
145
+ end # class LogStash::Inputs::Exec
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-ngc'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'summary'
6
+ s.description = 'description'
7
+ s.homepage = 'http://example.com'
8
+ s.authors = ['Chris Fischer']
9
+ s.email = 'chris.fischer36@gmail.com'
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-plugin-api", "~> 2"
22
+ s.add_runtime_dependency "stud", "~> 0.0.22"
23
+ s.add_runtime_dependency "logstash-codec-json", "~> 3"
24
+ s.add_runtime_dependency "rufus-scheduler", "~> 3"
25
+
26
+ s.add_development_dependency "logstash-devutils", "~> 1"
27
+ s.add_development_dependency "timecop", "~> 0"
28
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/inputs/ngc"
4
+
5
+ describe LogStash::Inputs::Ngc 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,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-ngc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Fischer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.0.22
33
+ name: stud
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.22
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3'
47
+ name: logstash-codec-json
48
+ prerelease: false
49
+ type: :runtime
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3'
61
+ name: rufus-scheduler
62
+ prerelease: false
63
+ type: :runtime
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1'
75
+ name: logstash-devutils
76
+ prerelease: false
77
+ type: :development
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1'
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ name: timecop
90
+ prerelease: false
91
+ type: :development
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: description
98
+ email: chris.fischer36@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - Gemfile
104
+ - README.md
105
+ - lib/logstash/inputs/ngc.rb
106
+ - logstash-input-ngc.gemspec
107
+ - spec/inputs/ngc_spec.rb
108
+ homepage: http://example.com
109
+ licenses:
110
+ - Apache-2.0
111
+ metadata:
112
+ logstash_plugin: 'true'
113
+ logstash_group: input
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: summary
134
+ test_files:
135
+ - spec/inputs/ngc_spec.rb