logstash-input-sensorpush 0.1.1

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: 93e14f3ab39ab1b2c9d8bf1c3003fc94b6c3b32d023697ecaa5f32ba1ee3e08b
4
+ data.tar.gz: 0062dd8aa9bf3cfd75cbbc242ba4b8ae1e6413c419200615914b51eccd38be9e
5
+ SHA512:
6
+ metadata.gz: 0fc2571df4b07d594fd1e36012b9f3c9d55f4e66d77432b90a8b3a2b817da41fbec44fbd6a72f7840674f643b7e09da89f6ef98c8155b8d639c2418eeff30ed4
7
+ data.tar.gz: 2ef87c6f719618405dfa6bac52012f4c0636e809b7a7dc94c66cbe42abb78f2e5d482ca68a3fc7288f3d3700f1b915df491a52947c0a6c83f801ba5c62adec8a
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -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
+ * Matthew Haugen - mhaugen@haugenapplications.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.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-input-sensorpush
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.
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/main/CONTRIBUTING.md) file.
@@ -0,0 +1,195 @@
1
+ # encoding: utf-8
2
+ require "logstash/inputs/base"
3
+ require "stud/interval"
4
+ require "socket" # for Socket.gethostname
5
+
6
+ # Generate a repeating message.
7
+ #
8
+ # This plugin is intented only as an example.
9
+
10
+ class SensorPushApiConnection
11
+ def initialize(email, password, logger)
12
+ @session = Net::HTTP.start("api.sensorpush.com", 443, use_ssl: true)
13
+
14
+ @logger = logger
15
+ @email = email
16
+ @password = password
17
+
18
+ @token_hash = {
19
+ expires: 0
20
+ }
21
+ end
22
+
23
+ def get_token
24
+ now = Time.now.to_i
25
+
26
+ expires = @token_hash[:expires]
27
+
28
+ if expires < now
29
+ @logger.debug("Getting SensorPush API authorization token")
30
+
31
+ auth_req = Net::HTTP::Post.new("/api/v1/oauth/authorize")
32
+ auth_req.body = JSON.generate({
33
+ email: @email,
34
+ password: @password.value
35
+ })
36
+ auth_req["Accept"] = "application/json"
37
+ auth_req["Content-Type"] = "application/json"
38
+
39
+ auth_resp = @session.request(auth_req)
40
+
41
+ auth_resp_json = JSON.parse(auth_resp.body)
42
+
43
+
44
+ @logger.debug("Getting SensorPush API access token")
45
+
46
+ token_req = Net::HTTP::Post.new("/api/v1/oauth/accesstoken")
47
+ token_req.body = JSON.generate({
48
+ authorization: auth_resp_json["authorization"]
49
+ })
50
+ token_req["Accept"] = "application/json"
51
+ token_req["Content-Type"] = "application/json"
52
+
53
+ token_resp = @session.request(token_req)
54
+
55
+ token_resp_json = JSON.parse(token_resp.body)
56
+
57
+ @token_hash = {
58
+ expires: now + 120,
59
+ response: token_resp_json
60
+ }
61
+ end
62
+
63
+ return @token_hash[:response]["accesstoken"]
64
+ end
65
+
66
+ def send_request(req)
67
+ req["Accept"] = "application/json"
68
+ req["Authorization"] = self.get_token
69
+
70
+ resp = @session.request(req)
71
+
72
+ resp_json = JSON.parse(resp.body)
73
+
74
+ error = resp_json["error_description"]
75
+ if error
76
+ @logger.error("Error received from SensorPush API", :error => error)
77
+ end
78
+
79
+ return resp_json
80
+ end
81
+
82
+ def get_sensors_map
83
+ resp = self.send_request(Net::HTTP::Post.new("/api/v1/devices/sensors"))
84
+
85
+ return resp
86
+ end
87
+
88
+ def get_latest_values(sensor, to = nil, from = nil)
89
+ sensor_id = sensor["id"]
90
+
91
+ body = {
92
+ sensors: [sensor_id],
93
+ limit: 300
94
+ }
95
+
96
+ if !from.nil?
97
+ body["startTime"] = from.iso8601(3)
98
+ end
99
+
100
+ if !to.nil?
101
+ body["stopTime"] = to.iso8601(3)
102
+ end
103
+
104
+ req = Net::HTTP::Post.new("/api/v1/samples")
105
+ req.body = JSON.generate(body)
106
+ resp = self.send_request(req)
107
+
108
+ ret = resp["sensors"][sensor_id]
109
+
110
+ if ret.nil?
111
+ return []
112
+ end
113
+
114
+ return ret
115
+ end
116
+ end
117
+
118
+ class LogStash::Inputs::SensorPush < LogStash::Inputs::Base
119
+ config_name "sensorpush"
120
+
121
+ # If undefined, Logstash will complain, even if codec is unused.
122
+ default :codec, "plain"
123
+
124
+ # The message string to use in the event.
125
+ config :email, :validate => :string
126
+ config :password, :validate => :password
127
+
128
+ # Set how frequently messages should be sent.
129
+ #
130
+ # The default, `1`, means send a message every second.
131
+ config :interval, :validate => :number, :default => 10
132
+
133
+ public
134
+ def register
135
+ @logger.info("Connecting to SensorPush API", :email => @email)
136
+ @conn = SensorPushApiConnection.new(@email, @password, @logger)
137
+ end # def register
138
+
139
+ def run(queue)
140
+ # we can abort the loop if stop? becomes true
141
+ time_by_sensor = {}
142
+
143
+ while !stop?
144
+ now = DateTime.now
145
+
146
+ @logger.debug("Getting sensors from SensorPush API")
147
+ sensors = @conn.get_sensors_map
148
+
149
+ sensors.each_value { |sensor|
150
+ sensor_id = sensor["id"]
151
+
152
+ last_read = time_by_sensor[sensor_id]
153
+
154
+ @logger.debug("Getting latest values from SensorPush API", :sensor_id => sensor_id, :since => last_read)
155
+
156
+ if last_read
157
+ latest_values = @conn.get_latest_values(sensor, now, last_read)
158
+ else
159
+ latest_values = @conn.get_latest_values(sensor)
160
+ end
161
+
162
+ unless latest_values.empty?
163
+ time_by_sensor[sensor_id] = DateTime.parse(latest_values.map { |v| v["observed"] }.max)
164
+
165
+ latest_values.each { |reading|
166
+ timestamp = reading["observed"]
167
+
168
+ sensorpush = {
169
+ sensor: sensor,
170
+ reading: reading
171
+ }
172
+
173
+ event = LogStash::Event.new("@timestamp" => timestamp, "sensorpush" => sensorpush)
174
+ decorate(event)
175
+ queue << event
176
+ }
177
+ end
178
+ }
179
+
180
+ # because the sleep interval can be big, when shutdown happens
181
+ # we want to be able to abort the sleep
182
+ # Stud.stoppable_sleep will frequently evaluate the given block
183
+ # and abort the sleep(@interval) if the return value is true
184
+ Stud.stoppable_sleep(@interval) { stop? }
185
+ end # loop
186
+ end # def run
187
+
188
+ def stop
189
+ # nothing to do in this case so it is not necessary to define stop
190
+ # examples of common "stop" tasks:
191
+ # * close sockets (unblocking blocking reads/accepts)
192
+ # * cleanup temporary files
193
+ # * terminate spawned threads
194
+ end
195
+ end # class LogStash::Inputs::Airthings
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-sensorpush'
3
+ s.version = '0.1.1'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Connect to SensorPush API'
6
+ s.description = 'Connect to SensorPush API'
7
+ s.homepage = 'https://www.haugenapplications.com'
8
+ s.authors = ['Matthew Haugen']
9
+ s.email = 'mhaugen@haugenapplications.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-plain'
23
+ s.add_runtime_dependency 'stud', '>= 0.0.22'
24
+ s.add_development_dependency 'logstash-devutils', '>= 0.0.16'
25
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/inputs/sensorpush"
4
+
5
+ describe LogStash::Inputs::SensorPush 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,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-sensorpush
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Haugen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
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
+ name: logstash-codec-plain
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: stud
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.22
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.22
55
+ - !ruby/object:Gem::Dependency
56
+ name: logstash-devutils
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.16
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.16
69
+ description: Connect to SensorPush API
70
+ email: mhaugen@haugenapplications.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - CHANGELOG.md
76
+ - CONTRIBUTORS
77
+ - DEVELOPER.md
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - lib/logstash/inputs/sensorpush.rb
82
+ - logstash-input-sensorpush.gemspec
83
+ - spec/inputs/sensorpush_spec.rb
84
+ homepage: https://www.haugenapplications.com
85
+ licenses:
86
+ - Apache-2.0
87
+ metadata:
88
+ logstash_plugin: 'true'
89
+ logstash_group: input
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.0.3.1
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Connect to SensorPush API
109
+ test_files:
110
+ - spec/inputs/sensorpush_spec.rb