cloudwatch-metrics-linux 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9a200fc1d494bbe9d953c572647db72663a910d
4
- data.tar.gz: fdef96b0ddba387525294b9b5560220e1dadd826
3
+ metadata.gz: 6594499ef6096ad115765e158314e3a08335c617
4
+ data.tar.gz: 3511cb052b53c85e6c11365211a475cc5d4dea85
5
5
  SHA512:
6
- metadata.gz: 40a73c129523bda8609620705d9c06f8d4abbbe8226c6fb4ba0ac32860c329d4beb3657b5f6f027d1f4bf2ce6c8929ca16f03ca7d33c9c3461a3e4d231fbdc4a
7
- data.tar.gz: f3379a74ad2f791d0776a178e232f52a16653f22f714af67f307209ecfc74d6e6813b0efb2ad88a0f548e09d4b0fc4bca755f6dab5c237d12c585e1a89999198
6
+ metadata.gz: 6fddf3eea38c07fcbec608deafca3ffa0ceaa8f1d65d7b63eed7322236b9af08d0e753f9b07976630662a05acdf44cdd7f43a764f674778badd1988682a953ca
7
+ data.tar.gz: 85415ec8297e2eebe4a73ee4f57758cdf40dd1756705c5764f0c0b612b25386f5824172f9d8bc4a87e80c01aa46786187e29e1ebce7db4ae057b4e9d4e352b07
data/README.md CHANGED
@@ -32,8 +32,15 @@ Or use Docker:
32
32
  Usage: cloudwatch-metrics-linux [options]
33
33
  --namespace <namespace>
34
34
  --dimensions <name1=value1,name2=value2,...>
35
+ --[no-]memory-used
36
+ --[no-]memory-utilization
37
+ --[no-]swap-used
38
+ --[no-]swap-utilization
39
+ --[no-]load-average-1min
40
+ --[no-]load-average-5min
41
+ --[no-]load-average-15min
35
42
  --interval <seconds>
