cw_ec2_cm 0.1.0

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
+ SHA1:
3
+ metadata.gz: af6413aa031e3836da1000b4bafde0b29b1b7bcd
4
+ data.tar.gz: aa9d924c1036d31a45e19a78c82fe46b40675641
5
+ SHA512:
6
+ metadata.gz: 4be10b2204bb22769c77cbf27b8aed2154d4ba2ed20c3d5d4be76a9f50268534e6cc32d99de7be3240bf9d3f6a1a42e3c983ecd264c6d5b037921b989afb45fa
7
+ data.tar.gz: 6fc30b865d09b75e5d84deb3bfcb788ccefda3fa45b424a366c1a0c52e858e953e5281d44142b9a8a93a9b7fedd820898b955e2fd925f298b8a4914fc9e5f4a5
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cw_ec2_cm.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 George Georgiev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # CwEc2Cm
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cw_ec2_cm`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cw_ec2_cm'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cw_ec2_cm
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cw_ec2_cm.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cw_ec2_cm"
5
+
6
+ CwEc2Cm.update_crontab('--update-crontab')
7
+
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cw_ec2_cm"
5
+
6
+ CwEc2Cm.update_crontab('--clear-crontab')
7
+
data/build.sh ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+ exec gem build cw_ec2_cm.gemspec
3
+
data/cw_ec2_cm.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cw_ec2_cm/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cw_ec2_cm"
8
+ spec.version = CwEc2Cm::VERSION
9
+ spec.authors = ["George Georgiev"]
10
+ spec.email = ["georgiev@heatbs.com"]
11
+
12
+ spec.summary = %q{AWS CloudWatch custom ec2 metrics}
13
+ spec.description = %q{Push instance memory and disk usage to EC2Custom CloudWatch namespace}
14
+ spec.homepage = "https://github.com/georgiev/cw_ec2_cm"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "bin"
21
+ spec.executables = %w(cw-ec2-cm-install cw-ec2-cm-uninstall)
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency 'whenever', '~> 0.9.7'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ end
@@ -0,0 +1,3 @@
1
+ module CwEc2Cm
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cw_ec2_cm.rb ADDED
@@ -0,0 +1,89 @@
1
+ require "cw_ec2_cm/version"
2
+ require 'tempfile'
3
+ require 'json'
4
+ require 'net/http'
5
+
6
+ module CwEc2Cm
7
+ WHENEVER_ID='cw-ec2-cm-schedule'
8
+ def update_crontab(update_cmd)
9
+ Tempfile.open(WHENEVER_ID) do |f|
10
+ f.write <<-SCHEDULE
11
+ every 1.minute do
12
+ runner "CwEc2Cm.push_metrics"
13
+ end
14
+ SCHEDULE
15
+ f.close
16
+ system('whenever', '--load-file', f.path, update_cmd, WHENEVER_ID)
17
+ end
18
+ end
19
+ module_function :update_crontab
20
+
21
+ def push_metrics
22
+ metadata_endpoint = 'http://169.254.169.254/latest/meta-data/'
23
+ instance_id = Net::HTTP.get( URI.parse( metadata_endpoint + 'instance-id' ) )
24
+
25
+ metric_data = []
26
+
27
+ # disk usage
28
+ df_fields = %w(fstype source size used avail pcent target)
29
+ metric_time = Time.now
30
+ df_data = `df -l -k --output=#{df_fields.join(',')}`.lines.collect do |l|
31
+ line_array = l.split
32
+ while line_array.size > df_fields.size do
33
+ line_array.last << ' ' << line_array.pop
34
+ end
35
+ {}.tap do |line_data|
36
+ df_fields.each_with_index do |fld, idx|
37
+ line_data[fld] = line_array[idx]
38
+ end
39
+ line_data['pcent'].gsub!('%', '')
40
+ end
41
+ end
42
+ df_data.shift #strip header line
43
+ df_data.reject!{|fs| /tmpfs/ =~ fs['fstype']} #strip tmpfs mounts
44
+
45
+ if worst_fs = df_data.max_by{ |fs| fs['pcent'].to_f }
46
+ metric_data << {
47
+ "MetricName": "FSUsagePercent",
48
+ "Dimensions": [{ "Name": "InstanceId", "Value": instance_id }],
49
+ "Timestamp": metric_time,
50
+ "Value": worst_fs['pcent'].to_f,
51
+ "Unit": "Percent" # accepts Seconds, Microseconds, Milliseconds, Bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, Bits, Kilobits, Megabits, Gigabits, Terabits, Percent, Count, Bytes/Second, Kilobytes/Second, Megabytes/Second, Gigabytes/Second, Terabytes/Second, Bits/Second, Kilobits/Second, Megabits/Second, Gigabits/Second, Terabits/Second, Count/Second, None
52
+ }
53
+ end
54
+
55
+ # mem usage
56
+ meminfo = {}
57
+ metric_time = Time.now
58
+ `cat /proc/meminfo`.each_line do |l|
59
+ line_array = l.split
60
+ line_array[0].gsub!(':', '')
61
+ meminfo[line_array[0]] = line_array[1].to_f * 1024
62
+ end
63
+ mem_total = meminfo['MemTotal']
64
+ mem_free = meminfo['MemFree'] + meminfo['Cached'] + meminfo['Buffers']
65
+ mem_used = mem_total - mem_free
66
+ mem_percent = (mem_used/mem_total*100).ceil
67
+ metric_data << {
68
+ "MetricName": "MemUsagePercent",
69
+ "Dimensions": [{ "Name": "InstanceId", "Value": instance_id }],
70
+ "Timestamp": metric_time,
71
+ "Value": mem_percent,
72
+ "Unit": "Percent" # accepts Seconds, Microseconds, Milliseconds, Bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, Bits, Kilobits, Megabits, Gigabits, Terabits, Percent, Count, Bytes/Second, Kilobytes/Second, Megabytes/Second, Gigabytes/Second, Terabytes/Second, Bits/Second, Kilobits/Second, Megabits/Second, Gigabits/Second, Terabits/Second, Count/Second, None
73
+ }
74
+ metric_data << {
75
+ "MetricName": "MemFree",
76
+ "Dimensions": [{ "Name": "InstanceId", "Value": instance_id }],
77
+ "Timestamp": metric_time,
78
+ "Value": mem_free/1024/1024,
79
+ "Unit": "Megabytes" # accepts Seconds, Microseconds, Milliseconds, Bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, Bits, Kilobits, Megabits, Gigabits, Terabits, Percent, Count, Bytes/Second, Kilobytes/Second, Megabytes/Second, Gigabytes/Second, Terabytes/Second, Bits/Second, Kilobits/Second, Megabits/Second, Gigabits/Second, Terabits/Second, Count/Second, None
80
+ }
81
+
82
+ Tempfile.open('cw-ec2-cm-metrics') do |f|
83
+ f.write(JSON.dump(metric_data))
84
+ f.close
85
+ `aws cloudwatch put-metric-data --namespace EC2Custom --metric-data file://#{f.path}`
86
+ end
87
+ end
88
+ module_function :push_metrics
89
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cw_ec2_cm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - George Georgiev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: whenever
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ description: Push instance memory and disk usage to EC2Custom CloudWatch namespace
42
+ email:
43
+ - georgiev@heatbs.com
44
+ executables:
45
+ - cw-ec2-cm-install
46
+ - cw-ec2-cm-uninstall
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - ".travis.yml"
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - bin/cw-ec2-cm-install
57
+ - bin/cw-ec2-cm-uninstall
58
+ - build.sh
59
+ - cw_ec2_cm.gemspec
60
+ - lib/cw_ec2_cm.rb
61
+ - lib/cw_ec2_cm/version.rb
62
+ homepage: https://github.com/georgiev/cw_ec2_cm
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.5.1
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: AWS CloudWatch custom ec2 metrics
86
+ test_files: []