malsh 0.2.0 → 0.3.3

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: 347a943b1a31d9e96ce868829ed7244ee1f9b7e4
4
- data.tar.gz: d8292cf71492b05ec754738f8674af60160c806d
2
+ SHA256:
3
+ metadata.gz: 53560007bf799b565171bcfb12e482e6b197b55114259f6df3795df69f1427cf
4
+ data.tar.gz: 71469c9e09c80c1386b31b5a06a0d3fb80bc38850fa225d36ef7ebc02e3d471d
5
5
  SHA512:
6
- metadata.gz: 8063808bd39e8639033f6586ea3df71dd820221e228e9c6cb6ca937fc2c41ebed7df0de82e8a0b0fb23936836516ffe78030cd376315862302aa74b16eddd47e
7
- data.tar.gz: 27656b665489d656235323d93015cb915e8c212646510885f4f66c555288ddf9669e51b98dbfb9f84fb028ed18b46cb89c49b7554ac727cc91efc227716971b7
6
+ metadata.gz: a62d25c6b1906f595cc816765cbd38957b03393826e46def774ea348d440978cd6f223e8a9ad98769b3a66561ff3f1d6cbc2efaf13545fc860be522fc683fd77
7
+ data.tar.gz: 884c91493164eae6609000338255ccd8dcd78083c25410c437cf1a718a3b8f115a2699aae44d56e15b6f60ffc1d9e98ba12fe1265aad22a74c05adac9388df0a
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
@@ -66,5 +83,13 @@ module Malsh
66
83
  puts e
67
84
  end
68
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
69
94
  end
70
95
  end
data/lib/malsh/cli.rb CHANGED
@@ -8,6 +8,7 @@ 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
12
13
  class_option :invert_role, :type => :array, :aliases => :r
13
14
  class_option :regexp,:type => :array, :aliases => :e
@@ -16,23 +17,27 @@ module Malsh
16
17
  def retire
17
18
  Malsh.init options
18
19
 
19
- host_names = Parallel.map(Malsh.metrics('memory.used')) do|memory|
20
- host = Malsh.host_by_id memory.first
21
- 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"] && m["metrics"].size == 0)) && !(h["meta"].has_key?("cloud") && %w(elb rds cloudfront).include?(h["meta"]["cloud"]["provider"]))
22
28
  end.flatten.compact
23
-
24
- Malsh.notify("退役未了ホスト一覧", host_names)
29
+ Malsh.notify_host("退役未了ホスト一覧", hosts)
25
30
  end
26
31
 
27
32
  desc 'maverick', 'check no role'
28
33
  def maverick
29
34
  Malsh.init options
30
35
 
31
- host_names = Parallel.map(Malsh.hosts) do |h|
32
- h.name if h.roles.keys.size < 1
36
+ hosts = Parallel.map(Malsh.hosts) do |h|
37
+ h if h.roles.keys.size < 1
33
38
  end.flatten.compact
34
39
 
35
- Malsh.notify("ロール無所属ホスト一覧", host_names)
40
+ Malsh.notify_host("ロール無所属ホスト一覧", hosts)
36
41
  end
37
42
 
38
43
  desc 'search', 'search hosts'
@@ -41,14 +46,20 @@ module Malsh
41
46
  option :memory_threshold, :type => :numeric, :aliases => :m
42
47
  option :status, :type => :string, :aliases => :st
43
48
  def search
44
- _host_names = {}
45
49
  Malsh.init options
46
50
  o = options[:status] ? { status: options[:status] } : {}
47
51
  hosts = Malsh.hosts(o)
48
52
  Object.const_get("Malsh::HostMetrics").constants.each do |c|
49
53
  hosts = Object.const_get("Malsh::HostMetrics::#{c}").check(hosts)
50
54
  end
51
- 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)
52
63
  end
53
64
 
54
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
data/lib/malsh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Malsh
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.3"
3
3
  end
data/malsh.gemspec CHANGED
@@ -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.2.0
4
+ version: 0.3.3
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-06-07 00:00:00.000000000 Z
11
+ date: 2021-03-11 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.1.4
160
+ signing_key:
162
161
  specification_version: 4
163
162
  summary: mackerel tools.
164
163
  test_files: []