36
- --dryrun
43
+ --dry-run
37
44
  ```
38
45
 
39
46
  ## Development
@@ -5,7 +5,7 @@ GEM
5
5
  aws-sigv4 (~> 1.0)
6
6
  jmespath (~> 1.0)
7
7
  aws-sigv4 (1.0.0)
8
- cloudwatch-metrics-linux (0.1.0)
8
+ cloudwatch-metrics-linux (0.2.0)
9
9
  aws-sdk-core (~> 2)
10
10
  jmespath (1.3.1)
11
11
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'timeout'
4
+
3
5
  module CloudWatchMetrics
4
6
  module Base
5
7
  def self.included(mod)
@@ -14,12 +14,25 @@ module CloudWatchMetrics
14
14
  include Base
15
15
 
16
16
  DEFAULT_NAMESPACE = 'System/Linux'
17
+ DEFAULT_METRICS = {
18
+ memory_used: true,
19
+ memory_utilization: true,
20
+ swap_used: true,
21
+ swap_utilization: true,
22
+ load_average_1min: false,
23
+ load_average_5min: true,
24
+ load_average_15min: false,
25
+ }.freeze
17
26
 
18
27
  class << self
19
28
  private
20
29
 
21
30
  def parse_arguments(args)
22
- {}.tap { |options| option_parser.parse(args, into: options) }
31
+ {}.tap do |options|
32
+ option_parser.parse(args, into: options)
33
+ Util.convert_symbol_keys_from_dash_to_underscore!(options)
34
+ options[:metrics] = Util.delete_keys!(options, DEFAULT_METRICS.keys)
35
+ end
23
36
  end
24
37
 
25
38
  def option_parser
@@ -27,8 +40,13 @@ module CloudWatchMetrics
27
40
  Util.accept_hash(opt)
28
41
  opt.on('--namespace <namespace>', String)
29
42
  opt.on('--dimensions <name1=value1,name2=value2,...>', Hash)
43
+
44
+ DEFAULT_METRICS.each_key do |key|
45
+ opt.on("--[no-]#{key.to_s.tr('_', '-')}", TrueClass)
46
+ end
47
+
30
48
  opt.on('--interval <seconds>', Float)
31
- opt.on('--dryrun', TrueClass)
49
+ opt.on('--dry-run', TrueClass)
32
50
  end
33
51
  end
34
52
  end
@@ -36,24 +54,26 @@ module CloudWatchMetrics
36
54
  def initialize(
37
55
  namespace: DEFAULT_NAMESPACE,
38
56
  dimensions: {},
57
+ metrics: {},
39
58
  interval: nil,
40
- dryrun: false
59
+ dry_run: false
41
60
  )
42
61
  @namespace = namespace
43
62
  @dimensions = dimensions
63
+ @metrics = DEFAULT_METRICS.merge(metrics)
44
64
  @interval = interval
45
- @dryrun = dryrun
65
+ @dry_run = dry_run
46
66
  end
47
67
 
48
68
  private
49
69
 
50
70
  def run_once
51
71
  metric_data = builder.build(MemInfo.new, LoadAvg.new)
52
- Util.put_metric_data(@namespace, metric_data, dryrun: @dryrun)
72
+ Util.put_metric_data(@namespace, metric_data, dry_run: @dry_run)
53
73
  end
54
74
 
55
75
  def builder
56
- @_builder ||= Builder.new(@dimensions)
76
+ @_builder ||= Builder.new(@dimensions, @metrics)
57
77
  end
58
78
  end
59
79
  end
@@ -3,13 +3,18 @@
3
3
  module CloudWatchMetrics
4
4
  class Linux
5
5
  class Builder
6
- def initialize(dimensions)
6
+ def initialize(dimensions, metrics)
7
7
  @dimensions_array = [Dimensions.new(InstanceId: MetaData.instance_id)]
8
8
  @dimensions_array << Dimensions.new(dimensions) unless dimensions.empty?
9
+
10
+ @metric_names = metrics.select { |_, v| v }.keys
9
11
  end
10
12
 
11
13
  def build(meminfo, loadavg)
12
- build_without_dimensions(meminfo, loadavg).flat_map do |metric_datum|
14
+ @meminfo = meminfo
15
+ @loadavg = loadavg
16
+
17
+ build_without_dimensions.flat_map do |metric_datum|
13
18
  @dimensions_array.map do |dimensions|
14
19
  metric_datum.merge(dimensions: dimensions.to_cloudwatch)
15
20
  end
@@ -18,57 +23,71 @@ module CloudWatchMetrics
18
23
 
19
24
  private
20
25
 
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)
26
+ def build_without_dimensions
27
+ @metric_names.map { |key| __send__(key) }.compact
29
28
  end
30
29
 
31
- def memory_used(meminfo)
30
+ def memory_used
32
31
  {
33
32
  metric_name: 'MemoryUsed',
34
- timestamp: meminfo.time,
35
- value: meminfo.mem_used / 1024,
33
+ timestamp: @meminfo.time,
34
+ value: @meminfo.mem_used / 1024,
36
35
  unit: 'Megabytes',
37
36
  }
38
37
  end
39
38
 
40
- def memory_utilization(meminfo)
39
+ def memory_utilization
41
40
  {
42
41
  metric_name: 'MemoryUtilization',
43
- timestamp: meminfo.time,
44
- value: (meminfo.mem_util * 100).round(3),
42
+ timestamp: @meminfo.time,
43
+ value: (@meminfo.mem_util * 100).round(3),
45
44
  unit: 'Percent',
46
45
  }
47
46
  end
48
47
 
49
- def swap_used(meminfo)
48
+ def swap_used
49
+ return unless @meminfo.swap?
50
50
  {
51
51
  metric_name: 'SwapUsed',
52
- timestamp: meminfo.time,
53
- value: meminfo.swap_used / 1024,
52
+ timestamp: @meminfo.time,
53
+ value: @meminfo.swap_used / 1024,
54
54
  unit: 'Megabytes',
55
55
  }
56
56
  end
57
57
 
58
- def swap_utilization(meminfo)
58
+ def swap_utilization
59
+ return unless @meminfo.swap?
59
60
  {
60
61
  metric_name: 'SwapUtilization',
61
- timestamp: meminfo.time,
62
- value: (meminfo.swap_util * 100).round(3),
62
+ timestamp: @meminfo.time,
63
+ value: (@meminfo.swap_util * 100).round(3),
63
64
  unit: 'Percent',
64
65
  }
65
66
  end
66
67
 
67
- def load_average_1min(loadavg)
68
+ def load_average_1min
68
69
  {
69
70
  metric_name: 'LoadAverage1Min',
70
- timestamp: loadavg.time,
71
- value: loadavg.loadavg1,
71
+ timestamp: @loadavg.time,
72
+ value: @loadavg.loadavg1,
73
+ unit: 'Count',
74
+ }
75
+ end
76
+
77
+ def load_average_5min
78
+ {
79
+ metric_name: 'LoadAverage5Min',
80
+ timestamp: @loadavg.time,
81
+ value: @loadavg.loadavg5,
82
+ unit: 'Count',
83
+ }
84
+ end
85
+
86
+ def load_average_15min
87
+ {
88
+ metric_name: 'LoadAverage15Min',
89
+ timestamp: @loadavg.time,
90
+ value: @loadavg.loadavg15,
72
91
  unit: 'Count',
73
92
  }
74
93
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module CloudWatchMetrics
4
4
  class Linux
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -10,6 +10,20 @@ module CloudWatchMetrics
10
10
  MAX_METRIC_DATA_PER_PUT = 20
11
11
 
12
12
  class << self
13
+ def convert_symbol_keys_from_dash_to_underscore!(hash)
14
+ hash.keys.each do |key|
15
+ if key.match?('-')
16
+ hash[key.to_s.tr('-', '_').to_sym] = hash.delete(key)
17
+ end
18
+ end
19
+ end
20
+
21
+ def delete_keys!(hash, keys)
22
+ hash
23
+ .select { |key,| keys.include?(key) }
24
+ .each_key { |key| hash.delete(key) }
25
+ end
26
+
13
27
  def accept_hash(option_parser)
14
28
  option_parser.accept(Hash) do |s,|
15
29
  break s unless s
@@ -21,8 +35,8 @@ module CloudWatchMetrics
21
35
  end
22
36
 
23
37
  # @return [void]
24
- def put_metric_data(namespace, metric_data, dryrun: false)
25
- return dump_metric_data(namespace, metric_data) if dryrun
38
+ def put_metric_data(namespace, metric_data, dry_run: false)
39
+ return dump_metric_data(namespace, metric_data) if dry_run
26
40
 
27
41
  metric_data.each_slice(MAX_METRIC_DATA_PER_PUT).map do |data|
28
42
  Thread.start(data, cloudwatch) do |data_, cloudwatch_|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudwatch-metrics-linux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaki Takeuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-09 00:00:00.000000000 Z
11
+ date: 2017-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core