cloudwatch-metrics-linux 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +72 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +4 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/cloudwatch-metrics-linux.gemspec +31 -0
- data/docker/.gitignore +1 -0
- data/docker/Dockerfile +15 -0
- data/docker/Gemfile +5 -0
- data/docker/Gemfile.lock +19 -0
- data/exe/cloudwatch-metrics-linux +7 -0
- data/lib/cloud_watch_metrics.rb +8 -0
- data/lib/cloud_watch_metrics/base.rb +27 -0
- data/lib/cloud_watch_metrics/dimensions.rb +14 -0
- data/lib/cloud_watch_metrics/linux.rb +59 -0
- data/lib/cloud_watch_metrics/linux/builder.rb +77 -0
- data/lib/cloud_watch_metrics/linux/loadavg.rb +28 -0
- data/lib/cloud_watch_metrics/linux/meminfo.rb +68 -0
- data/lib/cloud_watch_metrics/linux/version.rb +7 -0
- data/lib/cloud_watch_metrics/meta_data.rb +13 -0
- data/lib/cloud_watch_metrics/util.rb +54 -0
- data/lib/cloudwatch/metrics/linux.rb +3 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e9a200fc1d494bbe9d953c572647db72663a910d
|
4
|
+
data.tar.gz: fdef96b0ddba387525294b9b5560220e1dadd826
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40a73c129523bda8609620705d9c06f8d4abbbe8226c6fb4ba0ac32860c329d4beb3657b5f6f027d1f4bf2ce6c8929ca16f03ca7d33c9c3461a3e4d231fbdc4a
|
7
|
+
data.tar.gz: f3379a74ad2f791d0776a178e232f52a16653f22f714af67f307209ecfc74d6e6813b0efb2ad88a0f548e09d4b0fc4bca755f6dab5c237d12c585e1a89999198
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
|
4
|
+
### customize
|
5
|
+
|
6
|
+
Metrics/ParameterLists:
|
7
|
+
CountKeywordArgs: false
|
8
|
+
|
9
|
+
Style/Lambda:
|
10
|
+
EnforcedStyle: literal
|
11
|
+
|
12
|
+
Style/LambdaCall:
|
13
|
+
EnforcedStyle: braces
|
14
|
+
|
15
|
+
Style/MultilineMethodCallIndentation:
|
16
|
+
EnforcedStyle: indented
|
17
|
+
|
18
|
+
Style/TrailingCommaInArguments:
|
19
|
+
EnforcedStyleForMultiline: comma
|
20
|
+
|
21
|
+
Style/TrailingCommaInLiteral:
|
22
|
+
EnforcedStyleForMultiline: comma
|
23
|
+
|
24
|
+
### disabled
|
25
|
+
|
26
|
+
Style/Documentation:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
### enabled
|
30
|
+
|
31
|
+
Style/AutoResourceCleanup:
|
32
|
+
Enabled: true
|
33
|
+
|
34
|
+
Style/CollectionMethods:
|
35
|
+
Enabled: true
|
36
|
+
|
37
|
+
Style/Encoding:
|
38
|
+
Enabled: true
|
39
|
+
|
40
|
+
Style/FirstArrayElementLineBreak:
|
41
|
+
Enabled: true
|
42
|
+
|
43
|
+
Style/FirstHashElementLineBreak:
|
44
|
+
Enabled: true
|
45
|
+
|
46
|
+
Style/FirstMethodArgumentLineBreak:
|
47
|
+
Enabled: true
|
48
|
+
|
49
|
+
Style/FirstMethodParameterLineBreak:
|
50
|
+
Enabled: true
|
51
|
+
|
52
|
+
Style/ImplicitRuntimeError:
|
53
|
+
Enabled: true
|
54
|
+
|
55
|
+
Style/InlineComment:
|
56
|
+
Enabled: true
|
57
|
+
|
58
|
+
Style/MultilineAssignmentLayout:
|
59
|
+
Enabled: true
|
60
|
+
EnforcedStyle: same_line
|
61
|
+
|
62
|
+
Style/OptionHash:
|
63
|
+
Enabled: true
|
64
|
+
|
65
|
+
Style/Send:
|
66
|
+
Enabled: true
|
67
|
+
|
68
|
+
Style/StringMethods:
|
69
|
+
Enabled: true
|
70
|
+
|
71
|
+
Style/SymbolArray:
|
72
|
+
Enabled: true
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at m.ishihara@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Masaki Takeuchi
|
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,51 @@
|
|
1
|
+
# CloudWatchMetrics::Linux
|
2
|
+
|
3
|
+
Send meminfo and loadavg to CloudWatch Metrics
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/cloudwatch-metrics-linux.svg)](https://badge.fury.io/rb/cloudwatch-metrics-linux)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/m4i/cloudwatch-metrics-linux/badges/gpa.svg)](https://codeclimate.com/github/m4i/cloudwatch-metrics-linux)
|
7
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/m4i/cloudwatch-metrics-linux.svg)](https://gemnasium.com/github.com/m4i/cloudwatch-metrics-linux)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'cloudwatch-metrics-linux'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install cloudwatch-metrics-linux
|
24
|
+
|
25
|
+
Or use Docker:
|
26
|
+
|
27
|
+
$ docker run --rm m4i0/cloudwatch-metrics-linux --help
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```
|
32
|
+
Usage: cloudwatch-metrics-linux [options]
|
33
|
+
--namespace <namespace>
|
34
|
+
--dimensions <name1=value1,name2=value2,...>
|
35
|
+
--interval <seconds>
|
36
|
+
--dryrun
|
37
|
+
```
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
42
|
+
|
43
|
+
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).
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/m4i/cloudwatch-metrics-linux. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'cloudwatch/metrics/linux'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'cloud_watch_metrics/linux/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'cloudwatch-metrics-linux'
|
9
|
+
spec.version = CloudWatchMetrics::Linux::VERSION
|
10
|
+
spec.authors = ['Masaki Takeuchi']
|
11
|
+
spec.email = ['m.ishihara@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Send meminfo and loadavg to CloudWatch Metrics'
|
14
|
+
spec.homepage = 'https://github.com/m4i/cloudwatch-metrics-linux'
|
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 = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.required_ruby_version = '>= 2.4'
|
25
|
+
|
26
|
+
spec.add_dependency 'aws-sdk-core', '~> 2'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
29
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
30
|
+
spec.add_development_dependency 'rubocop', '>= 0.46'
|
31
|
+
end
|
data/docker/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/.bundle/
|
data/docker/Dockerfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
FROM ruby:2.4-alpine
|
2
|
+
|
3
|
+
RUN set -eux \
|
4
|
+
&& apk add --no-cache \
|
5
|
+
openssl \
|
6
|
+
# dumb-init
|
7
|
+
&& VERSION=1.2.0 \
|
8
|
+
&& wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v$VERSION/dumb-init_${VERSION}_amd64 \
|
9
|
+
&& chmod +x /usr/local/bin/dumb-init
|
10
|
+
|
11
|
+
ENTRYPOINT ["dumb-init", "cloudwatch-metrics-linux"]
|
12
|
+
WORKDIR /app
|
13
|
+
|
14
|
+
COPY Gemfile Gemfile.lock /app/
|
15
|
+
RUN bundle install -j4 --frozen
|
data/docker/Gemfile
ADDED
data/docker/Gemfile.lock
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
aws-sdk-core (2.6.44)
|
5
|
+
aws-sigv4 (~> 1.0)
|
6
|
+
jmespath (~> 1.0)
|
7
|
+
aws-sigv4 (1.0.0)
|
8
|
+
cloudwatch-metrics-linux (0.1.0)
|
9
|
+
aws-sdk-core (~> 2)
|
10
|
+
jmespath (1.3.1)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
cloudwatch-metrics-linux
|
17
|
+
|
18
|
+
BUNDLED WITH
|
19
|
+
1.13.7
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CloudWatchMetrics
|
4
|
+
module Base
|
5
|
+
def self.included(mod)
|
6
|
+
mod.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def run(args)
|
11
|
+
new(parse_arguments(args)).run
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def run
|
16
|
+
if @interval
|
17
|
+
loop do
|
18
|
+
start = Time.now
|
19
|
+
Timeout.timeout(@interval * 2 - 1) { run_once }
|
20
|
+
sleep((start - Time.now) % @interval)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
run_once
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'irb'
|
5
|
+
|
6
|
+
require 'cloud_watch_metrics'
|
7
|
+
require 'cloud_watch_metrics/linux/builder'
|
8
|
+
require 'cloud_watch_metrics/linux/loadavg'
|
9
|
+
require 'cloud_watch_metrics/linux/meminfo'
|
10
|
+
require 'cloud_watch_metrics/linux/version'
|
11
|
+
|
12
|
+
module CloudWatchMetrics
|
13
|
+
class Linux
|
14
|
+
include Base
|
15
|
+
|
16
|
+
DEFAULT_NAMESPACE = 'System/Linux'
|
17
|
+
|
18
|
+
class << self
|
19
|
+
private
|
20
|
+
|
21
|
+
def parse_arguments(args)
|
22
|
+
{}.tap { |options| option_parser.parse(args, into: options) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def option_parser
|
26
|
+
OptionParser.new do |opt|
|
27
|
+
Util.accept_hash(opt)
|
28
|
+
opt.on('--namespace <namespace>', String)
|
29
|
+
opt.on('--dimensions <name1=value1,name2=value2,...>', Hash)
|
30
|
+
opt.on('--interval <seconds>', Float)
|
31
|
+
opt.on('--dryrun', TrueClass)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(
|
37
|
+
namespace: DEFAULT_NAMESPACE,
|
38
|
+
dimensions: {},
|
39
|
+
interval: nil,
|
40
|
+
dryrun: false
|
41
|
+
)
|
42
|
+
@namespace = namespace
|
43
|
+
@dimensions = dimensions
|
44
|
+
@interval = interval
|
45
|
+
@dryrun = dryrun
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def run_once
|
51
|
+
metric_data = builder.build(MemInfo.new, LoadAvg.new)
|
52
|
+
Util.put_metric_data(@namespace, metric_data, dryrun: @dryrun)
|
53
|
+
end
|
54
|
+
|
55
|
+
def builder
|
56
|
+
@_builder ||= Builder.new(@dimensions)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CloudWatchMetrics
|
4
|
+
class Linux
|
5
|
+
class Builder
|
6
|
+
def initialize(dimensions)
|
7
|
+
@dimensions_array = [Dimensions.new(InstanceId: MetaData.instance_id)]
|
8
|
+
@dimensions_array << Dimensions.new(dimensions) unless dimensions.empty?
|
9
|
+
end
|
10
|
+
|
11
|
+
def build(meminfo, loadavg)
|
12
|
+
build_without_dimensions(meminfo, loadavg).flat_map do |metric_datum|
|
13
|
+
@dimensions_array.map do |dimensions|
|
14
|
+
metric_datum.merge(dimensions: dimensions.to_cloudwatch)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def build_without_dimensions(meminfo, loadavg)
|
22
|
+
metric_data = [memory_used(meminfo), memory_utilization(meminfo)]
|
23
|
+
|
24
|
+
if meminfo.swap?
|
25
|
+
metric_data << swap_used(meminfo) << swap_utilization(meminfo)
|
26
|
+
end
|
27
|
+
|
28
|
+
metric_data << load_average_1min(loadavg)
|
29
|
+
end
|
30
|
+
|
31
|
+
def memory_used(meminfo)
|
32
|
+
{
|
33
|
+
metric_name: 'MemoryUsed',
|
34
|
+
timestamp: meminfo.time,
|
35
|
+
value: meminfo.mem_used / 1024,
|
36
|
+
unit: 'Megabytes',
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def memory_utilization(meminfo)
|
41
|
+
{
|
42
|
+
metric_name: 'MemoryUtilization',
|
43
|
+
timestamp: meminfo.time,
|
44
|
+
value: (meminfo.mem_util * 100).round(3),
|
45
|
+
unit: 'Percent',
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def swap_used(meminfo)
|
50
|
+
{
|
51
|
+
metric_name: 'SwapUsed',
|
52
|
+
timestamp: meminfo.time,
|
53
|
+
value: meminfo.swap_used / 1024,
|
54
|
+
unit: 'Megabytes',
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def swap_utilization(meminfo)
|
59
|
+
{
|
60
|
+
metric_name: 'SwapUtilization',
|
61
|
+
timestamp: meminfo.time,
|
62
|
+
value: (meminfo.swap_util * 100).round(3),
|
63
|
+
unit: 'Percent',
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
def load_average_1min(loadavg)
|
68
|
+
{
|
69
|
+
metric_name: 'LoadAverage1Min',
|
70
|
+
timestamp: loadavg.time,
|
71
|
+
value: loadavg.loadavg1,
|
72
|
+
unit: 'Count',
|
73
|
+
}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CloudWatchMetrics
|
4
|
+
class Linux
|
5
|
+
class LoadAvg
|
6
|
+
PATH = '/proc/loadavg'
|
7
|
+
|
8
|
+
attr_reader :time
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@time = Time.now
|
12
|
+
@data = File.read(PATH).split(/\s+/)
|
13
|
+
end
|
14
|
+
|
15
|
+
def loadavg1
|
16
|
+
@data.fetch(0).to_f
|
17
|
+
end
|
18
|
+
|
19
|
+
def loadavg5
|
20
|
+
@data.fetch(1).to_f
|
21
|
+
end
|
22
|
+
|
23
|
+
def loadavg15
|
24
|
+
@data.fetch(2).to_f
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CloudWatchMetrics
|
4
|
+
class Linux
|
5
|
+
class MemInfo
|
6
|
+
PATH = '/proc/meminfo'
|
7
|
+
|
8
|
+
attr_reader :time
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@time = Time.now
|
12
|
+
@data = File.readlines(PATH)
|
13
|
+
.map { |line| /^([^:]+):\s+(\d+)/.match(line)&.captures }
|
14
|
+
.compact
|
15
|
+
.map { |key, value| [key, value.to_i] }
|
16
|
+
.to_h
|
17
|
+
end
|
18
|
+
|
19
|
+
def mem_total
|
20
|
+
@data.fetch('MemTotal')
|
21
|
+
end
|
22
|
+
|
23
|
+
def mem_free
|
24
|
+
@data.fetch('MemFree')
|
25
|
+
end
|
26
|
+
|
27
|
+
def cached
|
28
|
+
@data.fetch('Cached')
|
29
|
+
end
|
30
|
+
|
31
|
+
def buffers
|
32
|
+
@data.fetch('Buffers')
|
33
|
+
end
|
34
|
+
|
35
|
+
def swap_total
|
36
|
+
@data.fetch('SwapTotal')
|
37
|
+
end
|
38
|
+
|
39
|
+
def swap_free
|
40
|
+
@data.fetch('SwapFree')
|
41
|
+
end
|
42
|
+
|
43
|
+
def mem_avail
|
44
|
+
mem_free + cached + buffers
|
45
|
+
end
|
46
|
+
|
47
|
+
def mem_used
|
48
|
+
mem_total - mem_avail
|
49
|
+
end
|
50
|
+
|
51
|
+
def mem_util
|
52
|
+
mem_used.to_f / mem_total
|
53
|
+
end
|
54
|
+
|
55
|
+
def swap?
|
56
|
+
swap_total.positive?
|
57
|
+
end
|
58
|
+
|
59
|
+
def swap_used
|
60
|
+
swap_total - swap_free
|
61
|
+
end
|
62
|
+
|
63
|
+
def swap_util
|
64
|
+
swap_used.to_f / swap_total
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'aws-sdk-core'
|
6
|
+
|
7
|
+
module CloudWatchMetrics
|
8
|
+
module Util
|
9
|
+
# http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html
|
10
|
+
MAX_METRIC_DATA_PER_PUT = 20
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def accept_hash(option_parser)
|
14
|
+
option_parser.accept(Hash) do |s,|
|
15
|
+
break s unless s
|
16
|
+
s
|
17
|
+
.split(',').reject(&:empty?)
|
18
|
+
.map { |kv| kv.include?('=') ? kv.split('=', 2) : [kv, true] }
|
19
|
+
.to_h
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [void]
|
24
|
+
def put_metric_data(namespace, metric_data, dryrun: false)
|
25
|
+
return dump_metric_data(namespace, metric_data) if dryrun
|
26
|
+
|
27
|
+
metric_data.each_slice(MAX_METRIC_DATA_PER_PUT).map do |data|
|
28
|
+
Thread.start(data, cloudwatch) do |data_, cloudwatch_|
|
29
|
+
cloudwatch_.put_metric_data(
|
30
|
+
namespace: namespace,
|
31
|
+
metric_data: data_,
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end.each(&:join)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def cloudwatch
|
40
|
+
@_cloudwatch ||= Aws::CloudWatch::Client.new
|
41
|
+
end
|
42
|
+
|
43
|
+
def dump_metric_data(namespace, metric_data)
|
44
|
+
json = { namespace: namespace, metric_data: metric_data }.to_json(
|
45
|
+
indent: ' ' * 2,
|
46
|
+
space: ' ',
|
47
|
+
object_nl: "\n",
|
48
|
+
array_nl: "\n",
|
49
|
+
)
|
50
|
+
puts json
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloudwatch-metrics-linux
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masaki Takeuchi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2'
|
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.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '12.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '12.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.46'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.46'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- m.ishihara@gmail.com
|
72
|
+
executables:
|
73
|
+
- cloudwatch-metrics-linux
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- cloudwatch-metrics-linux.gemspec
|
87
|
+
- docker/.gitignore
|
88
|
+
- docker/Dockerfile
|
89
|
+
- docker/Gemfile
|
90
|
+
- docker/Gemfile.lock
|
91
|
+
- exe/cloudwatch-metrics-linux
|
92
|
+
- lib/cloud_watch_metrics.rb
|
93
|
+
- lib/cloud_watch_metrics/base.rb
|
94
|
+
- lib/cloud_watch_metrics/dimensions.rb
|
95
|
+
- lib/cloud_watch_metrics/linux.rb
|
96
|
+
- lib/cloud_watch_metrics/linux/builder.rb
|
97
|
+
- lib/cloud_watch_metrics/linux/loadavg.rb
|
98
|
+
- lib/cloud_watch_metrics/linux/meminfo.rb
|
99
|
+
- lib/cloud_watch_metrics/linux/version.rb
|
100
|
+
- lib/cloud_watch_metrics/meta_data.rb
|
101
|
+
- lib/cloud_watch_metrics/util.rb
|
102
|
+
- lib/cloudwatch/metrics/linux.rb
|
103
|
+
homepage: https://github.com/m4i/cloudwatch-metrics-linux
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '2.4'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.6.8
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Send meminfo and loadavg to CloudWatch Metrics
|
127
|
+
test_files: []
|