logstash-input-ping 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: de8cd823d25369b0b14bdf3af18a4703e8493cad139b1d2c20a48c9fa372a654
4
+ data.tar.gz: 53347e595d9a9a0279afb2b3468ae8fe9a455a3c96b1023ba7162835c65cdd98
5
+ SHA512:
6
+ metadata.gz: 59697dc6258d8aef750156d8b725abffc2e2fec52c2a1f085e0e00c915e6f158937204686e352a43e9d543323f903c40a9421c986db98e2d654555eb3791d05f
7
+ data.tar.gz: 19c14a33e777f8f08c3ba8412611c77fff4c9598fa02635ac8e6d728ccd352e4b731b6d8d81781d2f1de7cac2547c710f4b5f9710f61d5234bedd8dbdeddcb7d
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Davide Cavestro - davide.cavestro@gmail.com
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
@@ -0,0 +1,2 @@
1
+ # logstash-input-ping
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
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
@@ -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,230 @@
1
+ # encoding: utf-8
2
+ require "logstash/inputs/base"
3
+ require "logstash/namespace"
4
+ require "stud/interval"
5
+ require 'net/ping'
6
+
7
+ # This plugin was created as a way to ingest data from ping probes
8
+ # into Logstash. You can periodically schedule ingestion
9
+ # using a cron syntax (see `schedule` setting).
10
+ #
11
+ #
12
+ # ==== Ping types
13
+ #
14
+ # This plugin uses 'net/ping' which provides different ways to ping
15
+ # computers. The desired ping mode must be explicitly passed in to the
16
+ # plugin using the `ping_mode` configuration option.
17
+ #
18
+ # ==== Scheduling
19
+ #
20
+ # Input from this plugin can be scheduled to run periodically according to a specific
21
+ # schedule. This scheduling syntax is powered by https://github.com/jmettraux/rufus-scheduler[rufus-scheduler].
22
+ # The syntax is cron-like with some extensions specific to Rufus (e.g. timezone support ).
23
+ #
24
+ # Examples:
25
+ #
26
+ # |==========================================================
27
+ # | `* 5 * 1-3 *` | will execute every minute of 5am every day of January through March.
28
+ # | `0 * * * *` | will execute on the 0th minute of every hour every day.
29
+ # | `0 6 * * * America/Chicago` | will execute at 6:00am (UTC/GMT -5) every day.
30
+ # |==========================================================
31
+ #
32
+ #
33
+ # Further documentation describing this syntax can be found https://github.com/jmettraux/rufus-scheduler#parsing-cronlines-and-time-strings[here].
34
+ #
35
+ # ==== Usage:
36
+ #
37
+ # Here is an example of setting up the plugin to ping google DNS.
38
+ # First, we place the appropriate JDBC driver library in our current
39
+ # path (this can be placed anywhere on your filesystem). In this example, we connect to
40
+ # the 'mydb' database using the user: 'mysql' and wish to input all rows in the 'songs'
41
+ # table that match a specific artist. The following examples demonstrates a possible
42
+ # Logstash configuration for this. The `schedule` option in this example will
43
+ # instruct the plugin to execute this input statement on the minute, every minute.
44
+ #
45
+ # [source,ruby]
46
+ # ------------------------------------------------------------------------------
47
+ # input {
48
+ # ping {
49
+ # mode => "external"
50
+ # host => "8.8.8.8"
51
+ # timeout => 5
52
+ # schedule => "* * * * * *"
53
+ # }
54
+ # }
55
+ # ------------------------------------------------------------------------------
56
+ #
57
+
58
+ class LogStash::Inputs::Ping < LogStash::Inputs::Base
59
+ config_name "ping"
60
+
61
+ # If undefined, Logstash will complain, even if codec is unused.
62
+ default :codec, "json"
63
+
64
+ # The host to ping.
65
+ #
66
+ # The default is `127.0.0.1`.
67
+ config :host, :validate => :string, :default => "127.0.0.1"
68
+
69
+ # The ping mode to use.
70
+ #
71
+ # The default is `external`.
72
+ config :mode, :validate => :string, :default => "external"
73
+
74
+ # Set how frequently messages should be sent.
75
+ #
76
+ # The default, `1`, means send a message every second.
77
+ config :interval, :validate => :number, :default => 1
78
+
79
+ # Schedule of when to periodically run statement, in Cron format
80
+ # for example: "* * * * * *" (execute ping every second, on the second)
81
+ #
82
+ # There is no schedule by default. If no schedule is given, then the ping is run
83
+ # using `interval` confguration.
84
+ config :schedule, :validate => :string, :default => nil #"* * * * * *"
85
+
86
+
87
+ public
88
+
89
+ def register
90
+ @thishost = Socket.gethostname
91
+ if @schedule
92
+ require "rufus/scheduler"
93
+ end
94
+
95
+ # @logger = self.logger
96
+ end # def register
97
+
98
+ def run(queue)
99
+ create_probe
100
+ if @schedule
101
+ @scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
102
+ @scheduler.cron @schedule do
103
+ do_ping (queue)
104
+ end
105
+
106
+ @scheduler.join
107
+ else
108
+ # we can abort the loop if stop? becomes true
109
+ while !stop?
110
+ do_ping (queue)
111
+ # because the sleep interval can be big, when shutdown happens
112
+ # we want to be able to abort the sleep
113
+ # Stud.stoppable_sleep will frequently evaluate the given block
114
+ # and abort the sleep(@interval) if the return value is true
115
+ Stud.stoppable_sleep(@interval) { stop? }
116
+ end # loop
117
+ end
118
+ end # def run
119
+
120
+ def stop
121
+ @scheduler.stop if @scheduler
122
+
123
+ end
124
+
125
+ private
126
+
127
+ def create_probe ()
128
+ @probe = case @mode.upcase
129
+ when "ICMP" then IcmpProbe.new
130
+ when "EXTERNAL" then ExternalProbe.new
131
+ when "HTTP" then HttpProbe.new
132
+ when "TCP" then TcpProbe.new
133
+ when "UDP" then UdpProbe.new
134
+ else
135
+ raise(LogStash::ConfigurationError, "Must set a valid :mode.")
136
+ end
137
+
138
+ @probe
139
+ end
140
+
141
+ def do_ping (queue)
142
+ success = @probe.ping(@host)
143
+ duration = @probe.duration
144
+ message = {"success" => success, "duration" => duration, "host" => @host}
145
+ event = LogStash::Event.new("message" => message, "host" => @thishost)
146
+ decorate(event)
147
+ queue << event
148
+ end
149
+
150
+ class Probe
151
+ def ping (host)
152
+ end
153
+
154
+ def duration ()
155
+ end
156
+ end
157
+
158
+ class IcmpProbe < Probe
159
+ def initialize
160
+ @delegate = Net::Ping::ICMP.new
161
+ end
162
+
163
+ def ping (host)
164
+ @delegate.ping (host)
165
+ end
166
+
167
+ def duration ()
168
+ @delegate.duration
169
+ end
170
+ end
171
+
172
+ class ExternalProbe < Probe
173
+ def initialize
174
+ @delegate = Net::Ping::External.new
175
+ end
176
+
177
+ def ping (host)
178
+ @delegate.ping (host)
179
+ end
180
+
181
+ def duration ()
182
+ @delegate.duration
183
+ end
184
+ end
185
+
186
+ class HttpProbe < Probe
187
+ # warning, exception
188
+ def initialize
189
+ @delegate = Net::Ping::HTTP.new
190
+ end
191
+
192
+ def ping (host)
193
+ @delegate.ping (host)
194
+ end
195
+
196
+ def duration ()
197
+ @delegate.duration
198
+ end
199
+ end
200
+
201
+ class TcpProbe < Probe
202
+ # http?
203
+ def initialize
204
+ @delegate = Net::Ping::TCP.new
205
+ end
206
+
207
+ def ping (host)
208
+ @delegate.ping (host)
209
+ end
210
+
211
+ def duration ()
212
+ @delegate.duration
213
+ end
214
+ end
215
+
216
+ class UdpProbe < Probe
217
+ # http?
218
+ def initialize
219
+ @delegate = Net::Ping::UDP.new
220
+ end
221
+
222
+ def ping (host)
223
+ @delegate.ping (host)
224
+ end
225
+
226
+ def duration ()
227
+ @delegate.duration
228
+ end
229
+ end
230
+ end # class LogStash::Inputs::Ping
@@ -0,0 +1,31 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-ping'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Provides ping data as input for Logstash'
6
+ s.description = 'This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program'
7
+ s.homepage = 'https://github.com/davidecavestro/logstash-input-ping'
8
+ s.authors = ['Davide Cavestro']
9
+ s.email = 'davide.cavestro@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.0"
22
+ s.add_runtime_dependency 'logstash-codec-json'
23
+ s.add_runtime_dependency 'logstash-codec-plain'
24
+ s.add_runtime_dependency 'tzinfo'
25
+ s.add_runtime_dependency 'tzinfo-data'
26
+ s.add_runtime_dependency 'rufus-scheduler'
27
+ s.add_runtime_dependency 'net-ping'
28
+
29
+ s.add_development_dependency 'logstash-devutils', '>= 0.0.16'
30
+ s.add_development_dependency 'timecop'
31
+ end
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/inputs/ping"
4
+ require "net/ping"
5
+ require "timecop"
6
+ require "time"
7
+ require "date"
8
+
9
+ describe LogStash::Inputs::Ping do
10
+
11
+ let(:plugin) { LogStash::Inputs::Ping.new(config) }
12
+ let(:queue) { Queue.new }
13
+
14
+ it_behaves_like "an interruptible input plugin" do
15
+ let(:config) do
16
+ {
17
+ "host" => ["127.0.0.1"],
18
+ "mode" => "external",
19
+ "interval" => 1
20
+ }
21
+ end
22
+ end
23
+
24
+ context "when scheduling" do
25
+ let(:config) do
26
+ {
27
+ "host" => ["127.0.0.1"],
28
+ "mode" => "external",
29
+ "schedule" => "* * * * * UTC"
30
+ }
31
+ end
32
+
33
+ before do
34
+ plugin.register
35
+ end
36
+
37
+ it "should properly schedule" do
38
+
39
+ Timecop.travel(Time.new(2000))
40
+ Timecop.scale(60)
41
+ runner = Thread.new do
42
+ expect(plugin).to receive(:do_ping) {
43
+ queue << LogStash::Event.new({})
44
+ }.at_least(:twice)
45
+
46
+ plugin.run(queue)
47
+ end
48
+ sleep 3
49
+ plugin.stop
50
+ runner.kill
51
+ runner.join
52
+ expect(queue.size).to eq(2)
53
+ Timecop.return
54
+ end
55
+
56
+ end
57
+
58
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-ping
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Davide Cavestro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-15 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.0'
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.0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: logstash-codec-json
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ name: logstash-codec-plain
48
+ prerelease: false
49
+ type: :runtime
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: tzinfo
62
+ prerelease: false
63
+ type: :runtime
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ name: tzinfo-data
76
+ prerelease: false
77
+ type: :runtime
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ name: rufus-scheduler
90
+ prerelease: false
91
+ type: :runtime
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ name: net-ping
104
+ prerelease: false
105
+ type: :runtime
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 0.0.16
117
+ name: logstash-devutils
118
+ prerelease: false
119
+ type: :development
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 0.0.16
125
+ - !ruby/object:Gem::Dependency
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ name: timecop
132
+ prerelease: false
133
+ type: :development
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: This gem is a Logstash plugin required to be installed on top of the
140
+ Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
141
+ gem is not a stand-alone program
142
+ email: davide.cavestro@gmail.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - CHANGELOG.md
148
+ - CONTRIBUTORS
149
+ - DEVELOPER.md
150
+ - Gemfile
151
+ - LICENSE
152
+ - README.md
153
+ - lib/logstash/inputs/ping.rb
154
+ - logstash-input-ping.gemspec
155
+ - spec/inputs/ping_spec.rb
156
+ homepage: https://github.com/davidecavestro/logstash-input-ping
157
+ licenses:
158
+ - Apache-2.0
159
+ metadata:
160
+ logstash_plugin: 'true'
161
+ logstash_group: input
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubyforge_project:
178
+ rubygems_version: 2.6.13
179
+ signing_key:
180
+ specification_version: 4
181
+ summary: Provides ping data as input for Logstash
182
+ test_files:
183
+ - spec/inputs/ping_spec.rb