malsh 0.1.9 → 0.3.2

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
- SHA1:
3
- metadata.gz: 909e9f89338418efa785be0451d5ac92efde5d8c
4
- data.tar.gz: 49af40c47f81489fecbfadfa21ef39f4008dc607
2
+ SHA256:
3
+ metadata.gz: f7f1081b9a67afe8ff4e228f5070dcf491a507b2165ac51490a17817640a1712
4
+ data.tar.gz: 6bb4a33106d0c9847b2d2073dfe2e30c2b65542885e9c30a133ebba78581635b
5
5
  SHA512:
6
- metadata.gz: bcc02b83833ad620e47ab21c3cc5a99c8ecb4a74ccf95e788b7fc13f3a1ac2c187ad5a5c5ba4af36e3dd528645b6c267fca2b27d87571450dc7d3f3e8e1cf754
7
- data.tar.gz: 5627a26be7a04d0bd7ed5fe5eebdf713bac8251da1a7dbb7a9f4a94b78b469f930c328882e845dce24891577d31b993494b6194fe6d08fd3d4789a419da62f23
6
+ metadata.gz: 135c8b2509f0fb9b3738469cb7f5bb19d357faa71b7b1f5c36c27a2202872f58d824f68303bb846f7dcd5bcdbf9133fc7fea876cfa3463294676144de7d934f6
7
+ data.tar.gz: c32b5fe8280c1bd682cb361bf61eee792bb865ea9e3eb44ed34bd00b8d143370522761a8df93ac9cb01cbe5b887b0444edd74e08be8c555be965d2ff94bd26db
data/README.md CHANGED
@@ -54,6 +54,8 @@ $ malsh search --past_date 7 --cpu 40 --memory 40 --subject 過去7日間のCPU
54
54
  * 除外したいホスト名を正規表現で指定します。
55
55
  * --regex
56
56
  * 特定したいホスト名を正規表現で指定します。
57
+ * --invert-role
58
+ * 除外したいロール名を`service:role`で指定します。
57
59
 
58
60
  ## Author
59
61
  pyama86
@@ -7,9 +7,15 @@ require "malsh/host_metrics"
7
7
 
8
8
  module Malsh
9
9
  class << self
10
- def notify(subject, host)
10
+ def notify_host(subject, host)
11
11
  Malsh::Notification.constants.each do |c|
12
- Object.const_get("Malsh::Notification::#{c}").notify(options[:subject] || subject, host)
12
+ Object.const_get("Malsh::Notification::#{c}").notify_host(options[:subject] || subject, host)
13
+ end
14
+ end
15
+
16
+ def notify_alert(subject, alerts)
17
+ Malsh::Notification.constants.each do |c|
18
+ Object.const_get("Malsh::Notification::#{c}").notify_alert(options[:subject] || subject, alerts)
13
19
  end
14
20
  end
15
21
 
