nephelae 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nephelae.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Stuart Eccles
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Nephelae
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nephelae'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nephelae
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'daemons'
4
+ require 'nephelae'
5
+ require 'optparse'
6
+
7
+ settings = {}
8
+
9
+ OptionParser.new do |o|
10
+ o.banner << " command"
11
+ o.on("-k", "--aws-access-key-id ACCESSKEY", "AWS Access Key to use for cloudwatch") do |v|
12
+ settings[:aws_access_key_id] = v
13
+ end
14
+ o.on("-s", "--aws-secret-access-key SECRETKEY", "AWS Secret Access Key to use for cloudwatch") do |v|
15
+ settings[:aws_secret_access_key] = v
16
+ end
17
+
18
+ end.parse!
19
+
20
+ new_argv = [ARGV.first]
21
+ Daemons.run_proc('nephelae', {ARGV: new_argv}) do
22
+ r = Nephelae::Runner.new(settings)
23
+ r.run
24
+ end
@@ -0,0 +1,15 @@
1
+ require "nephelae/version"
2
+ require "nephelae/cloud_watch/simple_aws"
3
+ require "nephelae/cloud_watch/cloud_watch"
4
+ require "nephelae/cloud_watch/metrics"
5
+
6
+ require "nephelae/plugins/disk_space"
7
+ require "nephelae/plugins/mem_usage"
8
+ require "nephelae/plugins/nephelae_process"
9
+ require "nephelae/plugins/passenger_status"
10
+
11
+ require "nephelae/runner"
12
+
13
+ module Nephelae
14
+ # Your code goes here...
15
+ end
@@ -0,0 +1,50 @@
1
+ module Nephelae
2
+ class CloudWatch
3
+
4
+ attr_accessor :aws_access_key_id, :aws_secret_access_key, :aws_session_token, :aws_credentials_expire_at, :url
5
+
6
+ def initialize(options={})
7
+ options[:region] ||= 'us-east-1'
8
+ @host = options[:host] || "monitoring.#{options[:region]}.amazonaws.com"
9
+ @path = options[:path] || '/'
10
+ @port = options[:port] || 443
11
+ @scheme = options[:scheme] || 'https'
12
+ @aws_access_key_id = options[:aws_access_key_id]
13
+ @aws_secret_access_key = options[:aws_secret_access_key]
14
+ @aws_session_token = options[:aws_session_token]
15
+ @aws_credentials_expire_at = options[:aws_credentials_expire_at]
16
+
17
+ @url = "#{@scheme}://#{@host}:#{@port}#{@path}"
18
+
19
+ end
20
+
21
+ def request(params)
22
+ body = AWS.signed_params(
23
+ params,
24
+ {
25
+ :aws_access_key_id => @aws_access_key_id,
26
+ :aws_session_token => @aws_session_token,
27
+ :aws_secret_access_key => @aws_secret_access_key,
28
+ :host => @host,
29
+ :path => @path,
30
+ :port => @port,
31
+ :version => '2010-08-01'
32
+ }
33
+ )
34
+
35
+ AWS.request(@url, {
36
+ :body => body,
37
+ :expects => 200,
38
+ :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
39
+ :host => @host,
40
+ :method => 'POST'
41
+ })
42
+ end
43
+
44
+ def put_metric(metric)
45
+ params = metric.params
46
+ request(metric.params) unless params.nil?
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,36 @@
1
+ module Nephelae
2
+ class Metrics
3
+ attr_accessor :params, :namespace, :instance_id
4
+
5
+ attr_reader :mcount
6
+
7
+ def initialize(namespace, options = {})
8
+ #code to get the instance id from ec2 metadata
9
+ @instance_id = options[:instance_id] || AWS.get_instance_id
10
+ @params = {}
11
+ @params['Action'] = 'PutMetricData'
12
+ @params['Namespace'] = namespace
13
+ @mcount = 0
14
+ end
15
+
16
+ def append_metric(name, value, options = {})
17
+ dim_count = 1
18
+ @mcount = @mcount + 1
19
+ params["MetricData.member.#{@mcount}.MetricName"] = name
20
+ params["MetricData.member.#{@mcount}.Value"] = value
21
+ params["MetricData.member.#{@mcount}.Unit"] = options[:unit] || 'None'
22
+ params["MetricData.member.#{@mcount}.Timestamp"] = options[:timestamp] if options[:timestamp]
23
+
24
+ if @instance_id
25
+ params["MetricData.member.#{@mcount}.Dimensions.member.#{dim_count}.Name"] = 'InstanceId'
26
+ params["MetricData.member.#{@mcount}.Dimensions.member.#{dim_count}.Value"] = @instance_id
27
+ end
28
+
29
+ end
30
+
31
+ def params
32
+ @mcount == 0 ? nil : @params
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,56 @@
1
+ require 'base64'
2
+ require 'excon'
3
+ require 'openssl'
4
+
5
+ module Nephelae
6
+ module AWS
7
+
8
+ def self.escape(string)
9
+ string.gsub(/([^a-zA-Z0-9_.\-~]+)/) {
10
+ "%" + $1.unpack("H2" * $1.bytesize).join("%").upcase
11
+ }
12
+ end
13
+
14
+ def self.hmac_sign(string_to_sign, key)
15
+ digest = OpenSSL::Digest::Digest.new('sha256')
16
+ OpenSSL::HMAC.digest(digest, key, string_to_sign)
17
+ end
18
+
19
+ def self.signed_params(params, options = {})
20
+ params.merge!({
21
+ 'AWSAccessKeyId' => options[:aws_access_key_id],
22
+ 'SignatureMethod' => 'HmacSHA256',
23
+ 'SignatureVersion' => '2',
24
+ 'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
25
+ 'Version' => options[:version]
26
+ })
27
+
28
+ params.merge!({
29
+ 'SecurityToken' => options[:aws_session_token]
30
+ }) if options[:aws_session_token]
31
+
32
+ body = ''
33
+ for key in params.keys.sort
34
+ unless (value = params[key]).nil?
35
+ body << "#{key}=#{escape(value.to_s)}&"
36
+ end
37
+ end
38
+ string_to_sign = "POST\n#{options[:host]}:#{options[:port]}\n#{options[:path]}\n" << body.chop
39
+ signed_string = hmac_sign(string_to_sign, options[:aws_secret_access_key])
40
+ body << "Signature=#{escape(Base64.encode64(signed_string).chomp!)}"
41
+
42
+ body
43
+ end
44
+
45
+ def self.request(url, params, &block)
46
+ excon = Excon.new(url, params)
47
+ excon.request(params, &block)
48
+ end
49
+
50
+ def self.get_instance_id
51
+ #Excon.get('http://169.254.169.254/latest/meta-data/instance-id').body
52
+ 'i-016c576f'
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,30 @@
1
+ module Nephelae
2
+
3
+ class DiskSpace
4
+ attr_accessor :path
5
+
6
+ def initialize(params = {})
7
+ @path = params[:path] || '/'
8
+ end
9
+
10
+ def command
11
+ "/bin/df -k -l -P #{@path}"
12
+ end
13
+
14
+ def get_metrics
15
+ metrics = Metrics.new('System/Linux')
16
+ output = `#{command}`
17
+ stats = output.split(/\n/).last.split(/\s+/)
18
+
19
+ percent = stats[4].chomp('%')
20
+ metrics.append_metric('DiskSpaceUtilization', percent, {unit: 'Percent'})
21
+ metrics.append_metric('DiskSpaceUsed', stats[2], {unit: 'Kilobytes'})
22
+ metrics.append_metric('DiskSpaceAvailable', stats[1], {unit: 'Kilobytes'})
23
+
24
+ return metrics
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,31 @@
1
+ require 'eventmachine'
2
+ require 'rufus/scheduler'
3
+ module Nephelae
4
+ class Runner
5
+
6
+ attr_accessor :aws_access_key_id, :aws_secret_access_key
7
+
8
+ def initialize(config = {})
9
+ @aws_access_key_id = config[:aws_access_key_id]
10
+ @aws_secret_access_key = config[:aws_secret_access_key]
11
+ end
12
+
13
+ def run
14
+
15
+ EM.run {
16
+ cloud = CloudWatch.new({aws_access_key_id: @aws_access_key_id, aws_secret_access_key: @aws_secret_access_key})
17
+
18
+ #make one request to put a cloud watch metric for nephelae being up. hopefully this can make it bork early if anything is wrong
19
+ cloud.put_metric(NephelaeProcess.new.get_metrics)
20
+
21
+ plugins = [DiskSpace.new, MemUsage.new, NephelaeProcess.new, PassengerStatus.new]
22
+ scheduler = Rufus::Scheduler::EmScheduler.start_new
23
+
24
+ scheduler.every '5m' do
25
+ plugins.each {|p| cloud.put_metric(p.get_metrics) }
26
+ end
27
+ }
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Nephelae
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/nephelae/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Stuart Eccles"]
6
+ gem.email = ["stuart@madebymany.co.uk"]
7
+ gem.description = %q{Nephelae is a daemon process that will upload custom cloud watch metrics in AWS}
8
+ gem.summary = %q{Poll for server metrics and post them to AWS as custom CloudWatch metrics}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "nephelae"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Nephelae::VERSION
17
+
18
+ gem.add_dependency('excon', '~>0.14')
19
+ gem.add_dependency('eventmachine')
20
+ gem.add_dependency('rufus-scheduler')
21
+ gem.add_dependency('daemons')
22
+ gem.add_dependency('ruby-hmac')
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nephelae
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stuart Eccles
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: excon
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.14'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.14'
30
+ - !ruby/object:Gem::Dependency
31
+ name: eventmachine
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rufus-scheduler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: daemons
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: ruby-hmac
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Nephelae is a daemon process that will upload custom cloud watch metrics
95
+ in AWS
96
+ email:
97
+ - stuart@madebymany.co.uk
98
+ executables:
99
+ - nephelae
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - .gitignore
104
+ - Gemfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - bin/nephelae
109
+ - lib/nephelae.rb
110
+ - lib/nephelae/cloud_watch/cloud_watch.rb
111
+ - lib/nephelae/cloud_watch/metrics.rb
112
+ - lib/nephelae/cloud_watch/simple_aws.rb
113
+ - lib/nephelae/plugins/disk_space.rb
114
+ - lib/nephelae/runner.rb
115
+ - lib/nephelae/version.rb
116
+ - nephelae.gemspec
117
+ homepage: ''
118
+ licenses: []
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 1.8.24
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: Poll for server metrics and post them to AWS as custom CloudWatch metrics
141
+ test_files: []