malsh 0.2.1 → 0.3.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
- SHA1:
3
- metadata.gz: 858533993fc700f1d6b4fc20d5d5c441ae037f0f
4
- data.tar.gz: 15bdd45140ce7f0fb3faadbd94da1cf051886f45
2
+ SHA256:
3
+ metadata.gz: 7618152e48a3184a6f9f031a40fc3d1ff48bc16541a545fb27aaf18664d17a8a
4
+ data.tar.gz: 061b00940628b739d6b59dc38efd20fad83ab1f061774436450d09dad2a6c198
5
5
  SHA512:
6
- metadata.gz: a5bbf854b5e324f132626028a63d1d0487f361463d8c7045be77b6e95cb7c7eb7cf7f5f29b45eee34df931e59996dfdeef48bc3533d69b7de93a4b488e5974e7
7
- data.tar.gz: 2acfb0ef1aa9e8558f18b62b05cb4717300fb32c50e72158e7c2b7fa9658aa40aa753b34127cc5bbef8dca695654feb8dda3df76c1ddcb89410b02ec508cbffc
6
+ metadata.gz: 7dc99e3b6b1afc7f72ad041d873803a4aa77b23d0443b6684bdcef79702163ba306f02987d1afa2e161a65a7e5413f76b8d0de57ec24b164e66ee44b55adf5ea
7
+ data.tar.gz: be5c086daa10999317afd36754b576d4812e301624291c5bbf041a92055003843822cf932caa774faec1fcbe04f41cfff8b5efa460ed91c8181017239212174e
data/lib/malsh.rb CHANGED
@@ -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
 
@@ -43,6 +49,17 @@ module Malsh
43
49
  end
44
50
  end
45
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
60
+ end
61
+ end
62
+
46
63
  def host_name(host)
47
64
  host.displayName || host.name
48
65
  end
data/lib/malsh/cli.rb CHANGED
@@ -22,7 +22,7 @@ module Malsh
22
22
  host if (!memory.last["memory.used"].respond_to?(:value) || !memory.last["memory.used"].value)
23
23
  end.flatten.compact
24
24
 
25
- Malsh.notify("退役未了ホスト一覧", hosts)
25
+ Malsh.notify_host("退役未了ホスト一覧", hosts)
26
26
  end
27
27
 
28
28
  desc 'maverick', 'check no role'
@@ -33,7 +33,7 @@ module Malsh
33
33
  h if h.roles.keys.size < 1
34
34
  end.flatten.compact
35
35
 
36
- Malsh.notify("ロール無所属ホスト一覧", hosts)
36
+ Malsh.notify_host("ロール無所属ホスト一覧", hosts)
37
37
  end
38
38
 
39
39
  desc 'search', 'search hosts'
@@ -48,7 +48,14 @@ module Malsh
48
48
  Object.const_get("Malsh::HostMetrics").constants.each do |c|
49
49
  hosts = Object.const_get("Malsh::HostMetrics::#{c}").check(hosts)
50
50
  end
51
- Malsh.notify("ホスト一覧", hosts.compact)
51
+ Malsh.notify_host("ホスト一覧", hosts.compact)
52
+ end
53
+
54
+ desc 'alert', 'list alerts'
55
+ def alert
56
+ Malsh.init options
57
+ alerts = Malsh.alerts
58
+ Malsh.notify_alert('アラート一覧', alerts)
52
59
  end
53
60
 
54
61
  map %w[--version -v] => :__print_version
@@ -1,10 +1,23 @@
1
1
  module Malsh::Notification
2
2
  class Base
3
- def self.notify(subject, hosts)
3
+ def self.notify_host(subject, hosts)
4
4
  names = hosts.map(&:name)
5
5
  puts "#{subject}: \n#{names.join("\n")}" if names.size > 0 && doit?
6
6
  end
7
7
 
8
+ def self.notify_alert(subject, alerts)
9
+ puts "#{subject}: "
10
+ alerts.each do |alert|
11
+ title = case alert.type
12
+ when 'external'
13
+ alert.monitor.name
14
+ else
15
+ alert.host.name
16
+ end
17
+ puts "#{title}: #{alert.message}"
18
+ end
19
+ end
20
+
8
21
  def self.doit?
9
22
  true
10
23
  end
@@ -1,7 +1,7 @@
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
6
  lists = if Malsh.options[:org]
7
7
  hosts.map do |h|
@@ -17,6 +17,52 @@ module Malsh::Notification
17
17
  notifier.ping "*#{subject}*", attachments: [note]
18
18
  end
19
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
+
20
66
  def self.doit?
21
67
  %i(slack_webhook slack_channel).all? {|k| Malsh.options.key?(k) || ENV[k.to_s.upcase] }
22
68
  end
data/lib/malsh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Malsh
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/malsh.gemspec CHANGED
@@ -23,7 +23,7 @@ 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
29
  spec.add_development_dependency "bundler", "~> 1.9"
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.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuhiko yamahsita
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-26 00:00:00.000000000 Z
11
+ date: 2018-11-30 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
@@ -157,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
157
  version: '0'
158
158
  requirements: []
159
159
  rubyforge_project:
160
- rubygems_version: 2.5.2
160
+ rubygems_version: 2.7.5
161
161
  signing_key:
162
162
  specification_version: 4
163
163
  summary: mackerel tools.