logstash-input-ngn 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: 4d0d4c0a535ade6b061172c3a3e2405d9d9a78964ce52adb9b27eb310154529c
4
+ data.tar.gz: a8a87e1547dd598eb6ab7374f4d772f11fb70b205931217c068d3842e4ce4be3
5
+ SHA512:
6
+ metadata.gz: 1e2aad57a0d348f3ad8c42a59b8d6e10c26d386c17a1fe2f91d2315e1f8521e0cd0bcd4c22e959cd604cc00e7ac8154e845880508cf424a04834367ba5b27b1a
7
+ data.tar.gz: c6a1e3211bdcc1ef772fda06829f28c19f42f05e733d97d7f7389e50ad8ca5d3547ee3aeacf03d411418e62ad65dbce51b0b6b6785b148c17f8376806678400b
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,149 @@
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 `command` field of this event will be the command run.
13
+ # * The `message` field of this event will be the entire stdout of the command.
14
+ #
15
+ class LogStash::Inputs::Ngn < LogStash::Inputs::Base
16
+
17
+ config_name "ngn"
18
+
19
+ default :codec, "json"
20
+
21
+ # Command to run. For example : `uptime`
22
+ config :script, :validate => :string, :required => true
23
+
24
+ # ngn username
25
+ config :username, :validate => :string, :required => true
26
+
27
+ # ngn password
28
+ config :password, :validate => :string, :required => true
29
+
30
+ # authentication token
31
+ config :token, :validate => :string
32
+
33
+ # Interval to run the command. Value is in seconds.
34
+ # Either `interval` or `schedule` option must be defined.
35
+ config :interval, :validate => :number
36
+
37
+ # Schedule of when to periodically run command, in Cron format
38
+ # For example: "* * * * *" (execute command every minute, on the minute)
39
+ # Either `interval` or `schedule` option must be defined.
40
+ config :schedule, :validate => :string
41
+
42
+ def register
43
+ @logger.info("Registering Exec Input", :type => @type, :script => @script, :interval => @interval, :schedule => @schedule)
44
+ @hostname = Socket.gethostname
45
+ @io = nil
46
+
47
+ if (@interval.nil? && @schedule.nil?) || (@interval && @schedule)
48
+ raise LogStash::ConfigurationError, "jdbc input: either 'interval' or 'schedule' option must be defined."
49
+ end
50
+ end # def register
51
+
52
+ def run(queue)
53
+ if @schedule
54
+ @scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
55
+ @scheduler.cron @schedule do
56
+ execute(queue)
57
+ end
58
+ @scheduler.join
59
+ else
60
+ while !stop?
61
+ duration = execute(queue)
62
+ wait_until_end_of_interval(duration)
63
+ end # loop
64
+ end
65
+ end # def run
66
+
67
+ def stop
68
+ close_io()
69
+ @scheduler.shutdown(:wait) if @scheduler
70
+ end
71
+
72
+ # Execute a given script
73
+ # @param [String] A script string
74
+ # @param [Array or Queue] A queue to append events to
75
+ def execute(queue)
76
+ start = Time.now
77
+ output = exit_status = nil
78
+ begin
79
+ @logger.debug? && @logger.debug("Running exec", :script => @script)
80
+ output, exit_status = run_script()
81
+ rescue StandardError => e
82
+ @logger.error("Error while running script",
83
+ :script => @script, :e => e, :backtrace => e.backtrace)
84
+ rescue Exception => e
85
+ @logger.error("Exception while running script",
86
+ :script => @script, :e => e, :backtrace => e.backtrace)
87
+ end
88
+ duration = Time.now - start
89
+ @logger.debug? && @logger.debug("Command completed", :script => @script, :duration => duration)
90
+ if output
91
+ @codec.decode(output) do |decoded|
92
+
93
+ @token = decoded.get("token")
94
+
95
+ decoded.get("values").each do |val|
96
+ event = LogStash::Event.new(val)
97
+ decorate(event)
98
+ event.set("[@metadata][host]", @hostname)
99
+ event.set("[@metadata][duration]", duration)
100
+ event.set("[@metadata][exit_status]", exit_status)
101
+ queue << event
102
+ end
103
+ end
104
+ end
105
+ duration
106
+ end
107
+
108
+ private
109
+
110
+ def run_script
111
+ command = "python3 #{@script} -u #{@username} -p #{@password}"
112
+ if @token
113
+ command << " -t \"#{@token}\""
114
+ end
115
+
116
+ @logger.debug? && @logger.debug("Running exec", :command => command)
117
+
118
+ @io = IO.popen(command)
119
+ output = @io.read
120
+ @io.close # required in order to read $?
121
+ exit_status = $?.exitstatus # should be threadsafe as per rb_thread_save_context
122
+ [output, exit_status]
123
+ ensure
124
+ close_io()
125
+ end
126
+
127
+ # Close @io
128
+ def close_io
129
+ return if @io.nil? || @io.closed?
130
+ @io.close
131
+ @io = nil
132
+ end
133
+
134
+ # Wait until the end of the interval
135
+ # @param [Integer] the duration of the last command executed
136
+ def wait_until_end_of_interval(duration)
137
+ # Sleep for the remainder of the interval, or 0 if the duration ran
138
+ # longer than the interval.
139
+ sleeptime = [0, @interval - duration].max
140
+ if sleeptime > 0
141
+ Stud.stoppable_sleep(sleeptime) { stop? }
142
+ else
143
+ @logger.warn("Execution ran longer than the interval. Skipping sleep.",
144
+ :script => @script, :duration => duration, :interval => @interval)
145
+ end
146
+ end
147
+
148
+
149
+ end # class LogStash::Inputs::Exec
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-ngn'
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/ngn"
4
+
5
+ describe LogStash::Inputs::Ngn 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-ngn
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/ngn.rb
106
+ - logstash-input-ngn.gemspec
107
+ - spec/inputs/ngn_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/ngn_spec.rb