@@ -35,6 +41,22 @@ module Malsh
35
41
  Malsh.options[:invert_match] && Malsh.options[:invert_match].find {|v| host_name(h).match(/#{v}/) }
36
42
  end.reject do |h|
37
43
  Malsh.options[:regexp] && Malsh.options[:regexp].all? {|r| !host_name(h).match(/#{r}/)}
44
+ end.reject do |h|
45
+ Malsh.options[:invert_role] && Malsh.options[:invert_role].find do |r|
46
+ service, role = r.split(/:/)
47
+ h.roles[service] && h.roles[service].include?(role)
48
+ end
49
+ end
50
+ end
51
+
52
+ def alerts()
53
+ @_alerts ||= Mackerel.alerts.map do |alert|
54
+ if alert.type == 'external'
55
+ alert['monitor'] = Mackerel.monitor(alert.monitorId)
56
+ else
57
+ alert['host'] = Malsh.host_by_id(alert.hostId)
58
+ end
59
+ alert
38
60
  end
39
61
  end
40
62
 
@@ -61,5 +83,13 @@ module Malsh
61
83
  puts e
62
84
  end
63
85
  end
86
+
87
+ def host_metric_names(id)
88
+ begin
89
+ Mackerel.host_metric_names(id)
90
+ rescue => e
91
+ puts e
92
+ end
93
+ end
64
94
  end
65
95
  end
@@ -8,30 +8,36 @@ module Malsh
8
8
  class_option :slack_user, :type => :string, :aliases => :su
9
9
  class_option :api_key, :type => :string, :aliases => :k
10
10
  class_option :subject, :type => :string, :aliases => :s
11
+ class_option :org, :type => :string, :aliases => :o
11
12
  class_option :invert_match, :type => :array, :aliases => :v
13
+ class_option :invert_role, :type => :array, :aliases => :r
12
14
  class_option :regexp,:type => :array, :aliases => :e
13
15
 
14
16
  desc 'retire', 'check forget retire host'
15
17
  def retire
16
18
  Malsh.init options
17
19
 
18
- host_names = Parallel.map(Malsh.metrics('memory.used')) do|memory|
19
- host = Malsh.host_by_id memory.first
20
- host.name if (!memory.last["memory.used"].respond_to?(:value) || !memory.last["memory.used"].value)
20
+ hosts = Parallel.map(Malsh.hosts) do |h|
21
+ m = Malsh.host_metrics(
22
+ h.id,
23
+ Malsh.host_metric_names(h.id).first,
24
+ Time.now.to_i - 86400,
25
+ Time.now.to_i
26
+ )
27
+ h if !m || m["metrics"].size == 0
21
28
  end.flatten.compact
22
-
23
- Malsh.notify("退役未了ホスト一覧", host_names)
29
+ Malsh.notify_host("退役未了ホスト一覧", hosts)
24
30
  end
25
31
 
26
32
  desc 'maverick', 'check no role'
27
33
  def maverick
28
34
  Malsh.init options
29
35
 
30
- host_names = Parallel.map(Malsh.hosts) do |h|
31
- h.name if h.roles.keys.size < 1
36
+ hosts = Parallel.map(Malsh.hosts) do |h|
37
+ h if h.roles.keys.size < 1
32
38
  end.flatten.compact
33
39
 
34
- Malsh.notify("ロール無所属ホスト一覧", host_names)
40
+ Malsh.notify_host("ロール無所属ホスト一覧", hosts)
35
41
  end
36
42
 
37
43
  desc 'search', 'search hosts'
@@ -40,14 +46,20 @@ module Malsh
40
46
  option :memory_threshold, :type => :numeric, :aliases => :m
41
47
  option :status, :type => :string, :aliases => :st
42
48
  def search
43
- _host_names = {}
44
49
  Malsh.init options
45
50
  o = options[:status] ? { status: options[:status] } : {}
46
51
  hosts = Malsh.hosts(o)
47
52
  Object.const_get("Malsh::HostMetrics").constants.each do |c|
48
53
  hosts = Object.const_get("Malsh::HostMetrics::#{c}").check(hosts)
49
54
  end
50
- Malsh.notify("ホスト一覧", hosts.compact.map {|h| h["name"]})
55
+ Malsh.notify_host("ホスト一覧", hosts.compact)
56
+ end
57
+
58
+ desc 'alert', 'list alerts'
59
+ def alert
60
+ Malsh.init options
61
+ alerts = Malsh.alerts
62
+ Malsh.notify_alert('アラート一覧', alerts)
51
63
  end
52
64
 
53
65
  map %w[--version -v] => :__print_version
@@ -1,7 +1,23 @@
1
1
  module Malsh::Notification
2
2
  class Base
3
- def self.notify(subject, hosts)
4
- puts "#{subject}: \n#{hosts.join("\n")}" if hosts.size > 0 && doit?
3
+ def self.notify_host(subject, hosts)
4
+ puts "#{subject}:"
5
+ hosts.map do |h|
6
+ puts "#{h.name}(#{h.roles.keys.join(",")})"
7
+ end if doit?
8
+ end
9
+
10
+ def self.notify_alert(subject, alerts)
11
+ puts "#{subject}: "
12
+ alerts.each do |alert|
13
+ title = case alert.type
14
+ when 'external'
15
+ alert.monitor.name
16
+ else
17
+ alert.host.name
18
+ end
19
+ puts "#{title}: #{alert.message}"
20
+ end
5
21
  end
6
22
 
7
23
  def self.doit?
@@ -1,15 +1,68 @@
1
1
  require 'slack-notifier'
2
2
  module Malsh::Notification
3
3
  class Slack < Base
4
- def self.notify(subject, hosts)
4
+ def self.notify_host(subject, hosts)
5
5
  return unless doit?
6
+ lists = if Malsh.options[:org]
7
+ hosts.map do |h|
8
+ "<https://mackerel.io/orgs/#{Malsh.options[:org]}/hosts/#{h.id}/-/setting|#{h.name}(#{h.roles.keys.join(",")})>"
9
+ end
10
+ else
11
+ hosts.map(&:name)
12
+ end
6
13
  note = {
7
- text: hosts.join("\n"),
14
+ text: lists.join("\n"),
8
15
  color: "warning"
9
16
  }
10
17
  notifier.ping "*#{subject}*", attachments: [note]
11
18
  end
12
19
 
20
+ def self.notify_alert(subject, alerts)
21
+ org = Mackerel.org.name
22
+ attachments = []
23
+ alerts.each do |alert|
24
+ color = case alert.status
25
+ when 'CRITICAL'
26
+ 'danger'
27
+ when 'WARNING'
28
+ 'warning'
29
+ else
30
+ ''
31
+ end
32
+
33
+ title = if alert.type == 'external'
34
+ alert.monitor.name
35
+ else
36
+ alert.host.name
37
+ end
38
+
39
+ author_name = if alert.type == 'external'
40
+ ''
41
+ else
42
+ alert.host.roles.map{|k, v| v.map{|r| "#{k}: #{r}"}}.flatten.join(" ")
43
+ end
44
+
45
+ attachments << {
46
+ author_name: author_name,
47
+ title: title,
48
+ title_link: "https://mackerel.io/orgs/#{org}/alerts/#{alert.id}",
49
+ text: alert.message,
50
+ color: color,
51
+ fields: [
52
+ {
53
+ title: 'Type',
54
+ value: alert.type
55
+ },
56
+ {
57
+ title: 'OpenedAt',
58
+ value: Time.at(alert.openedAt).strftime("%Y/%m/%d %H:%M:%S")
59
+ }
60
+ ]
61
+ }
62
+ end
63
+ notifier.ping "*#{subject}*", attachments: attachments
64
+ end
65
+
13
66
  def self.doit?
14
67
  %i(slack_webhook slack_channel).all? {|k| Malsh.options.key?(k) || ENV[k.to_s.upcase] }
15
68
  end
@@ -1,3 +1,3 @@
1
1
  module Malsh
2
- VERSION = "0.1.9"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -23,10 +23,10 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ["lib"]
24
24
 
25
25
  spec.add_dependency 'thor'
26
- spec.add_dependency 'mackerel-rb', "~> 0.1.5"
26
+ spec.add_dependency 'mackerel-rb'
27
27
  spec.add_dependency 'slack-notifier'
28
28
  spec.add_dependency 'parallel'
29
- spec.add_development_dependency "bundler", "~> 1.9"
30
- spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "bundler"
30
+ spec.add_development_dependency "rake"
31
31
  spec.add_development_dependency "rspec"
32
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: malsh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuhiko yamahsita
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-11 00:00:00.000000000 Z
11
+ date: 2020-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: mackerel-rb
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.5
33
+ version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.1.5
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: slack-notifier
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -70,30 +70,30 @@ dependencies:
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '1.9'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '1.9'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '10.0'
89
+ version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '10.0'
96
+ version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -141,7 +141,7 @@ homepage: http://ten-snapon.com.
141
141
  licenses:
142
142
  - MIT
143
143
  metadata: {}
144
- post_install_message:
144
+ post_install_message:
145
145
  rdoc_options: []
146
146
  require_paths:
147
147
  - lib
@@ -156,9 +156,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  - !ruby/object:Gem::Version
157
157
  version: '0'
158
158
  requirements: []
159
- rubyforge_project:
160
- rubygems_version: 2.4.8
161
- signing_key:
159
+ rubygems_version: 3.0.6
160
+ signing_key:
162
161
  specification_version: 4
163
162
  summary: mackerel tools.
164
163
  test_files: []