logstash-input-cloudwatch 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
+ SHA1:
3
+ metadata.gz: bd905939912c0289d1f422f637b80f0bd4abc723
4
+ data.tar.gz: 7da492dea138521b8b92e2756ffd3d4be2cfe615
5
+ SHA512:
6
+ metadata.gz: 09eadecfe87039ba40806927cd530e8a0c1f838e95b1d5ae3c69a784120173af75023c5f7436b5bceac6e91491dfe77018cd4c1d1f6706a609e473778c9ba350
7
+ data.tar.gz: 91f39d44ffb49e98a3ccfd855a5e01606728b9a0b4baa714ee23d6f0ce3e738853c1eaf51c10b8016329d4d355afd8bab2275c43e40f7741b42a0e2b9bc4cf12
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ Gemfile.bak
4
+ .bundle
5
+ vendor
File without changes
@@ -0,0 +1,2 @@
1
+ # logstash-input-example
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
+ ruby "1.9.3", :engine => "jruby", :engine_version => "1.7.19"
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elasticsearch/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.elasticsearch.org/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/elasticsearch/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/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/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/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rake"
@@ -0,0 +1,147 @@
1
+ # encoding: utf-8
2
+ require "logstash/inputs/base"
3
+ require "logstash/namespace"
4
+ require "logstash/timestamp"
5
+ require "logstash/util"
6
+ require "logstash/plugin_mixins/aws_config"
7
+ require "stud/interval"
8
+
9
+ # Pull events from the Amazon Web Services CloudWatch API.
10
+ #
11
+ # CloudWatch provides various metrics on EC2, EBS and SNS.
12
+ #
13
+ # To use this plugin, you *must* have an AWS account
14
+
15
+ class LogStash::Inputs::CloudWatch < LogStash::Inputs::Base
16
+ include LogStash::PluginMixins::AwsConfig
17
+
18
+ config_name "cloudwatch"
19
+
20
+ # If undefined, LogStash will complain, even if codec is unused.
21
+ default :codec, "json"
22
+
23
+ # Set how frequently CloudWatch should be queried
24
+ #
25
+ # The default, `900`, means check every 15 minutes
26
+ config :interval, :validate => :number, :default => (60 * 15)
27
+
28
+ # Set the granularity of the retruned datapoints.
29
+ #
30
+ # Must be at least 60 seconds and in multiples of 60.
31
+ config :period, :validate => :number, :default => 60
32
+
33
+ # The service namespace of the metrics to fetch.
34
+ #
35
+ # The default is for the EC2 service. Valid values are 'AWS/EC2', 'AWS/EBS' and
36
+ # 'AWS/SNS'.
37
+ config :namespace, :validate => :string, :default => 'AWS/EC2'
38
+
39
+ # The instances to check.
40
+ #
41
+ # Either specify specific instances using this setting, or use `tag_name` and
42
+ # `tag_values`. The `instances` setting takes precedence.
43
+ config :instances, :validate => :array
44
+
45
+ # Specify which tag to use when determining what instances to fetch metrics for
46
+ #
47
+ # You need to specify `tag_values` when using this setting. The `instances` setting
48
+ # takes precedence.
49
+ config :tag_name, :validate => :string
50
+
51
+ # Specify which tag value to check when determining what instances to fetch metrics for
52
+ #
53
+ # You need to specify `tag_name` when using this setting. The `instances` setting
54
+ # takes precedence.
55
+ config :tag_values, :validate => :array
56
+
57
+ # Specify the metrics to fetch for each instance
58
+ config :metrics, :validate => :array, :default => [ 'CPUUtilization', 'DiskReadOps', 'DiskWriteOps', 'NetworkIn', 'NetworkOut' ]
59
+
60
+ # Specify the statistics to fetch for each metric
61
+ config :statistics, :validate => :array, :default => [ 'SampleCount', 'Average', 'Minimum', 'Maximum', 'Sum' ]
62
+
63
+ public
64
+ def aws_service_endpoint(region)
65
+ { region: region }
66
+ end
67
+
68
+ public
69
+ def register
70
+ require "aws-sdk"
71
+ AWS.config(:logger => @logger)
72
+
73
+ @cloudwatch = AWS::CloudWatch::Client.new(aws_options_hash)
74
+ @ec2 = AWS::EC2::Client.new(aws_options_hash)
75
+
76
+ end # def register
77
+
78
+ def run(queue)
79
+ Stud.interval(@interval) do
80
+ @logger.debug('Polling CloudWatch API')
81
+ instance_ids.each do |instance|
82
+ metrics(instance).each do |metric|
83
+ opts = options(metric, instance)
84
+ @cloudwatch.get_metric_statistics(opts)[:datapoints].each do |dp|
85
+ event = LogStash::Event.new(LogStash::Util.stringify_symbols(dp))
86
+ event['@timestamp'] = LogStash::Timestamp.new(dp[:timestamp])
87
+ event['metric'] = metric
88
+ event['instance'] = instance
89
+ decorate(event)
90
+ queue << event
91
+ end
92
+ end
93
+ end
94
+ end # loop
95
+ end # def run
96
+
97
+ private
98
+ def options(metric, instance)
99
+ {
100
+ namespace: @namespace,
101
+ metric_name: metric,
102
+ dimensions: [ { name: 'InstanceId', value: instance } ],
103
+ start_time: (Time.now - @interval).iso8601,
104
+ end_time: Time.now.iso8601,
105
+ period: @period,
106
+ statistics: @statistics
107
+ }
108
+ end
109
+
110
+ private
111
+ def metrics(instance)
112
+ metrics_available(instance) & @metrics
113
+ end
114
+
115
+ private
116
+ def metrics_available(instance)
117
+ @metrics ||= Hash.new do |h, k|
118
+ opts = {
119
+ namespace: @namespace,
120
+ dimensions: [ { name: 'InstanceId', value: instance } ]
121
+ }
122
+
123
+ h[k] = []
124
+ @cloudwatch.list_metrics(opts)[:metrics].each do |metrics|
125
+ h[k].push metrics[:metric_name]
126
+ end
127
+ h[k]
128
+ end
129
+ end
130
+
131
+ private
132
+ def instance_ids
133
+ return @instances unless @instances.nil?
134
+
135
+ raise('Both the tag_name and tag_values needs to be set if no instances are specified') if @tag_name.nil? || @tag_values.nil?
136
+ @instances = []
137
+ @ec2.describe_instances(filters: [ { name: "tag:#{@tag_name}", values: @tag_values } ])[:reservation_set].each do |reservation|
138
+ @logger.debug reservation
139
+ reservation[:instances_set].each do |instance|
140
+ @instances.push instance[:instance_id]
141
+ end
142
+ end
143
+ @logger.debug 'Fetching metrics for the following instances', instances: @instances
144
+ @instances
145
+ end
146
+
147
+ end # class LogStash::Inputs::CloudWatch
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+ require "logstash/inputs/base"
3
+ require "logstash/namespace"
4
+ require "logstash/plugin_mixins/aws_config"
5
+ require 'pp'
6
+ require 'logger'
7
+
8
+ class LogStash::Inputs::Test < LogStash::Inputs::Base
9
+ include LogStash::PluginMixins::AwsConfig
10
+
11
+ config_name "cloudwatch"
12
+
13
+ # If undefined, Logstash will complain, even if codec is unused.
14
+ default :codec, "plain"
15
+
16
+ public
17
+ def aws_service_endpoint(region)
18
+ {
19
+ region: region
20
+ }
21
+ end
22
+
23
+ public
24
+ def register
25
+ require "aws-sdk"
26
+ AWS.config(:logger => Logger.new($stdout))
27
+ AWS.config(:log_level => :debug)
28
+
29
+ @interval = 60 * 15
30
+ @region = 'eu-west-1'
31
+ @namespace = 'AWS/EC2'
32
+ @metrics = [ 'CPUUtilization' ]
33
+ @period = 60
34
+ @statistics = ["SampleCount", "Average", "Minimum", "Maximum", "Sum"]
35
+ @tag_name = 'Managed'
36
+ @tag_values = [ 'Yes' ]
37
+
38
+ @cloudwatch = ::AWS::CloudWatch::Client.new(aws_options_hash)
39
+ @ec2 = ::AWS::EC2::Client.new(aws_options_hash)
40
+ end
41
+
42
+ def tryit
43
+ instance_ids.each do |instance|
44
+ metrics(instance).each do |metric|
45
+ opts = {
46
+ namespace: @namespace,
47
+ metric_name: metric,
48
+ dimensions: [ { name: 'InstanceId', value: instance } ],
49
+ start_time: (Time.now - @interval).iso8601,
50
+ end_time: Time.now.iso8601,
51
+ period: @period,
52
+ statistics: @statistics
53
+ }
54
+ puts "#{instance} #{metric}"
55
+ @cloudwatch.get_metric_statistics(opts)[:datapoints].each do |dp|
56
+ puts dp.inspect
57
+ # event = Logstash::Event.new(dp)
58
+ # puts event.inspect
59
+ # decorate(event)
60
+ # queue << event
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def metrics(instance)
67
+ metrics_available(instance) & @metrics
68
+ end
69
+
70
+ def instance_ids
71
+ @instances = []
72
+ @ec2.describe_instances(filters: [ { name: "tag:#{@tag_name}", values: @tag_values } ])[:reservation_set].each do |reservation|
73
+ reservation[:instances_set].each do |instance|
74
+ @instances.push instance[:instance_id]
75
+ end
76
+ end
77
+ @instances
78
+ end
79
+
80
+ def metrics_available(instance)
81
+ opts = { namespace: @namespace, dimensions: [ { name: 'InstanceId', value: instance } ] }
82
+ results = []
83
+ @cloudwatch.list_metrics(opts)[:metrics].each do |metrics|
84
+ results.push metrics[:metric_name]
85
+ end
86
+ results
87
+ end
88
+ end
89
+
90
+ puts "Starting up"
91
+ test = LogStash::Inputs::Test.new
92
+ puts "Registering"
93
+ test.register
94
+ puts "Get the instances"
95
+ test.tryit
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-cloudwatch'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "Retrieve stats from AWS CloudWatch."
6
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
7
+ s.authors = ["Jurgens du Toit"]
8
+ s.email = 'jrgns@jadeit.co.za'
9
+ s.homepage = "http://jadeit.co.za"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = `git ls-files`.split($\)
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", '>= 1.4.0', '< 2.0.0'
22
+ s.add_runtime_dependency 'logstash-codec-plain'
23
+ s.add_runtime_dependency 'stud'
24
+ s.add_runtime_dependency 'logstash-mixin-aws'
25
+ s.add_runtime_dependency 'aws-sdk'
26
+ s.add_development_dependency 'logstash-devutils'
27
+ end
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-cloudwatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jurgens du Toit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 1.4.0
28
+ - - <
29
+ - !ruby/object:Gem::Version
30
+ version: 2.0.0
31
+ prerelease: false
32
+ type: :runtime
33
+ - !ruby/object:Gem::Dependency
34
+ name: logstash-codec-plain
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ prerelease: false
46
+ type: :runtime
47
+ - !ruby/object:Gem::Dependency
48
+ name: stud
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ prerelease: false
60
+ type: :runtime
61
+ - !ruby/object:Gem::Dependency
62
+ name: logstash-mixin-aws
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ prerelease: false
74
+ type: :runtime
75
+ - !ruby/object:Gem::Dependency
76
+ name: aws-sdk
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ prerelease: false
88
+ type: :runtime
89
+ - !ruby/object:Gem::Dependency
90
+ name: logstash-devutils
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirement: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ prerelease: false
102
+ type: :development
103
+ description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
104
+ email: jrgns@jadeit.co.za
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - .gitignore
110
+ - CHANGELOG.md
111
+ - DEVELOPER.md
112
+ - Gemfile
113
+ - LICENSE
114
+ - README.md
115
+ - Rakefile
116
+ - lib/logstash/inputs/cloudwatch.rb
117
+ - lib/logstash/inputs/test.rb
118
+ - logstash-input-cloudwatch.gemspec
119
+ - spec/inputs/cloudwatch_spec.rb
120
+ homepage: http://jadeit.co.za
121
+ licenses:
122
+ - Apache License (2.0)
123
+ metadata:
124
+ logstash_plugin: 'true'
125
+ logstash_group: input
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.5
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Retrieve stats from AWS CloudWatch.
146
+ test_files:
147
+ - spec/inputs/cloudwatch_spec.rb