cwa 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
+ SHA256:
3
+ metadata.gz: f02a90de61af29e0f454486e6d40a8a0e0a608c4ae20c03277a03914d8e6a4c6
4
+ data.tar.gz: a8f49d0b87808469443edae18562e6d3a55567dbab8ed0681975b001bef1bd67
5
+ SHA512:
6
+ metadata.gz: cde5c6dfa58620d43288b34ed245fb789276f5283e0a8e25e4f53cee2f93134e4400de5791973c13f7d4871e168af7003bbb1355bdcf4ba878ec59eed4721261
7
+ data.tar.gz: a3c7baa0337cc92238b0cf0bdf41218c5a8179049654fe4e5ab2b01ff6a7cd796996003b33443f703363aec7bb2d04b521e846371e6f6c6519a3291bccb87fa0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cwa.gemspec
6
+ gemspec
@@ -0,0 +1,30 @@
1
+ # CWA
2
+ cloudwatch alarms cli tool
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'cwa'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install cwa
19
+
20
+ ## Usage
21
+ ```
22
+ Commands:
23
+ cwa alarms --name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE # show cloudwatch alms
24
+ cwa disable --name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE # disable cloudwatch alms
25
+ cwa enable --name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE # enable cloudwatch alms
26
+ cwa help [COMMAND] # Describe available commands or one specific command
27
+
28
+ Options:
29
+ [--verbose], [--no-verbose]
30
+ ```
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cwa"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cwa/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cwa"
8
+ spec.version = CWA::VERSION
9
+ spec.authors = ["gen"]
10
+ spec.email = ["ymdtshm@gmail.com"]
11
+
12
+ spec.summary = "cloudwatch alarm client"
13
+ spec.description = "cloudwatch alarm client"
14
+ spec.homepage = "https://github.com/Gen-Arch/cwa"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_runtime_dependency"aws-sdk-cloudwatch", "~> 1"
36
+ spec.add_runtime_dependency"thor", "~> 1"
37
+ spec.add_runtime_dependency"terminal-table", "~> 1.8"
38
+ spec.add_runtime_dependency"colorize", "~> 0.7"
39
+
40
+ spec.add_development_dependency "bundler", "~> 1.17"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ end
data/exe/cwa ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cwa/cli"
5
+
6
+
7
+ CWA::Cli.start(ARGV)
@@ -0,0 +1,17 @@
1
+ require "cwa/client"
2
+ require "cwa/version"
3
+
4
+ module CWA
5
+ class Error < StandardError; end
6
+
7
+ class << self
8
+ def configure(**opts)
9
+ @aws_opts = opts
10
+ end
11
+
12
+ def get
13
+ @aws_opts = {} unless @aws_opts
14
+ Client.new(@aws_opts)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,45 @@
1
+ module CWA
2
+ class Alarms
3
+ class Error < StandardError; end
4
+
5
+ def initialize(client, **opts)
6
+ @opts = opts
7
+ @client = client
8
+ end
9
+
10
+ def filter(query)
11
+ alms = _alarms.metric_alarms
12
+
13
+ # querys
14
+ name_query = ->(alm) { alm.alarm_name == query[:name] }
15
+ regexp_query = ->(alm) { alm.alarm_name =~ /#{query[:regexp]}/ }
16
+ namespace_query = ->(alm) { alm.namespace == query[:namespace] }
17
+
18
+ alms = alms.select(&name_query) if query[:name ]
19
+ alms = alms.select(&regexp_query) if query[:regexp ]
20
+ alms = alms.select(&namespace_query) if query[:namespace ]
21
+ alms = _dimension?(alms, query[:dimensions]) if query[:dimensions]
22
+ alms
23
+ end
24
+
25
+ def refresh
26
+ @alms = nil
27
+ _alarms
28
+ end
29
+
30
+ private
31
+ def _alarms(**opts)
32
+ opts[:alarm_name_prefix] = "*" unless opts
33
+ @alms ||= @client.describe_alarms(opts)
34
+ end
35
+
36
+ def _dimension?(alms, dimensions)
37
+ alms.select do |alm|
38
+ alm.dimensions.any? do |dims|
39
+ dimensions.keys.any?(dims.name) && dimensions.values.any?(dims.value)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,141 @@
1
+ require 'cwa'
2
+ require 'thor'
3
+ require 'terminal-table'
4
+ require 'colorize'
5
+
6
+ OUTPUT_KEYS = [
7
+ :namespace,
8
+ :alarm_name,
9
+ :actions_enabled,
10
+ :dimensions,
11
+ :alarm_arn,
12
+ :alarm_description,
13
+ ]
14
+
15
+ OPTIONS = "--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE"
16
+
17
+ module CWA
18
+ class Cli < Thor
19
+ class_option :verbose, :type => :boolean
20
+
21
+ desc "alarms #{OPTIONS}", "show cloudwatch alms"
22
+ option :name, type: :string, aliases: "n"
23
+ option :namespace, type: :string, aliases: "s"
24
+ option :regexp, type: :string, aliases: "r"
25
+ option :dimensions, type: :hash, aliases: "d"
26
+ #option ambiguous: :string
27
+ def alarms
28
+ begin
29
+ alms = _output_alms
30
+ raise "not alarms" if alms.empty?
31
+
32
+ head = alms.first.keys
33
+ rows = alms.map{|alm| alm.values }
34
+ table = Terminal::Table.new :headings => head, :rows => rows
35
+
36
+ puts table
37
+ rescue => err
38
+ puts "error => #{err}".colorize(:red)
39
+ exit 1
40
+ end
41
+ end
42
+
43
+ desc "enable #{OPTIONS}", "enable cloudwatch alms"
44
+ option :name, type: :string, aliases: "n"
45
+ option :namespace, type: :string, aliases: "s"
46
+ option :regexp, type: :string, aliases: "r"
47
+ option :dimensions, type: :hash, aliases: "d"
48
+ def enable
49
+ begin
50
+ cwa = CWA.get
51
+ alms = cwa.alarms(options)
52
+ alms = _check_alm(alms, :enable)
53
+
54
+ raise "not alarms" if alms.empty?
55
+ _confirm("cloudwatch alarm enable?")
56
+
57
+ alms.each do |alm|
58
+ cwa.enable(alm)
59
+ puts "#{'done'.colorize(:green)} => #{alm[:alarm_name]}"
60
+ end
61
+ puts
62
+ alarms
63
+ rescue => err
64
+ puts "error => #{err}".colorize(:red)
65
+ exit 1
66
+ end
67
+ end
68
+
69
+ desc "disable #{OPTIONS}", "disable cloudwatch alms"
70
+ option :name, type: :string, aliases: "n"
71
+ option :namespace, type: :string, aliases: "s"
72
+ option :regexp, type: :string, aliases: "r"
73
+ option :dimensions, type: :hash, aliases: "d"
74
+ def disable
75
+ begin
76
+ cwa = CWA.get
77
+ alms = cwa.alarms(options)
78
+ alms = _check_alm(alms, :disable)
79
+
80
+ raise "not alarms" if alms.empty?
81
+ _confirm("cloudwatch alarm disable?")
82
+
83
+ alms.each do |alm|
84
+ cwa.disable(alm)
85
+ puts "#{'done'.colorize(:green)} => #{alm[:alarm_name]}"
86
+ end
87
+ puts
88
+ alarms
89
+ rescue => err
90
+ puts "error => #{err}".colorize(:red)
91
+ exit 1
92
+ end
93
+ end
94
+
95
+ private
96
+ def _output_alms
97
+ cwa = CWA.get
98
+ alms = cwa.alarms(options)
99
+
100
+ alms.map do |alm|
101
+ v = Hash.new
102
+ OUTPUT_KEYS.each do |key|
103
+ v[key] = alm.method(key).call
104
+ end
105
+ v
106
+ end
107
+ end
108
+
109
+ def _check_alm(alms, mode)
110
+ check = false if mode == :enable
111
+ check = true if mode == :disable
112
+ alms.map do |alm|
113
+ puts "-" * 50
114
+ puts "namespace : #{alm[:namespace ]}"
115
+ puts "alarm_name : #{alm[:alarm_name ]}"
116
+ puts "dimensions : #{alm[:dimensions ]}"
117
+ puts "actions_enabled : #{alm[:actions_enabled]}"
118
+ unless alm[:actions_enabled] == check
119
+ puts "=> #{'skip'.colorize(:yellow)}"
120
+ puts "-" * 50
121
+ next
122
+ end
123
+ puts "-" * 50
124
+ alm
125
+ end.compact
126
+ end
127
+
128
+ def _confirm(check_word, **opt)
129
+ true_word = ( opt[:true] || /yes|y/ )
130
+ false_word = ( opt[:false] || /no/ )
131
+
132
+ while true
133
+ print "#{check_word} (#{true_word.inspect.delete("/")}/#{false_word.inspect.delete("/")}) : "
134
+ case anser = $stdin.gets.strip
135
+ when true_word then return true
136
+ when false_word then return false
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,35 @@
1
+ require 'aws-sdk-cloudwatch'
2
+ require "cwa/alarms"
3
+
4
+ module CWA
5
+ class Client
6
+ class Error < StandardError; end
7
+
8
+ def initialize(opts)
9
+ @client = Aws::CloudWatch::Client.new(opts)
10
+ end
11
+
12
+ def alarms(query)
13
+ @alarms ||= Alarms.new(@client)
14
+ alms = @alarms.filter(query)
15
+ if block_given?
16
+ alms.each{|alm| yield alm }
17
+ end
18
+ alms
19
+ end
20
+
21
+ def refresh
22
+ @alarms.refresh
23
+ end
24
+
25
+ def enable(alm)
26
+ alm = alm[:alarm_name]
27
+ @client.enable_alarm_actions({alarm_names: [alm] })
28
+ end
29
+
30
+ def disable(alm)
31
+ alm = alm[:alarm_name]
32
+ @client.disable_alarm_actions({alarm_names: [alm] })
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module CWA
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cwa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-cloudwatch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: terminal-table
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.17'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.17'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: cloudwatch alarm client
98
+ email:
99
+ - ymdtshm@gmail.com
100
+ executables:
101
+ - cwa
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - README.md
108
+ - Rakefile
109
+ - bin/console
110
+ - bin/setup
111
+ - cwa.gemspec
112
+ - exe/cwa
113
+ - lib/cwa.rb
114
+ - lib/cwa/alarms.rb
115
+ - lib/cwa/cli.rb
116
+ - lib/cwa/client.rb
117
+ - lib/cwa/version.rb
118
+ homepage: https://github.com/Gen-Arch/cwa
119
+ licenses: []
120
+ metadata:
121
+ homepage_uri: https://github.com/Gen-Arch/cwa
122
+ source_code_uri: https://github.com/Gen-Arch/cwa
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.1.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: cloudwatch alarm client
142
+ test_files: []