logstash-input-airthings 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: 56f55efa19883141d45444ffe73ec75843a87daf31ada2dccd0c50b0939ec513
4
+ data.tar.gz: dd14c9309040acb8d395968ed2a3b430ca8de78d95630c9350a699107f40e430
5
+ SHA512:
6
+ metadata.gz: 37e2832143c829773a0f846302708a1ce17f98edd208f26f93fddbceb0786146db4a94200248b617c0fbae1f34290a539fbd682a145c7d8c8ffa11c84e638696
7
+ data.tar.gz: b11fb6c327ae9c5fd488b3989de00d45418e61ab4c7815e44383b5653e2579362a07dbe9937d2c5efb7b44215ded8e8ba15e30b01c7d1704fecc0f7b6830219a
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-airthings
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,173 @@
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 AirThingsApiConnection
11
+ def initialize(client_id, client_secret, logger)
12
+ @session = Net::HTTP.start("ext-api.airthings.com", 443, use_ssl: true)
13
+
14
+ @logger = logger
15
+ @client_id = client_id
16
+ @client_secret = client_secret
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 new Airthings API access token")
30
+
31
+ req = Net::HTTP::Post.new("https://accounts-api.airthings.com/v1/token")
32
+ req.body = JSON.generate({
33
+ grant_type: "client_credentials",
34
+ client_id: @client_id,
35
+ client_secret: @client_secret.value,
36
+ scope: [ "read:device:current_values" ]
37
+ })
38
+ req["Content-Type"] = "application/json"
39
+
40
+ resp = @session.request(req)
41
+
42
+ resp_json = JSON.parse(resp.body)
43
+
44
+ @token_hash = {
45
+ expires: now + resp_json["expires_in"] - 60,
46
+ response: resp_json
47
+ }
48
+ end
49
+
50
+ return @token_hash[:response]["access_token"]
51
+ end
52
+
53
+ def send_request(method_class, path)
54
+ unless path.start_with?("/")
55
+ path = "/" + path
56
+ end
57
+
58
+ req = method_class.new("https://ext-api.airthings.com" + path)
59
+ req["Accept"] = "application/json"
60
+ req["Authorization"] = "Bearer " + self.get_token
61
+
62
+ resp = @session.request(req)
63
+
64
+ resp_json = JSON.parse(resp.body)
65
+
66
+ error = resp_json["error_description"]
67
+ if error
68
+ @logger.error("Error received from Airthings API", :error => error)
69
+ end
70
+
71
+ return resp_json
72
+ end
73
+
74
+ def get_devices
75
+ resp = self.send_request(Net::HTTP::Get, "/v1/devices")
76
+
77
+ return resp["devices"]
78
+ end
79
+
80
+ def get_latest_values(device_id)
81
+ resp = self.send_request(Net::HTTP::Get, "/v1/devices/#{device_id}/latest-samples")
82
+
83
+ return resp["data"]
84
+ end
85
+ end
86
+
87
+ class LogStash::Inputs::Airthings < LogStash::Inputs::Base
88
+ config_name "airthings"
89
+
90
+ # If undefined, Logstash will complain, even if codec is unused.
91
+ default :codec, "plain"
92
+
93
+ # The message string to use in the event.
94
+ config :client_id, :validate => :string
95
+ config :client_secret, :validate => :password
96
+
97
+ # Set how frequently messages should be sent.
98
+ #
99
+ # The default, `1`, means send a message every second.
100
+ config :interval, :validate => :number, :default => 60
101
+
102
+ config :device_list_interval, :validate => :number, :default => 600
103
+
104
+ public
105
+ def register
106
+ @logger.info("Connecting to Airthings API", :client_id => @client_id)
107
+ @conn = AirThingsApiConnection.new(@client_id, @client_secret, @logger)
108
+ end # def register
109
+
110
+ def run(queue)
111
+ # we can abort the loop if stop? becomes true
112
+ reload_devices_at = 0
113
+ devices = []
114
+ time_by_device = {}
115
+
116
+ while !stop?
117
+ now = Time.now.to_i
118
+
119
+ if !devices || now >= reload_devices_at
120
+ @logger.info("Getting devices from Airthings API")
121
+ devices = @conn.get_devices
122
+
123
+ unless devices
124
+ devices = []
125
+ end
126
+
127
+ reload_devices_at = now + @device_list_interval
128
+ end
129
+
130
+ devices.each { |device|
131
+ device_id = device["id"]
132
+
133
+ @logger.debug("Getting latest values from Airthings API", :device_id => device_id)
134
+ latest_values = @conn.get_latest_values(device_id)
135
+
136
+ time = latest_values.delete("time")
137
+
138
+ unless time_by_device[device_id] == time
139
+ time_by_device[device_id] = time
140
+
141
+ airthings = {
142
+ device: {
143
+ id: device_id,
144
+ type: device["deviceType"],
145
+ segment: device["segment"],
146
+ location: device["location"]
147
+ },
148
+ time: time,
149
+ metrics: latest_values
150
+ }
151
+
152
+ event = LogStash::Event.new("airthings" => airthings)
153
+ decorate(event)
154
+ queue << event
155
+ end
156
+ }
157
+
158
+ # because the sleep interval can be big, when shutdown happens
159
+ # we want to be able to abort the sleep
160
+ # Stud.stoppable_sleep will frequently evaluate the given block
161
+ # and abort the sleep(@interval) if the return value is true
162
+ Stud.stoppable_sleep(@interval) { stop? }
163
+ end # loop
164
+ end # def run
165
+
166
+ def stop
167
+ # nothing to do in this case so it is not necessary to define stop
168
+ # examples of common "stop" tasks:
169
+ # * close sockets (unblocking blocking reads/accepts)
170
+ # * cleanup temporary files
171
+ # * terminate spawned threads
172
+ end
173
+ end # class LogStash::Inputs::Airthings
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-airthings'
3
+ s.version = '0.1.1'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Connect to AirThings API'
6
+ s.description = 'Connect to AirThings 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/airthings"
4
+
5
+ describe LogStash::Inputs::Airthings 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-airthings
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 AirThings 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/airthings.rb
82
+ - logstash-input-airthings.gemspec
83
+ - spec/inputs/airthings_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 AirThings API
109
+ test_files:
110
+ - spec/inputs/airthings_spec.rb